File: action.hpp

package info (click to toggle)
sight 25.2.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 42,180 kB
  • sloc: cpp: 289,476; xml: 17,257; ansic: 9,878; python: 1,379; sh: 144; makefile: 33
file content (234 lines) | stat: -rw-r--r-- 9,086 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
/************************************************************************
 *
 * 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/>.
 *
 ***********************************************************************/

#pragma once

#include <sight/ui/__/config.hpp>

#include <data/boolean.hpp>

#include <service/base.hpp>

namespace sight::ui
{

namespace detail::registry
{

class action;

}

/**
 * @brief   Defines the service interface managing menu or toolbar items. It can be used with properties or only with
 * signals.
 *
 * @section Signals Signals
 * - \b is_checked(bool): Emitted when the action is checked or unchecked.
 * - \b checked(): Emitted when the action is checked.
 * - \b unchecked(): Emitted when the action is unchecked.
 * - \b is_enabled(bool): Emitted when the action is enabled or disabled.
 * - \b enabled(): Emitted when the action is enabled.
 * - \b disabled(): Emitted when the action is disabled.
 * - \b is_visible(bool): Emitted when the action is visible or invisible
 *
 * @section Slots Slots
 * - \b set_checked(bool): sets whether the action is checked.
 * - \b check(): check the action.
 * - \b uncheck(): uncheck the action.
 * - \b set_visible(bool): sets whether the action is visible in its container.
 * - \b set_hidden(bool): sets whether the action is hidden in its container.
 * - \b show(): make the action visible.
 * - \b hide(): make the action invisible.
 * - \b toggle_visibility(): make the action visible if it was invisible or invisible if it was visible
 * - \b set_enabled(bool): sets whether the action can be interacted with.
 * - \b set_disabled(bool): opposite of setEnabled(bool).
 * - \b enable(): make the action interactive.
 * - \b disable(): make the action not interactive.
 *
 * Example of configuration using properties
 * @code{.xml}
    <service uid="item" type="sight::module::ui::action" >
        <properties checked="false" enabled="false" inverse="true" visible="true" />
        <confirmation message="..." />
    </service>
   @endcode
 *
 * Example of configuration using signals
 * @code{.xml}
    <service uid="item" type="sight::module::ui::action" >
        <state checked="false" enabled="false" inverse="true" visible="true" />
        <confirmation message="..." />
    </service>
   @endcode
 *
 * All configurations options are optional.
 * - \<state checked="false" enabled="false" /\> : fix the state of the action in the menu and Toolbar.
 *   - \b enabled not mandatory (Default value true ) : allows to enable/disable the execution of the action.
 *     If the action appears in the interface it will be enabled/disabled.
 *   - \b checked not mandatory (Default value false ):
 *     If the action appears in the interface it will be checked/unchecked.
 *   - \b visible not mandatory (Default value true ):
 *     If true, the action is visible in the interface (and if the action is associated to a menu and/or a toolbar).
 *   - \b inverse not mandatory (Default value true) : allow to invert the state of the action (if "check")
 * - \b sync: set to true to emit the 'checked' signals synchronously instead of the default, asynchronously.
 * - \<confirmation message="..." /\> : configure if the action must be confirmed by the user before executing it.
 *   - \b message not mandatory : if not empty the message is shown in dialog box.
 *   - \b defaultButton (optional) (default defined by the gui backend): dialog default button (true or false)
 */
class SIGHT_UI_CLASS_API action : public sight::service::base
{
public:

    SIGHT_DECLARE_SERVICE(action, service::base);
    SIGHT_ALLOW_SHARED_FROM_THIS();

    struct signals
    {
        using bool_t = core::com::signal<void (bool)>;
        using void_t = core::com::signal<void ()>;

        static inline const core::com::signals::key_t IS_ENABLED = "is_enabled";
        static inline const core::com::signals::key_t ENABLED    = "enabled";
        static inline const core::com::signals::key_t DISABLED   = "disabled";
        static inline const core::com::signals::key_t IS_CHECKED = "is_checked";
        static inline const core::com::signals::key_t CHECKED    = "checked";
        static inline const core::com::signals::key_t UNCHECKED  = "unchecked";
        static inline const core::com::signals::key_t IS_VISIBLE = "is_visible";
    };

