Debian Codegraph @

src/Gui/InputHint.h

raw
1 // SPDX-License-Identifier: LGPL-2.1-or-later
2 /****************************************************************************
3 * *
4 * Copyright (c) 2024 Kacper Donat <[email protected]> *
5 * *
6 * This file is part of FreeCAD. *
7 * *
8 * FreeCAD is free software: you can redistribute it and/or modify it *
9 * under the terms of the GNU Lesser General Public License as *
10 * published by the Free Software Foundation, either version 2.1 of the *
11 * License, or (at your option) any later version. *
12 * *
13 * FreeCAD is distributed in the hope that it will be useful, but *
14 * WITHOUT ANY WARRANTY; without even the implied warranty of *
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
16 * Lesser General Public License for more details. *
17 * *
18 * You should have received a copy of the GNU Lesser General Public *
19 * License along with FreeCAD. If not, see *
20 * <https://www.gnu.org/licenses/>. *
21 * *
22 ***************************************************************************/
23
24 #ifndef GUI_INPUTHINT_H
25 #define GUI_INPUTHINT_H
26
27 #include "FCGlobal.h"
28
29 #include <Qt>
30 #include <QString>
31
32 #include <list>
33 #include <vector>
34
35 namespace Gui
36 {
37
38 /**
39 * @brief Representation of one input hint.
40 *
41 * Each input is essentially a short message defining action with reference to inputs that will
42 * trigger that action.
43 *
44 * For example,
45 * @code{c++}
46 * using enum InputHint::UserInput;
47 *
48 * // simple example of hint
49 * InputHint selectPointHint {
50 * .message = QWidget::tr("%1 select point"),
51 * .sequences = { MouseLeft }
52 * }
53 *
54 * // multiple sequences are also allowed in one hint
55 * InputHint lockAxisHint {
56 * .message = QWidget::tr("%1, %2 or %3 lock X, Y or Z axis"),
57 * .sequences = { KeyX, KeyY, KeyZ }
58 * }
59 *
60 * // hints can also use sequences consisting of multiple keys
61 * InputHint exitProgramHint {
62 * .message = QWidget::tr("%1 exit program"),
63 * .sequences = { {KeyAlt, KeyF4} }
64 * }
65 * @endcode
66 */
67 struct InputHint
68 {
69 enum class UserInput
70 {
71 // Modifier
72 ModifierShift = Qt::KeyboardModifier::ShiftModifier,
73 ModifierCtrl = Qt::KeyboardModifier::ControlModifier,
74 ModifierAlt = Qt::KeyboardModifier::AltModifier,
75 ModifierMeta = Qt::KeyboardModifier::MetaModifier,
76
77 // Keyboard Keys
78 KeySpace = Qt::Key_Space,
79 KeyExclam = Qt::Key_Exclam,
80 KeyQuoteDbl = Qt::Key_QuoteDbl,
81 KeyNumberSign = Qt::Key_NumberSign,
82 KeyDollar = Qt::Key_Dollar,
83 KeyPercent = Qt::Key_Percent,
84 KeyAmpersand = Qt::Key_Ampersand,
85 KeyApostrophe = Qt::Key_Apostrophe,
86 KeyParenLeft = Qt::Key_ParenLeft,
87 KeyParenRight = Qt::Key_ParenRight,
88 KeyAsterisk = Qt::Key_Asterisk,
89 KeyPlus = Qt::Key_Plus,
90 KeyComma = Qt::Key_Comma,
91 KeyMinus = Qt::Key_Minus,
92 KeyPeriod = Qt::Key_Period,
93 KeySlash = Qt::Key_Slash,
94 Key0 = Qt::Key_0,
95 Key1 = Qt::Key_1,
96 Key2 = Qt::Key_2,
97 Key3 = Qt::Key_3,
98 Key4 = Qt::Key_4,
99 Key5 = Qt::Key_5,
100 Key6 = Qt::Key_6,
101 Key7 = Qt::Key_7,
102 Key8 = Qt::Key_8,
103 Key9 = Qt::Key_9,
104 KeyColon = Qt::Key_Colon,
105 KeySemicolon = Qt::Key_Semicolon,
106 KeyLess = Qt::Key_Less,
107 KeyEqual = Qt::Key_Equal,
108 KeyGreater = Qt::Key_Greater,
109 KeyQuestion = Qt::Key_Question,
110 KeyAt = Qt::Key_At,
111 KeyA = Qt::Key_A,
112 KeyB = Qt::Key_B,
113 KeyC = Qt::Key_C,
114 KeyD = Qt::Key_D,
115 KeyE = Qt::Key_E,
116 KeyF = Qt::Key_F,
117 KeyG = Qt::Key_G,
118 KeyH = Qt::Key_H,
119 KeyI = Qt::Key_I,
120 KeyJ = Qt::Key_J,
121 KeyK = Qt::Key_K,
122 KeyL = Qt::Key_L,
123 KeyM = Qt::Key_M,
124 KeyN = Qt::Key_N,
125 KeyO = Qt::Key_O,
126 KeyP = Qt::Key_P,
127 KeyQ = Qt::Key_Q,
128 KeyR = Qt::Key_R,
129 KeyS = Qt::Key_S,
130 KeyT = Qt::Key_T,
131 KeyU = Qt::Key_U,
132 KeyV = Qt::Key_V,
133 KeyW = Qt::Key_W,
134 KeyX = Qt::Key_X,
135 KeyY = Qt::Key_Y,
136 KeyZ = Qt::Key_Z,
137 KeyBracketLeft = Qt::Key_BracketLeft,
138 KeyBackslash = Qt::Key_Backslash,
139 KeyBracketRight = Qt::Key_BracketRight,
140 KeyAsciiCircum = Qt::Key_AsciiCircum,
141 KeyUnderscore = Qt::Key_Underscore,
142 KeyQuoteLeft = Qt::Key_QuoteLeft,
143 KeyBraceLeft = Qt::Key_BraceLeft,
144 KeyBar = Qt::Key_Bar,
145 KeyBraceRight = Qt::Key_BraceRight,
146 KeyAsciiTilde = Qt::Key_AsciiTilde,
147
148 // misc keys
149 KeyEscape = Qt::Key_Escape,
150 KeyTab = Qt::Key_Tab,
151 KeyBacktab = Qt::Key_Backtab,
152 KeyBackspace = Qt::Key_Backspace,
153 KeyReturn = Qt::Key_Return,
154 KeyEnter = Qt::Key_Enter,
155 KeyInsert = Qt::Key_Insert,
156 KeyDelete = Qt::Key_Delete,
157 KeyPause = Qt::Key_Pause,
158 KeyPrintScr = Qt::Key_Print,
159 KeySysReq = Qt::Key_SysReq,
160 KeyClear = Qt::Key_Clear,
161
162 // cursor movement
163 KeyHome = Qt::Key_Home,
164 KeyEnd = Qt::Key_End,
165 KeyLeft = Qt::Key_Left,
166 KeyUp = Qt::Key_Up,
167 KeyRight = Qt::Key_Right,
168 KeyDown = Qt::Key_Down,
169 KeyPageUp = Qt::Key_PageUp,
170 KeyPageDown = Qt::Key_PageDown,
171
172 // modifiers
173 KeyShift = Qt::Key_Shift,
174 KeyControl = Qt::Key_Control,
175 KeyMeta = Qt::Key_Meta,
176 KeyAlt = Qt::Key_Alt,
177 KeyCapsLock = Qt::Key_CapsLock,
178 KeyNumLock = Qt::Key_NumLock,
179 KeyScrollLock = Qt::Key_ScrollLock,
180
181 // function keys
182 KeyF1 = Qt::Key_F1,
183 KeyF2 = Qt::Key_F2,
184 KeyF3 = Qt::Key_F3,
185 KeyF4 = Qt::Key_F4,
186 KeyF5 = Qt::Key_F5,
187 KeyF6 = Qt::Key_F6,
188 KeyF7 = Qt::Key_F7,
189 KeyF8 = Qt::Key_F8,
190 KeyF9 = Qt::Key_F9,
191 KeyF10 = Qt::Key_F10,
192 KeyF11 = Qt::Key_F11,
193 KeyF12 = Qt::Key_F12,
194 KeyF13 = Qt::Key_F13,
195 KeyF14 = Qt::Key_F14,
196 KeyF15 = Qt::Key_F15,
197 KeyF16 = Qt::Key_F16,
198 KeyF17 = Qt::Key_F17,
199 KeyF18 = Qt::Key_F18,
200 KeyF19 = Qt::Key_F19,
201 KeyF20 = Qt::Key_F20,
202 KeyF21 = Qt::Key_F21,
203 KeyF22 = Qt::Key_F22,
204 KeyF23 = Qt::Key_F23,
205 KeyF24 = Qt::Key_F24,
206 KeyF25 = Qt::Key_F25,
207 KeyF26 = Qt::Key_F26,
208 KeyF27 = Qt::Key_F27,
209 KeyF28 = Qt::Key_F28,
210 KeyF29 = Qt::Key_F29,
211 KeyF30 = Qt::Key_F30,
212 KeyF31 = Qt::Key_F31,
213 KeyF32 = Qt::Key_F32,
214 KeyF33 = Qt::Key_F33,
215 KeyF34 = Qt::Key_F34,
216 KeyF35 = Qt::Key_F35,
217
218 // numpad keys
219 KeyNum0 = static_cast<int>(Qt::Key_0) | static_cast<int>(Qt::KeypadModifier),
220 KeyNum1 = static_cast<int>(Qt::Key_1) | static_cast<int>(Qt::KeypadModifier),
221 KeyNum2 = static_cast<int>(Qt::Key_2) | static_cast<int>(Qt::KeypadModifier),
222 KeyNum3 = static_cast<int>(Qt::Key_3) | static_cast<int>(Qt::KeypadModifier),
223 KeyNum4 = static_cast<int>(Qt::Key_4) | static_cast<int>(Qt::KeypadModifier),
224 KeyNum5 = static_cast<int>(Qt::Key_5) | static_cast<int>(Qt::KeypadModifier),
225 KeyNum6 = static_cast<int>(Qt::Key_6) | static_cast<int>(Qt::KeypadModifier),
226 KeyNum7 = static_cast<int>(Qt::Key_7) | static_cast<int>(Qt::KeypadModifier),
227 KeyNum8 = static_cast<int>(Qt::Key_8) | static_cast<int>(Qt::KeypadModifier),
228 KeyNum9 = static_cast<int>(Qt::Key_9) | static_cast<int>(Qt::KeypadModifier),
229
230 // Mouse Keys
231 MouseMove = 1 << 16,
232 MouseLeft = 2 << 16,
233 MouseRight = 3 << 16,
234 MouseMiddle = 4 << 16,
235 MouseScroll = 5 << 16,
236 MouseScrollUp = 6 << 16,
237 MouseScrollDown = 7 << 16,
238 };
239
240 struct InputSequence
241 {
242 std::list<UserInput> keys {};
243
244 // This is intentionally left as an implicit conversion. For the intended use one UserInput
245 // is de facto InputSequence of length one. Therefore, there is no need to make that explicit.
246 explicit(false) InputSequence(UserInput key)
247 : InputSequence({key})
248 {}
249
250 explicit InputSequence(std::list<UserInput> keys)
251 : keys(std::move(keys))
252 {}
253
254 InputSequence(const std::initializer_list<UserInput> keys)
255 : keys(keys)
256 {}
257
258 friend bool operator==(const InputSequence& lhs, const InputSequence& rhs)
259 {
260 return lhs.keys == rhs.keys;
261 }
262
263 friend bool operator!=(const InputSequence& lhs, const InputSequence& rhs)
264 {
265 return !(lhs == rhs);
266 }
267 };
268
269 /**
270 * @brief Message associated with the hint.
271 *
272 * The message should confirm to rules stated in the documentation of the InputHint class.
273 * Message can contain placeholders like %1, %2 etc. that will then be replaced with graphical
274 * key representation from sequences field.
275 *
276 * @see https://freecad.github.io/DevelopersHandbook/designguide/input-hints.html
277 */
278 QString message;
279
280 /**
281 * @brief List of sequences to be substituted.
282 */
283 std::list<InputSequence> sequences;
284
285 friend bool operator==(const InputHint& lhs, const InputHint& rhs)
286 {
287 return lhs.message == rhs.message && lhs.sequences == rhs.sequences;
288 }
289
290 friend bool operator!=(const InputHint& lhs, const InputHint& rhs)
291 {
292 return !(lhs == rhs);
293 }
294 };
295
296 template<typename T>
297 struct StateHints
298 {
299 T state;
300 std::list<InputHint> hints;
301 };
302
303 template<typename T>
304 using HintTable = std::vector<StateHints<T>>;
305
306 template<typename T>
307 static std::list<InputHint> lookupHints(
308 T state,
309 HintTable<T> table,
310 const std::list<InputHint>& fallback = {}
311 )
312 {
313 const auto stateMatches = [&state](const StateHints<T>& entry) {
314 return entry.state == state;
315 };
316
317 if (auto it = std::ranges::find_if(table, stateMatches); it != table.end()) {
318 return it->hints;
319 }
320
321 return fallback;
322 }
323
324 } // namespace Gui
325
326 #endif // GUI_INPUTHINT_H