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
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
58{
59 switch (context) {
61 return true;
63 while (obj && !obj->isWindowType()) {
64 obj = obj->parent();
65 if (QQuickItem *item = qobject_cast<QQuickItem *>(obj))
66 obj = item->window();
67 }
68 if (QWindow *renderWindow = QQuickRenderControl::renderWindowFor(qobject_cast<QQuickWindow *>(obj)))
69 obj = renderWindow;
71 default:
72 return false;
73 }
74}
75
77
79
81{
82 return *ctxMatcher();
83}
84
86{
87 if (!ctxMatcher.isDestroyed())
88 *ctxMatcher() = matcher;
89}
90
92
94{
95 if (value.userType() == QMetaType::Int) {
96 const QVector<QKeySequence> s =
98 if (s.size() > 1) {
99 const QString templateString = QString::fromUtf16(
100 u"Shortcut: Only binding to one of multiple key bindings associated with %1. "
101 u"Use 'sequences: [ <key> ]' to bind to all of them.");
103 << templateString.arg(static_cast<QKeySequence::StandardKey>(value.toInt()));
104 }
105 return s.size() > 0 ? s[0] : QKeySequence {};
106 }
107
108 return QKeySequence::fromString(value.toString());
109}
110
111static QList<QKeySequence> valueToKeySequences(const QVariant &value)
112{
113 if (value.userType() == QMetaType::Int) {
114 return QKeySequence::keyBindings(static_cast<QKeySequence::StandardKey>(value.toInt()));
115 } else {
116 QList<QKeySequence> result;
117 result.push_back(QKeySequence::fromString(value.toString()));
118 return result;
119 }
120}
121
123 m_enabled(true), m_completed(false), m_autorepeat(true), m_context(Qt::WindowShortcut)
124{
125}
126
128{
129 ungrabShortcut(m_shortcut);
130 for (Shortcut &shortcut : m_shortcuts)
132}
133
158{
159 return m_shortcut.userValue;
160}
161
163{
164 if (value == m_shortcut.userValue)
165 return;
166
167 QKeySequence keySequence = valueToKeySequence(value, this);
168
169 ungrabShortcut(m_shortcut);
170 m_shortcut.userValue = value;
171 m_shortcut.keySequence = keySequence;
172 grabShortcut(m_shortcut, m_context);
174}
175
193{
195 for (const Shortcut &shortcut : m_shortcuts)
196 values += shortcut.userValue;
197 return values;
198}
199
201{
202 // convert QVariantList to QVector<QKeySequence>
203 QVector<Shortcut> requestedShortcuts;
204 for (const QVariant &v : values) {
205 const QVector<QKeySequence> list = valueToKeySequences(v);
206 for (const QKeySequence &s : list) {
207 Shortcut sc;
208 sc.userValue = v;
209 sc.keySequence = s;
210 requestedShortcuts.push_back(sc);
211 }
212 }
213
214 // if nothing has changed, just return:
215 if (m_shortcuts.size() == requestedShortcuts.size()) {
216 bool changed = false;
217 for (int i = 0; i < requestedShortcuts.size(); ++i) {
218 const Shortcut &requestedShortcut = requestedShortcuts[i];
219 const Shortcut &shortcut = m_shortcuts[i];
220 if (!(requestedShortcut.userValue == shortcut.userValue
221 && requestedShortcut.keySequence == shortcut.keySequence)) {
222 changed = true;
223 break;
224 }
225 }
226 if (!changed) {
227 return;
228 }
229 }
230
231 for (Shortcut &s : m_shortcuts)
233 m_shortcuts = requestedShortcuts;
234 for (Shortcut &s : m_shortcuts)
235 grabShortcut(s, m_context);
236
237 emit sequencesChanged();
238}
239
255
270
279{
280 return m_enabled;
281}
282
284{
285 if (enabled == m_enabled)
286 return;
287
288 setEnabled(m_shortcut, enabled);
289 for (Shortcut &shortcut : m_shortcuts)
291
292 m_enabled = enabled;
294}
295
304{
305 return m_autorepeat;
306}
307
309{
310 if (repeat == m_autorepeat)
311 return;
312
313 setAutoRepeat(m_shortcut, repeat);
314 for (Shortcut &shortcut : m_shortcuts)
315 setAutoRepeat(shortcut, repeat);
316
317 m_autorepeat = repeat;
319}
320
342{
343 return m_context;
344}
345
347{
348 if (context == m_context)
349 return;
350
351 ungrabShortcut(m_shortcut);
352 for (auto &s : m_shortcuts)
354
355 m_context = context;
356
357 grabShortcut(m_shortcut, context);
358 for (auto &s : m_shortcuts)
360
362}
363
367
369{
370 m_completed = true;
371 grabShortcut(m_shortcut, m_context);
372 for (Shortcut &shortcut : m_shortcuts)
373 grabShortcut(shortcut, m_context);
374}
375
377{
378 if (m_enabled && event->type() == QEvent::Shortcut) {
379 QShortcutEvent *se = static_cast<QShortcutEvent *>(event);
380 bool match = m_shortcut.matches(se);
381 int i = 0;
382 while (!match && i < m_shortcuts.size())
383 match |= m_shortcuts.at(i++).matches(se);
384 if (match) {
385 if (se->isAmbiguous())
387 else
388 emit activated();
389 return true;
390 }
391 }
392 return false;
393}
394
396{
397 return event->shortcutId() == id && event->key() == keySequence;
398}
399
401{
402 if (shortcut.id)
403 QGuiApplicationPrivate::instance()->shortcutMap.setShortcutEnabled(enabled, shortcut.id, this);
404}
405
407{
408 if (shortcut.id)
409 QGuiApplicationPrivate::instance()->shortcutMap.setShortcutAutoRepeat(repeat, shortcut.id, this);
410}
411
413{
414 if (m_completed && !shortcut.keySequence.isEmpty()) {
416 shortcut.id = pApp->shortcutMap.addShortcut(this, shortcut.keySequence, context, *ctxMatcher());
417 if (!m_enabled)
418 pApp->shortcutMap.setShortcutEnabled(false, shortcut.id, this);
419 if (!m_autorepeat)
420 pApp->shortcutMap.setShortcutAutoRepeat(false, shortcut.id, this);
421 }
422}
423
425{
426 if (shortcut.id) {
427 QGuiApplicationPrivate::instance()->shortcutMap.removeShortcut(shortcut.id, this);
428 shortcut.id = 0;
429 }
430}
431
433
434#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
\inmodule QtCore
Definition qobject.h:103
The QQuickItem class provides the most basic of all visual items in \l {Qt Quick}.
Definition qquickitem.h:63
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:129
static QString fromUtf16(const char16_t *, qsizetype size=-1)
Definition qstring.cpp:6045
\inmodule QtCore
Definition qvariant.h:65
\inmodule QtGui
Definition qwindow.h:63
Combined button and popup list for selecting options.
Definition qcompare.h:63
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]
GLdouble s
[6]
Definition qopenglext.h:235
GLuint64EXT * result
[6]
Q_QML_EXPORT QQmlInfo qmlWarning(const QObject *me)
bool(* ContextMatcher)(QObject *, Qt::ShortcutContext)
static bool qQuickShortcutContextMatcher(QObject *obj, Qt::ShortcutContext context)
\qmltype Shortcut \instantiates QQuickShortcut \inqmlmodule QtQuick
Q_QUICK_EXPORT ContextMatcher qt_quick_shortcut_context_matcher()
static QT_BEGIN_NAMESPACE QKeySequence valueToKeySequence(const QVariant &value, const QQuickShortcut *const shortcut)
static QList< QKeySequence > valueToKeySequences(const QVariant &value)
Q_QUICK_EXPORT void qt_quick_set_shortcut_context_matcher(ContextMatcher matcher)
#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