File: signal_shortcut.cpp

package info (click to toggle)
sight 25.2.0-2
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 42,184 kB
  • sloc: cpp: 289,476; xml: 17,257; ansic: 9,878; python: 1,379; sh: 144; makefile: 33
file content (293 lines) | stat: -rw-r--r-- 9,003 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
/************************************************************************
 *
 * Copyright (C) 2018-2025 IRCAD France
 * Copyright (C) 2018-2020 IHU Strasbourg
 *
 * This file is part of Sight.
 *
 * Sight is free software: you can redistribute it and/or modify it under
 * the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * Sight is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with Sight. If not, see <https://www.gnu.org/licenses/>.
 *
 ***********************************************************************/

#include "signal_shortcut.hpp"

#include <core/com/signal.hxx>
#include <core/com/slots.hxx>
#include <core/ptree.hpp>

#include <service/op.hpp>

#include <ui/__/registry.hpp>
#include <ui/__/service.hpp>
#include <ui/qt/container/widget.hpp>

#include <QKeySequence>
#include <QWidget>

#include <memory>

namespace sight::module::ui::qt::com
{

//-----------------------------------------------------------------------------

signal_shortcut::signal_shortcut() noexcept
{
    new_signal<signals::bool_t>(signals::IS_ENABLED);
    new_signal<signals::void_t>(signals::ENABLED);
    new_signal<signals::void_t>(signals::DISABLED);
    new_signal<signals::void_t>(signals::ACTIVATED);

    new_signal<signals::bool_t>(signals::IS_CHECKED);
    new_signal<signals::void_t>(signals::CHECKED);
    new_signal<signals::void_t>(signals::UNCHECKED);

    new_slot(slots::SET_ENABLED, &signal_shortcut::set_enabled, this);
    new_slot(slots::SET_DISABLED, [this](bool _disabled){this->set_enabled(!_disabled);});
    new_slot(slots::ENABLE, [this](){this->set_enabled(true);});
    new_slot(slots::DISABLE, [this](){this->set_enabled(false);});
    new_slot(slots::APPLY_ENABLED, [this](){this->set_enabled(*m_enabled);});

    new_slot(slots::SET_CHECKED, &signal_shortcut::set_checked, this);
    new_slot(slots::CHECK, [this](){this->set_checked(true);});
    new_slot(slots::UNCHECK, [this](){this->set_checked(false);});
    new_slot(slots::APPLY_CHECKED, [this](){this->set_checked(*m_checked);});
}

//-----------------------------------------------------------------------------

void signal_shortcut::configuring()
{
    const auto config_tree = this->get_config();

    if(auto properties = config_tree.get_child_optional("properties"); not properties.has_value())
    {
        const auto enabled = m_enabled.lock();
        *enabled = core::ptree::get_value(config_tree, "state.<xmlattr>.enabled", true);
    }

    const auto config_shortcut = config_tree.get_child("config.<xmlattr>");
    m_shortcut = config_shortcut.get<std::string>("shortcut", m_shortcut);
    SIGHT_ASSERT("Shortcut must not be empty", !m_shortcut.empty());

    m_wid = config_shortcut.get<std::string>("wid", m_wid);
    m_sid = config_shortcut.get<std::string>("sid", m_sid);
    SIGHT_ASSERT(
        "Either The wid or sid attribute must be specified for signal_shortcut",
        !m_wid.empty() || !m_sid.empty()
    );
}

//-----------------------------------------------------------------------------

void signal_shortcut::set_enabled(bool _enabled)
{
    {
        const auto enabled = m_enabled.lock();

        if(_enabled != enabled->value())
        {
            *enabled = _enabled;
            enabled->async_emit(this, data::object::MODIFIED_SIG);
        }
    }

    if(_enabled)
    {
        this->enable();
        auto sig = this->signal<signals::void_t>(signals::ENABLED);
        sig->async_emit();
    }
    else
    {
        this->disable();
        auto sig = this->signal<signals::void_t>(signals::DISABLED);
        sig->async_emit();
    }

    auto sig = this->signal<signals::bool_t>(signals::IS_ENABLED);
    sig->async_emit(_enabled);
}

//-----------------------------------------------------------------------------

void signal_shortcut::set_checked(bool _checked)
{
    {
        const auto checked = m_checked.lock();

        if(_checked != checked->value())
        {
            *checked = _checked;
            checked->async_emit(this, data::object::MODIFIED_SIG);
        }
    }

    if(_checked)
    {
        auto sig = this->signal<signals::void_t>(signals::CHECKED);
        sig->async_emit();
    }
    else
    {
        auto sig = this->signal<signals::void_t>(signals::UNCHECKED);
        sig->async_emit();
    }

    auto sig = this->signal<signals::bool_t>(signals::IS_CHECKED);
    sig->async_emit(_checked);
}

//-----------------------------------------------------------------------------

void signal_shortcut::starting()
{
    if(*m_enabled)
    {
        this->enable();

        // Make sure that we propagate this status to imitate action behaviour.
        if(*m_checked)
        {
            auto sig = this->signal<signals::void_t>(signals::CHECKED);
            sig->async_emit();
        }
        else
        {
            auto sig = this->signal<signals::void_t>(signals::UNCHECKED);
            sig->async_emit();
        }
    }
}

//-----------------------------------------------------------------------------

void signal_shortcut::stopping()
{
    if(*m_enabled)
    {
        this->disable();
    }
}

//-----------------------------------------------------------------------------

void signal_shortcut::updating()
{
}

//------------------------------------------------------------------------------

service::connections_t signal_shortcut::auto_connections() const
{
    return {
        {m_checked, sight::data::object::MODIFIED_SIG, slots::APPLY_CHECKED},
        {m_enabled, sight::data::object::MODIFIED_SIG, slots::APPLY_ENABLED}
    };
}

//------------------------------------------------------------------------------

void signal_shortcut::on_activation()
{
    if(*m_enabled)
    {
        this->signal<signals::void_t>(signals::ACTIVATED)->async_emit();

        const auto checked = [&](){return m_checked.lock()->value();}();
        this->set_checked(not checked);
    }
}

//------------------------------------------------------------------------------

void signal_shortcut::enable()
{
    sight::ui::container::widget::sptr fwc = nullptr;
    // Either get the container via a service id
    if(!m_sid.empty())
    {
        if(const bool sid_exists = core::id::exist(m_sid); sid_exists)
        {
            service::base::sptr service = service::get(m_sid);
            auto container_srv          = std::dynamic_pointer_cast<sight::ui::service>(service);
            fwc = container_srv->get_container();
        }
        else
        {
            SIGHT_ERROR("Invalid service id " << m_sid);
        }
    }
    // or a window id
    else if(!m_wid.empty())
    {
        fwc = sight::ui::registry::get_wid_container(m_wid);
        if(!fwc)
        {
            SIGHT_ERROR("Invalid window id " << m_wid);
        }
    }

    if(fwc != nullptr)
    {
        if(auto qtc = std::dynamic_pointer_cast<sight::ui::qt::container::widget>(fwc); qtc != nullptr)
        {
            // Get the associated widget to use as parent for the shortcut
            QWidget* widget = qtc->get_qt_container();

            std::vector<std::string> shortcuts;
            boost::split(shortcuts, m_shortcut, boost::is_any_of(";,"));

            for(const auto& shortcut : shortcuts)
            {
                // Create a key sequence from the string and its associated QShortcut
                QKeySequence shortcut_sequence = QKeySequence(QString::fromStdString(shortcut));

                auto* shortcut_object = new QShortcut(shortcut_sequence, widget);
                shortcut_object->setContext(Qt::ApplicationShortcut);

                // Connect the activated signal to the onActivation method of this class
                QObject::connect(shortcut_object, &QShortcut::activated, this, &self_t::on_activation);

                m_shortcut_objects.emplace_back(shortcut_object);
            }
        }
    }
    else
    {
        SIGHT_ERROR(
            "Cannot setup shortcut " << m_shortcut << " on invalid "
            << (!m_wid.empty() ? "wid " + m_wid : "sid " + m_sid)
        );
    }
}

//------------------------------------------------------------------------------

void signal_shortcut::disable()
{
    for(const auto& shortcut_object : m_shortcut_objects)
    {
        QObject::disconnect(shortcut_object, &QShortcut::activated, this, &self_t::on_activation);

        // We need to delete manually otherwise it stays referenced by the widget and can never enable again
        delete shortcut_object;
    }

    m_shortcut_objects.clear();
}

//------------------------------------------------------------------------------

} // namespace sight::module::ui::qt::com