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
qquickaction.cpp
Go to the documentation of this file.
1// Copyright (C) 2017 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 "qquickaction_p.h"
5#include "qquickaction_p_p.h"
8
9#include <QtGui/qevent.h>
10#if QT_CONFIG(shortcut)
11# include <QtGui/private/qshortcutmap_p.h>
12#endif
13#include <QtGui/private/qguiapplication_p.h>
14#include <QtQuick/private/qquickitem_p.h>
15
16#include <QtCore/qpointer.h>
17
19
24
87#if QT_CONFIG(shortcut)
88static QKeySequence variantToKeySequence(const QVariant &var)
89{
90 if (var.metaType().id() == QMetaType::Int)
91 return QKeySequence(static_cast<QKeySequence::StandardKey>(var.toInt()));
93}
94
95QQuickActionPrivate::ShortcutEntry::ShortcutEntry(QObject *target)
96 : m_target(target)
97{
98}
99
100QQuickActionPrivate::ShortcutEntry::~ShortcutEntry()
101{
102 ungrab();
103}
104
105QObject *QQuickActionPrivate::ShortcutEntry::target() const
106{
107 return m_target;
108}
109
110int QQuickActionPrivate::ShortcutEntry::shortcutId() const
111{
112 return m_shortcutId;
113}
114
115void QQuickActionPrivate::ShortcutEntry::grab(const QKeySequence &shortcut, bool enabled)
116{
117 if (shortcut.isEmpty() || m_shortcutId)
118 return;
119
121 m_shortcutId = QGuiApplicationPrivate::instance()->shortcutMap.addShortcut(m_target, shortcut, context, QQuickShortcutContext::matcher);
122
123 if (!enabled)
124 QGuiApplicationPrivate::instance()->shortcutMap.setShortcutEnabled(false, m_shortcutId, m_target);
125}
126
127void QQuickActionPrivate::ShortcutEntry::ungrab()
128{
129 if (!m_shortcutId)
130 return;
131
132 QGuiApplicationPrivate::instance()->shortcutMap.removeShortcut(m_shortcutId, m_target);
133 m_shortcutId = 0;
134}
135
136void QQuickActionPrivate::ShortcutEntry::setEnabled(bool enabled)
137{
138 if (!m_shortcutId)
139 return;
140
141 QGuiApplicationPrivate::instance()->shortcutMap.setShortcutEnabled(enabled, m_shortcutId, m_target);
142}
143
144QVariant QQuickActionPrivate::shortcut() const
145{
146 return vshortcut;
147}
148
149void QQuickActionPrivate::setShortcut(const QVariant &var)
150{
151 Q_Q(QQuickAction);
152 if (vshortcut == var)
153 return;
154
155 defaultShortcutEntry->ungrab();
156 for (QQuickActionPrivate::ShortcutEntry *entry : std::as_const(shortcutEntries))
157 entry->ungrab();
158
159 vshortcut = var;
160 keySequence = variantToKeySequence(var);
161
162 defaultShortcutEntry->grab(keySequence, enabled);
163 for (QQuickActionPrivate::ShortcutEntry *entry : std::as_const(shortcutEntries))
164 entry->grab(keySequence, enabled);
165
166 emit q->shortcutChanged(keySequence);
167}
168#endif // QT_CONFIG(shortcut)
169
171{
172 Q_Q(QQuickAction);
173 if (enabled == enable)
174 return;
175
176 enabled = enable;
177
178#if QT_CONFIG(shortcut)
179 defaultShortcutEntry->setEnabled(enable);
180 for (QQuickActionPrivate::ShortcutEntry *entry : std::as_const(shortcutEntries))
181 entry->setEnabled(enable);
182#endif
183
184 emit q->enabledChanged(enable);
185}
186
188{
189 Q_Q(QQuickAction);
190 if (!item)
191 return false;
192
193 item->installEventFilter(q);
195 return true;
196}
197
199{
200 Q_Q(QQuickAction);
201 if (!item)
202 return false;
203
204 item->removeEventFilter(q);
206 return true;
207}
208
210{
211 if (!watchItem(item))
212 return;
213
214#if QT_CONFIG(shortcut)
215 QQuickActionPrivate::ShortcutEntry *entry = new QQuickActionPrivate::ShortcutEntry(item);
216 if (item->isVisible())
217 entry->grab(keySequence, enabled);
218 shortcutEntries += entry;
219
220 updateDefaultShortcutEntry();
221#endif
222}
223
225{
226#if QT_CONFIG(shortcut)
227 QQuickActionPrivate::ShortcutEntry *entry = findShortcutEntry(item);
228 if (!entry || !unwatchItem(item))
229 return;
230
231 shortcutEntries.removeOne(entry);
232 delete entry;
233
234 updateDefaultShortcutEntry();
235#else
236 Q_UNUSED(item);
237#endif
238}
239
241{
242#if QT_CONFIG(shortcut)
243 QQuickActionPrivate::ShortcutEntry *entry = findShortcutEntry(item);
244 if (!entry)
245 return;
246
247 if (item->isVisible())
248 entry->grab(keySequence, enabled);
249 else
250 entry->ungrab();
251
252 updateDefaultShortcutEntry();
253#else
254 Q_UNUSED(item);
255#endif
256}
257
262
263#if QT_CONFIG(shortcut)
265{
266 Q_Q(QQuickAction);
267 if (event->key() != keySequence)
268 return false;
269
270 QQuickActionPrivate::ShortcutEntry *entry = findShortcutEntry(object);
271 if (!entry || event->shortcutId() != entry->shortcutId())
272 return false;
273
274 q->trigger(entry->target());
275 return true;
276}
277
278QQuickActionPrivate::ShortcutEntry *QQuickActionPrivate::findShortcutEntry(QObject *target) const
279{
280 Q_Q(const QQuickAction);
281 if (target == q)
282 return defaultShortcutEntry;
283 for (QQuickActionPrivate::ShortcutEntry *entry : shortcutEntries) {
284 if (entry->target() == target)
285 return entry;
286 }
287 return nullptr;
288}
289
290void QQuickActionPrivate::updateDefaultShortcutEntry()
291{
292 bool hasActiveShortcutEntries = false;
293 for (QQuickActionPrivate::ShortcutEntry *entry : std::as_const(shortcutEntries)) {
294 if (entry->shortcutId()) {
295 hasActiveShortcutEntries = true;
296 break;
297 }
298 }
299
300 if (hasActiveShortcutEntries)
301 defaultShortcutEntry->ungrab();
302 else if (!defaultShortcutEntry->shortcutId())
303 defaultShortcutEntry->grab(keySequence, enabled);
304}
305#endif // QT_CONFIG(shortcut)
306
308 : QObject(*(new QQuickActionPrivate), parent)
309{
310#if QT_CONFIG(shortcut)
311 Q_D(QQuickAction);
312 d->defaultShortcutEntry = new QQuickActionPrivate::ShortcutEntry(this);
313#endif
314}
315
317{
318 Q_D(QQuickAction);
319 if (d->group)
320 d->group->removeAction(this);
321
322#if QT_CONFIG(shortcut)
323 for (QQuickActionPrivate::ShortcutEntry *entry : std::as_const(d->shortcutEntries))
324 d->unwatchItem(qobject_cast<QQuickItem *>(entry->target()));
325
326 qDeleteAll(d->shortcutEntries);
327 delete d->defaultShortcutEntry;
328#endif
329}
330
337{
338 Q_D(const QQuickAction);
339 return d->text;
340}
341
343{
344 Q_D(QQuickAction);
345 if (d->text == text)
346 return;
347
348 d->text = text;
350}
351
363{
364 Q_D(const QQuickAction);
365 return d->icon;
366}
367
369{
370 Q_D(QQuickAction);
371 if (d->icon == icon)
372 return;
373
374 d->icon = icon;
377}
378
385{
386 Q_D(const QQuickAction);
387 return d->enabled && (!d->group || d->group->isEnabled());
388}
389
391{
392 Q_D(QQuickAction);
393 d->explicitEnabled = true;
394 d->setEnabled(enabled);
395}
396
398{
399 Q_D(QQuickAction);
400 if (!d->explicitEnabled)
401 return;
402
403 d->explicitEnabled = false;
404 d->setEnabled(true);
405}
406
415{
416 Q_D(const QQuickAction);
417 return d->checked;
418}
419
420void QQuickAction::setChecked(bool checked)
421{
422 Q_D(QQuickAction);
423 if (d->checked == checked)
424 return;
425
426 d->checked = checked;
428}
429
440{
441 Q_D(const QQuickAction);
442 return d->checkable;
443}
444
445void QQuickAction::setCheckable(bool checkable)
446{
447 Q_D(QQuickAction);
448 if (d->checkable == checkable)
449 return;
450
451 d->checkable = checkable;
453}
454
455#if QT_CONFIG(shortcut)
471QKeySequence QQuickAction::shortcut() const
472{
473 Q_D(const QQuickAction);
474 return d->keySequence;
475}
476
477void QQuickAction::setShortcut(const QKeySequence &shortcut)
478{
479 Q_D(QQuickAction);
480 d->setShortcut(shortcut.toString());
481}
482#endif // QT_CONFIG(shortcut)
483
490{
491 Q_D(QQuickAction);
492 if (!d->enabled)
493 return;
494
495 if (d->checkable)
496 setChecked(!d->checked);
497
499}
500
507{
508 Q_D(QQuickAction);
509 d->trigger(source, true);
510}
511
513{
514 Q_Q(QQuickAction);
515 if (!enabled)
516 return;
517
518 QPointer<QObject> guard = q;
519 // the checked action of an exclusive group cannot be unchecked
520 if (checkable && (!checked || !group || !group->isExclusive() || group->checkedAction() != q)) {
521 if (doToggle)
522 q->toggle(source);
523 else
524 emit q->toggled(source);
525 }
526
527 if (!guard.isNull())
528 emit q->triggered(source);
529}
530
532{
533#if QT_CONFIG(shortcut)
534 Q_D(QQuickAction);
535 if (event->type() == QEvent::Shortcut)
536 return d->handleShortcutEvent(this, static_cast<QShortcutEvent *>(event));
537#endif
538 return QObject::event(event);
539}
540
542{
543#if QT_CONFIG(shortcut)
544 Q_D(QQuickAction);
545 if (event->type() == QEvent::Shortcut)
546 return d->handleShortcutEvent(object, static_cast<QShortcutEvent *>(event));
547#else
548 Q_UNUSED(object);
550#endif
551 return false;
552}
553
555
556#include "moc_qquickaction_p.cpp"
\inmodule QtCore
Definition qcoreevent.h:45
bool isVisible() const
Returns true if the item is visible; otherwise, false is returned.
static QGuiApplicationPrivate * instance()
The QKeySequence class encapsulates a key sequence as used by shortcuts.
static QKeySequence fromString(const QString &str, SequenceFormat format=PortableText)
int id(int=0) const
Definition qmetatype.h:475
\inmodule QtCore
Definition qobject.h:103
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
void trigger(QObject *, bool doToggle)
bool handleShortcutEvent(QObject *object, QShortcutEvent *event)
void itemDestroyed(QQuickItem *item) override
void registerItem(QQuickItem *item)
bool watchItem(QQuickItem *item)
void setEnabled(bool enable)
Abstract user interface action.
void itemVisibilityChanged(QQuickItem *item) override
bool unwatchItem(QQuickItem *item)
void unregisterItem(QQuickItem *item)
void toggled(QObject *source=nullptr)
bool eventFilter(QObject *object, QEvent *event) override
Filters events if this object has been installed as an event filter for the watched object.
void setEnabled(bool enabled)
bool isCheckable() const
\qmlproperty bool QtQuick.Controls::Action::checkable
void setIcon(const QQuickIcon &icon)
QQuickAction(QObject *parent=nullptr)
void toggle(QObject *source=nullptr)
\qmlmethod void QtQuick.Controls::Action::toggle(QtObject source)
void checkedChanged(bool checked)
void checkableChanged(bool checkable)
void trigger(QObject *source=nullptr)
\qmlmethod void QtQuick.Controls::Action::trigger(QtObject source)
void setCheckable(bool checkable)
QQuickIcon icon
void setChecked(bool checked)
bool event(QEvent *event) override
This virtual function receives events to an object and should return true if the event e was recogniz...
void setText(const QString &text)
void textChanged(const QString &text)
void iconChanged(const QQuickIcon &icon)
bool isEnabled() const
\qmlproperty bool QtQuick.Controls::Action::enabled
bool isChecked() const
\qmlproperty bool QtQuick.Controls::Action::checked
void ensureRelativeSourceResolved(const QObject *owner)
static QQuickItemPrivate * get(QQuickItem *item)
The QQuickItem class provides the most basic of all visual items in \l {Qt Quick}.
Definition qquickitem.h:63
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
\inmodule QtCore
Definition qvariant.h:65
int toInt(bool *ok=nullptr) const
Returns the variant as an int if the variant has userType() \l QMetaType::Int, \l QMetaType::Bool,...
QString toString() const
Returns the variant as a QString if the variant has a userType() including, but not limited to:
QMetaType metaType() const
QString text
qDeleteAll(list.begin(), list.end())
Combined button and popup list for selecting options.
ShortcutContext
@ WindowShortcut
static void * context
GLenum GLenum GLsizei const GLuint GLboolean enabled
GLboolean GLuint group
GLenum target
GLboolean enable
GLsizei GLsizei GLchar * source
struct _cl_event * event
GLuint entry
GLdouble GLdouble GLdouble GLdouble q
Definition qopenglext.h:259
#define emit
#define Q_UNUSED(x)
QGraphicsItem * item
static bool matcher(QObject *object, Qt::ShortcutContext context)