File: interaction.hpp

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 (239 lines) | stat: -rw-r--r-- 7,167 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
/************************************************************************
 *
 * Copyright (C) 2022-2024 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 <sight/ui/test/config.hpp>

#include <QPoint>
#include <QWidget>
#include <QWindow>

#include <string>

namespace sight::ui::test
{

/// Represents a user interaction on the interace, such as a mouse click, keyboard tap...
class SIGHT_UI_TEST_CLASS_API interaction
{
public:

    /// Destructor. Does nothing.
    virtual ~interaction() = default;

    /// Use this interaction to interact with a graphic component.
    /// @{
    virtual void interact_with(QWidget* _widget) const = 0;
    virtual void interact_with(QWindow* _window) const = 0;
    /// @}

    /// Returns a string representation of the interaction.
    [[nodiscard]] virtual std::string to_string() const = 0;
};

/// Represents a mouse click.
class SIGHT_UI_TEST_CLASS_API mouse_click : public interaction
{
public:

    SIGHT_UI_TEST_API mouse_click(
        Qt::MouseButton _button          = Qt::MouseButton::LeftButton,
        Qt::KeyboardModifiers _modifiers = Qt::NoModifier,
        const QPoint& _pos               = QPoint()
    );

    void interact_with(QWidget* _widget) const override;
    void interact_with(QWindow* _window) const override;

    [[nodiscard]] std::string to_string() const override;

private:

    template<typename T>
    void interact_with(T _thing) const;

    Qt::MouseButton m_button;
    Qt::KeyboardModifiers m_modifiers;
    QPoint m_pos;
};

/// Represents a mouse double click.
class SIGHT_UI_TEST_CLASS_API mouse_double_click : public interaction
{
public:

    SIGHT_UI_TEST_API mouse_double_click(
        Qt::MouseButton _button          = Qt::MouseButton::LeftButton,
        Qt::KeyboardModifiers _modifiers = Qt::NoModifier,
        const QPoint& _pos               = QPoint()
    );

    void interact_with(QWidget* _widget) const override;
    void interact_with(QWindow* _window) const override;

    [[nodiscard]] std::string to_string() const override;

private:

    template<typename T>
    void interact_with(T _thing) const;

    Qt::MouseButton m_button;
    Qt::KeyboardModifiers m_modifiers;
    QPoint m_pos;
};

/// Represents a dragging of the mouse over a component.
class SIGHT_UI_TEST_CLASS_API mouse_drag : public interaction
{
public:

    SIGHT_UI_TEST_API mouse_drag(
        const QPoint& _from,
        const QPoint& _to,
        Qt::MouseButton _button          = Qt::MouseButton::LeftButton,
        Qt::KeyboardModifiers _modifiers = Qt::NoModifier
    );

    void interact_with(QWidget* _widget) const override;
    void interact_with(QWindow* _window) const override;

    [[nodiscard]] std::string to_string() const override;

private:

    template<typename T>
    void interact_with(T _thing) const;

    QPoint m_from;
    QPoint m_to;
    Qt::MouseButton m_button;
    Qt::KeyboardModifiers m_modifiers;
};

/// Represents the use of the mouse wheel over a component.
class SIGHT_UI_TEST_CLASS_API mouse_wheel : public interaction
{
public:

    /**
     * @param _angle_delta relative amount that the wheel is to be rotated, in eighths of a degree. A positive value
     * means
     * that the wheel is rotated forwards away from the user.
     * The x part of the QPoint represents a horizontal wheel move, while the y part is the classical top vertical wheel
     * move.
     * Most mouse types work in steps of 15 degrees, in which case the delta value is a multiple of 120;
     *  i.e., 120 units * 1/8 = 15 degrees.
     * For a classical test this value can be set to QPoint(0, 1200) for a 150° turn of the top wheel.
     * see @ref https://doc.qt.io/qt-5/qwheelevent.html#angleDelta for more information
     * @param _modifiers the keyboard modifier to be held when the mouse wheel is used
     * @param _position the position of the widget where the mouse wheel is to be rotated
     */
    SIGHT_UI_TEST_API mouse_wheel(
        const QPoint& _angle_delta,
        Qt::KeyboardModifiers _modifiers = Qt::NoModifier,
        const QPoint& _position          = QPoint()
    );

    void interact_with(QWidget* _widget) const override;
    void interact_with(QWindow* _window) const override;

    [[nodiscard]] std::string to_string() const override;

private:

    template<typename T>
    void interact_with(T _thing) const;

    QPoint m_angle_delta;
    Qt::KeyboardModifiers m_modifiers;
    QPoint m_position;
};

/// Represents the typing of a sequence of printable caracters.
class SIGHT_UI_TEST_CLASS_API keyboard_sequence : public interaction
{
public:

    SIGHT_UI_TEST_API keyboard_sequence(std::string _text, Qt::KeyboardModifiers _modifiers = Qt::NoModifier);

    void interact_with(QWidget* _widget) const override;
    void interact_with(QWindow* _window) const override;
    [[nodiscard]] std::string to_string() const override;

private:

    template<typename T>
    void interact_with(T _thing) const;

    std::string m_text;
    Qt::KeyboardModifiers m_modifiers;
};

/// Represents a key tap.
class SIGHT_UI_TEST_CLASS_API keyboard_click : public interaction
{
public:

    SIGHT_UI_TEST_API keyboard_click(Qt::Key _key, Qt::KeyboardModifiers _modifiers = Qt::NoModifier);

    void interact_with(QWidget* _widget) const override;
    void interact_with(QWindow* _window) const override;
    [[nodiscard]] std::string to_string() const override;

private:

    template<typename T>
    void interact_with(T _thing) const;

    Qt::Key m_key;
    Qt::KeyboardModifiers m_modifiers;
};

/// Represents a pinch gesture with two fingers on a component.
class SIGHT_UI_TEST_CLASS_API pinch_gesture : public interaction
{
public:

    /**
     * @param _first_finger_pos the starting position and ending position of the first finger
     * @param _second_finger_pos the starting position and ending position of the second finger
     */
    SIGHT_UI_TEST_API pinch_gesture(
        std::pair<QPoint, QPoint> _first_finger_pos,
        std::pair<QPoint, QPoint> _second_finger_pos
    );

    void interact_with(QWidget* _widget) const override;
    void interact_with(QWindow* _window) const override;
    [[nodiscard]] std::string to_string() const override;

private:

    template<typename T>
    void interact_with(T _thing) const;

    std::pair<QPoint, QPoint> m_first_finger_pos;
    std::pair<QPoint, QPoint> m_second_finger_pos;
};

} // namespace sight::ui::testCore