Qt
Internal/Contributor docs for the Qt SDK. <b>Note:</b> These are NOT official API docs; those are found <a href='https://doc.qt.io/'>here</a>.
Loading...
Searching...
No Matches
qshortcut.cpp
Go to the documentation of this file.
1// Copyright (C) 2019 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
3
4#include "qshortcut.h"
5#include "qshortcut_p.h"
6
7#include <qevent.h>
8#include <qguiapplication.h>
9#include <qwindow.h>
10#include <private/qguiapplication_p.h>
11#include <qpa/qplatformmenu.h>
12
14
15#define QAPP_CHECK(functionName) \
16 if (Q_UNLIKELY(!qApp)) { \
17 qWarning("QShortcut: Initialize QGuiApplication before calling '" functionName "'."); \
18 return; \
19 }
20
100{
101 auto guiShortcut = qobject_cast<QShortcut *>(object);
102 if (QGuiApplication::applicationState() != Qt::ApplicationActive || guiShortcut == nullptr)
103 return false;
105 return true;
106 auto focusWindow = QGuiApplication::focusWindow();
107 if (!focusWindow)
108 return false;
109 auto window = qobject_cast<const QWindow *>(guiShortcut->parent());
110 if (!window)
111 return false;
112 if (focusWindow == window && focusWindow->isTopLevel())
114 return focusWindow->isAncestorOf(window, QWindow::ExcludeTransients);
115}
116
121
123{
124 Q_Q(QShortcut);
125 if (Q_UNLIKELY(!parent)) {
126 qWarning("QShortcut: No window parent defined");
127 return;
128 }
129
130 for (int id : std::as_const(sc_ids))
131 map.removeShortcut(id, q);
132
133 sc_ids.clear();
134 if (sc_sequences.isEmpty())
135 return;
137 for (const auto &keySequence : std::as_const(sc_sequences)) {
138 if (keySequence.isEmpty())
139 continue;
140 int id = map.addShortcut(q, keySequence, sc_context, contextMatcher());
141 sc_ids.append(id);
142 if (!sc_enabled)
143 map.setShortcutEnabled(false, id, q);
144 if (!sc_autorepeat)
145 map.setShortcutAutoRepeat(false, id, q);
146 }
147}
148
153
164 : QObject(*QGuiApplicationPrivate::instance()->createShortcutPrivate(), parent)
165{
166 Q_ASSERT(parent != nullptr);
167}
168
179 const char *member, const char *ambiguousMember,
181 : QShortcut(parent)
182{
183 Q_D(QShortcut);
184 d->sc_context = context;
185 if (!key.isEmpty()) {
186 d->sc_sequences = { key };
187 d->redoGrab(QGuiApplicationPrivate::instance()->shortcutMap);
188 }
189 if (member)
190 connect(this, SIGNAL(activated()), parent, member);
191 if (ambiguousMember)
192 connect(this, SIGNAL(activatedAmbiguously()), parent, ambiguousMember);
193}
194
206 const char *member, const char *ambiguousMember,
208 : QShortcut(parent)
209{
210 Q_D(QShortcut);
211 d->sc_context = context;
212 d->sc_sequences = QKeySequence::keyBindings(standardKey);
213 d->redoGrab(QGuiApplicationPrivate::instance()->shortcutMap);
214 if (member)
215 connect(this, SIGNAL(activated()), parent, member);
216 if (ambiguousMember)
217 connect(this, SIGNAL(activatedAmbiguously()), parent, ambiguousMember);
218}
219
220
337{
338 Q_D(QShortcut);
339 if (qApp) {
340 for (int id : std::as_const(d->sc_ids))
341 QGuiApplicationPrivate::instance()->shortcutMap.removeShortcut(id, this);
342 }
343}
344
357{
358 if (key.isEmpty())
359 setKeys({});
360 else
361 setKeys({ key });
362}
363
365{
366 Q_D(const QShortcut);
367 if (d->sc_sequences.isEmpty())
368 return QKeySequence();
369 return d->sc_sequences.first();
370}
371
380void QShortcut::setKeys(const QList<QKeySequence> &keys)
381{
382 Q_D(QShortcut);
383 if (d->sc_sequences == keys)
384 return;
385 QAPP_CHECK("setKeys");
386 d->sc_sequences = keys;
387 d->redoGrab(QGuiApplicationPrivate::instance()->shortcutMap);
388}
389
401
409QList<QKeySequence> QShortcut::keys() const
410{
411 Q_D(const QShortcut);
412 return d->sc_sequences;
413}
414
431{
432 Q_D(QShortcut);
433 if (d->sc_enabled == enable)
434 return;
435 QAPP_CHECK("setEnabled");
436 d->sc_enabled = enable;
437 for (int id : d->sc_ids)
438 QGuiApplicationPrivate::instance()->shortcutMap.setShortcutEnabled(enable, id, this);
439}
440
442{
443 Q_D(const QShortcut);
444 return d->sc_enabled;
445}
446
460{
461 Q_D(QShortcut);
462 if (d->sc_context == context)
463 return;
464 QAPP_CHECK("setContext");
465 d->sc_context = context;
466 d->redoGrab(QGuiApplicationPrivate::instance()->shortcutMap);
467}
468
470{
471 Q_D(const QShortcut);
472 return d->sc_context;
473}
474
485{
486 Q_D(QShortcut);
487 if (d->sc_autorepeat == on)
488 return;
489 QAPP_CHECK("setAutoRepeat");
490 d->sc_autorepeat = on;
491 for (int id : d->sc_ids)
492 QGuiApplicationPrivate::instance()->shortcutMap.setShortcutAutoRepeat(on, id, this);
493}
494
496{
497 Q_D(const QShortcut);
498 return d->sc_autorepeat;
499}
500
501
519{
520 Q_D(QShortcut);
521 d->sc_whatsthis = text;
522}
523
530{
531 Q_D(const QShortcut);
532 return d->sc_whatsthis;
533}
534
535#if QT_DEPRECATED_SINCE(6,0)
543int QShortcut::id() const
544{
545 Q_D(const QShortcut);
546 if (d->sc_ids.isEmpty())
547 return 0;
548 return d->sc_ids.first();
549}
550#endif
551
562{
563 Q_D(QShortcut);
564 if (d->sc_enabled && e->type() == QEvent::Shortcut) {
565 auto se = static_cast<QShortcutEvent *>(e);
566 if (!d->handleWhatsThis()) {
567 Q_ASSERT_X(d->sc_ids.contains(se->shortcutId()), "QShortcut::event", "Received shortcut event from wrong shortcut");
568 if (se->isAmbiguous())
570 else
571 emit activated();
572 return true;
573 }
574 }
575 return QObject::event(e);
576}
577
579
580#include "moc_qshortcut.cpp"
\inmodule QtCore
Definition qcoreevent.h:45
Type type() const
Returns the event type.
Definition qcoreevent.h:304
static QGuiApplicationPrivate * instance()
virtual QShortcutPrivate * createShortcutPrivate() const
static Qt::ApplicationState applicationState()
static QWindow * focusWindow()
Returns the QWindow that receives events tied to focus, such as key events.
The QKeySequence class encapsulates a key sequence as used by shortcuts.
static QList< QKeySequence > keyBindings(StandardKey key)
qsizetype size() const noexcept
Definition qlist.h:397
bool isEmpty() const noexcept
Definition qlist.h:401
void reserve(qsizetype size)
Definition qlist.h:753
void append(parameter_type t)
Definition qlist.h:458
void clear()
Definition qlist.h:434
QObject * parent
Definition qobject.h:73
\inmodule QtCore
Definition qobject.h:103
QObject * parent() const
Returns a pointer to the parent object.
Definition qobject.h:346
static QMetaObject::Connection connect(const QObject *sender, const char *signal, const QObject *receiver, const char *member, Qt::ConnectionType=Qt::AutoConnection)
\threadsafe
Definition qobject.cpp:2960
virtual bool event(QEvent *event)
This virtual function receives events to an object and should return true if the event e was recogniz...
Definition qobject.cpp:1389
The QShortcutEvent class provides an event which is generated when the user presses a key combination...
bool(* ContextMatcher)(QObject *object, Qt::ShortcutContext context)
QList< QKeySequence > sc_sequences
Definition qshortcut_p.h:48
Qt::ShortcutContext sc_context
Definition qshortcut_p.h:50
static bool simpleContextMatcher(QObject *object, Qt::ShortcutContext context)
Definition qshortcut.cpp:99
void redoGrab(QShortcutMap &map)
QList< int > sc_ids
Definition qshortcut_p.h:53
virtual QShortcutMap::ContextMatcher contextMatcher() const
The QShortcut class is used to create keyboard shortcuts.
Definition qshortcut.h:19
bool autoRepeat
whether the shortcut can auto repeat
Definition qshortcut.h:24
void setKey(const QKeySequence &key)
Qt::ShortcutContext context
the context in which the shortcut is valid
Definition qshortcut.h:25
QKeySequence key
the shortcut's primary key sequence
Definition qshortcut.h:22
void setKeys(QKeySequence::StandardKey key)
Sets the triggers to those matching the standard key key.
void activatedAmbiguously()
When a key sequence is being typed at the keyboard, it is said to be ambiguous as long as it matches ...
void setWhatsThis(const QString &text)
Sets the shortcut's "What's This?" help text.
void setAutoRepeat(bool on)
QString whatsThis() const
Returns the shortcut's "What's This?" help text.
QList< QKeySequence > keys() const
Returns the list of key sequences which trigger this shortcut.
~QShortcut()
Destroys the shortcut.
void activated()
This signal is emitted when the user types the shortcut's key sequence.
bool event(QEvent *e) override
bool isEnabled() const
QShortcut(QObject *parent)
Constructs a QShortcut object for the parent, which should be a QWindow or a QWidget.
void setContext(Qt::ShortcutContext context)
void setEnabled(bool enable)
\macro QT_RESTRICTED_CAST_FROM_ASCII
Definition qstring.h:129
QMap< QString, QString > map
[6]
QString text
Combined button and popup list for selecting options.
@ ApplicationActive
Definition qnamespace.h:266
ShortcutContext
@ WidgetWithChildrenShortcut
@ WindowShortcut
@ ApplicationShortcut
#define QAPP_CHECK(functionName)
Definition qaction.cpp:18
static void * context
#define Q_UNLIKELY(x)
#define qApp
#define qWarning
Definition qlogging.h:166
#define SIGNAL(a)
Definition qobjectdefs.h:53
GLuint64 key
GLboolean enable
GLdouble GLdouble GLdouble GLdouble q
Definition qopenglext.h:259
#define Q_ASSERT(cond)
Definition qrandom.cpp:47
#define Q_ASSERT_X(cond, x, msg)
Definition qrandom.cpp:48
#define emit
const QWindow * qobject_cast< const QWindow * >(const QObject *o)
Definition qwindow.h:372
QStringList keys
aWidget window() -> setWindowTitle("New Window Title")
[2]