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 <QList>
15#include <QPushButton>
16
17class KKeySequenceWidgetPrivate;
18class QAction;
20
21/**
22 * @class KKeySequenceWidget kkeysequencewidget.h KKeySequenceWidget
23 *
24 * @short A widget to input a QKeySequence.
25 *
26 * This widget lets the user choose a QKeySequence, which is usually used as a
27 * shortcut key. The recording is initiated by calling captureKeySequence() or
28 * the user clicking into the widget.
29 *
30 * The widgets provides support for conflict handling. See
31 * setCheckForConflictsAgainst() for more information.
32 *
33 * \image html kkeysequencewidget.png "KKeySequenceWidget"
34 *
35 * @author Mark Donohoe <donohoe@kde.org>
36 */
37class KXMLGUI_EXPORT KKeySequenceWidget : public QWidget
38{
39 Q_OBJECT
40
41 /// @since 5.65
42 Q_PROPERTY(QKeySequence keySequence READ keySequence WRITE setKeySequence NOTIFY keySequenceChanged)
43
44 Q_PROPERTY(bool multiKeyShortcutsAllowed READ multiKeyShortcutsAllowed WRITE setMultiKeyShortcutsAllowed)
45
46 Q_PROPERTY(ShortcutTypes checkForConflictsAgainst READ checkForConflictsAgainst WRITE setCheckForConflictsAgainst)
47
48 Q_PROPERTY(bool modifierlessAllowed READ isModifierlessAllowed WRITE setModifierlessAllowed)
49
50public:
51 /// An enum about validation when setting a key sequence.
52 ///@see setKeySequence()
54 /// Validate key sequence
55 Validate = 0,
56 /// Use key sequence without validation
57 NoValidate = 1,
58 };
59
60 /**
61 * Constructor.
62 */
63 explicit KKeySequenceWidget(QWidget *parent = nullptr);
64
65 /**
66 * Destructs the widget.
67 */
68 ~KKeySequenceWidget() override;
69
70 /**
71 * \name Configuration
72 *
73 * Configuration options for the widget.
74 * @see ShortcutTypes
75 */
76 //@{
77
79 None = 0x00, //!< No checking for conflicts
80 LocalShortcuts = 0x01, //!< Check with local shortcuts. @see setCheckActionCollections()
81 StandardShortcuts = 0x02, //!< Check against standard shortcuts. @see KStandardShortcut
82 GlobalShortcuts = 0x04, //!< Check against global shortcuts. @see KGlobalAccel
83 };
84 /**
85 * Stores a combination of #ShortcutType values.
86 */
87 Q_DECLARE_FLAGS(ShortcutTypes, ShortcutType)
89
90 /**
91 * Configure if the widget should check for conflicts with existing
92 * shortcuts.
93 *
94 * When capturing a key sequence for local shortcuts you should check
95 * against GlobalShortcuts and your other local shortcuts. This is the
96 * default.
97 *
98 * You have to provide the local actions to check against with
99 * setCheckActionCollections().
100 *
101 * When capturing a key sequence for a global shortcut you should
102 * check against StandardShortcuts, GlobalShortcuts and your local
103 * shortcuts.
104 *
105 * There are two ways to react to a user agreeing to steal a shortcut:
106 *
107 * 1. Listen to the stealShortcut() signal and steal the shortcuts
108 * manually. It's your responsibility to save that change later when
109 * you think it is appropriate.
110 *
111 * 2. Call applyStealShortcut() and KKeySequenceWidget will steal the
112 * shortcut. This will save the actionCollections the shortcut is part
113 * of so make sure it doesn't inadvertly save some unwanted changes
114 * too. Read its documentation for some limitation when handling
115 * global shortcuts.
116 *
117 * If you want to do the conflict checking yourself here are some code
118 * snippets for global ...
119 *
120 * \code
121 * QStringList conflicting = KGlobalAccel::findActionNameSystemwide(keySequence);
122 * if (!conflicting.isEmpty()) {
123 * // Inform and ask the user about the conflict and reassigning
124 * // the keys sequence
125 * if (!KGlobalAccel::promptStealShortcutSystemwide(q, conflicting, keySequence)) {
126 * return true;
127 * }
128 * KGlobalAccel::stealShortcutSystemwide(keySequence);
129 * }
130 * \endcode
131 *
132 * ... and standard shortcuts
133 *
134 * \code
135 * KStandardShortcut::StandardShortcut ssc = KStandardShortcut::find(keySequence);
136 * if (ssc != KStandardShortcut::AccelNone) {
137 * // We have a conflict
138 * }
139 * \endcode
140 *
141 *
142 * @since 4.2
143 */
144 void setCheckForConflictsAgainst(ShortcutTypes types);
145
146 /**
147 * The shortcut types we check for conflicts.
148 *
149 * @see setCheckForConflictsAgainst()
150 * @since 4.2
151 */
152 ShortcutTypes checkForConflictsAgainst() const;
153
154 /**
155 * Allow multikey shortcuts?
156 */
157 void setMultiKeyShortcutsAllowed(bool);
158 bool multiKeyShortcutsAllowed() const;
159
160 /**
161 * This only applies to user input, not to setKeySequence().
162 * Set whether to accept "plain" keys without modifiers (like Ctrl, Alt, Meta).
163 * Plain keys by our definition include letter and symbol keys and
164 * text editing keys (Return, Space, Tab, Backspace, Delete).
165 * "Special" keys like F1, Cursor keys, Insert, PageDown will always work.
166 */
167 void setModifierlessAllowed(bool allow);
168
169 /**
170 * @see setModifierlessAllowed()
171 */
172 bool isModifierlessAllowed();
173
174 /**
175 * Set whether a small button to set an empty key sequence should be displayed next to the
176 * main input widget. The default is to show the clear button.
177 */
178 void setClearButtonShown(bool show);
179
180 //@}
181
182 /**
183 * Checks whether the key sequence @p seq is available to grab.
184 *
185 * The sequence is checked under the same rules as if it has been typed by
186 * the user. This method is useful if you get key sequences from another
187 * input source and want to check if it is save to set them.
188 *
189 * @since 4.2
190 */
191 bool isKeySequenceAvailable(const QKeySequence &seq) const;
192
193 /**
194 * Return the currently selected key sequence.
195 */
196 QKeySequence keySequence() const;
197
198 /**
199 * Set a list of action collections to check against for conflictuous shortcut.
200 *
201 * @see setCheckForConflictsAgainst()
202 *
203 * If a QAction with a conflicting shortcut is found inside this list and
204 * its shortcut can be configured (KActionCollection::isShortcutConfigurable()
205 * returns true) the user will be prompted whether to steal the shortcut
206 * from this action.
207 *
208 * @since 4.1
209 */
210 void setCheckActionCollections(const QList<KActionCollection *> &actionCollections);
211
212 /**
213 * If the component using this widget supports shortcuts contexts, it has
214 * to set its component name so we can check conflicts correctly.
215 */
216 void setComponentName(const QString &componentName);
217
219
220 /**
221 * This signal is emitted when the current key sequence has changed, be it by user
222 * input or programmatically.
223 */
225
226 /**
227 * This signal is emitted after the user agreed to steal a shortcut from
228 * an action. This is only done for local shortcuts. So you can be sure \a
229 * action is one of the actions you provided with setCheckActionList() or
230 * setCheckActionCollections().
231 *
232 * If you listen to that signal and don't call applyStealShortcut() you
233 * are supposed to steal the shortcut and save this change.
234 */
235 void stealShortcut(const QKeySequence &seq, QAction *action);
236
237public Q_SLOTS:
238
239 /**
240 * Capture a shortcut from the keyboard. This call will only return once a key sequence
241 * has been captured or input was aborted.
242 * If a key sequence was input, keySequenceChanged() will be emitted.
243 *
244 * @see setModifierlessAllowed()
245 */
246 void captureKeySequence();
247
248 /**
249 * Set the key sequence.
250 *
251 * If @p val == Validate, and the call is actually changing the key sequence,
252 * conflictuous shortcut will be checked.
253 */
254 void setKeySequence(const QKeySequence &seq, Validation val = NoValidate);
255
256 /**
257 * Clear the key sequence.
258 */
259 void clearKeySequence();
260
261 /**
262 * Actually remove the shortcut that the user wanted to steal, from the
263 * action that was using it. This only applies to actions provided to us
264 * by setCheckActionCollections() and setCheckActionList().
265 *
266 * Global and Standard Shortcuts have to be stolen immediately when the
267 * user gives his consent (technical reasons). That means those changes
268 * will be active even if you never call applyStealShortcut().
269 *
270 * To be called before you apply your changes. No local shortcuts are
271 * stolen until this function is called.
272 */
273 void applyStealShortcut();
274
275private:
276 friend class KKeySequenceWidgetPrivate;
277 KKeySequenceWidgetPrivate *const d;
278
279 bool event(QEvent *ev) override;
280
282};
283
284Q_DECLARE_OPERATORS_FOR_FLAGS(KKeySequenceWidget::ShortcutTypes)
285
286#endif // KKEYSEQUENCEWIDGET_H
A container for a set of QAction objects.
A widget to input a QKeySequence.
void keySequenceChanged(const QKeySequence &seq)
This signal is emitted when the current key sequence has changed, be it by user input or programmatic...
Validation
An enum about validation when setting a key sequence.
void stealShortcut(const QKeySequence &seq, QAction *action)
This signal is emitted after the user agreed to steal a shortcut from an action.
Q_FLAG(...)
Q_PROPERTY(...)
Q_SIGNALSQ_SIGNALS
Q_SLOTSQ_SLOTS
QList< T > findChildren(Qt::FindChildOptions options) const const
virtual bool event(QEvent *event) override
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Tue Mar 26 2024 11:21:12 by doxygen 1.10.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.