File: selector.cpp

package info (click to toggle)
sight 25.1.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 43,252 kB
  • sloc: cpp: 310,629; xml: 17,622; ansic: 9,960; python: 1,379; sh: 144; makefile: 33
file content (428 lines) | stat: -rw-r--r-- 14,765 bytes parent folder | download
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
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
/************************************************************************
 *
 * 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/base.hpp>
#include <core/com/signal.hxx>
#include <core/com/slots.hpp>
#include <core/com/slots.hxx>

#include <io/__/service/reader.hpp>
#include <io/__/service/writer.hpp>

#include <service/extension/factory.hpp>
#include <service/op.hpp>

#include <ui/__/cursor.hpp>
#include <ui/__/dialog/message.hpp>
#include <ui/__/dialog/selector.hpp>

#include <app/extension/config.hpp>

#include <boost/property_tree/xml_parser.hpp>
#include <boost/range/iterator_range_core.hpp>

#include <sstream>
#include <string>

namespace sight::module::ui::io
{

namespace io = sight::io;

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

selector::selector() :
    has_jobs(m_signals),
    m_sig_failed(new_signal<signals::failed_t>(signals::FAILED)),
    m_sig_succeeded(new_signal<signals::succeeded_t>(signals::SUCCEEDED)),
    m_slot_forward_job(new_slot(slots::FORWARD_JOB, &selector::forward_job, this))
{
}

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

void selector::configuring()
{
    const config_t srv_config = this->get_config();

    const std::string mode = srv_config.get<std::string>("type.<xmlattr>.mode", "reader");
    SIGHT_ASSERT(
        "The xml attribute <mode> must be 'reader' (to open file) or 'writer' (to write a new file).",
        mode == "writer" || mode == "reader"
    );
    m_mode = (mode == "writer") ? writer_mode : reader_mode;
    SIGHT_DEBUG("mode => " + mode);

    const std::string selection_mode = srv_config.get<std::string>("selection.<xmlattr>.mode", "exclude");
    SIGHT_ASSERT(
        "The xml attribute <mode> must be 'include' (to add the selection to selector list ) or "
        "'exclude' (to exclude the selection of the selector list).",
        selection_mode == "exclude" || selection_mode == "include"
    );
    m_services_are_excluded = (selection_mode == "exclude");
    SIGHT_DEBUG("selection mode => " + selection_mode);

    for(const auto& it_selection : boost::make_iterator_range(srv_config.equal_range("addSelection")))
    {
        const auto service = it_selection.second.get<std::string>("<xmlattr>.service");
        m_selected_services.push_back(service);
        SIGHT_DEBUG("add selection => " + service);

        const std::string config_id = it_selection.second.get<std::string>("<xmlattr>.config", "");
        if(!config_id.empty())
        {
            m_service_to_config[service] = config_id;
            SIGHT_DEBUG(std::string("add config '") + config_id + "' for service '" + service + "'");
        }
    }

    for(const auto& it_cfg : boost::make_iterator_range(srv_config.equal_range("config")))
    {
        const auto service   = it_cfg.second.get<std::string>("<xmlattr>.service");
        const auto config_id = it_cfg.second.get<std::string>("<xmlattr>.id");

        m_service_to_config[service] = config_id;
        SIGHT_DEBUG(std::string("add config '") + config_id + "' for service '" + service + "'");
    }
}

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

void selector::starting()
{
}

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

void selector::stopping()
{
}

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

void selector::updating()
{
    std::vector<std::string> available_extensions_id;
    {
        auto obj_lock          = m_data.lock();
        data::object::sptr obj = obj_lock.get_shared();

        // Retrieve implementation of type io::service::reader for this object
        if(m_mode == reader_mode)
        {
            SIGHT_ASSERT("An inout key '" + io::service::DATA_KEY + "' must be defined.", obj);
            const auto classname = obj->get_classname();

            available_extensions_id =
                service::extension::factory::get()->get_implementation_id_from_object_and_type(
                    classname,
                    "sight::io::service::reader"
                );
        }
        else // m_mode == WRITER_MODE
        {
            SIGHT_ASSERT("The inout key '" + io::service::DATA_KEY + "' is not correctly set.", obj);

            available_extensions_id =
                service::extension::factory::get()->get_implementation_id_from_object_and_type(
                    obj->get_classname(),
                    "sight::io::service::writer"
                );
        }
    }

    // filter available extensions and replace id by service description
    std::vector<std::pair<std::string, std::string> > available_extensions_map;
    std::vector<std::string> available_extensions_selector;

    for(const std::string& service_id : available_extensions_id)
    {
        bool service_is_selected_by_user =
            std::find(
                m_selected_services.begin(),
                m_selected_services.end(),
                service_id
            ) != m_selected_services.end();

        // Test if the service is considered here as available by users, if yes push in availableExtensionsSelector
        // excluded mode => add services that are not selected by users
        // included mode => add services selected by users
        if((m_services_are_excluded && !service_is_selected_by_user)
           || (!m_services_are_excluded && service_is_selected_by_user))
        {
            // Add this service
            std::string info_user =
                service::extension::factory::get()->get_service_description(service_id);

            auto iter = m_service_to_config.find(service_id);
            if(iter != m_service_to_config.end())
            {
                info_user = service::extension::config::get_default()->get_config_desc(iter->second);
            }

            if(!info_user.empty())
            {
                available_extensions_map.emplace_back(service_id, info_user);
                available_extensions_selector.push_back(info_user);
            }
            else
            {
                available_extensions_map.emplace_back(service_id, service_id);
                available_extensions_selector.push_back(service_id);
            }
        }
    }

    // Sort available services (lexical string sort)
    std::sort(available_extensions_selector.begin(), available_extensions_selector.end());

    // Test if we have an extension
    if(!available_extensions_map.empty())
    {
        std::string extension_id             = available_extensions_map[0].first;
        bool extension_selection_is_canceled = false;

        // Selection of extension when availableExtensions.size() > 1
        if(available_extensions_selector.size() > 1)
        {
            sight::ui::dialog::selector selector;

            if(m_mode != reader_mode)
            {
                selector.set_title("Writer to use");
            }
            else
            {
                selector.set_title("Reader to use");
            }

            selector.set_choices(available_extensions_selector);

            if(const auto& choices = selector.show(); !choices.empty())
            {
                const auto& choice      = choices.front();
                bool extension_id_found = false;

                using pair_t = std::pair<std::string, std::string>;
                for(const pair_t& pair : available_extensions_map)
                {
                    if(pair.second == choice)
                    {
                        extension_id       = pair.first;
                        extension_id_found = true;
                    }
                }

                if(!extension_id_found)
                {
                    m_sig_failed->async_emit();
                }

                SIGHT_ASSERT("Problem to find the selected string.", extension_id_found);
            }
            else
            {
                extension_selection_is_canceled = true;
            }
        }

        if(!extension_selection_is_canceled)
        {
            // Get config
            bool has_config_for_service = false;
            service::config_t srv_cfg;
            if(m_service_to_config.find(extension_id) != m_service_to_config.end())
            {
                has_config_for_service = true;
                srv_cfg                = service::extension::config::get_default()->get_service_config(
                    m_service_to_config[extension_id],
                    extension_id
                );
                SIGHT_ASSERT(
                    "No service configuration of type service::extension::config was found",
                    !srv_cfg.empty()
                );
            }

            // Configure and start service
            if(m_mode == reader_mode)
            {
                auto reader = service::add<io::service::reader>(extension_id);
                {
                    auto obj = m_data.lock();
                    reader->set_inout(obj.get_shared(), io::service::DATA_KEY);
                }

                reader->set_worker(this->worker());

                if(has_config_for_service)
                {
                    reader->set_config(srv_cfg);
                }

                reader->configure();

                auto job_created_signal_t = reader->signal(core::jobs::has_jobs::signals::JOB_CREATED);
                if(job_created_signal_t)
                {
                    job_created_signal_t->connect(m_slot_forward_job);
                }

                try
                {
                    reader->start();
                    reader->open_location_dialog();

                    sight::ui::cursor cursor;
                    cursor.set_cursor(sight::ui::cursor_base::busy);
                    reader->update();
                    cursor.set_default_cursor();

                    reader->stop();
                    service::unregister_service(reader);
                }
                catch(std::exception& e)
                {
                    std::string msg = "Failed to read : \n" + std::string(e.what());
                    sight::ui::dialog::message::show("Reader Error", msg);
                    m_sig_failed->async_emit();
                }
                if(reader->has_failed())
                {
                    m_sig_failed->async_emit();
                }
                else
                {
                    m_sig_succeeded->async_emit();
                }
            }
            else
            {
                auto writer = service::add<io::service::writer>(extension_id);
                {
                    auto obj = m_data.lock();
                    writer->set_input(obj.get_shared(), io::service::DATA_KEY);
                }

                writer->set_worker(this->worker());

                if(has_config_for_service)
                {
                    writer->set_config(srv_cfg);
                }

                writer->configure();

                auto job_created_signal_t = writer->signal("job_created");
                if(job_created_signal_t)
                {
                    job_created_signal_t->connect(m_slot_forward_job);
                }

                try
                {
                    writer->start();
                    writer->open_location_dialog();

                    sight::ui::cursor cursor;
                    cursor.set_cursor(sight::ui::cursor_base::busy);
                    writer->update();
                    cursor.set_default_cursor();

                    writer->stop();
                    service::unregister_service(writer);
                }
                catch(std::exception& e)
                {
                    std::string msg = "Failed to write : \n" + std::string(e.what());
                    sight::ui::dialog::message::show("Writer Error", msg);
                    m_sig_failed->async_emit();
                }

                if(writer->has_failed())
                {
                    m_sig_failed->async_emit();
                }
                else
                {
                    m_sig_succeeded->async_emit();
                }
            }
        }
        else
        {
            m_sig_failed->async_emit();
        }
    }
    else
    {
        SIGHT_WARN("selector::load : availableExtensions is empty.");
        if(m_mode == reader_mode)
        {
            sight::ui::dialog::message message_box;
            message_box.set_title("Reader not found");
            message_box.set_message("There are no available readers for this data type.");
            message_box.set_icon(sight::ui::dialog::message::warning);
            message_box.add_button(sight::ui::dialog::message::ok);
            message_box.show();
        }
        else // m_mode == WRITER_MODE
        {
            sight::ui::dialog::message message_box;
            message_box.set_title("Writer not found");
            message_box.set_message("There are no available writers for this data type.");
            message_box.set_icon(sight::ui::dialog::message::warning);
            message_box.add_button(sight::ui::dialog::message::ok);
            message_box.show();
        }

        m_sig_failed->async_emit();
    }
}

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

void selector::info(std::ostream& _sstream)
{
    // Update message
    _sstream << "selector";
}

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

void selector::set_io_mode(io_mode _mode)
{
    m_mode = _mode;
}

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

void selector::forward_job(core::jobs::base::sptr _job)
{
    this->emit(core::jobs::has_jobs::signals::JOB_CREATED, _job);
}

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

} // namespace sight::module::ui::io