File: frame_manager.cpp

package info (click to toggle)
sight 25.0.0-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 43,108 kB
  • sloc: cpp: 306,170; xml: 18,037; ansic: 9,960; python: 1,379; sh: 144; makefile: 33
file content (241 lines) | stat: -rw-r--r-- 8,332 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
/************************************************************************
 *
 * 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 "ui/__/layout/frame_manager.hpp"

#include <core/base.hpp>
#include <core/runtime/path.hpp>

#include <data/integer.hpp>
#include <data/string.hpp>

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

#include <ui/__/preferences.hpp>

#include <boost/lexical_cast.hpp>

#include <filesystem>

namespace sight::ui::layout
{

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

const frame_manager::registry_key_t frame_manager::REGISTRY_KEY = "sight::ui::layout::frame";

const std::string frame_manager::SOFTWARE_UI         = "SOFTWARE_UI";
const std::string frame_manager::FRAME_STATE_UI      = "FRAME_STATE_UI";
const std::string frame_manager::FRAME_SIZE_W_UI     = "FRAME_SIZE_W_UI";
const std::string frame_manager::FRAME_SIZE_H_UI     = "FRAME_SIZE_H_UI";
const std::string frame_manager::FRAME_POSITION_X_UI = "FRAME_POSITION_X_UI";
const std::string frame_manager::FRAME_POSITION_Y_UI = "FRAME_POSITION_Y_UI";

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

inline static std::string get_frame_key(const std::string& _name)
{
    return std::string(frame_manager::SOFTWARE_UI) + "." + _name;
}

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

inline static std::string get_frame_state_key(const std::string& _name)
{
    return get_frame_key(_name) + "." + frame_manager::FRAME_STATE_UI;
}

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

inline static std::string get_frame_w_key(const std::string& _name)
{
    return get_frame_key(_name) + "." + frame_manager::FRAME_SIZE_W_UI;
}

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

inline static std::string get_frame_h_key(const std::string& _name)
{
    return get_frame_key(_name) + "." + frame_manager::FRAME_SIZE_H_UI;
}

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

inline static std::string get_frame_x_key(const std::string& _name)
{
    return get_frame_key(_name) + "." + frame_manager::FRAME_POSITION_X_UI;
}

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

inline static std::string get_frame_y_key(const std::string& _name)
{
    return get_frame_key(_name) + "." + frame_manager::FRAME_POSITION_Y_UI;
}

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

frame_manager::frame_manager()
{
    this->frame_manager::set_close_callback(default_close_callback);
}

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

void frame_manager::initialize(const ui::config_t& _configuration)
{
    m_frame_info.m_name       = _configuration.get<std::string>("name", m_frame_info.m_name);
    m_frame_info.m_version    = _configuration.get<std::string>("version", "");
    m_frame_info.m_visibility = _configuration.get<bool>("visibility", m_frame_info.m_visibility);

    if(const auto icon = _configuration.get_optional<std::string>("icon"); icon.has_value())
    {
        m_frame_info.m_icon_path = core::runtime::get_module_resource_file_path(icon.value());
        SIGHT_ASSERT(
            "The icon " << m_frame_info.m_icon_path << " doesn't exist, please ensure that the path is correct",
            std::filesystem::exists(m_frame_info.m_icon_path)
        );
    }

    m_frame_info.m_min_size.first  = _configuration.get<int>("minSize.<xmlattr>.width", m_frame_info.m_min_size.first);
    m_frame_info.m_min_size.second =
        _configuration.get<int>("minSize.<xmlattr>.height", m_frame_info.m_min_size.second);
    m_frame_info.m_max_size.first  = _configuration.get<int>("maxSize.<xmlattr>.width", m_frame_info.m_max_size.first);
    m_frame_info.m_max_size.second = _configuration.get<int>(
        "maxSize.<xmlattr>.height",
        m_frame_info.m_max_size.second
    );

    if(const auto mode = _configuration.get_optional<std::string>("style.<xmlattr>.mode"); mode.has_value())
    {
        if(mode.value() == "DEFAULT")
        {
            m_frame_info.m_style = DEFAULT;
        }
        else if(mode.value() == "STAY_ON_TOP")
        {
            m_frame_info.m_style = stay_on_top;
        }
        else if(mode.value() == "MODAL")
        {
            m_frame_info.m_style = modal;
        }
        else if(mode.value() == "FULLSCREEN")
        {
            m_frame_info.m_style = fullscreen;
        }
        else
        {
            SIGHT_FATAL(
                "The style " << mode.value() << " is unknown, it should be DEFAULT, STAY_ON_TOP, FULLSCREEN or MODAL."
            );
        }
    }

    if(const auto qss_class = _configuration.get_optional<std::string>(
           "style.<xmlattr>.QSSClass"
    ); qss_class.has_value())
    {
        m_frame_info.m_qss_class = qss_class.get();
    }

    m_frame_info.m_screen = _configuration.get<int>("screen.<xmlattr>.index", m_frame_info.m_screen);

    this->read_config();
}

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

void frame_manager::set_close_callback(CloseCallback _fct)
{
    this->m_close_callback = _fct;
}

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

void frame_manager::default_close_callback()
{
    SIGHT_WARN("No specific close callback defined");
}

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

void frame_manager::read_config()
{
    try
    {
        ui::preferences preferences;

        m_frame_info.m_state =
            static_cast<frame_state>(preferences.get(
                                         get_frame_state_key(m_frame_info.m_name),
                                         static_cast<std::uint8_t>(m_frame_info.m_state)
            ));
        m_frame_info.m_size.first =
            preferences.get(get_frame_w_key(m_frame_info.m_name), m_frame_info.m_size.first);
        m_frame_info.m_size.second =
            preferences.get(get_frame_h_key(m_frame_info.m_name), m_frame_info.m_size.second);
        m_frame_info.m_position.first = preferences.get(
            get_frame_x_key(m_frame_info.m_name),
            m_frame_info.m_position.first
        );
        m_frame_info.m_position.second = preferences.get(
            get_frame_y_key(m_frame_info.m_name),
            m_frame_info.m_position.second
        );
    }
    catch(const ui::preferences_disabled&)
    {
        return;
    }
}

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

void frame_manager::write_config() const
{
    try
    {
        ui::preferences preferences;

        preferences.put(
            get_frame_state_key(m_frame_info.m_name),
            static_cast<std::uint8_t>(
                m_frame_info.m_state == frame_state::iconized ? frame_state::normal : m_frame_info.m_state
            )
        );

        preferences.put(get_frame_w_key(m_frame_info.m_name), m_frame_info.m_size.first);
        preferences.put(get_frame_h_key(m_frame_info.m_name), m_frame_info.m_size.second);
        preferences.put(get_frame_x_key(m_frame_info.m_name), m_frame_info.m_position.first);
        preferences.put(get_frame_y_key(m_frame_info.m_name), m_frame_info.m_position.second);
    }
    catch(const ui::preferences_disabled&)
    {
        return;
    }
}

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

} // namespace sight::ui::layout