File: registry.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 (553 lines) | stat: -rw-r--r-- 20,221 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
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
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
/************************************************************************
 *
 * Copyright (C) 2009-2024 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 "ui/__/registry.hpp"

#include "ui/__/menu.hpp"
#include "ui/__/toolbar.hpp"

#include <core/id.hpp>

#include <service/macros.hpp>
#include <service/op.hpp>

namespace sight::ui::registry
{

static container_map_t m_global_sid_to_fw_container;
static container_map_t m_global_wid_to_fw_container;
static menu_bar_map_t m_global_sid_to_fw_menu_bar;
static tool_bar_map_t m_global_sid_to_fw_tool_bar;
static menu_map_t m_global_sid_to_fw_menu;

/// Parent sid can be menu sid or tool_bar sid
static action_to_parent_map_type m_action_sid_to_parent_sid;

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

void register_sid_container(std::string _sid, ui::container::widget::sptr _container)
{
    SIGHT_ASSERT(
        "The service '" + _sid + "' is already registered in the container map, it cannot be added twice.",
        m_global_sid_to_fw_container.find(_sid) == m_global_sid_to_fw_container.end()
    );
    m_global_sid_to_fw_container[_sid] = _container;
}

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

void unregister_sid_container(std::string _sid)
{
    bool service_exists = core::id::exist(_sid);
    SIGHT_INFO_IF("The service '" + _sid + "' does not exist.", !service_exists);
    if(service_exists)
    {
        service::base::sptr service = service::get(_sid);
        SIGHT_ASSERT(
            "The service '" + _sid + "' must be stopped before unregistering the container.",
            service->stopped()
        );
    }

    SIGHT_ASSERT(
        "The service '" + _sid + "' is not registered in the container map, it cannot be removed.",
        m_global_sid_to_fw_container.find(_sid) != m_global_sid_to_fw_container.end()
    );

    // Removes container in SID container map
    m_global_sid_to_fw_container.erase(_sid);
}

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

ui::container::widget::sptr get_sid_container(std::string _sid)
{
    // Returns container in SID container map, null if  not found.
    if(!has_sid_container(_sid))
    {
        return nullptr;
    }

    return m_global_sid_to_fw_container[_sid];
}

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

bool has_sid_container(std::string _sid)
{
    // Returns 'true' if the specified 'sid' is found in the SID container map, else 'false'.
    return m_global_sid_to_fw_container.find(_sid) != m_global_sid_to_fw_container.end();
}

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

void register_wid_container(std::string _wid, ui::container::widget::sptr _container)
{
    SIGHT_ASSERT(
        "The wid container '" + _wid + "' is already registered in the container map, it cannot be added twice.",
        m_global_wid_to_fw_container.find(_wid) == m_global_wid_to_fw_container.end()
    );
    m_global_wid_to_fw_container[_wid] = _container;
}

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

void unregister_wid_container(std::string _wid)
{
    SIGHT_ASSERT(
        "The wid container '" + _wid + "' is not registered in the container map.",
        m_global_wid_to_fw_container.find(_wid) != m_global_wid_to_fw_container.end()
    );

    // Removes container in WID container map
    m_global_wid_to_fw_container.erase(_wid);
}

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

ui::container::widget::sptr get_wid_container(std::string _wid)
{
    // Returns container in WID container map, null if  not found.
    if(!has_wid_container(_wid))
    {
        return nullptr;
    }

    return m_global_wid_to_fw_container[_wid];
}

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

bool has_wid_container(std::string _wid)
{
    // Returns 'true' if the specified 'wid' is found in the WID container map, else 'false'.
    return m_global_wid_to_fw_container.find(_wid) != m_global_wid_to_fw_container.end();
}

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

void register_sid_menu_bar(std::string _sid, ui::container::menubar::sptr _menu_bar)
{
    SIGHT_ASSERT(
        "The menubar '" + _sid + "' is already registered in the container map, it cannot be added twice.",
        m_global_sid_to_fw_menu_bar.find(_sid) == m_global_sid_to_fw_menu_bar.end()
    );
    m_global_sid_to_fw_menu_bar[_sid] = _menu_bar;
}

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

void unregister_sid_menu_bar(std::string _sid)
{
    bool service_exists = core::id::exist(_sid);
    SIGHT_INFO_IF("The menubar '" + _sid + "' does not exist.", !service_exists);
    if(service_exists)
    {
        service::base::sptr service = service::get(_sid);
        SIGHT_ASSERT(
            "The service '" + _sid + "' must be stopped before unregistering the menubar.",
            service->stopped()
        );
    }

    SIGHT_ASSERT(
        "The menubar '" + _sid + "' is not registered in the container map, it cannot be removed.",
        m_global_sid_to_fw_menu_bar.find(_sid) != m_global_sid_to_fw_menu_bar.end()
    );

    // Removes container in SID container map
    m_global_sid_to_fw_menu_bar.erase(_sid);
}

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

ui::container::menubar::sptr get_sid_menu_bar(std::string _sid)
{
    SIGHT_ASSERT(
        "The menubar '" + _sid + "' is not registered in the container map.",
        m_global_sid_to_fw_menu_bar.find(_sid) != m_global_sid_to_fw_menu_bar.end()
    );
    // returns container in SID container map
    return m_global_sid_to_fw_menu_bar[_sid];
}

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

void register_sid_tool_bar(std::string _sid, ui::container::toolbar::sptr _tool_bar)
{
    SIGHT_ASSERT(
        "The toolbar '" + _sid + "' is already registered in the container map, it cannot be added twice.",
        m_global_sid_to_fw_tool_bar.find(_sid) == m_global_sid_to_fw_tool_bar.end()
    );
    m_global_sid_to_fw_tool_bar[_sid] = _tool_bar;
}

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

void unregister_sid_tool_bar(std::string _sid)
{
    bool service_exists = core::id::exist(_sid);
    SIGHT_INFO_IF("The toolbar '" + _sid + "' does not exist.", !service_exists);
    if(service_exists)
    {
        service::base::sptr service = service::get(_sid);
        SIGHT_ASSERT(
            "The service '" + _sid + "' must be stopped before unregistering the menubar.",
            service->stopped()
        );
    }

    SIGHT_ASSERT(
        "The toolbar '" + _sid + "' is not registered in the container map, it cannot be removed.",
        m_global_sid_to_fw_tool_bar.find(_sid) != m_global_sid_to_fw_tool_bar.end()
    );

    // Removes container in SID container map
    m_global_sid_to_fw_tool_bar.erase(_sid);
}

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

ui::container::toolbar::sptr get_sid_tool_bar(std::string _sid)
{
    SIGHT_ASSERT(
        "The toolbar '" + _sid + "' is not registered in the container map.",
        m_global_sid_to_fw_tool_bar.find(_sid) != m_global_sid_to_fw_tool_bar.end()
    );
    // returns container in SID container map
    return m_global_sid_to_fw_tool_bar[_sid];
}

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

void register_sid_menu(std::string _sid, ui::container::menu::sptr _menu)
{
    SIGHT_ASSERT(
        "The menu '" + _sid + "' is already registered in the container map, it cannot be added twice.",
        m_global_sid_to_fw_menu.find(_sid) == m_global_sid_to_fw_menu.end()
    );
    m_global_sid_to_fw_menu[_sid] = _menu;
}

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

void unregister_sid_menu(std::string _sid)
{
    bool service_exists = core::id::exist(_sid);
    SIGHT_INFO_IF("The menu " << _sid << " does not exist.", !service_exists);
    if(service_exists)
    {
        service::base::sptr service = service::get(_sid);
        SIGHT_ASSERT("The service '" + _sid + "' must be stopped before unregistering the menu.", service->stopped());
    }

    SIGHT_ASSERT(
        "The menu '" + _sid + "' is not registered in the container map, it cannot be removed.",
        m_global_sid_to_fw_menu.find(_sid) != m_global_sid_to_fw_menu.end()
    );

    // Removes container in SID container map
    m_global_sid_to_fw_menu.erase(_sid);
}

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

ui::container::menu::sptr get_sid_menu(std::string _sid)
{
    SIGHT_ASSERT(
        "The menu '" + _sid + "' is not registered in the container map.",
        m_global_sid_to_fw_menu.find(_sid) != m_global_sid_to_fw_menu.end()
    );
    // returns container in SID container map
    return m_global_sid_to_fw_menu[_sid];
}

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

void register_action_sid_to_parent_sid(std::string _action_sid, std::string _parent_sid)
{
    if(m_action_sid_to_parent_sid.find(_action_sid) != m_action_sid_to_parent_sid.end())
    {
        // action already exist in map
        SIGHT_ASSERT(
            std::string("The action '") + _action_sid + "' already exists for the parent '" + _parent_sid + "'",
            std::find(
                m_action_sid_to_parent_sid[_action_sid].begin(),
                m_action_sid_to_parent_sid[_action_sid].end(),
                _parent_sid
            ) == m_action_sid_to_parent_sid[_action_sid].end()
        );
    }

    m_action_sid_to_parent_sid[_action_sid].push_back(_parent_sid);
}

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

void unregister_action_sid_to_parent_sid(std::string _action_sid, std::string _parent_sid)
{
    SIGHT_ASSERT(
        "The action '" + _action_sid + "' is not registered.",
        m_action_sid_to_parent_sid.find(_action_sid) != m_action_sid_to_parent_sid.end()
    );

    if(m_action_sid_to_parent_sid[_action_sid].size() == 1)
    {
        // action has one parent
        bool service_exists = core::id::exist(_action_sid);
        SIGHT_INFO_IF("The service '" + _action_sid + "' does not exist.", !service_exists);
        if(service_exists)
        {
            service::base::sptr service = service::get(_action_sid);
            SIGHT_ASSERT(
                "The service '" + _action_sid + "' must be stopped before unregistering the action.",
                service->stopped()
            );
        }

        m_action_sid_to_parent_sid.erase(_action_sid);
    }
    else
    {
        // action has several parents
        auto iter =
            std::find(
                m_action_sid_to_parent_sid[_action_sid].begin(),
                m_action_sid_to_parent_sid[_action_sid].end(),
                _parent_sid
            );
        SIGHT_ASSERT(
            std::string("The action with the sid '") + _action_sid + "' has no parent named '" + _parent_sid + "'",
            iter != m_action_sid_to_parent_sid[_action_sid].end()
        );
        m_action_sid_to_parent_sid[_action_sid].erase(iter);
    }
}

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

void action_service_stopping(std::string _action_sid)
{
    if(m_action_sid_to_parent_sid.find(_action_sid) != m_action_sid_to_parent_sid.end())
    {
        parent_sids_t parent_sids = m_action_sid_to_parent_sid[_action_sid];

        for(const std::string& parent_sid : parent_sids)
        {
            bool service_exists = core::id::exist(parent_sid);
            SIGHT_INFO_IF(
                std::string("The service '") + parent_sid + "' managing the action '" + _action_sid
                + "' does not exist.",
                !service_exists
            );
            if(service_exists)
            {
                service::base::sptr service   = service::get(parent_sid);
                ui::menu::sptr menu_srv       = std::dynamic_pointer_cast<ui::menu>(service);
                ui::toolbar::sptr toolbar_srv = std::dynamic_pointer_cast<ui::toolbar>(service);
                if(menu_srv)
                {
                    menu_srv->action_service_stopping(_action_sid);
                }
                else if(toolbar_srv)
                {
                    toolbar_srv->action_service_stopping(_action_sid);
                }
                else
                {
                    SIGHT_FATAL(
                        std::string("The service '") + parent_sid + "' managing the action '"
                        + _action_sid + "' must be managed by a menu or a toolbar"
                    );
                }
            }
        }
    }
}

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

void action_service_starting(std::string _action_sid)
{
    if(m_action_sid_to_parent_sid.find(_action_sid) != m_action_sid_to_parent_sid.end())
    {
        parent_sids_t parent_sids = m_action_sid_to_parent_sid[_action_sid];

        for(const std::string& parent_sid : parent_sids)
        {
            bool service_exists = core::id::exist(parent_sid);
            SIGHT_INFO_IF(
                std::string("The service '") + parent_sid + "' managing the action '" + _action_sid
                + "' does not exist.",
                !service_exists
            );
            if(service_exists)
            {
                service::base::sptr service   = service::get(parent_sid);
                ui::menu::sptr menu_srv       = std::dynamic_pointer_cast<ui::menu>(service);
                ui::toolbar::sptr toolbar_srv = std::dynamic_pointer_cast<ui::toolbar>(service);
                if(menu_srv)
                {
                    menu_srv->action_service_starting(_action_sid);
                }
                else if(toolbar_srv)
                {
                    toolbar_srv->action_service_starting(_action_sid);
                }
                else
                {
                    SIGHT_FATAL(
                        std::string("The service '") + parent_sid + "' managing the action '"
                        + _action_sid + "' must be managed by a menu or a toolbar"
                    );
                }
            }
        }
    }
}

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

void action_service_set_checked(std::string _action_sid, bool _is_checked)
{
    if(m_action_sid_to_parent_sid.find(_action_sid) != m_action_sid_to_parent_sid.end())
    {
        parent_sids_t parent_sids = m_action_sid_to_parent_sid[_action_sid];

        for(const std::string& parent_sid : parent_sids)
        {
            bool service_exists = core::id::exist(parent_sid);
            SIGHT_INFO_IF(
                std::string("The service '") + parent_sid + "' managing the action '" + _action_sid
                + "' does not exist.",
                !service_exists
            );
            if(service_exists)
            {
                service::base::sptr service   = service::get(parent_sid);
                ui::menu::sptr menu_srv       = std::dynamic_pointer_cast<ui::menu>(service);
                ui::toolbar::sptr toolbar_srv = std::dynamic_pointer_cast<ui::toolbar>(service);
                if(menu_srv)
                {
                    menu_srv->action_service_set_checked(_action_sid, _is_checked);
                }
                else if(toolbar_srv)
                {
                    toolbar_srv->action_service_set_checked(_action_sid, _is_checked);
                }
                else
                {
                    SIGHT_FATAL(
                        std::string("The service '") + parent_sid + "' managing the action '"
                        + _action_sid + "' must be managed by a menu or a toolbar"
                    );
                }
            }
        }
    }
}

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

void action_service_set_enabled(std::string _action_sid, bool _is_enabled)
{
    if(m_action_sid_to_parent_sid.find(_action_sid) != m_action_sid_to_parent_sid.end())
    {
        parent_sids_t parent_sids = m_action_sid_to_parent_sid[_action_sid];

        for(const std::string& parent_sid : parent_sids)
        {
            bool service_exists = core::id::exist(parent_sid);
            SIGHT_INFO_IF(
                std::string("The service '") + parent_sid + "' managing the action '" + _action_sid
                + "' does not exist.",
                !service_exists
            );
            if(service_exists)
            {
                service::base::sptr service   = service::get(parent_sid);
                ui::menu::sptr menu_srv       = std::dynamic_pointer_cast<ui::menu>(service);
                ui::toolbar::sptr toolbar_srv = std::dynamic_pointer_cast<ui::toolbar>(service);
                if(menu_srv)
                {
                    menu_srv->action_service_set_enabled(_action_sid, _is_enabled);
                }
                else if(toolbar_srv)
                {
                    toolbar_srv->action_service_set_enabled(_action_sid, _is_enabled);
                }
                else
                {
                    SIGHT_FATAL(
                        std::string("The service '") + parent_sid + "' managing the action '"
                        + _action_sid + "' must be managed by a menu or a toolbar"
                    );
                }
            }
        }
    }
}

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

void action_service_set_visible(std::string _action_sid, bool _is_visible)
{
    if(m_action_sid_to_parent_sid.find(_action_sid) != m_action_sid_to_parent_sid.end())
    {
        parent_sids_t parent_sids = m_action_sid_to_parent_sid[_action_sid];

        for(const std::string& parent_sid : parent_sids)
        {
            bool service_exists = core::id::exist(parent_sid);
            SIGHT_INFO_IF(
                std::string("The service '") + parent_sid + "' managing the action '" + _action_sid
                + "' does not exist.",
                !service_exists
            );
            if(service_exists)
            {
                service::base::sptr service   = service::get(parent_sid);
                ui::menu::sptr menu_srv       = std::dynamic_pointer_cast<ui::menu>(service);
                ui::toolbar::sptr toolbar_srv = std::dynamic_pointer_cast<ui::toolbar>(service);
                if(menu_srv)
                {
                    menu_srv->action_service_set_visible(_action_sid, _is_visible);
                }
                else if(toolbar_srv)
                {
                    toolbar_srv->action_service_set_visible(_action_sid, _is_visible);
                }
                else
                {
                    SIGHT_FATAL(
                        std::string("The service '") + parent_sid + "' managing the action '"
                        + _action_sid + "' must be managed by a menu or a toolbar"
                    );
                }
            }
        }
    }
}

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

} // namespace sight::ui::registry