1/****************************************************************************
2**
3** Copyright (C) 2017 The Qt Company Ltd.
4** Contact: http://www.qt.io/licensing/
5**
6** This file is part of the Qt Quick Templates 2 module of the Qt Toolkit.
7**
8** $QT_BEGIN_LICENSE:LGPL3$
9** Commercial License Usage
10** Licensees holding valid commercial Qt licenses may use this file in
11** accordance with the commercial license agreement provided with the
12** Software or, alternatively, in accordance with the terms contained in
13** a written agreement between you and The Qt Company. For licensing terms
14** and conditions see http://www.qt.io/terms-conditions. For further
15** information use the contact form at http://www.qt.io/contact-us.
16**
17** GNU Lesser General Public License Usage
18** Alternatively, this file may be used under the terms of the GNU Lesser
19** General Public License version 3 as published by the Free Software
20** Foundation and appearing in the file LICENSE.LGPLv3 included in the
21** packaging of this file. Please review the following information to
22** ensure the GNU Lesser General Public License version 3 requirements
23** will be met: https://www.gnu.org/licenses/lgpl.html.
24**
25** GNU General Public License Usage
26** Alternatively, this file may be used under the terms of the GNU
27** General Public License version 2.0 or later as published by the Free
28** Software Foundation and appearing in the file LICENSE.GPL included in
29** the packaging of this file. Please review the following information to
30** ensure the GNU General Public License version 2.0 requirements will be
31** met: http://www.gnu.org/licenses/gpl-2.0.html.
32**
33** $QT_END_LICENSE$
34**
35****************************************************************************/
36
37#include "qquickmenuitem_p.h"
38#include "qquickmenuitem_p_p.h"
39#include "qquickmenu_p.h"
40#include "qquickdeferredexecute_p_p.h"
41
42#include <QtGui/qpa/qplatformtheme.h>
43#include <QtQuick/private/qquickevents_p_p.h>
44
45QT_BEGIN_NAMESPACE
46
47/*!
48 \qmltype MenuItem
49 \inherits AbstractButton
50//! \instantiates QQuickMenuItem
51 \inqmlmodule QtQuick.Controls
52 \since 5.7
53 \ingroup qtquickcontrols2-menus
54 \brief Presents an item within a Menu.
55
56 MenuItem is a convenience type that implements the AbstractButton API,
57 providing a familiar way to respond to menu items being \l triggered, for
58 example.
59
60 MenuItem inherits its API from AbstractButton. For instance, you can set
61 \l {AbstractButton::text}{text} and \l {Icons in Qt Quick Controls}{icon}
62 using the AbstractButton API.
63
64 \code
65 Button {
66 id: fileButton
67 text: "File"
68 onClicked: menu.open()
69
70 Menu {
71 id: menu
72
73 MenuItem {
74 text: "New..."
75 onTriggered: document.reset()
76 }
77 MenuItem {
78 text: "Open..."
79 onTriggered: openDialog.open()
80 }
81 MenuItem {
82 text: "Save"
83 onTriggered: saveDialog.open()
84 }
85 }
86 }
87 \endcode
88
89 \sa {Customizing Menu}, Menu, {Menu Controls}
90*/
91
92void QQuickMenuItemPrivate::setMenu(QQuickMenu *newMenu)
93{
94 Q_Q(QQuickMenuItem);
95 if (menu == newMenu)
96 return;
97
98 menu = newMenu;
99 emit q->menuChanged();
100}
101
102void QQuickMenuItemPrivate::setSubMenu(QQuickMenu *newSubMenu)
103{
104 Q_Q(QQuickMenuItem);
105 if (subMenu == newSubMenu)
106 return;
107
108 if (subMenu) {
109 QObject::disconnect(sender: subMenu, signal: &QQuickMenu::titleChanged, receiver: q, slot: &QQuickAbstractButton::setText);
110 QObjectPrivate::disconnect(sender: subMenu, signal: &QQuickPopup::enabledChanged, receiverPrivate: this, slot: &QQuickMenuItemPrivate::updateEnabled);
111 }
112
113 if (newSubMenu) {
114 QObject::connect(sender: newSubMenu, signal: &QQuickMenu::titleChanged, receiver: q, slot: &QQuickAbstractButton::setText);
115 QObjectPrivate::connect(sender: newSubMenu, signal: &QQuickPopup::enabledChanged, receiverPrivate: this, slot: &QQuickMenuItemPrivate::updateEnabled);
116 q->setText(newSubMenu->title());
117 }
118
119 subMenu = newSubMenu;
120 updateEnabled();
121 emit q->subMenuChanged();
122}
123
124void QQuickMenuItemPrivate::updateEnabled()
125{
126 Q_Q(QQuickMenuItem);
127 q->setEnabled(subMenu && subMenu->isEnabled());
128}
129
130static inline QString arrowName() { return QStringLiteral("arrow"); }
131
132void QQuickMenuItemPrivate::cancelArrow()
133{
134 Q_Q(QQuickAbstractButton);
135 quickCancelDeferred(object: q, property: arrowName());
136}
137
138void QQuickMenuItemPrivate::executeArrow(bool complete)
139{
140 Q_Q(QQuickMenuItem);
141 if (arrow.wasExecuted())
142 return;
143
144 if (!arrow || complete)
145 quickBeginDeferred(object: q, property: arrowName(), delegate&: arrow);
146 if (complete)
147 quickCompleteDeferred(object: q, property: arrowName(), delegate&: arrow);
148}
149
150bool QQuickMenuItemPrivate::acceptKeyClick(Qt::Key key) const
151{
152 return key == Qt::Key_Space || key == Qt::Key_Return || key == Qt::Key_Enter;
153}
154
155/*!
156 \qmlsignal void QtQuick.Controls::MenuItem::triggered()
157
158 This signal is emitted when the menu item is triggered by the user.
159*/
160
161QQuickMenuItem::QQuickMenuItem(QQuickItem *parent)
162 : QQuickAbstractButton(*(new QQuickMenuItemPrivate), parent)
163{
164 connect(sender: this, signal: &QQuickAbstractButton::clicked, receiver: this, slot: &QQuickMenuItem::triggered);
165}
166
167/*!
168 \qmlproperty bool QtQuick.Controls::MenuItem::highlighted
169
170 This property holds whether the menu item is highlighted by the user.
171
172 A menu item can be highlighted by mouse hover or keyboard navigation.
173
174 The default value is \c false.
175
176 \sa Menu::currentIndex
177*/
178bool QQuickMenuItem::isHighlighted() const
179{
180 Q_D(const QQuickMenuItem);
181 return d->highlighted;
182}
183
184void QQuickMenuItem::setHighlighted(bool highlighted)
185{
186 Q_D(QQuickMenuItem);
187 if (highlighted == d->highlighted)
188 return;
189
190 d->highlighted = highlighted;
191 emit highlightedChanged();
192}
193
194/*!
195 \since QtQuick.Controls 2.3 (Qt 5.10)
196 \qmlproperty Item QtQuick.Controls::MenuItem::arrow
197
198 This property holds the sub-menu arrow item.
199
200 \sa {Customizing Menu}
201*/
202QQuickItem *QQuickMenuItem::arrow() const
203{
204 QQuickMenuItemPrivate *d = const_cast<QQuickMenuItemPrivate *>(d_func());
205 if (!d->arrow)
206 d->executeArrow();
207 return d->arrow;
208}
209
210void QQuickMenuItem::setArrow(QQuickItem *arrow)
211{
212 Q_D(QQuickMenuItem);
213 if (d->arrow == arrow)
214 return;
215
216 if (!d->arrow.isExecuting())
217 d->cancelArrow();
218
219 QQuickControlPrivate::hideOldItem(item: d->arrow);
220 d->arrow = arrow;
221 if (arrow && !arrow->parentItem())
222 arrow->setParentItem(this);
223 if (!d->arrow.isExecuting())
224 emit arrowChanged();
225}
226
227/*!
228 \since QtQuick.Controls 2.3 (Qt 5.10)
229 \qmlproperty Menu QtQuick.Controls::MenuItem::menu
230 \readonly
231
232 This property holds the menu that contains this menu item,
233 or \c null if the item is not in a menu.
234*/
235QQuickMenu *QQuickMenuItem::menu() const
236{
237 Q_D(const QQuickMenuItem);
238 return d->menu;
239}
240
241/*!
242 \since QtQuick.Controls 2.3 (Qt 5.10)
243 \qmlproperty Menu QtQuick.Controls::MenuItem::subMenu
244 \readonly
245
246 This property holds the sub-menu that this item presents in
247 the parent menu, or \c null if this item is not a sub-menu item.
248*/
249QQuickMenu *QQuickMenuItem::subMenu() const
250{
251 Q_D(const QQuickMenuItem);
252 return d->subMenu;
253}
254
255void QQuickMenuItem::componentComplete()
256{
257 Q_D(QQuickMenuItem);
258 d->executeArrow(complete: true);
259 QQuickAbstractButton::componentComplete();
260}
261
262QFont QQuickMenuItem::defaultFont() const
263{
264 return QQuickTheme::font(scope: QQuickTheme::Menu);
265}
266
267QPalette QQuickMenuItem::defaultPalette() const
268{
269 return QQuickTheme::palette(scope: QQuickTheme::Menu);
270}
271
272#if QT_CONFIG(accessibility)
273QAccessible::Role QQuickMenuItem::accessibleRole() const
274{
275 return QAccessible::MenuItem;
276}
277#endif
278
279QT_END_NAMESPACE
280

source code of qtquickcontrols2/src/quicktemplates2/qquickmenuitem.cpp