Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
qquicklabsplatformmenuitemgroup.cpp
Go to the documentation of this file.
1// Copyright (C) 2020 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// Qt-Security score:significant reason:default
4
7
9
10/*!
11 \qmltype MenuItemGroup
12 \inherits QtObject
13//! \nativetype QQuickLabsPlatformMenuItemGroup
14 \inqmlmodule Qt.labs.platform
15 \since 5.8
16 \brief A group for managing native menu items.
17
18 The MenuItemGroup groups native menu items together.
19
20 MenuItemGroup is exclusive by default. In an exclusive menu item
21 group, only one item can be checked at any time; checking another
22 item automatically unchecks the previously checked one. MenuItemGroup
23 can be configured as non-exclusive, which is particularly useful for
24 showing, hiding, enabling and disabling items together as a group.
25
26 The most straight-forward way to use MenuItemGroup is to assign
27 a list of items.
28
29 \code
30 Menu {
31 id: verticalMenu
32 title: qsTr("Vertical")
33
34 MenuItemGroup {
35 id: verticalGroup
36 items: verticalMenu.items
37 }
38
39 MenuItem { text: qsTr("Top"); checkable: true }
40 MenuItem { text: qsTr("Center"); checked: true }
41 MenuItem { text: qsTr("Bottom"); checkable: true }
42 }
43 \endcode
44
45 The same menu may sometimes contain items that should not be included
46 in the same exclusive group. Such cases are best handled using the
47 \l {MenuItem::group}{group} property.
48
49 \code
50 Menu {
51 id: horizontalMenu
52 title: qsTr("Horizontal")
53
54 MenuItemGroup {
55 id: horizontalGroup
56 }
57
58 MenuItem {
59 checked: true
60 text: qsTr("Left")
61 group: horizontalGroup
62 }
63 MenuItem {
64 checkable: true
65 text: qsTr("Center")
66 group: horizontalGroup
67 }
68 MenuItem {
69 text: qsTr("Right")
70 checkable: true
71 group: horizontalGroup
72 }
73
74 MenuItem { separator: true }
75 MenuItem { text: qsTr("Justify"); checkable: true }
76 MenuItem { text: qsTr("Absolute"); checkable: true }
77 }
78 \endcode
79
80 More advanced use cases can be handled using the addItem() and
81 removeItem() methods.
82
83 \labs
84
85 \sa MenuItem
86*/
87
88/*!
89 \qmlsignal Qt.labs.platform::MenuItemGroup::triggered(MenuItem item)
90
91 This signal is emitted when an \a item in the group is triggered by the user.
92
93 \sa MenuItem::triggered()
94*/
95
96/*!
97 \qmlsignal Qt.labs.platform::MenuItemGroup::hovered(MenuItem item)
98
99 This signal is emitted when an \a item in the group is hovered by the user.
100
101 \sa MenuItem::hovered()
102*/
103
105 : QObject(parent), m_enabled(true), m_visible(true), m_exclusive(true), m_checkedItem(nullptr)
106{
107}
108
113
114/*!
115 \qmlproperty bool Qt.labs.platform::MenuItemGroup::enabled
116
117 This property holds whether the group is enabled. The default value is \c true.
118
119 The enabled state of the group affects the enabled state of each item in the group,
120 except that explicitly disabled items are not enabled even if the group is enabled.
121*/
123{
124 return m_enabled;
125}
126
128{
129 if (m_enabled == enabled)
130 return;
131
132 m_enabled = enabled;
133 emit enabledChanged();
134
135 for (QQuickLabsPlatformMenuItem *item : std::as_const(m_items)) {
136 if (item->m_enabled) {
137 item->sync();
138 emit item->enabledChanged();
139 }
140 }
141}
142
143/*!
144 \qmlproperty bool Qt.labs.platform::MenuItemGroup::visible
145
146 This property holds whether the group is visible. The default value is \c true.
147
148 The visibility of the group affects the visibility of each item in the group,
149 except that explicitly hidden items are not visible even if the group is visible.
150*/
152{
153 return m_visible;
154}
155
157{
158 if (m_visible == visible)
159 return;
160
161 m_visible = visible;
162 emit visibleChanged();
163
164 for (QQuickLabsPlatformMenuItem *item : std::as_const(m_items)) {
165 if (item->m_visible) {
166 item->sync();
167 emit item->visibleChanged();
168 }
169 }
170}
171
172/*!
173 \qmlproperty bool Qt.labs.platform::MenuItemGroup::exclusive
174
175 This property holds whether the group is exclusive. The default value is \c true.
176
177 In an exclusive menu item group, only one item can be checked at any time;
178 checking another item automatically unchecks the previously checked one.
179*/
181{
182 return m_exclusive;
183}
184
186{
187 if (m_exclusive == exclusive)
188 return;
189
190 m_exclusive = exclusive;
191 emit exclusiveChanged();
192
193 for (QQuickLabsPlatformMenuItem *item : std::as_const(m_items))
194 item->sync();
195}
196
197/*!
198 \qmlproperty MenuItem Qt.labs.platform::MenuItemGroup::checkedItem
199
200 This property holds the currently checked item in the group, or \c null if no item is checked.
201*/
203{
204 return m_checkedItem;
205}
206
208{
209 if (m_checkedItem == item)
210 return;
211
212 if (m_checkedItem)
213 m_checkedItem->setChecked(false);
214
215 m_checkedItem = item;
216 emit checkedItemChanged();
217
218 if (item)
219 item->setChecked(true);
220}
221
222/*!
223 \qmlproperty list<MenuItem> Qt.labs.platform::MenuItemGroup::items
224
225 This property holds the list of items in the group.
226*/
228{
229 return QQmlListProperty<QQuickLabsPlatformMenuItem>(this, nullptr, items_append, items_count, items_at, items_clear);
230}
231
232/*!
233 \qmlmethod void Qt.labs.platform::MenuItemGroup::addItem(MenuItem item)
234
235 Adds an \a item to the group.
236*/
238{
239 if (!item || m_items.contains(item))
240 return;
241
242 m_items.append(item);
243 item->setGroup(this);
244
246 connect(item, &QQuickLabsPlatformMenuItem::triggered, this, &QQuickLabsPlatformMenuItemGroup::activateItem);
248
249 if (m_exclusive && item->isChecked())
251
252 emit itemsChanged();
253}
254
255/*!
256 \qmlmethod void Qt.labs.platform::MenuItemGroup::removeItem(MenuItem item)
257
258 Removes an \a item from the group.
259*/
261{
262 if (!item || !m_items.contains(item))
263 return;
264
265 m_items.removeOne(item);
266 item->setGroup(nullptr);
267
268 disconnect(item, &QQuickLabsPlatformMenuItem::checkedChanged, this, &QQuickLabsPlatformMenuItemGroup::updateCurrent);
269 disconnect(item, &QQuickLabsPlatformMenuItem::triggered, this, &QQuickLabsPlatformMenuItemGroup::activateItem);
270 disconnect(item, &QQuickLabsPlatformMenuItem::hovered, this, &QQuickLabsPlatformMenuItemGroup::hoverItem);
271
272 if (m_checkedItem == item)
273 setCheckedItem(nullptr);
274
275 emit itemsChanged();
276}
277
278/*!
279 \qmlmethod void Qt.labs.platform::MenuItemGroup::clear()
280
281 Removes all items from the group.
282*/
284{
285 if (m_items.isEmpty())
286 return;
287
288 for (QQuickLabsPlatformMenuItem *item : std::as_const(m_items)) {
289 item->setGroup(nullptr);
290 disconnect(item, &QQuickLabsPlatformMenuItem::checkedChanged, this, &QQuickLabsPlatformMenuItemGroup::updateCurrent);
291 disconnect(item, &QQuickLabsPlatformMenuItem::triggered, this, &QQuickLabsPlatformMenuItemGroup::activateItem);
292 disconnect(item, &QQuickLabsPlatformMenuItem::hovered, this, &QQuickLabsPlatformMenuItemGroup::hoverItem);
293 }
294
295 setCheckedItem(nullptr);
296
297 m_items.clear();
298 emit itemsChanged();
299}
300
302{
303 for (QQuickLabsPlatformMenuItem *item : m_items) {
304 if (item->isChecked())
305 return item;
306 }
307 return nullptr;
308}
309
310void QQuickLabsPlatformMenuItemGroup::updateCurrent()
311{
312 if (!m_exclusive)
313 return;
314
315 QQuickLabsPlatformMenuItem *item = qobject_cast<QQuickLabsPlatformMenuItem*>(sender());
316 if (item && item->isChecked())
318}
319
320void QQuickLabsPlatformMenuItemGroup::activateItem()
321{
322 QQuickLabsPlatformMenuItem *item = qobject_cast<QQuickLabsPlatformMenuItem*>(sender());
323 if (item)
324 emit triggered(item);
325}
326
327void QQuickLabsPlatformMenuItemGroup::hoverItem()
328{
329 QQuickLabsPlatformMenuItem *item = qobject_cast<QQuickLabsPlatformMenuItem*>(sender());
330 if (item)
331 emit hovered(item);
332}
333
334void QQuickLabsPlatformMenuItemGroup::items_append(QQmlListProperty<QQuickLabsPlatformMenuItem> *prop, QQuickLabsPlatformMenuItem *item)
335{
336 QQuickLabsPlatformMenuItemGroup *group = static_cast<QQuickLabsPlatformMenuItemGroup *>(prop->object);
337 group->addItem(item);
338}
339
340qsizetype QQuickLabsPlatformMenuItemGroup::items_count(QQmlListProperty<QQuickLabsPlatformMenuItem> *prop)
341{
342 QQuickLabsPlatformMenuItemGroup *group = static_cast<QQuickLabsPlatformMenuItemGroup *>(prop->object);
343 return group->m_items.size();
344}
345
346QQuickLabsPlatformMenuItem *QQuickLabsPlatformMenuItemGroup::items_at(QQmlListProperty<QQuickLabsPlatformMenuItem> *prop, qsizetype index)
347{
348 QQuickLabsPlatformMenuItemGroup *group = static_cast<QQuickLabsPlatformMenuItemGroup *>(prop->object);
349 return group->m_items.value(index);
350}
351
352void QQuickLabsPlatformMenuItemGroup::items_clear(QQmlListProperty<QQuickLabsPlatformMenuItem> *prop)
353{
354 QQuickLabsPlatformMenuItemGroup *group = static_cast<QQuickLabsPlatformMenuItemGroup *>(prop->object);
355 group->clear();
356}
357
358QT_END_NAMESPACE
359
360#include "moc_qquicklabsplatformmenuitemgroup_p.cpp"
QObject * parent
Definition qobject.h:73
\inmodule QtCore
Definition qobject.h:105
Q_INVOKABLE void removeItem(QQuickLabsPlatformMenuItem *item)
\qmlmethod void Qt.labs.platform::MenuItemGroup::removeItem(MenuItem item)
Q_INVOKABLE void clear()
\qmlmethod void Qt.labs.platform::MenuItemGroup::clear()
void setCheckedItem(QQuickLabsPlatformMenuItem *item)
bool isEnabled() const
\qmlproperty bool Qt.labs.platform::MenuItemGroup::enabled
bool isVisible() const
\qmlproperty bool Qt.labs.platform::MenuItemGroup::visible
QQuickLabsPlatformMenuItem * checkedItem() const
\qmlproperty MenuItem Qt.labs.platform::MenuItemGroup::checkedItem
Q_INVOKABLE void addItem(QQuickLabsPlatformMenuItem *item)
\qmlmethod void Qt.labs.platform::MenuItemGroup::addItem(MenuItem item)
QQmlListProperty< QQuickLabsPlatformMenuItem > items()
bool isExclusive() const
\qmlproperty bool Qt.labs.platform::MenuItemGroup::exclusive
bool isChecked() const
\qmlproperty bool Qt.labs.platform::MenuItem::checked
void setGroup(QQuickLabsPlatformMenuItemGroup *group)