File: selector.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 (319 lines) | stat: -rw-r--r-- 10,266 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
/************************************************************************
 *
 * Copyright (C) 2009-2025 IRCAD France
 * Copyright (C) 2012-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 "selector.hpp"

#include "core/spy_log.hpp"

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

#include <data/series.hpp>
#include <data/series_set.hpp>

#include <ui/__/dialog/message.hpp>
#include <ui/qt/container/widget.hpp>
#include <ui/qt/series/selector.hpp>

#include <boost/range/iterator_range_core.hpp>

#include <QVBoxLayout>

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

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

static const core::com::signals::key_t SERIES_DOUBLE_CLICKED_SIG = "series_double_clicked";

static const core::com::slots::key_t ADD_SERIES_SLOT    = "addSeries";
static const core::com::slots::key_t REMOVE_SERIES_SLOT = "removeSeries";

static const std::string SELECTION_MODE_CONFIG    = "selectionMode";
static const std::string ALLOWED_REMOVE_CONFIG    = "allowedRemove";
static const std::string INSERT_MODE_CONFIG       = "insertMode";
static const std::string REMOVE_STUDY_ICON_CONFIG = "removeStudyIcon";
static const std::string REMOVE_SERIE_ICON_CONFIG = "removeSerieIcon";
static const std::string DISPLAYED_COLUMN_CONFIG  = "displayedColumns";

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

selector::selector()
{
    m_sig_series_double_clicked = new_signal<series_double_clicked_signal_t>(SERIES_DOUBLE_CLICKED_SIG);

    new_slot(ADD_SERIES_SLOT, &selector::add_series, this);
    m_slot_remove_series = new_slot(REMOVE_SERIES_SLOT, &selector::remove_series, this);
}

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

selector::~selector() noexcept =
    default;

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

void selector::configuring()
{
    this->sight::ui::service::initialize();

    const config_t tree = this->get_config();

    if(const auto& icons = tree.get_child_optional("icons"); icons.has_value())
    {
        for(const auto& elt : boost::make_iterator_range(icons->equal_range("icon")))
        {
            const auto series = elt.second.get<std::string>("<xmlattr>.series");
            SIGHT_ASSERT("'series' attribute is missing", !series.empty());

            const auto icon = elt.second.get<std::string>("<xmlattr>.icon");
            SIGHT_ASSERT("'icon' attribute is missing", !icon.empty());

            const auto file = core::runtime::get_resource_file_path(icon);
            m_series_icons[series] = file.string();
        }
    }

    const auto config = tree.get_child_optional("config");

    if(config)
    {
        const auto config_attr = config->get_child_optional("<xmlattr>");

        const auto remove_study_icon_cfg = config_attr->get_optional<std::string>(REMOVE_STUDY_ICON_CONFIG);
        if(remove_study_icon_cfg)
        {
            m_remove_study_icon = core::runtime::get_module_resource_file_path(remove_study_icon_cfg.value());
        }

        const auto remove_serie_icon_cfg = config_attr->get_optional<std::string>(REMOVE_SERIE_ICON_CONFIG);
        if(remove_serie_icon_cfg)
        {
            m_remove_series_icon = core::runtime::get_module_resource_file_path(remove_serie_icon_cfg.value());
        }

        const auto selection_mode = config_attr->get<std::string>(SELECTION_MODE_CONFIG, "extended");
        if(selection_mode == "single")
        {
            m_selection_mode = QAbstractItemView::SingleSelection;
        }
        else if(selection_mode == "extended")
        {
            m_selection_mode = QAbstractItemView::ExtendedSelection;
        }
        else
        {
            SIGHT_WARN(
                std::string("value ") + selection_mode + " is not managed for '"
                + SELECTION_MODE_CONFIG + "'"
            );
        }

        m_remove_allowed = config_attr->get<bool>(ALLOWED_REMOVE_CONFIG, m_remove_allowed);
        m_insert_mode    = config_attr->get<bool>(INSERT_MODE_CONFIG, m_insert_mode);

        m_displayed_columns = config_attr->get(DISPLAYED_COLUMN_CONFIG, m_displayed_columns);
    }
}

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

void selector::starting()
{
    this->sight::ui::service::create();

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

    m_selector_widget = new sight::ui::qt::series::selector(m_displayed_columns);
    m_selector_widget->set_series_icons(m_series_icons);
    m_selector_widget->setSelectionMode(m_selection_mode);
    m_selector_widget->allow_remove(m_remove_allowed);
    m_selector_widget->set_insert_mode(m_insert_mode);
    m_selector_widget->set_remove_study_icon(m_remove_study_icon);
    m_selector_widget->set_remove_series_icon(m_remove_series_icon);

    auto* layout = new QVBoxLayout();
    layout->setContentsMargins(0, 0, 0, 0);
    layout->addWidget(m_selector_widget);
    qt_container->set_layout(layout);

    QObject::connect(
        m_selector_widget,
        SIGNAL(
            series_selected(
                QVector<data::series::sptr>,
                QVector<data::series::sptr>
            )
        ),
        this,
        SLOT(
            on_selected_series(
                QVector<data::series::sptr>,
                QVector<data::series::sptr>
            )
        )
    );

    if(!m_insert_mode)
    {
        QObject::connect(
            m_selector_widget,
            SIGNAL(doubleClicked(const QModelIndex&)),
            this,
            SLOT(on_double_click(const QModelIndex&))
        );
    }

    if(m_remove_allowed)
    {
        QObject::connect(
            m_selector_widget,
            SIGNAL(remove_series(QVector<data::series::sptr>)),
            this,
            SLOT(on_remove_series(QVector<data::series::sptr>))
        );
    }

    this->updating();
}

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

service::connections_t selector::auto_connections() const
{
    connections_t connections;
    connections.push(SERIES_SET,data::series_set::ADDED_OBJECTS_SIG,ADD_SERIES_SLOT);
    connections.push(SERIES_SET,data::series_set::REMOVED_OBJECTS_SIG,REMOVE_SERIES_SLOT);

    return connections;
}

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

void selector::updating()
{
    const auto series_set = m_series_set.lock();

    m_selector_widget->clear();

    for(const auto& series : *series_set)
    {
        m_selector_widget->add_series(series);
    }
}

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

void selector::stopping()
{
    this->destroy();
}

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

void selector::on_selected_series(
    QVector<data::series::sptr> _selection,
    QVector<data::series::sptr> _deselection
)
{
    const auto selection_vector = m_selection.lock();
    const auto scoped_emitter   = selection_vector->scoped_emit();

    for(const auto& series : _deselection)
    {
        selection_vector->remove(series);
    }

    for(const auto& series : _selection)
    {
        selection_vector->push_back(series);
    }
}

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

void selector::on_double_click(const QModelIndex& _index)
{
    m_selector_widget->clearSelection();
    m_selector_widget->setCurrentIndex(_index);

    const auto selection_vector = m_selection.lock();

    if(m_selector_widget->get_item_type(_index) == sight::ui::qt::series::selector_model::series)
    {
        SIGHT_ASSERT("There must be only one object selected",selection_vector->size() == 1);
        data::object::sptr obj    = selection_vector->front();
        data::series::sptr series = std::dynamic_pointer_cast<data::series>(obj);
        SIGHT_ASSERT("Object must be a 'data::series'",series);

        m_sig_series_double_clicked->async_emit(series);
    }
}

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

void selector::on_remove_series(QVector<data::series::sptr> _selection)
{
    const auto series_set = m_series_set.lock();

    auto sig = series_set->signal<data::series_set::removed_signal_t>(data::series_set::REMOVED_OBJECTS_SIG);
    core::com::connection::blocker block(sig->get_connection(m_slot_remove_series));

    {
        const auto scoped_emitter = series_set->scoped_emit();

        for(const auto& series : _selection)
        {
            series_set->remove(series);
        }
    }
}

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

void selector::add_series(data::series_set::container_t _added_series)
{
    for(const auto& series : _added_series)
    {
        SIGHT_ASSERT("Series must not be null",series != nullptr);
        m_selector_widget->add_series(series);
    }
}

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

void selector::remove_series(data::series_set::container_t _removed_series)
{
    for(const auto& series : _removed_series)
    {
        SIGHT_ASSERT("Series must not be null",series != nullptr);
        m_selector_widget->remove_series(series);
    }
}

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

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