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
qquicklabsplatformmenuitem.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
8
9#include <QtGui/qicon.h>
10#if QT_CONFIG(shortcut)
11#include <QtGui/qkeysequence.h>
12#endif
13#include <QtGui/qpa/qplatformtheme.h>
14#include <QtGui/private/qguiapplication_p.h>
15#include <QtQuickTemplates2/private/qquickshortcutcontext_p_p.h>
16
18
20
25
71 : QObject(parent),
72 m_complete(false),
73 m_enabled(true),
74 m_visible(true),
75 m_separator(false),
76 m_checkable(false),
77 m_checked(false),
78 m_role(QPlatformMenuItem::TextHeuristicRole),
79 m_menu(nullptr),
80 m_subMenu(nullptr),
81 m_group(nullptr),
82 m_iconLoader(nullptr),
83 m_handle(nullptr)
84{
85}
86
88{
89 if (m_menu)
90 m_menu->removeItem(this);
91 if (m_group)
92 m_group->removeItem(this);
93 removeShortcut();
94 delete m_iconLoader;
95 m_iconLoader = nullptr;
96 delete m_handle;
97 m_handle = nullptr;
98}
99
101{
102 return m_handle;
103}
104
106{
107 if (!m_handle && m_menu && m_menu->handle()) {
108 m_handle = m_menu->handle()->createMenuItem();
109
110 // TODO: implement QCocoaMenu::createMenuItem()
111 if (!m_handle)
112 m_handle = QGuiApplicationPrivate::platformTheme()->createPlatformMenuItem();
113
114 if (!m_handle)
116
117 if (m_handle) {
118 connect(m_handle, &QPlatformMenuItem::activated, this, &QQuickLabsPlatformMenuItem::activate);
120 }
121 }
122 return m_handle;
123}
124
126{
127 if (!m_complete || !create())
128 return;
129
130 m_handle->setEnabled(isEnabled());
131 m_handle->setVisible(isVisible());
132 m_handle->setIsSeparator(m_separator);
133 m_handle->setCheckable(m_checkable);
134 m_handle->setChecked(m_checked);
135 m_handle->setRole(m_role);
136 m_handle->setText(m_text);
137 m_handle->setFont(m_font);
138 m_handle->setHasExclusiveGroup(m_group && m_group->isExclusive());
139
140 if (m_iconLoader)
141 m_handle->setIcon(m_iconLoader->toQIcon());
142
143 if (m_subMenu) {
144 // Sync first as dynamically created menus may need to get the
145 // handle recreated
146 m_subMenu->sync();
147 if (m_subMenu->handle())
148 m_handle->setMenu(m_subMenu->handle());
149 }
150
151#if QT_CONFIG(shortcut)
152 QKeySequence sequence;
153 if (m_shortcut.metaType().id() == QMetaType::Int)
154 sequence = QKeySequence(static_cast<QKeySequence::StandardKey>(m_shortcut.toInt()));
155 else if (m_shortcut.metaType().id() == QMetaType::QKeySequence)
156 sequence = m_shortcut.value<QKeySequence>();
157 else
158 sequence = QKeySequence::fromString(m_shortcut.toString());
159 m_handle->setShortcut(sequence.toString());
160#endif
161
162 if (m_menu && m_menu->handle())
163 m_menu->handle()->syncMenuItem(m_handle);
164}
165
174{
175 return m_menu;
176}
177
179{
180 if (m_menu == menu)
181 return;
182
183 m_menu = menu;
185}
186
195{
196 return m_subMenu;
197}
198
200{
201 if (m_subMenu == menu)
202 return;
203
204 m_subMenu = menu;
205 sync();
207}
208
219
221{
222 if (m_group == group)
223 return;
224
225 bool wasEnabled = isEnabled();
226 bool wasVisible = isVisible();
227
228 if (group)
229 group->addItem(this);
230
231 m_group = group;
232 sync();
234
235 if (isEnabled() != wasEnabled)
237 if (isVisible() != wasVisible)
239}
240
253{
254 return m_enabled && (!m_group || m_group->isEnabled());
255}
256
258{
259 if (m_enabled == enabled)
260 return;
261
262 if (!enabled)
263 removeShortcut();
264
265 bool wasEnabled = isEnabled();
266 m_enabled = enabled;
267
268 if (enabled)
269 addShortcut();
270
271 sync();
272 if (isEnabled() != wasEnabled)
274}
275
282{
283 return m_visible && (!m_group || m_group->isVisible());
284}
285
287{
288 if (m_visible == visible)
289 return;
290
291 bool wasVisible = isVisible();
292 m_visible = visible;
293 sync();
294 if (isVisible() != wasVisible)
296}
297
307{
308 return m_separator;
309}
310
312{
313 if (m_separator == separator)
314 return;
315
316 m_separator = separator;
317 sync();
319}
320
335{
336 return m_checkable;
337}
338
340{
341 if (m_checkable == checkable)
342 return;
343
344 m_checkable = checkable;
345 sync();
347}
348
358{
359 return m_checked;
360}
361
363{
364 if (m_checked == checked)
365 return;
366
367 if (checked && !m_checkable)
368 setCheckable(true);
369
370 m_checked = checked;
371 sync();
373}
374
402
404{
405 if (m_role == role)
406 return;
407
408 m_role = role;
409 sync();
411}
412
419{
420 return m_text;
421}
422
424{
425 if (m_text == text)
426 return;
427
428 m_text = text;
429 sync();
431}
432
453{
454 return m_shortcut;
455}
456
458{
459#if QT_CONFIG(shortcut)
460 if (e->type() == QEvent::Shortcut) {
461 QShortcutEvent *se = static_cast<QShortcutEvent *>(e);
462 if (se->shortcutId() == m_shortcutId) {
463 activate();
464 return true;
465 }
466 }
467#endif
468 return QObject::event(e);
469}
470
472{
473 if (m_shortcut == shortcut)
474 return;
475
476 removeShortcut();
477 m_shortcut = shortcut;
478 sync();
479 addShortcut();
481}
482
491{
492 return m_font;
493}
494
496{
497 if (m_font == font)
498 return;
499
500 m_font = font;
501 sync();
503}
504
524{
525 if (!m_iconLoader)
526 return QQuickLabsPlatformIcon();
527
528 return m_iconLoader->icon();
529}
530
532{
533 if (iconLoader()->icon() == icon)
534 return;
535
537 emit iconChanged();
538}
539
546{
547 if (m_checkable)
548 setChecked(!m_checked);
549}
550
554
556{
557 if (m_iconLoader)
558 m_iconLoader->setEnabled(true);
559 m_complete = true;
560 sync();
561}
562
564{
565 if (!m_iconLoader) {
566 QQuickLabsPlatformMenuItem *that = const_cast<QQuickLabsPlatformMenuItem *>(this);
567 static int slot = staticMetaObject.indexOfSlot("updateIcon()");
568 m_iconLoader = new QQuickLabsPlatformIconLoader(slot, that);
569 m_iconLoader->setEnabled(m_complete);
570 }
571 return m_iconLoader;
572}
573
574void QQuickLabsPlatformMenuItem::activate()
575{
576 toggle();
577 emit triggered();
578}
579
580void QQuickLabsPlatformMenuItem::updateIcon()
581{
582 sync();
583}
584
585void QQuickLabsPlatformMenuItem::addShortcut()
586{
587#if QT_CONFIG(shortcut)
588 QKeySequence sequence;
589 if (m_shortcut.metaType().id() == QMetaType::Int)
590 sequence = QKeySequence(static_cast<QKeySequence::StandardKey>(m_shortcut.toInt()));
591 else if (m_shortcut.metaType().id() == QMetaType::QKeySequence)
592 sequence = m_shortcut.value<QKeySequence>();
593 else
594 sequence = QKeySequence::fromString(m_shortcut.toString());
595 if (!sequence.isEmpty() && m_enabled) {
596 m_shortcutId = QGuiApplicationPrivate::instance()->shortcutMap.addShortcut(this, sequence,
598 } else {
599 m_shortcutId = -1;
600 }
601#endif
602}
603
604void QQuickLabsPlatformMenuItem::removeShortcut()
605{
606#if QT_CONFIG(shortcut)
607 if (m_shortcutId == -1)
608 return;
609
610 QKeySequence sequence;
611 if (m_shortcut.metaType().id() == QMetaType::Int)
612 sequence = QKeySequence(static_cast<QKeySequence::StandardKey>(m_shortcut.toInt()));
613 else if (m_shortcut.metaType().id() == QMetaType::QKeySequence)
614 sequence = m_shortcut.value<QKeySequence>();
615 else
616 sequence = QKeySequence::fromString(m_shortcut.toString());
617 QGuiApplicationPrivate::instance()->shortcutMap.removeShortcut(m_shortcutId, this, sequence);
618#endif
619}
620
622
623#include "moc_qquicklabsplatformmenuitem_p.cpp"
\inmodule QtCore
Definition qcoreevent.h:45
Type type() const
Returns the event type.
Definition qcoreevent.h:304
\reentrant
Definition qfont.h:22
static QGuiApplicationPrivate * instance()
static QPlatformTheme * platformTheme()
The QKeySequence class encapsulates a key sequence as used by shortcuts.
static QKeySequence fromString(const QString &str, SequenceFormat format=PortableText)
bool isEmpty() const
Returns true if the key sequence is empty; otherwise returns false.
QString toString(SequenceFormat format=PortableText) const
int id(int=0) const
Definition qmetatype.h:475
\inmodule QtCore
Definition qobject.h:103
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
virtual void setHasExclusiveGroup(bool hasExclusiveGroup)
virtual void setCheckable(bool checkable)=0
virtual void setIsSeparator(bool isSeparator)=0
virtual void setIcon(const QIcon &icon)=0
virtual void setRole(MenuRole role)=0
virtual void setChecked(bool isChecked)=0
virtual void setFont(const QFont &font)=0
virtual void setText(const QString &text)=0
virtual void setMenu(QPlatformMenu *menu)=0
virtual void setVisible(bool isVisible)=0
virtual void setEnabled(bool enabled)=0
virtual QPlatformMenuItem * createMenuItem() const
virtual void syncMenuItem(QPlatformMenuItem *menuItem)=0
void setIcon(const QQuickLabsPlatformIcon &icon)
QQuickLabsPlatformIcon icon() const
Q_INVOKABLE void removeItem(QQuickLabsPlatformMenuItem *item)
\qmlmethod void Qt.labs.platform::MenuItemGroup::removeItem(MenuItem item)
bool isEnabled() const
\qmlproperty bool Qt.labs.platform::MenuItemGroup::enabled
bool isVisible() const
\qmlproperty bool Qt.labs.platform::MenuItemGroup::visible
bool isExclusive() const
\qmlproperty bool Qt.labs.platform::MenuItemGroup::exclusive
QQuickLabsPlatformIconLoader * iconLoader() const
QQuickLabsPlatformMenuItem(QObject *parent=nullptr)
A native menu item.
QQuickLabsPlatformMenuItemGroup * group
bool isChecked() const
\qmlproperty bool Qt.labs.platform::MenuItem::checked
void setIcon(const QQuickLabsPlatformIcon &icon)
bool isCheckable() const
\qmlproperty bool Qt.labs.platform::MenuItem::checkable
bool isEnabled() const
\qmlproperty bool Qt.labs.platform::MenuItem::enabled
void setRole(QPlatformMenuItem::MenuRole role)
void setMenu(QQuickLabsPlatformMenu *menu)
bool event(QEvent *e) override
This virtual function receives events to an object and should return true if the event e was recogniz...
void setSubMenu(QQuickLabsPlatformMenu *menu)
void componentComplete() override
Invoked after the root component that caused this instantiation has completed construction.
bool isVisible() const
\qmlproperty bool Qt.labs.platform::MenuItem::visible
void setGroup(QQuickLabsPlatformMenuItemGroup *group)
bool isSeparator() const
\qmlproperty bool Qt.labs.platform::MenuItem::separator
void classBegin() override
Invoked after class creation, but before any properties have been set.
void toggle()
\qmlmethod void Qt.labs.platform::MenuItem::toggle()
void setShortcut(const QVariant &shortcut)
Q_INVOKABLE void removeItem(QQuickLabsPlatformMenuItem *item)
\qmlmethod void Qt.labs.platform::Menu::removeItem(MenuItem item)
QPlatformMenu * handle() const
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
T value() const &
Definition qvariant.h:516
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
Combined button and popup list for selecting options.
static QPlatformMenuItem * createMenuItem(QObject *parent=nullptr)
@ WindowShortcut
GLenum GLenum GLsizei const GLuint GLboolean enabled
GLboolean GLuint group
#define emit
QObject::connect nullptr
QMenu menu
[5]
static bool matcher(QObject *object, Qt::ShortcutContext context)