KXmlGui

kkeysequencewidget.h
1/*
2 This file is part of the KDE libraries
3 SPDX-FileCopyrightText: 2001, 2002 Ellis Whitehead <ellis@kde.org>
4 SPDX-FileCopyrightText: 2007 Andreas Hartmetz <ahartmetz@gmail.com>
5
6 SPDX-License-Identifier: LGPL-2.0-or-later
7*/
8
9#ifndef KKEYSEQUENCEWIDGET_H
10#define KKEYSEQUENCEWIDGET_H
11
12#include <kxmlgui_export.h>
13
14#include <KKeySequenceRecorder>
15
16#include <QList>
17#include <QPushButton>
18
19class KKeySequenceWidgetPrivate;
20class QAction;
22
23/**
24 * @class KKeySequenceWidget kkeysequencewidget.h KKeySequenceWidget
25 *
26 * @short A widget to input a QKeySequence.
27 *
28 * This widget lets the user choose a QKeySequence, which is usually used as a
29 * shortcut key. The recording is initiated by calling captureKeySequence() or
30 * the user clicking into the widget.
31 *
32 * The widgets provides support for conflict handling. See
33 * setCheckForConflictsAgainst() for more information.
34 *
35 * \image html kkeysequencewidget.png "KKeySequenceWidget"
36 *
37 * @author Mark Donohoe <donohoe@kde.org>
38 */
39class KXMLGUI_EXPORT KKeySequenceWidget : public QWidget
40{
42
43 /// @since 5.65
45
46 Q_PROPERTY(bool multiKeyShortcutsAllowed READ multiKeyShortcutsAllowed WRITE setMultiKeyShortcutsAllowed)
47
48 Q_PROPERTY(ShortcutTypes checkForConflictsAgainst READ checkForConflictsAgainst WRITE setCheckForConflictsAgainst)
49
50 /**
51 * @deprecated since 6.12, use the patterns property
52 */
54
55 /**
56 * @since 6.1
57 * @deprecated since 6.12, use the patterns property
58 */
60
61 /**
62 * Indicates whether a key sequence is currently being recorded.
63 *
64 * @since 6.12
65 */
67
68 /**
69 * Specifies the accepted shortcut formats.
70 *
71 * Default is `KKeySequenceRecorder::ModifierAndKey`.
72 *
73 * @since 6.12
74 */
76
77public:
78 /// An enum about validation when setting a key sequence.
79 ///@see setKeySequence()
81 /// Validate key sequence
83 /// Use key sequence without validation
85 };
86
87 /**
88 * Constructor.
89 */
90 explicit KKeySequenceWidget(QWidget *parent = nullptr);
91
92 /**
93 * Destructs the widget.
94 */
95 ~KKeySequenceWidget() override;
96
97 /**
98 * \name Configuration
99 *
100 * Configuration options for the widget.
101 * @see ShortcutTypes
102 */
103 //@{
104
106 None = 0x00, //!< No checking for conflicts
107 LocalShortcuts = 0x01, //!< Check with local shortcuts. @see setCheckActionCollections()
108 StandardShortcuts = 0x02, //!< Check against standard shortcuts. @see KStandardShortcut
109 GlobalShortcuts = 0x04, //!< Check against global shortcuts. @see KGlobalAccel
110 };
111 /**
112 * Stores a combination of #ShortcutType values.
113 */
114 Q_DECLARE_FLAGS(ShortcutTypes, ShortcutType)
116
117 /**
118 * Configure if the widget should check for conflicts with existing
119 * shortcuts.
120 *
121 * When capturing a key sequence for local shortcuts you should check
122 * against GlobalShortcuts and your other local shortcuts. This is the
123 * default.
124 *
125 * You have to provide the local actions to check against with
126 * setCheckActionCollections().
127 *
128 * When capturing a key sequence for a global shortcut you should
129 * check against StandardShortcuts, GlobalShortcuts and your local
130 * shortcuts.
131 *
132 * There are two ways to react to a user agreeing to steal a shortcut:
133 *
134 * 1. Listen to the stealShortcut() signal and steal the shortcuts
135 * manually. It's your responsibility to save that change later when
136 * you think it is appropriate.
137 *
138 * 2. Call applyStealShortcut() and KKeySequenceWidget will steal the
139 * shortcut. This will save the actionCollections the shortcut is part
140 * of so make sure it doesn't inadvertly save some unwanted changes
141 * too. Read its documentation for some limitation when handling
142 * global shortcuts.
143 *
144 * If you want to do the conflict checking yourself here are some code
145 * snippets for global ...
146 *
147 * \code
148 * QStringList conflicting = KGlobalAccel::findActionNameSystemwide(keySequence);
149 * if (!conflicting.isEmpty()) {
150 * // Inform and ask the user about the conflict and reassigning
151 * // the keys sequence
152 * if (!KGlobalAccel::promptStealShortcutSystemwide(q, conflicting, keySequence)) {
153 * return true;
154 * }
155 * KGlobalAccel::stealShortcutSystemwide(keySequence);
156 * }
157 * \endcode
158 *
159 * ... and standard shortcuts
160 *
161 * \code
162 * KStandardShortcut::StandardShortcut ssc = KStandardShortcut::find(keySequence);
163 * if (ssc != KStandardShortcut::AccelNone) {
164 * // We have a conflict
165 * }
166 * \endcode
167 *
168 *
169 * @since 4.2
170 */
172
173 /**
174 * The shortcut types we check for conflicts.
175 *
176 * @see setCheckForConflictsAgainst()
177 * @since 4.2
178 */
179 ShortcutTypes checkForConflictsAgainst() const;
180
181 /**
182 * Allow multikey shortcuts?
183 */
185 bool multiKeyShortcutsAllowed() const;
186
187#if KXMLGUI_ENABLE_DEPRECATED_SINCE(6, 12)
188 /**
189 * This only applies to user input, not to setKeySequence().
190 * Set whether to accept "plain" keys without modifiers (like Ctrl, Alt, Meta).
191 * Plain keys by our definition include letter and symbol keys and
192 * text editing keys (Return, Space, Tab, Backspace, Delete).
193 * "Special" keys like F1, Cursor keys, Insert, PageDown will always work.
194 *
195 * @deprecated since 6.12, use setPatterns() instead
196 */
197 KXMLGUI_DEPRECATED_VERSION(6, 12, "Use setPatterns()") void setModifierlessAllowed(bool allow);
198
199 /**
200 * @see setModifierlessAllowed()
201 *
202 * @deprecated since 6.12, use patterns() instead
203 */
204 KXMLGUI_DEPRECATED_VERSION(6, 12, "Use patterns()") bool isModifierlessAllowed();
205
206 /**
207 * Whether to allow modifier-only key sequences.
208 * @since 6.1
209 * @deprecated since 6.12, use setPatterns() instead
210 */
211 KXMLGUI_DEPRECATED_VERSION(6, 12, "Use setPatterns()") void setModifierOnlyAllowed(bool allow);
212
213 /**
214 * @deprecated since 6.12, use patterns() instead
215 */
216 KXMLGUI_DEPRECATED_VERSION(6, 12, "Use patterns()") bool modifierOnlyAllowed() const;
217#endif
218
219 /**
220 * Set whether a small button to set an empty key sequence should be displayed next to the
221 * main input widget. The default is to show the clear button.
222 */
223 void setClearButtonShown(bool show);
224
225 //@}
226
227 /**
228 * Checks whether the key sequence @p seq is available to grab.
229 *
230 * The sequence is checked under the same rules as if it has been typed by
231 * the user. This method is useful if you get key sequences from another
232 * input source and want to check if it is save to set them.
233 *
234 * @since 4.2
235 */
236 bool isKeySequenceAvailable(const QKeySequence &seq) const;
237
238 /**
239 * Return the currently selected key sequence.
240 */
242
243 /**
244 * Set a list of action collections to check against for conflictuous shortcut.
245 *
246 * @see setCheckForConflictsAgainst()
247 *
248 * If a QAction with a conflicting shortcut is found inside this list and
249 * its shortcut can be configured (KActionCollection::isShortcutConfigurable()
250 * returns true) the user will be prompted whether to steal the shortcut
251 * from this action.
252 *
253 * @since 4.1
254 */
255 void setCheckActionCollections(const QList<KActionCollection *> &actionCollections);
256
257 /**
258 * If the component using this widget supports shortcuts contexts, it has
259 * to set its component name so we can check conflicts correctly.
260 */
261 void setComponentName(const QString &componentName);
262
263 /**
264 * Returns @c true if a key sequence is currently being recorded; otherwise returns @c false.
265 *
266 * @since 6.12
267 */
268 bool isRecording() const;
269
270 /**
271 * Sets the accepted shortcut patterns to @a patterns. A shortcut pattern specifies what
272 * components the recoreded shortcut must have, e.g. whether it should include modifier keys, etc.
273 *
274 * @since 6.12
275 */
276 void setPatterns(KKeySequenceRecorder::Patterns patterns);
277
278 /**
279 * Returns the accepted shortcut patterns.
280 *
281 * Default is `Modifier | Key`.
282 *
283 * @since 6.12
284 */
285 KKeySequenceRecorder::Patterns patterns() const;
286
288
289 /**
290 * This signal is emitted when the current key sequence has changed, be it by user
291 * input or programmatically.
292 */
294
295 /**
296 * This signal is emitted after the user agreed to steal a shortcut from
297 * an action. This is only done for local shortcuts. So you can be sure \a
298 * action is one of the actions you provided with setCheckActionList() or
299 * setCheckActionCollections().
300 *
301 * If you listen to that signal and don't call applyStealShortcut() you
302 * are supposed to steal the shortcut and save this change.
303 */
304 void stealShortcut(const QKeySequence &seq, QAction *action);
305
306 /**
307 * This signal is emitted when the user begins or finishes recording a key sequence. It
308 * is not emitted when the current key sequence is changed using setKeySequence().
309 *
310 * @since 6.12
311 */
313
314public Q_SLOTS:
315
316 /**
317 * Capture a shortcut from the keyboard. This call will only return once a key sequence
318 * has been captured or input was aborted.
319 * If a key sequence was input, keySequenceChanged() will be emitted.
320 *
321 * @see setModifierlessAllowed()
322 */
323 void captureKeySequence();
324
325 /**
326 * Set the key sequence.
327 *
328 * If @p val == Validate, and the call is actually changing the key sequence,
329 * conflictuous shortcut will be checked.
330 */
331 void setKeySequence(const QKeySequence &seq, Validation val = NoValidate);
332
333 /**
334 * Clear the key sequence.
335 */
336 void clearKeySequence();
337
338 /**
339 * Actually remove the shortcut that the user wanted to steal, from the
340 * action that was using it. This only applies to actions provided to us
341 * by setCheckActionCollections() and setCheckActionList().
342 *
343 * Global and Standard Shortcuts have to be stolen immediately when the
344 * user gives his consent (technical reasons). That means those changes
345 * will be active even if you never call applyStealShortcut().
346 *
347 * To be called before you apply your changes. No local shortcuts are
348 * stolen until this function is called.
349 */
350 void applyStealShortcut();
351
352private:
353 friend class KKeySequenceWidgetPrivate;
354 KKeySequenceWidgetPrivate *const d;
355
356 bool event(QEvent *ev) override;
357
358 Q_DISABLE_COPY(KKeySequenceWidget)
359};
360
361Q_DECLARE_OPERATORS_FOR_FLAGS(KKeySequenceWidget::ShortcutTypes)
362
363#endif // KKEYSEQUENCEWIDGET_H
A container for a set of QAction objects.
A widget to input a QKeySequence.
void setModifierlessAllowed(bool allow)
This only applies to user input, not to setKeySequence().
QFlags< ShortcutType > ShortcutTypes
Stores a combination of ShortcutType values.
@ None
No checking for conflicts.
@ GlobalShortcuts
Check against global shortcuts.
@ StandardShortcuts
Check against standard shortcuts.
@ LocalShortcuts
Check with local shortcuts.
void clearKeySequence()
Clear the key sequence.
bool isKeySequenceAvailable(const QKeySequence &seq) const
Checks whether the key sequence seq is available to grab.
void setModifierOnlyAllowed(bool allow)
Whether to allow modifier-only key sequences.
void setClearButtonShown(bool show)
Set whether a small button to set an empty key sequence should be displayed next to the main input wi...
void setMultiKeyShortcutsAllowed(bool)
Allow multikey shortcuts?
void keySequenceChanged(const QKeySequence &seq)
This signal is emitted when the current key sequence has changed, be it by user input or programmatic...
bool recording
Indicates whether a key sequence is currently being recorded.
void recordingChanged()
This signal is emitted when the user begins or finishes recording a key sequence.
void setComponentName(const QString &componentName)
If the component using this widget supports shortcuts contexts, it has to set its component name so w...
void captureKeySequence()
Capture a shortcut from the keyboard.
KKeySequenceWidget(QWidget *parent=nullptr)
Constructor.
void applyStealShortcut()
Actually remove the shortcut that the user wanted to steal, from the action that was using it.
void setKeySequence(const QKeySequence &seq, Validation val=NoValidate)
Set the key sequence.
KKeySequenceRecorder::Patterns patterns
Specifies the accepted shortcut formats.
void setCheckActionCollections(const QList< KActionCollection * > &actionCollections)
Set a list of action collections to check against for conflictuous shortcut.
void setPatterns(KKeySequenceRecorder::Patterns patterns)
Sets the accepted shortcut patterns to patterns.
Validation
An enum about validation when setting a key sequence.
@ Validate
Validate key sequence.
@ NoValidate
Use key sequence without validation.
void stealShortcut(const QKeySequence &seq, QAction *action)
This signal is emitted after the user agreed to steal a shortcut from an action.
bool isRecording() const
Returns true if a key sequence is currently being recorded; otherwise returns false.
void setCheckForConflictsAgainst(ShortcutTypes types)
Configure if the widget should check for conflicts with existing shortcuts.
Q_FLAG(...)
Q_OBJECTQ_OBJECT
Q_PROPERTY(...)
Q_SIGNALSQ_SIGNALS
Q_SLOTSQ_SLOTS
QWidget(QWidget *parent, Qt::WindowFlags f)
virtual bool event(QEvent *event) override
void show()
This file is part of the KDE documentation.
Documentation copyright © 1996-2025 The KDE developers.
Generated on Fri Feb 28 2025 11:58:03 by doxygen 1.13.2 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.