File: action.hpp

package info (click to toggle)
sight 25.2.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 42,184 kB
  • sloc: cpp: 289,476; xml: 17,257; ansic: 9,878; python: 1,379; sh: 144; makefile: 33
file content (128 lines) | stat: -rw-r--r-- 4,538 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
/************************************************************************
 *
 * Copyright (C) 2022-2025 IRCAD France
 *
 * 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 <data/boolean.hpp>

#include <io/joystick/interactor.hpp>

#include <ui/__/action.hpp>
#include <ui/__/parameter.hpp>

namespace sight::module::ui
{

/**
 * @brief  Default action for a simple toolbar button or menu item.*
 * Common signals and slots options can be found in the interface @see action.
 *
 * @section Signals Signals
 * - \b clicked(): Emitted when the action is clicked.
 * - \b parameter_changed(parameter_t, std::string): Emitted when the action is clicked or setChecked(true|false).
 *
 * Example of configuration
 * @code{.xml}
    <service uid="..." type="sight::module::ui::action" >
        <properties checked="false" enabled="false" inverse="true" visible="true" />
        <sync>true</sync>
        <confirmation message="..." />
        <!-- will send parameterChanged("left", "position") when checked and parameterChanged("center", "position") when
 * unchecked -->
        <parameter key="position" checked="left" unchecked="center" />
    </service>
   @endcode
 *
 * @subsection In-Out In-Out
 * - \b state [sight::data::boolean] (optional): data object storing the state of the action.
 *
 * All configurations options are optional. Common action options can be found in @see action.
 * - \b sync: set to true to emit the 'clicked' signals synchronously instead of the default, asynchronously.
 * - \b parameter: set the key and values to send in parameterChanged signal on click and setChecked(true|false).
 *   - \b key: the key used in parameterChanged signal
 *   - \b clicked: the value used in parameterChanged signal on click
 *   - \b checked: the value used in parameterChanged signal on setChecked(true)
 *   - \b unchecked: the value used in parameterChanged signal on setChecked(false)
 */
class action final : public sight::ui::action,
                     public sight::io::joystick::interactor
{
public:

    struct signals final
    {
        using key_t = core::com::signals::key_t;
        inline static const key_t CLICKED           = "clicked";
        inline static const key_t PARAMETER_CHANGED = "parameter_changed";

        using changed_t = core::com::signal<void (sight::ui::parameter_t, std::string)>;
    };

    SIGHT_DECLARE_SERVICE(action, sight::ui::action);

    action() noexcept = default;
    ~action() noexcept final = default;

protected:

    void configuring() final;

    void starting() final;

    void stopping() final;

    /// Emits the clicked signal
    void updating() final;

    /**
     * @brief Manage joystick events
     *
     * @param _event
     */
    void joystick_axis_direction_event(const sight::io::joystick::axis_direction_event& _event) final;

private:

    /// Signals
    const sight::ui::action::signals::void_t::sptr m_clicked_sig {new_signal<sight::ui::action::signals::void_t>(
                                                                      signals::CLICKED
    )
    };
    const signals::changed_t::sptr m_parameter_changed_sig {
        new_signal<signals::changed_t>(signals::PARAMETER_CHANGED)
    };

    /// Configuration option to emit the clicked signal synchronously or asynchronously
    bool m_sync {false};

    /// The optional key used in parameterChanged signal
    std::optional<std::string> m_key;

    /// The optional values used in parameterChanged signal on click and setChecked(true|false)
    std::optional<std::string> m_clicked;
    std::optional<std::string> m_checked;
    std::optional<std::string> m_unchecked;

    /// Left/Right or none for joystick usage.
    sight::io::joystick::joystick_t m_joystick_alias {sight::io::joystick::joystick_t::unknown};
};

} // namespace sight::module::ui