File: signal_button.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 (345 lines) | stat: -rw-r--r-- 10,338 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
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
/************************************************************************
 *
 * Copyright (C) 2015-2025 IRCAD France
 * Copyright (C) 2015-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_button.hpp"

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

#include <io/joystick/interactor.hpp>

#include <ui/qt/container/widget.hpp>

#include <QVariant>
#include <QVBoxLayout>

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

static const core::com::slots::key_t SET_CHECKED_SLOT = "set_checked";
static const core::com::slots::key_t CHECK_SLOT       = "check";
static const core::com::slots::key_t UNCHECK_SLOT     = "uncheck";

static const core::com::slots::key_t SET_ENABLED_SLOT = "set_enabled";
static const core::com::slots::key_t ENABLE_SLOT      = "enable";
static const core::com::slots::key_t DISABLE_SLOT     = "disable";
static const core::com::slots::key_t SET_VISIBLE_SLOT = "set_visible";
static const core::com::slots::key_t SHOW_SLOT        = "show";
static const core::com::slots::key_t HIDE_SLOT        = "hide";

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

signal_button::signal_button() noexcept
{
    new_signal<signals::void_t>(signals::CLICKED);
    new_signal<signals::bool_t>(signals::IS_CHECKED);
    new_signal<signals::bool_t>(signals::TOGGLED);
    new_signal<signals::void_t>(signals::CHECKED);
    new_signal<signals::void_t>(signals::UNCHECKED);

    new_slot(SET_CHECKED_SLOT, &signal_button::set_checked, this);
    new_slot(CHECK_SLOT, &signal_button::check, this);
    new_slot(UNCHECK_SLOT, &signal_button::uncheck, this);
    new_slot(SET_ENABLED_SLOT, &signal_button::set_enabled, this);
    new_slot(ENABLE_SLOT, &signal_button::enable, this);
    new_slot(DISABLE_SLOT, &signal_button::disable, this);
    new_slot(SET_VISIBLE_SLOT, &signal_button::set_visible, this);
    new_slot(SHOW_SLOT, &signal_button::show, this);
    new_slot(HIDE_SLOT, &signal_button::hide, this);
}

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

signal_button::~signal_button() noexcept =
    default;

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

void signal_button::configuring()
{
    this->initialize();

    const auto configuration = this->get_config();

    const auto cfg = configuration.get_child_optional("config");

    if(cfg.has_value())
    {
        m_checkable      = cfg->get<bool>("checkable", m_checkable);
        m_check_at_start = cfg->get<bool>("checked", m_check_at_start);
        m_enable         = cfg->get<bool>("enable", m_enable);

        m_text     = cfg->get<std::string>("text", m_text);
        m_text2    = cfg->get<std::string>("text2", m_text2);
        m_tool_tip = core::ptree::get_and_deprecate(*cfg, "tool_tip", "toolTip", "26.0", m_tool_tip);

        if(const auto icon = cfg->get_optional<std::string>("icon"); icon.has_value())
        {
            m_icon = core::runtime::get_module_resource_file_path(icon.value());
        }

        if(const auto icon = cfg->get_optional<std::string>("icon2"); icon.has_value())
        {
            m_icon2 = core::runtime::get_module_resource_file_path(icon.value());
        }

        m_joystick_alias = sight::io::joystick::interactor::to_joystick(cfg->get<std::string>("joystick", ""));

        m_icon_width  = core::ptree::get_and_deprecate(*cfg, "icon_width", "iconWidth", "26.0", m_icon_width);
        m_icon_height = core::ptree::get_and_deprecate(*cfg, "icon_height", "iconHeight", "26.0", m_icon_height);
    }
}

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

void signal_button::starting()
{
    this->create();

    auto qt_container = std::dynamic_pointer_cast<sight::ui::qt::container::widget>(this->get_container());

    auto* layout = new QVBoxLayout();
    m_button = new QPushButton(QString::fromStdString(m_text));
    m_button->setEnabled(m_enable);

    const QString service_id = QString::fromStdString(base_id());
    m_button->setObjectName(service_id + "/signal_button");

    m_button->setProperty("class", "signal-button");
    layout->addWidget(m_button);
    qt_container->set_layout(layout);

    if(!m_tool_tip.empty())
    {
        m_button->setToolTip(QString::fromStdString(m_tool_tip));
    }

    if(!m_icon.empty())
    {
        m_button->setIcon(QIcon(QString::fromStdString(m_icon.string())));
    }

    if(m_icon_width > 0 && m_icon_height > 0)
    {
        m_button->setIconSize(QSize(int(m_icon_width), int(m_icon_height)));
    }

    if(m_checkable)
    {
        m_button->setCheckable(true);

        if(m_check_at_start)
        {
            m_button->setChecked(true);
            if(!m_text2.empty())
            {
                m_button->setText(QString::fromStdString(m_text2));
            }

            if(!m_icon2.empty())
            {
                m_button->setIcon(QIcon(QString::fromStdString(m_icon2.string())));
            }
        }
    }

    QObject::connect(m_button.data(), &QPushButton::clicked, this, &signal_button::on_clicked);
    QObject::connect(m_button.data(), &QPushButton::toggled, this, &signal_button::on_toggled);

    if(m_joystick_alias != sight::io::joystick::joystick_t::unknown)
    {
        this->start_listening_joystick();
    }
}

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

void signal_button::updating()
{
}

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

void signal_button::stopping()
{
    this->destroy();

    if(m_joystick_alias != sight::io::joystick::joystick_t::unknown)
    {
        this->stop_listening_joystick();
    }
}

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

void signal_button::joystick_axis_direction_event(const sight::io::joystick::axis_direction_event& _event)
{
    if(_event.device->alias == m_joystick_alias)
    {
        if(_event.axis_alias == sight::io::joystick::axis_t::tz and _event.value
           == sight::io::joystick::axis_direction_event::direction_t::backward)
        {
            m_button->animateClick();
        }
    }
}

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

void signal_button::on_clicked()
{
    const auto sig = this->signal<signals::void_t>(signals::CLICKED);
    sig->async_emit();
}

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

void signal_button::on_toggled(bool _toggled)
{
    this->set_checked(_toggled);

    // legacy
    {
        const auto sig = this->signal<signals::bool_t>(signals::TOGGLED);
        sig->async_emit(_toggled);
    }

    // current signal
    {
        const auto sig = this->signal<signals::bool_t>(signals::IS_CHECKED);
        sig->async_emit(_toggled);
    }

    // checked/unchecked signals
    if(_toggled)
    {
        const auto sig = this->signal<signals::void_t>(signals::CHECKED);
        sig->async_emit();
    }
    else
    {
        const auto sig = this->signal<signals::void_t>(signals::UNCHECKED);
        sig->async_emit();
    }
}

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

void signal_button::set_checked(bool _checked)
{
    if(_checked)
    {
        if(!m_text2.empty())
        {
            m_button->setText(QString::fromStdString(m_text2));
        }

        if(!m_icon2.empty())
        {
            m_button->setIcon(QIcon(QString::fromStdString(m_icon2.string())));
        }
    }
    else
    {
        if(!m_text.empty())
        {
            m_button->setText(QString::fromStdString(m_text));
        }

        if(!m_icon.empty())
        {
            m_button->setIcon(QIcon(QString::fromStdString(m_icon.string())));
        }
    }

    // properly check/uncheck the button when this method is called from the sight slot.
    this->blockSignals(true);
    m_button->setChecked(_checked);
    this->blockSignals(false);
}

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

void signal_button::check()
{
    this->set_checked(true);
}

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

void signal_button::uncheck()
{
    this->set_checked(false);
}

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

void signal_button::set_enabled(bool _is_enabled)
{
    editor::set_enabled(_is_enabled);
    // Keep this in case of signal_button is used outside a view container
    m_button->setEnabled(_is_enabled);
}

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

void signal_button::enable()
{
    this->set_enabled(true);
}

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

void signal_button::disable()
{
    this->set_enabled(false);
}

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

void signal_button::set_visible(bool _is_visible)
{
    editor::set_visible(_is_visible);
    // Keep this in case of signal_button is used outside a view container
    m_button->setVisible(_is_visible);
}

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

void signal_button::show()
{
    this->set_visible(true);
}

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

void signal_button::hide()
{
    this->set_visible(false);
}

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

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