File: selection_menu_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 (180 lines) | stat: -rw-r--r-- 5,470 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
/************************************************************************
 *
 * Copyright (C) 2009-2025 IRCAD France
 * Copyright (C) 2012-2019 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 "selection_menu_button.hpp"

#include <core/com/signal.hxx>
#include <core/com/slot.hxx>
#include <core/com/slots.hxx>

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

#include <boost/range/iterator_range_core.hpp>

#include <QAction>
#include <QActionGroup>
#include <QMenu>
#include <QPushButton>
#include <QString>
#include <QVBoxLayout>
#include <QWidget>

#include <filesystem>

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

static const core::com::signals::key_t SELECTED_SIG = "selected";

static const core::com::slots::key_t SET_ENABLED_SIG = "set_enabled";
static const core::com::slots::key_t SENABLE_SIG     = "enable";
static const core::com::slots::key_t DISABLE_SIG     = "disable";

selection_menu_button::selection_menu_button() noexcept :
    m_sig_selected(new_signal<selected_signal_t>(SELECTED_SIG))
{
    new_slot(SET_ENABLED_SIG, &selection_menu_button::set_enabled, this);
    new_slot(SENABLE_SIG, &selection_menu_button::enable, this);
    new_slot(DISABLE_SIG, &selection_menu_button::disable, this);
}

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

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

    const auto& config = this->get_config();

    m_text      = config.get<std::string>("text", m_text);
    m_tool_tip  = config.get<std::string>("toolTip", m_tool_tip);
    m_selection = config.get<int>("selected", m_selection);

    const auto& items = config.get_child("items");
    for(const auto& elem : boost::make_iterator_range(items.equal_range("item")))
    {
        const auto txt  = elem.second.get<std::string>("<xmlattr>.text");
        const int value = elem.second.get<int>("<xmlattr>.value");
        m_items.emplace_back(value, txt);
    }
}

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

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

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

    m_drop_down_button = new QPushButton(QString::fromStdString(m_text));
    m_drop_down_button->setToolTip(QString::fromStdString(m_tool_tip));
//    m_dropDownButton->setMaximumWidth(40);

    m_p_drop_down_menu = new QMenu();
    m_action_group     = new QActionGroup(m_p_drop_down_menu);

    for(const auto& item : m_items)
    {
        auto* action = new QAction(QString::fromStdString(item.second), m_p_drop_down_menu);
        action->setCheckable(true);
        action->setData(QVariant(item.first));
        m_action_group->addAction(action);
        m_p_drop_down_menu->addAction(action);

        if(item.first == m_selection)
        {
            action->setChecked(true);
        }
    }

    QObject::connect(m_action_group, &QActionGroup::triggered, this, &self_t::on_selection);
    m_drop_down_button->setMenu(m_p_drop_down_menu);

    auto* v_layout = new QVBoxLayout();
    v_layout->addWidget(m_drop_down_button);
    v_layout->setContentsMargins(0, 0, 0, 0);

    qt_container->set_layout(v_layout);
}

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

void selection_menu_button::stopping()
{
    QObject::disconnect(m_action_group, &QActionGroup::triggered, this, &self_t::on_selection);
    for(QAction* action : m_action_group->actions())
    {
        m_action_group->removeAction(action);
    }

    this->destroy();
}

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

void selection_menu_button::updating()
{
}

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

void selection_menu_button::info(std::ostream& /*_sstream*/)
{
}

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

void selection_menu_button::on_selection(QAction* _action)
{
    if(_action->isChecked())
    {
        int value = _action->data().toInt();
        m_sig_selected->async_emit(value);
        return;
    }
}

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

void selection_menu_button::set_enabled(bool _enabled)
{
    m_drop_down_button->setEnabled(_enabled);
}

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

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

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

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

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

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