Qt 6.x
The Qt SDK
Loading...
Searching...
No Matches
qquickshortcut.cpp
Go to the documentation of this file.
1// Copyright (C) 2016 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 "qquickshortcut_p.h"
5
6#include <QtQuick/qquickitem.h>
7#include <QtQuick/qquickwindow.h>
8#include <QtQuick/qquickrendercontrol.h>
9#include <QtQuick/private/qtquickglobal_p.h>
10#include <QtGui/private/qguiapplication_p.h>
11#include <QtQml/qqmlinfo.h>
12
57{
58 switch (context) {
60 return true;
62 while (obj && !obj->isWindowType()) {
63 obj = obj->parent();
65 obj = item->window();
66 }
67 if (QWindow *renderWindow = QQuickRenderControl::renderWindowFor(qobject_cast<QQuickWindow *>(obj)))
68 obj = renderWindow;
70 default:
71 return false;
72 }
73}
74
76
78
80{
81 return *ctxMatcher();
82}
83
85{
86 if (!ctxMatcher.isDestroyed())
87 *ctxMatcher() = matcher;
88}
89
91
93{
94 if (value.userType() == QMetaType::Int) {
97 if (s.size() > 1) {
98 const QString templateString = QString::fromUtf16(
99 u"Shortcut: Only binding to one of multiple key bindings associated with %1. "
100 u"Use 'sequences: [ <key> ]' to bind to all of them.");
102 << templateString.arg(static_cast<QKeySequence::StandardKey>(value.toInt()));
103 }
104 return s.size() > 0 ? s[0] : QKeySequence {};
105 }
106
107 return QKeySequence::fromString(value.toString());
108}
109
111{
112 if (value.userType() == QMetaType::Int) {
113 return QKeySequence::keyBindings(static_cast<QKeySequence::StandardKey>(value.toInt()));
114 } else {
116 result.push_back(QKeySequence::fromString(value.toString()));
117 return result;
118 }
119}
120
122 m_enabled(true), m_completed(false), m_autorepeat(true), m_context(Qt::WindowShortcut)
123{
124}
125
127{
128 ungrabShortcut(m_shortcut);
129 for (Shortcut &shortcut : m_shortcuts)
131}
132
153{
154 return m_shortcut.userValue;
155}
156
158{
159 if (value == m_shortcut.userValue)
160 return;
161
162 QKeySequence keySequence = valueToKeySequence(value, this);
163
164 ungrabShortcut(m_shortcut);
165 m_shortcut.userValue = value;
166 m_shortcut.keySequence = keySequence;
167 grabShortcut(m_shortcut, m_context);
169}
170
188{
190 for (const Shortcut &shortcut : m_shortcuts)
191 values += shortcut.userValue;
192 return values;
193}
194
196{
197 // convert QVariantList to QVector<QKeySequence>
198 QVector<Shortcut> requestedShortcuts;
199 for (const QVariant &v : values) {
201 for (const QKeySequence &s : list) {
202 Shortcut sc;
203 sc.userValue = v;
204 sc.keySequence = s;
205 requestedShortcuts.push_back(sc);
206 }
207 }
208
209 // if nothing has changed, just return:
210 if (m_shortcuts.size() == requestedShortcuts.size()) {
211 bool changed = false;
212 for (int i = 0; i < requestedShortcuts.size(); ++i) {
213 const Shortcut &requestedShortcut = requestedShortcuts[i];
214 const Shortcut &shortcut = m_shortcuts[i];
215 if (!(requestedShortcut.userValue == shortcut.userValue
216 && requestedShortcut.keySequence == shortcut.keySequence)) {
217 changed = true;
218 break;
219 }
220 }
221 if (!changed) {
222 return;
223 }
224 }
225
226 for (Shortcut &s : m_shortcuts)
228 m_shortcuts = requestedShortcuts;
229 for (Shortcut &s : m_shortcuts)
230 grabShortcut(s, m_context);
231
232 emit sequencesChanged();
233}
234
247{
249}
250
262{
264}
265
274{
275 return m_enabled;
276}
277
279{
280 if (enabled == m_enabled)
281 return;
282
283 setEnabled(m_shortcut, enabled);
284 for (Shortcut &shortcut : m_shortcuts)
286
287 m_enabled = enabled;
289}
290
299{
300 return m_autorepeat;
301}
302
304{
305 if (repeat == m_autorepeat)
306 return;
307
308 setAutoRepeat(m_shortcut, repeat);
309 for (Shortcut &shortcut : m_shortcuts)
310 setAutoRepeat(shortcut, repeat);
311
312 m_autorepeat = repeat;
314}
315
337{
338 return m_context;
339}
340
342{
343 if (context == m_context)
344 return;
345
346 ungrabShortcut(m_shortcut);
347 for (auto &s : m_shortcuts)
349
350 m_context = context;
351
352 grabShortcut(m_shortcut, context);
353 for (auto &s : m_shortcuts)
355
357}
358
360{
361}
362
364{
365 m_completed = true;
366 grabShortcut(m_shortcut, m_context);
367 for (Shortcut &shortcut : m_shortcuts)
368 grabShortcut(shortcut, m_context);
369}
370
372{
373 if (m_enabled && event->type() == QEvent::Shortcut) {
374 QShortcutEvent *se = static_cast<QShortcutEvent *>(event);
375 bool match = m_shortcut.matches(se);
376 int i = 0;
377 while (!match && i < m_shortcuts.size())
378 match |= m_shortcuts.at(i++).matches(se);
379 if (match) {
380 if (se->isAmbiguous())
382 else
383 emit activated();
384 return true;
385 }
386 }
387 return false;
388}
389
391{
392 return event->shortcutId() == id && event->key() == keySequence;
393}
394
396{
397 if (shortcut.id)
398 QGuiApplicationPrivate::instance()->shortcutMap.setShortcutEnabled(enabled, shortcut.id, this);
399}
400
402{
403 if (shortcut.id)
404 QGuiApplicationPrivate::instance()->shortcutMap.setShortcutAutoRepeat(repeat, shortcut.id, this);
405}
406
408{
409 if (m_completed && !shortcut.keySequence.isEmpty()) {
411 shortcut.id = pApp->shortcutMap.addShortcut(this, shortcut.keySequence, context, *ctxMatcher());
412 if (!m_enabled)
413 pApp->shortcutMap.setShortcutEnabled(false, shortcut.id, this);
414 if (!m_autorepeat)
415 pApp->shortcutMap.setShortcutAutoRepeat(false, shortcut.id, this);
416 }
417}
418
420{
421 if (shortcut.id) {
422 QGuiApplicationPrivate::instance()->shortcutMap.removeShortcut(shortcut.id, this);
423 shortcut.id = 0;
424 }
425}
426
428
429#include "moc_qquickshortcut_p.cpp"
\inmodule QtCore
Definition qcoreevent.h:45
QGraphicsWidget * window() const
static QGuiApplicationPrivate * instance()
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 QKeySequence fromString(const QString &str, SequenceFormat format=PortableText)
static QList< QKeySequence > keyBindings(StandardKey key)
QString toString(SequenceFormat format=PortableText) const
Definition qlist.h:74
qsizetype size() const noexcept
Definition qlist.h:386
void push_back(parameter_type t)
Definition qlist.h:672
\inmodule QtCore
Definition qobject.h:90
The QQuickItem class provides the most basic of all visual items in \l {Qt Quick}.
Definition qquickitem.h:64
static QWindow * renderWindowFor(QQuickWindow *win, QPoint *offset=nullptr)
Returns the real window that win is being rendered to, if any.
void setSequence(const QVariant &sequence)
void autoRepeatChanged()
void setAutoRepeat(bool repeat)
void setEnabled(bool enabled)
void ungrabShortcut(Shortcut &shortcut)
void enabledChanged()
void classBegin() override
Invoked after class creation, but before any properties have been set.
void componentComplete() override
Invoked after the root component that caused this instantiation has completed construction.
void sequenceChanged()
void setContext(Qt::ShortcutContext context)
bool event(QEvent *event) override
This virtual function receives events to an object and should return true if the event e was recogniz...
void setSequences(const QVariantList &sequences)
void grabShortcut(Shortcut &shortcut, Qt::ShortcutContext context)
bool isEnabled() const
\qmlproperty bool QtQuick::Shortcut::enabled
void contextChanged()
Qt::ShortcutContext context
QQuickShortcut(QObject *parent=nullptr)
void activatedAmbiguously()
QVariantList sequences
The QShortcutEvent class provides an event which is generated when the user presses a key combination...
\macro QT_RESTRICTED_CAST_FROM_ASCII
Definition qstring.h:127
static QString fromUtf16(const char16_t *, qsizetype size=-1)
Definition qstring.cpp:5883
qsizetype size() const
Returns the number of characters in this string.
Definition qstring.h:182
QString arg(qlonglong a, int fieldwidth=0, int base=10, QChar fillChar=u' ') const
Definition qstring.cpp:8606
\inmodule QtCore
Definition qvariant.h:64
\inmodule QtGui
Definition qwindow.h:63
Combined button and popup list for selecting options.
ShortcutContext
@ WindowShortcut
@ ApplicationShortcut
static void * context
EGLOutputLayerEXT EGLint EGLAttrib value
[5]
#define Q_GLOBAL_STATIC_WITH_ARGS(TYPE, NAME, ARGS)
GLenum GLsizei GLsizei GLint * values
[15]
GLsizei const GLfloat * v
[13]
GLenum GLenum GLsizei const GLuint GLboolean enabled
struct _cl_event * event
GLhandleARB obj
[2]
GLuint64EXT * result
[6]
GLdouble s
[6]
Definition qopenglext.h:235
Q_QML_EXPORT QQmlInfo qmlWarning(const QObject *me)
QQuickItem * qobject_cast< QQuickItem * >(QObject *o)
Definition qquickitem.h:483
Q_QUICK_PRIVATE_EXPORT void qt_quick_set_shortcut_context_matcher(ContextMatcher matcher)
Q_QUICK_PRIVATE_EXPORT ContextMatcher qt_quick_shortcut_context_matcher()
bool(* ContextMatcher)(QObject *, Qt::ShortcutContext)
static bool qQuickShortcutContextMatcher(QObject *obj, Qt::ShortcutContext context)
\qmltype Shortcut \instantiates QQuickShortcut \inqmlmodule QtQuick
static QT_BEGIN_NAMESPACE QKeySequence valueToKeySequence(const QVariant &value, const QQuickShortcut *const shortcut)
static QList< QKeySequence > valueToKeySequences(const QVariant &value)
#define emit
static bool match(const uchar *found, uint foundLen, const char *target, uint targetLen)
QList< int > list
[14]
static const auto matcher
[0]
QGraphicsItem * item
bool matches(QShortcutEvent *event) const
IUIAutomationTreeWalker __RPC__deref_out_opt IUIAutomationElement ** parent