    struct slots
    {
        static inline const core::com::slots::key_t SET_CHECKED       = "set_checked";
        static inline const core::com::slots::key_t CHECK             = "check";
        static inline const core::com::slots::key_t UNCHECK           = "uncheck";
        static inline const core::com::slots::key_t APPLY_CHECKED     = "apply_checked";
        static inline const core::com::slots::key_t SET_VISIBLE       = "set_visible";
        static inline const core::com::slots::key_t SET_HIDDEN        = "set_hidden";
        static inline const core::com::slots::key_t SHOW              = "show";
        static inline const core::com::slots::key_t HIDE              = "hide";
        static inline const core::com::slots::key_t TOGGLE_VISIBILITY = "toggle_visibility";
        static inline const core::com::slots::key_t APPLY_VISIBLE     = "apply_visible";
        static inline const core::com::slots::key_t SET_ENABLED       = "set_enabled";
        static inline const core::com::slots::key_t SET_DISABLED      = "set_disabled";
        static inline const core::com::slots::key_t ENABLE            = "enable";
        static inline const core::com::slots::key_t DISABLE           = "disable";
        static inline const core::com::slots::key_t APPLY_ENABLED     = "apply_enabled";
    };

    /// Method called when the action service is stopping
    SIGHT_UI_API void action_service_stopping();

    /// Method called when the action service is starting
    SIGHT_UI_API void action_service_starting();

    /// Checks or unchecks the action service.
    SIGHT_UI_API virtual void set_checked(bool _checked);

    /// Sets the action service executable or not.
    [[nodiscard]] SIGHT_UI_API bool checked() const;

    /// Enables or disables the action service.
    SIGHT_UI_API void set_enabled(bool _enabled);

    /// Sets the action service executable or not.
    [[nodiscard]] SIGHT_UI_API bool enabled() const;

    /// Shows or hides the action.
    SIGHT_UI_API void set_visible(bool _is_visible);

    /// Shows the action.
    SIGHT_UI_API void show();

    /// Hides the action.
    SIGHT_UI_API void hide();

    /// Returns true if action is visible
    [[nodiscard]] SIGHT_UI_API bool visible() const;

    /// Returns true if the active state is inverted.
    [[nodiscard]] SIGHT_UI_API bool inverted() const;

    /**
     * @brief Confirms that the action must be executed.
     *
     * If action is configured to be confirmed : show a dialog box to confirm execution.
     * Else return true
     *
     * @return true if user click on 'true' button.
     */
    SIGHT_UI_API bool confirm_action();

protected:

    SIGHT_UI_API action();
    SIGHT_UI_API ~action() override = default;

    /// Initializes the action. This should be called in the configuring() method in derived classes.
    SIGHT_UI_API void initialize();

    /// Connects the properties
    SIGHT_UI_API service::connections_t auto_connections() const override;

private:

    SPTR(ui::detail::registry::action) m_registry;

    sight::data::property<sight::data::boolean> m_checked {this, "checked", false};
    sight::data::property<sight::data::boolean> m_enabled {this, "enabled", true};
    sight::data::property<sight::data::boolean> m_visible {this, "visible", true};
    sight::data::property<sight::data::boolean> m_inverse {this, "inverse", false};

    std::optional<bool> m_prev_checked;
    std::optional<bool> m_prev_enabled;
    std::optional<bool> m_prev_visible;

    bool m_confirm_action {false};
    bool m_default_button {false};
    std::string m_confirm_message;
};

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

class lock_action
{
public:

    lock_action(action::wptr _action) :
        m_action(_action)
    {
        m_action.lock()->set_enabled(false);
    }

    ~lock_action()
    {
        m_action.lock()->set_enabled(true);
    }

private:

    action::wptr m_action;
};

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

} // namespace sight::ui