File: modify_layout.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 (277 lines) | stat: -rw-r--r-- 8,963 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
/************************************************************************
 *
 * 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 "modify_layout.hpp"

#include <service/op.hpp>

#include <ui/__/dialog/message.hpp>
#include <ui/__/registry.hpp>
#include <ui/__/service.hpp>
#include <ui/__/toolbar.hpp>

namespace sight::module::ui
{

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

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

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

    for(const auto& action_cfg : config.get_child("config"))
    {
        if(action_cfg.first == "move")
        {
            const auto uid = action_cfg.second.get<std::string>("<xmlattr>.uid");
            const auto wid = action_cfg.second.get<std::string>("<xmlattr>.wid");

            m_move_srv.emplace_back(uid, wid);
        }
        else if(action_cfg.first == "show"
                || action_cfg.first == "hide"
                || action_cfg.first == "show_or_hide"
                || action_cfg.first == "toggle")
        {
            visibility_t visibility = visibility_t::show;
            if(action_cfg.first == "show")
            {
                visibility = visibility_t::show;
            }
            else if(action_cfg.first == "hide")
            {
                visibility = visibility_t::hide;
            }
            else if(action_cfg.first == "show_or_hide")
            {
                visibility = visibility_t::show_or_hide;
            }
            else if(action_cfg.first == "toggle")
            {
                visibility = visibility_t::toggle;
            }
            else
            {
                SIGHT_FATAL("Unknown visiblity parameter value : " + action_cfg.first)
            }

            if(const auto wid = action_cfg.second.get_optional<std::string>("<xmlattr>.wid"); wid.has_value())
            {
                m_show_srv_wid.emplace_back(wid.value(), visibility);
            }
            else if(const auto sid = action_cfg.second.get_optional<std::string>("<xmlattr>.sid"); sid.has_value())
            {
                m_show_srv_sid.emplace_back(sid.value(), visibility);
            }
            else
            {
                SIGHT_ERROR("Attribute wid or sid missing");
            }
        }
        else if(action_cfg.first == "enable" || action_cfg.first == "disable")
        {
            const auto uid = action_cfg.second.get<std::string>("<xmlattr>.uid");
            bool is_enable = (action_cfg.first == "enable");

            m_enable_srv.emplace_back(uid, is_enable);
        }
        else
        {
            SIGHT_FATAL("Invalid tag name " << action_cfg.first);
        }
    }
}

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

void modify_layout::starting()
{
    this->action_service_starting();
}

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

void modify_layout::stopping()
{
    this->action_service_stopping();
}

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

void modify_layout::info(std::ostream& _sstream)
{
    _sstream << "Starter action" << std::endl;
}

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

void modify_layout::updating()
{
    for(const auto& elt : m_move_srv)
    {
        std::string uid = elt.first;
        std::string wid = elt.second;
        SIGHT_ASSERT(uid << " doesn't exist", core::id::exist(uid));
        service::base::sptr service = service::get(uid);
        SIGHT_ASSERT("service not found", service);
        auto container = std::dynamic_pointer_cast<sight::ui::service>(service);
        SIGHT_ASSERT("::ui::container dynamicCast failed", container);

        container->set_parent(wid);
        service->update();
    }

    for(const auto& elt : m_enable_srv)
    {
        std::string uid = elt.first;
        bool is_enable  = elt.second;
        SIGHT_ASSERT(uid << " doesn't exist", core::id::exist(uid));
        service::base::sptr service = service::get(uid);
        SIGHT_ASSERT("service not found", service);
        if(service->started())
        {
            auto container_srv = std::dynamic_pointer_cast<sight::ui::service>(service);
            if(container_srv)
            {
                container_srv->get_container()->set_enabled(is_enable);
            }

            auto action_srv = std::dynamic_pointer_cast<sight::ui::action>(service);
            if(action_srv)
            {
                action_srv->set_enabled(is_enable);
            }
        }
    }

    for(const auto& elt : m_show_srv_wid)
    {
        const std::string wid                        = elt.first;
        const auto visibility                        = elt.second;
        sight::ui::container::widget::sptr container =
            sight::ui::registry::get_wid_container(wid);
        SIGHT_ASSERT("::ui::container " << wid << " is unknown", container);

        if(visibility == visibility_t::show)
        {
            container->set_visible(true);
        }
        else if(visibility == visibility_t::hide)
        {
            container->set_visible(false);
        }
        else if(visibility == visibility_t::show_or_hide)
        {
            container->set_visible(this->checked());
        }
        else if(visibility == visibility_t::toggle)
        {
            container->set_visible(!container->is_shown_on_screen());
        }
        else
        {
            SIGHT_FATAL("Unknown visiblity parameter value");
        }
    }

    auto set_visible =
        [](service::base::sptr _service, bool _visible)
        {
            auto container_srv = std::dynamic_pointer_cast<sight::ui::service>(_service);
            if(container_srv)
            {
                sight::ui::container::widget::sptr container = container_srv->get_container();
                container->set_visible(_visible);
            }
            else
            {
                auto toolbar_srv = std::dynamic_pointer_cast<sight::ui::toolbar>(_service);
                if(toolbar_srv)
                {
                    toolbar_srv->set_visible(_visible);
                }
                else
                {
                    SIGHT_ASSERT(
                        "Cannot cast service " << std::quoted(_service->get_id()) << " as a UI service.",
                        false
                    );
                }
            }
        };

    auto visible =
        [](service::base::sptr _service)
        {
            if(auto container_srv = std::dynamic_pointer_cast<sight::ui::service>(_service); container_srv)
            {
                sight::ui::container::widget::sptr container = container_srv->get_container();
                return !container->is_shown_on_screen();
            }

            if(auto toolbar_srv = std::dynamic_pointer_cast<sight::ui::toolbar>(_service); toolbar_srv)
            {
                return toolbar_srv->visible();
            }

            SIGHT_ASSERT(
                "Cannot cast service " << std::quoted(_service->get_id()) << " as a UI service.",
                false
            );
            return false;
        };

    for(const auto& elt : m_show_srv_sid)
    {
        std::string uid       = elt.first;
        const auto visibility = elt.second;
        SIGHT_ASSERT(uid << " doesn't exist", core::id::exist(uid));
        service::base::sptr service = service::get(uid);

        if(visibility == visibility_t::show)
        {
            set_visible(service, true);
        }
        else if(visibility == visibility_t::hide)
        {
            set_visible(service, false);
        }
        else if(visibility == visibility_t::show_or_hide)
        {
            set_visible(service, this->checked());
        }
        else if(visibility == visibility_t::toggle)
        {
            set_visible(service, visible(service));
        }
        else
        {
            SIGHT_FATAL("Unknown visiblity parameter value");
        }
    }
}

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

} // namespace sight::module::ui