8#include <private/qtquicktemplates2-config_p.h>
9#if QT_CONFIG(quicktemplates2_container)
10#include "qquickmenubaritem_p.h"
11#include "qquickmenubar_p_p.h"
21#include <QtCore/qloggingcategory.h>
22#include <QtGui/qevent.h>
23#include <QtGui/qcursor.h>
24#if QT_CONFIG(shortcut)
25#include <QtGui/qkeysequence.h>
27#include <QtGui/qpa/qplatformintegration.h>
28#include <QtGui/qpa/qplatformtheme.h>
29#include <QtGui/private/qhighdpiscaling_p.h>
30#include <QtGui/private/qguiapplication_p.h>
31#include <QtQml/qqmlcontext.h>
32#include <QtQml/qqmlcomponent.h>
33#include <QtQml/private/qqmlengine_p.h>
34#include <QtQml/private/qqmldata_p.h>
35#include <QtQml/private/qv4scopedvalue_p.h>
36#include <QtQml/private/qv4variantobject_p.h>
37#include <QtQml/private/qv4qobjectwrapper_p.h>
38#include <private/qqmlobjectmodel_p.h>
39#include <QtQuick/private/qquickitem_p.h>
40#include <QtQuick/private/qquickitemchangelistener_p.h>
41#include <QtQuick/private/qquickitemview_p_p.h>
42#include <QtQuick/private/qquickevents_p_p.h>
43#include <QtQuick/private/qquicklistview_p.h>
44#include <QtQuick/private/qquickrendercontrol_p.h>
45#include <QtQuick/private/qquickwindow_p.h>
49Q_STATIC_LOGGING_CATEGORY(lcMenu,
"qt.quick.controls.menu")
50Q_STATIC_LOGGING_CATEGORY(lcNativeMenus,
"qt.quick.controls.nativemenus")
53static const int SUBMENU_DELAY = 225;
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
254
255
256
257
258
259
260
261
262
263
264
265
266
267
274 return QGuiApplicationPrivate::platformIntegration()->hasCapability(QPlatformIntegration::MultipleWindows);
288QQuickMenuPrivate::QQuickMenuPrivate()
290 cascade = shouldCascade();
291#if QT_CONFIG(wayland)
292 extendedWindowType = QNativeInterface::Private::QWaylandWindow::Menu;
295 wmWindowType = QNativeInterface::Private::QXcbWindow::PopupMenu;
299void QQuickMenuPrivate::init()
302 contentModel =
new QQmlObjectModel(q);
305QQuickMenu *QQuickMenuPrivate::rootMenu()
const
307 Q_Q(
const QQuickMenu);
308 const QQuickMenu *rootMenu = q;
309 const QObject *p = q->parent();
311 if (
auto menu = qobject_cast<
const QQuickMenu *>(p))
316 return const_cast<QQuickMenu *>(rootMenu);
319QQuickPopup::PopupType QQuickMenuPrivate::resolvedPopupType()
const
323 QQuickMenu *root = rootMenu();
324 QQuickMenuPrivate *root_d = QQuickMenuPrivate::get(rootMenu());
326#if QT_CONFIG(quicktemplates2_container)
327 if (
auto menuBar = QQuickMenuPrivate::get(root)->menuBar.get()) {
332 if (QQuickMenuBarPrivate::get(menuBar)->useNativeMenu(q_func()))
333 return QQuickPopup::Native;
338 if (root_d->maybeNativeHandle()) {
339 return QQuickPopup::Native;
340 }
else if (!root_d->triedToCreateNativeMenu) {
346 if (root->popupType() == QQuickPopup::Native
347 && !QGuiApplication::testAttribute(Qt::AA_DontUseNativeMenuWindows)) {
348 return QQuickPopup::Native;
356 const auto type = root_d->QQuickPopupPrivate::resolvedPopupType();
357 Q_ASSERT(type != QQuickPopup::Native);
361bool QQuickMenuPrivate::useNativeMenu()
const
363 return resolvedPopupType() == QQuickPopup::Native;
366QPlatformMenu *QQuickMenuPrivate::nativeHandle()
368 Q_ASSERT(handle || useNativeMenu());
369 if (!handle && !triedToCreateNativeMenu)
374QPlatformMenu *QQuickMenuPrivate::maybeNativeHandle()
const
379bool QQuickMenuPrivate::createNativeMenu()
383 qCDebug(lcNativeMenus) <<
"createNativeMenu called on" << q;
385 if (
auto menuBar = QQuickMenuPrivate::get(rootMenu())->menuBar) {
386#if QT_CONFIG(quicktemplates2_container)
387 auto menuBarPrivate = QQuickMenuBarPrivate::get(menuBar);
388 if (menuBarPrivate->useNativeMenuBar()) {
389 qCDebug(lcNativeMenus) <<
"- creating native menu from native menubar";
390 if (QPlatformMenuBar *menuBarHandle = menuBarPrivate->nativeHandle())
391 handle.reset(menuBarHandle->createMenu());
397 QPlatformMenu *parentMenuHandle(parentMenu ? get(parentMenu)->handle.get() :
nullptr);
398 if (parentMenu && parentMenuHandle) {
399 qCDebug(lcNativeMenus) <<
"- creating native sub-menu";
400 handle.reset(parentMenuHandle->createSubMenu());
402 qCDebug(lcNativeMenus) <<
"- creating native menu";
403 handle.reset(QGuiApplicationPrivate::platformTheme()->createPlatformMenu());
407 triedToCreateNativeMenu =
true;
412 q->connect(handle.get(), &QPlatformMenu::aboutToShow, q, [q,
this](){
413 emit q->aboutToShow();
415 emit q->visibleChanged();
416 emit q->openedChanged();
419 q->connect(handle.get(), &QPlatformMenu::aboutToHide, q, [q](){
420 qCDebug(lcNativeMenus) <<
"QPlatformMenu::aboutToHide called; about to call setVisible(false) on Menu";
421 emit q->aboutToHide();
427 q->connect(handle.get(), &QPlatformMenu::aboutToHide, q, [q,
this](){
429 emit q->visibleChanged();
430 emit q->openedChanged();
432 }, Qt::QueuedConnection);
434 recursivelyCreateNativeMenuItems(q);
435 syncWithNativeMenu();
442 if (nativeItems.isEmpty())
443 return QStringLiteral(
"(Empty)");
446 QTextStream debug(&str);
447 for (
const auto *nativeItem : nativeItems)
448 debug << nativeItem->debugText() <<
", ";
450 if (!nativeItems.isEmpty())
455void QQuickMenuPrivate::syncWithNativeMenu()
458 if (!complete || !handle)
461 qCDebug(lcNativeMenus).nospace() <<
"syncWithNativeMenu called on " << q
462 <<
" (complete: " << complete <<
" visible: " << visible <<
") - "
463 <<
"syncing " << nativeItems.size() <<
" item(s)...";
467 handle->setText(title);
468 handle->setEnabled(q->isEnabled());
469 handle->setMinimumWidth(q->implicitWidth());
471 handle->setFont(q->font());
489 for (QQuickNativeMenuItem *item : std::as_const(nativeItems)) {
490 qCDebug(lcNativeMenus) <<
"- syncing" << item <<
"action" << item->action()
491 <<
"sub-menu" << item->subMenu() << item->debugText();
495 qCDebug(lcNativeMenus) <<
"... finished syncing" << q;
499
500
501
502
503
504
505
506void QQuickMenuPrivate::removeNativeMenu()
509 const int qtyItemsToRemove = nativeItems.size();
510 if (qtyItemsToRemove != 0)
511 Q_ASSERT(q->count() == qtyItemsToRemove);
512 for (
int i = 0; i < qtyItemsToRemove; ++i)
514 Q_ASSERT(nativeItems.isEmpty());
521void QQuickMenuPrivate::syncWithUseNativeMenu()
526 if (q->isVisible() || parentMenu)
529 if (maybeNativeHandle() && !useNativeMenu()) {
533 }
else if (useNativeMenu()) {
534 Q_ASSERT(nativeItems.isEmpty());
540void QQuickMenuPrivate::setNativeMenuVisible(
bool visible)
542 qCDebug(lcNativeMenus) <<
"setNativeMenuVisible called with visible" << visible;
544 this->visible = visible;
545 syncWithNativeMenu();
549 QWindow *window =
nullptr;
551 window = QQuickItemPrivate::get(parentItem)->renderWindow(&offset);
553 lastDevicePixelRatio = window ? window->devicePixelRatio() : qGuiApp->devicePixelRatio();
555 const QPointF globalPos = parentItem->mapToGlobal(x, y);
556 const QPoint windowPos = window ? window->mapFromGlobal(globalPos.toPoint()) : parentItem->mapToScene(QPoint(x, y)).toPoint();
557 QRect targetRect(windowPos, QSize(0, 0));
558 auto *daPriv = QQuickItemPrivate::get(parentItem)->deliveryAgentPrivate();
565 if (QPointerEvent *openingEvent = daPriv->eventInDelivery()) {
566 auto *devPriv = QPointingDevicePrivate::get(
const_cast<QPointingDevice *>(openingEvent->pointingDevice()));
567 for (
const auto &pt : std::as_const(openingEvent->points())) {
568 qCDebug(lcNativeMenus) <<
"popup over" << window <<
"its DA" << daPriv->q_func() <<
"opening due to" << openingEvent
569 <<
"with grabbers" << openingEvent->exclusiveGrabber(pt) << openingEvent->passiveGrabbers(pt);
571 if (
auto *opener = openingEvent->exclusiveGrabber(pt))
572 devPriv->removeGrabber(opener,
true);
573 for (
auto passiveGrabber : openingEvent->passiveGrabbers(pt)) {
574 if (
auto *opener = passiveGrabber.get())
575 devPriv->removeGrabber(opener,
true);
579 handle->showPopup(window, QHighDpi::toNativeLocalPosition(targetRect, window),
587void QQuickMenuPrivate::makeEditMenu()
590 syncWithUseNativeMenu();
592 handle->setMenuType(QPlatformMenu::EditMenu);
595QQuickItem *QQuickMenuPrivate::itemAt(
int index)
const
597 return qobject_cast<QQuickItem *>(contentModel->get(index));
600void QQuickMenuPrivate::insertItem(
int index, QQuickItem *item)
602 qCDebug(lcMenu) <<
"insert called with index" << index <<
"item" << item;
605 contentData.append(item);
606 item->setParentItem(contentItem);
607 QQuickItemPrivate::get(item)->setCulled(
true);
610 QQuickItemPrivate::get(item)->addItemChangeListener(
this, QQuickItemPrivate::Destroyed | QQuickItemPrivate::Parent | QQuickItemPrivate::ImplicitWidth);
611 QQuickItemPrivate::get(item)->updateOrAddGeometryChangeListener(
this, QQuickGeometryChange::Width);
612 contentModel->insert(index, item);
614 QQuickMenuItem *menuItem = qobject_cast<QQuickMenuItem *>(item);
616 QQuickMenuItemPrivate::get(menuItem)->setMenu(q);
617 if (QQuickMenu *subMenu = menuItem->subMenu())
618 QQuickMenuPrivate::get(subMenu)->setParentMenu(q);
619 QObjectPrivate::connect(menuItem, &QQuickMenuItem::triggered,
this, &QQuickMenuPrivate::onItemTriggered);
620 QObjectPrivate::connect(menuItem, &QQuickMenuItem::implicitTextPaddingChanged,
this, &QQuickMenuPrivate::updateTextPadding);
621 QObjectPrivate::connect(menuItem, &QQuickMenuItem::visibleChanged,
this, &QQuickMenuPrivate::onItemVisibleChanged);
622 QObjectPrivate::connect(menuItem, &QQuickItem::activeFocusChanged,
this, &QQuickMenuPrivate::onItemActiveFocusChanged);
623 QObjectPrivate::connect(menuItem, &QQuickControl::hoveredChanged,
this, &QQuickMenuPrivate::onItemHovered);
626 QQuickMenuSeparator *separator = qobject_cast<QQuickMenuSeparator *>(item);
628 QObjectPrivate::connect(separator, &QQuickMenuSeparator::visibleChanged,
this, &QQuickMenuPrivate::updateCollapsedSeparators);
630 if (maybeNativeHandle() && complete)
631 maybeCreateAndInsertNativeItem(index, item);
633 if (lcMenu().isDebugEnabled())
634 printContentModelItems();
637 updateContentWidth();
639 updateCollapsedSeparators();
642void QQuickMenuPrivate::maybeCreateAndInsertNativeItem(
int index, QQuickItem *item)
646 Q_ASSERT_X(handle, Q_FUNC_INFO, qPrintable(QString::fromLatin1(
647 "Expected %1 to be using a native menu").arg(QDebug::toString(q))));
648 std::unique_ptr<QQuickNativeMenuItem> nativeMenuItem(QQuickNativeMenuItem::createFromNonNativeItem(q, item));
649 if (!nativeMenuItem) {
651 qmlWarning(q) <<
"Native menu failed to create a native menu item for item at index" << index;
655 nativeItems.insert(index, nativeMenuItem.get());
659 if (nativeMenuItem->handle()) {
660 QQuickNativeMenuItem *before = nativeItems.value(index + 1);
661 handle->insertMenuItem(nativeMenuItem->handle(), before ? before->handle() :
nullptr);
662 qCDebug(lcNativeMenus) <<
"inserted native menu item at index" << index
663 <<
"before" << (before ? before->debugText() : QStringLiteral(
"null"));
665 if (nativeMenuItem->subMenu() && QQuickMenuPrivate::get(nativeMenuItem->subMenu())->nativeItems.count()
666 < nativeMenuItem->subMenu()->count()) {
672 recursivelyCreateNativeMenuItems(nativeMenuItem->subMenu());
676 nativeMenuItem.release();
678 qCDebug(lcNativeMenus) <<
"nativeItems now contains the following items:"
679 << nativeMenuItemListToString(nativeItems);
682void QQuickMenuPrivate::moveItem(
int from,
int to)
684 contentModel->move(from, to);
686 if (maybeNativeHandle())
687 nativeItems.move(from, to);
691
692
693
694
695
696
697
698
699
700void QQuickMenuPrivate::removeItem(
int index, QQuickItem *item, DestructionPolicy destructionPolicy)
702 qCDebug(lcMenu) <<
"removeItem called with index" << index <<
"item" << item;
704 if (maybeNativeHandle())
705 removeNativeItem(index);
707 contentData.removeOne(item);
709 QQuickItemPrivate::get(item)->removeItemChangeListener(
this, QQuickItemPrivate::Destroyed | QQuickItemPrivate::Parent | QQuickItemPrivate::ImplicitWidth);
710 QQuickItemPrivate::get(item)->removeItemChangeListener(
this, QQuickItemPrivate::Geometry);
711 item->setParentItem(
nullptr);
712 contentModel->remove(index);
714 QQuickMenuItem *menuItem = qobject_cast<QQuickMenuItem *>(item);
716 QQuickMenuItemPrivate::get(menuItem)->setMenu(
nullptr);
717 if (QQuickMenu *subMenu = menuItem->subMenu())
718 QQuickMenuPrivate::get(subMenu)->setParentMenu(
nullptr);
719 QObjectPrivate::disconnect(menuItem, &QQuickMenuItem::triggered,
this, &QQuickMenuPrivate::onItemTriggered);
720 QObjectPrivate::disconnect(menuItem, &QQuickMenuItem::implicitTextPaddingChanged,
this, &QQuickMenuPrivate::updateTextPadding);
721 QObjectPrivate::disconnect(menuItem, &QQuickMenuItem::visibleChanged,
this, &QQuickMenuPrivate::onItemVisibleChanged);
722 QObjectPrivate::disconnect(menuItem, &QQuickItem::activeFocusChanged,
this, &QQuickMenuPrivate::onItemActiveFocusChanged);
723 QObjectPrivate::disconnect(menuItem, &QQuickControl::hoveredChanged,
this, &QQuickMenuPrivate::onItemHovered);
726 QQuickMenuSeparator *separator = qobject_cast<QQuickMenuSeparator *>(item);
728 QObjectPrivate::disconnect(separator, &QQuickMenuSeparator::visibleChanged,
this, &QQuickMenuPrivate::updateCollapsedSeparators);
729 collapsedSeparators.remove(item);
732 if (destructionPolicy == DestructionPolicy::Destroy)
735 if (lcMenu().isDebugEnabled())
736 printContentModelItems();
738 updateContentWidth();
740 updateCollapsedSeparators();
744
745
746
747
748
749
750
751
752
753void QQuickMenuPrivate::removeNativeItem(
int index, SyncPolicy syncPolicy)
758 Q_ASSERT_X(index >= 0 && index < nativeItems.size(), Q_FUNC_INFO, qPrintable(QString::fromLatin1(
759 "index %1 is less than 0 or greater than or equal to %2").arg(index).arg(nativeItems.size())));
763 std::unique_ptr<QQuickNativeMenuItem> nativeItem(nativeItems.takeAt(index));
764 qCDebug(lcNativeMenus) <<
"removing native item" << nativeItem->debugText() <<
"at index" << index
765 <<
"from" << q_func() <<
"...";
766 QQuickMenu *subMenu = nativeItem->subMenu();
768 Q_ASSERT(nativeItem->handle());
769 auto *subMenuPrivate = QQuickMenuPrivate::get(subMenu);
770 while (!subMenuPrivate->nativeItems.isEmpty()) {
771 subMenuPrivate->removeNativeItem(0, SyncPolicy::DoNotSync);
775 Q_ASSERT(nativeItem->handle());
776 handle->removeMenuItem(nativeItem->handle());
777 if (syncPolicy == SyncPolicy::Sync)
778 syncWithNativeMenu();
781 auto *subMenuPrivate = QQuickMenuPrivate::get(subMenu);
787 subMenuPrivate->resetNativeData();
790 qCDebug(lcNativeMenus).nospace() <<
"... after removing item at index " << index
791 <<
", nativeItems now contains the following items: " << nativeMenuItemListToString(nativeItems);
794void QQuickMenuPrivate::resetNativeData()
796 qCDebug(lcNativeMenus) <<
"resetNativeData called on" << q_func();
798 triedToCreateNativeMenu =
false;
801void QQuickMenuPrivate::recursivelyCreateNativeMenuItems(QQuickMenu *menu)
803 auto *menuPrivate = QQuickMenuPrivate::get(menu);
806 if (!menuPrivate->triedToCreateNativeMenu)
807 menuPrivate->createNativeMenu();
809 const int qtyItemsToCreate = menuPrivate->contentModel->count();
810 if (menuPrivate->nativeItems.count() == qtyItemsToCreate)
813 qCDebug(lcNativeMenus) <<
"recursively creating" << qtyItemsToCreate <<
"menu item(s) for" << menu;
814 Q_ASSERT(menuPrivate->nativeItems.count() == 0);
815 for (
int i = 0; i < qtyItemsToCreate; ++i) {
816 QQuickItem *item = menu->itemAt(i);
817 menuPrivate->maybeCreateAndInsertNativeItem(i, item);
818 auto *menuItem = qobject_cast<QQuickMenuItem *>(item);
819 if (menuItem && menuItem->subMenu())
820 recursivelyCreateNativeMenuItems(menuItem->subMenu());
824void QQuickMenuPrivate::printContentModelItems()
const
826 qCDebug(lcMenu) <<
"contentModel now contains:";
827 for (
int i = 0; i < contentModel->count(); ++i)
828 qCDebug(lcMenu) <<
"-" << itemAt(i);
831QQuickItem *QQuickMenuPrivate::beginCreateItem()
837 QQmlContext *context = delegate->creationContext();
839 context = qmlContext(q);
841 QObject *object = delegate->beginCreate(context);
842 QQuickItem *item = qobject_cast<QQuickItem *>(object);
846 QQml_setParent_noEvent(item, q);
851void QQuickMenuPrivate::completeCreateItem()
856 delegate->completeCreate();
859QQuickItem *QQuickMenuPrivate::createItem(QQuickMenu *menu)
861 QQuickItem *item = beginCreateItem();
862 if (QQuickMenuItem *menuItem = qobject_cast<QQuickMenuItem *>(item))
863 QQuickMenuItemPrivate::get(menuItem)->setSubMenu(menu);
864 completeCreateItem();
868QQuickItem *QQuickMenuPrivate::createItem(QQuickAction *action)
870 QQuickItem *item = beginCreateItem();
871 if (QQuickAbstractButton *button = qobject_cast<QQuickAbstractButton *>(item))
872 button->setAction(action);
873 completeCreateItem();
877void QQuickMenuPrivate::resizeItem(QQuickItem *item)
879 if (!item || !contentItem)
882 QQuickItemPrivate *p = QQuickItemPrivate::get(item);
883 if (!p->widthValid()) {
884 item->setWidth(contentItem->width());
885 p->widthValidFlag =
false;
889void QQuickMenuPrivate::resizeItems()
894 for (
int i = 0; i < contentModel->count(); ++i)
895 resizeItem(itemAt(i));
898void QQuickMenuPrivate::itemChildAdded(QQuickItem *, QQuickItem *child)
901 if (!QQuickItemPrivate::get(child)->isTransparentForPositioner() && !contentData.contains(child))
902 insertItem(contentModel->count(), child);
905void QQuickMenuPrivate::itemParentChanged(QQuickItem *item, QQuickItem *parent)
909 removeItem(contentModel->indexOf(item,
nullptr), item);
912void QQuickMenuPrivate::itemSiblingOrderChanged(QQuickItem *)
916 QList<QQuickItem *> siblings = contentItem->childItems();
919 for (
int i = 0; i < siblings.size(); ++i) {
920 QQuickItem* sibling = siblings.at(i);
921 if (QQuickItemPrivate::get(sibling)->isTransparentForPositioner())
923 int index = contentModel->indexOf(sibling,
nullptr);
924 q->moveItem(index, to++);
928void QQuickMenuPrivate::itemDestroyed(QQuickItem *item)
930 if (item == contentItem) {
933 QQuickPopupPrivate::itemDestroyed(item);
935 int index = contentModel->indexOf(item,
nullptr);
937 removeItem(index, item);
942void QQuickMenuPrivate::itemGeometryChanged(QQuickItem *item, QQuickGeometryChange,
const QRectF &)
947 if (item == contentItem) {
958void QQuickMenuPrivate::itemImplicitWidthChanged(QQuickItem *item)
960 if (item != contentItem)
961 updateContentWidth();
964void QQuickMenuPrivate::updateContentWidth()
966 if (!contentModel || !contentItem)
971 for (
int i = 0; i < contentModel->count(); ++i) {
972 QQuickItem *item = q->itemAt(i);
973 if (item && QQuickItemPrivate::get(item)->explicitVisible)
974 maxWidth = qMax(maxWidth, item->implicitWidth());
980 if (!qFuzzyIsNull(maxWidth))
981 contentItem->setImplicitWidth(maxWidth);
984QQuickPopupPositioner *QQuickMenuPrivate::getPositioner()
988 positioner =
new QQuickMenuPositioner(q);
994 QQuickMenu *menu =
static_cast<QQuickMenu *>(popup());
995 QQuickMenuPrivate *menu_d = QQuickMenuPrivate::get(menu);
997 if (QQuickMenu *parentMenu = menu_d->parentMenu) {
998 if (menu_d->cascade) {
1001 if (menu_d->popupItem->isMirrored()) {
1002 const qreal distanceToFrame = parentMenu->leftPadding();
1003 const qreal menuX = -menu->width() - distanceToFrame + menu->overlap();
1004 menu->setPosition({menuX, -menu->topPadding()});
1005 }
else if (menu_d->parentItem) {
1006 const qreal distanceToFrame = parentMenu->rightPadding();
1007 const qreal menuX = menu_d->parentItem->width() + distanceToFrame - menu->overlap();
1008 menu->setPosition({menuX, -menu->topPadding()});
1011 const qreal menuX = parentMenu->x() + (parentMenu->width() - menu->width()) / 2;
1012 const qreal menuY = parentMenu->y() + (parentMenu->height() - menu->height()) / 2;
1013 menu->setPosition({menuX, menuY});
1017 QQuickPopupPositioner::reposition();
1020bool QQuickMenuPrivate::prepareEnterTransition()
1023 if (parentMenu && !cascade)
1024 parentMenu->close();
1028 allowHorizontalFlip = cascade && parentMenu;
1030 updateCollapsedSeparators();
1042 auto *contentItemAsListView = qobject_cast<QQuickListView *>(contentItem);
1043 if (contentItemAsListView) {
1044 if (QQuickItemViewPrivate::get(contentItemAsListView)->currentChanges.hasPendingChanges())
1045 contentItemAsListView->forceLayout();
1048 if (!QQuickPopupPrivate::prepareEnterTransition())
1051 if (!hasClosePolicy) {
1052 if (cascade && parentMenu)
1053 closePolicy = cascadingSubMenuClosePolicy;
1055 q->resetClosePolicy();
1060bool QQuickMenuPrivate::prepareExitTransition()
1062 if (!QQuickPopupPrivate::prepareExitTransition())
1067 QQuickMenu *subMenu = currentSubMenu();
1069 QPointer<QQuickMenuItem> currentSubMenuItem = QQuickMenuPrivate::get(subMenu)->currentItem;
1071 subMenu = currentSubMenuItem ? currentSubMenuItem->subMenu() :
nullptr;
1076bool QQuickMenuPrivate::blockInput(QQuickItem *item,
const QPointF &point)
const
1079 return (cascade && parentMenu && contains(point)) || QQuickPopupPrivate::blockInput(item, point);
1082bool QQuickMenuPrivate::handlePress(QQuickItem *item,
const QPointF &point, ulong timestamp)
1086 return QQuickPopupPrivate::handlePress(item, point, timestamp)
1087 || (popupItem == item);
1092
1093
1094
1095
1096
1097
1098bool QQuickMenuPrivate::handleReleaseWithoutGrab(
const QEventPoint &eventPoint)
1100 const QPointF scenePos = eventPoint.scenePosition();
1101 if (!contains(scenePos))
1104 auto *list = qobject_cast<QQuickListView *>(contentItem);
1108 const QPointF listPos = list->mapFromScene(scenePos);
1110 auto *menuItem = qobject_cast<QQuickMenuItem *>(list->itemAt(listPos.x(), listPos.y()));
1111 if (menuItem && menuItem->isHighlighted()) {
1112 menuItem->animateClick();
1119void QQuickMenuPrivate::onItemHovered()
1122 QQuickAbstractButton *button = qobject_cast<QQuickAbstractButton *>(q->sender());
1123 if (!button || !button->isHovered() || !button->isEnabled() || QQuickAbstractButtonPrivate::get(button)->touchId != -1)
1126 QQuickMenuItem *oldCurrentItem = currentItem;
1128 int index = contentModel->indexOf(button,
nullptr);
1130 setCurrentIndex(index, Qt::OtherFocusReason);
1131 if (oldCurrentItem != currentItem) {
1132 if (oldCurrentItem) {
1133 QQuickMenu *subMenu = oldCurrentItem->subMenu();
1138 QQuickMenu *subMenu = currentItem->menu();
1139 if (subMenu && subMenu->cascade())
1146void QQuickMenuPrivate::onItemTriggered()
1149 QQuickMenuItem *item = qobject_cast<QQuickMenuItem *>(q->sender());
1153 if (QQuickMenu *subMenu = item->subMenu()) {
1154 auto subMenuPrivate = QQuickMenuPrivate::get(subMenu);
1155 subMenuPrivate->popup(subMenuPrivate->firstEnabledMenuItem());
1161void QQuickMenuPrivate::onItemActiveFocusChanged()
1164 QQuickItem *item = qobject_cast<QQuickItem*>(q->sender());
1165 if (!item->hasActiveFocus())
1168 int indexOfItem = contentModel->indexOf(item,
nullptr);
1169 QQuickControl *control = qobject_cast<QQuickControl *>(item);
1170 setCurrentIndex(indexOfItem, control ? control->focusReason() : Qt::OtherFocusReason);
1173void QQuickMenuPrivate::onItemVisibleChanged()
1175 updateTextPadding();
1176 updateCollapsedSeparators();
1179void QQuickMenuPrivate::updateTextPadding()
1186 for (
int i = 0; i < q->count(); ++i) {
1187 if (
const auto menuItem = qobject_cast<QQuickMenuItem *>(itemAt(i)))
1188 if (menuItem->isVisible())
1189 padding = qMax(padding, menuItem->implicitTextPadding());
1192 if (padding == textPadding)
1195 textPadding = padding;
1197 for (
int i = 0; i < q->count(); ++i) {
1198 if (
const auto menuItem = qobject_cast<QQuickMenuItem *>(itemAt(i)))
1199 emit menuItem->textPaddingChanged();
1203void QQuickMenuPrivate::updateCollapsedSeparators()
1205 if (!complete || updatingCollapsedSeparators)
1210 QScopedValueRollback guard(updatingCollapsedSeparators,
true);
1212 auto isExplicitlyVisible = [](
const QQuickItem *item) {
1213 return QQuickItemPrivate::get(item)->explicitVisible;
1216 auto hasVisibleBinding = [](QQuickItem *item) {
1217 static const int visibleIndex = QQuickItem::staticMetaObject.indexOfProperty(
"visible");
1218 auto ddata = QQmlData::get(item,
false);
1221 return ddata->hasBindingBit(visibleIndex);
1224 auto hideSeparator = [
this](QQuickItem *separatorItem) {
1225 separatorItem->setVisible(
false);
1226 separatorItem->setHeight(0);
1227 collapsedSeparators.insert(separatorItem);
1230 auto showSeparator = [
this](QQuickItem *separatorItem) {
1231 if (!collapsedSeparators.contains(separatorItem))
1233 separatorItem->setVisible(
true);
1234 separatorItem->resetHeight();
1235 collapsedSeparators.remove(separatorItem);
1238 const int count = contentModel->count();
1240 if (!collapsibleSeparators) {
1241 for (
int i = 0; i < count; ++i)
1242 showSeparator(itemAt(i));
1246 QQuickItem *visibleSeparatorBefore =
nullptr;
1247 bool hasVisibleItemBefore =
false;
1249 for (
int i = 0; i < count; ++i) {
1250 QQuickItem *item = itemAt(i);
1254 if (qobject_cast<QQuickMenuSeparator *>(item)) {
1255 if (hasVisibleBinding(item)) {
1256 if (isExplicitlyVisible(item) && !visibleSeparatorBefore)
1257 visibleSeparatorBefore = item;
1261 if (visibleSeparatorBefore) {
1262 hideSeparator(item);
1263 }
else if (hasVisibleItemBefore) {
1264 visibleSeparatorBefore = item;
1265 showSeparator(item);
1267 hideSeparator(item);
1269 }
else if (isExplicitlyVisible(item)) {
1270 hasVisibleItemBefore =
true;
1271 visibleSeparatorBefore =
nullptr;
1276 if (visibleSeparatorBefore && !hasVisibleBinding(visibleSeparatorBefore))
1277 hideSeparator(visibleSeparatorBefore);
1280QQuickMenu *QQuickMenuPrivate::currentSubMenu()
const
1285 return currentItem->subMenu();
1288void QQuickMenuPrivate::setParentMenu(QQuickMenu *parent)
1291 if (parentMenu == parent)
1295 QObject::disconnect(parentMenu.data(), &QQuickMenu::cascadeChanged, q, &QQuickMenu::setCascade);
1296 disconnect(parentMenu.data(), &QQuickMenu::parentChanged,
this, &QQuickMenuPrivate::resolveParentItem);
1299 QObject::connect(parent, &QQuickMenu::cascadeChanged, q, &QQuickMenu::setCascade);
1300 connect(parent, &QQuickMenu::parentChanged,
this, &QQuickMenuPrivate::resolveParentItem);
1303 parentMenu = parent;
1305 resolveParentItem();
1310 QQuickMenu *menu = QQuickMenuPrivate::get(subMenu)->parentMenu;
1311 for (
int i = 0; i < QQuickMenuPrivate::get(menu)->contentModel->count(); ++i) {
1312 QQuickMenuItem *item = qobject_cast<QQuickMenuItem *>(menu->itemAt(i));
1313 if (item && item->subMenu() == subMenu)
1319void QQuickMenuPrivate::resolveParentItem()
1323 q->resetParentItem();
1325 q->setParentItem(parentMenu->parentItem());
1327 q->setParentItem(findParentMenuItem(q));
1330void QQuickMenuPrivate::propagateKeyEvent(QKeyEvent *event)
1332 if (QQuickMenuItem *menuItem = qobject_cast<QQuickMenuItem *>(parentItem)) {
1333 if (QQuickMenu *menu = menuItem->menu())
1334 QQuickMenuPrivate::get(menu)->propagateKeyEvent(event);
1335#if QT_CONFIG(quicktemplates2_container)
1336 }
else if (QQuickMenuBarItem *menuBarItem = qobject_cast<QQuickMenuBarItem *>(parentItem)) {
1337 if (QQuickMenuBar *menuBar = menuBarItem->menuBar()) {
1339 QCoreApplication::sendEvent(menuBar, event);
1345void QQuickMenuPrivate::startHoverTimer()
1349 hoverTimer = q->startTimer(SUBMENU_DELAY);
1352void QQuickMenuPrivate::stopHoverTimer()
1358 q->killTimer(hoverTimer);
1362void QQuickMenuPrivate::setCurrentIndex(
int index, Qt::FocusReason reason)
1365 if (currentIndex == index)
1368 QQuickMenuItem *newCurrentItem = qobject_cast<QQuickMenuItem *>(itemAt(index));
1369 if (currentItem != newCurrentItem) {
1372 currentItem->setHighlighted(
false);
1373 if (!newCurrentItem && window) {
1374 QQuickItem *focusItem = QQuickItemPrivate::get(contentItem)->subFocusItem;
1376 auto *daPriv = QQuickWindowPrivate::get(window)->deliveryAgentPrivate();
1377 daPriv->clearFocusInScope(contentItem, focusItem, Qt::OtherFocusReason);
1381 if (newCurrentItem) {
1382 newCurrentItem->setHighlighted(
true);
1383 newCurrentItem->forceActiveFocus(reason);
1385 currentItem = newCurrentItem;
1388 currentIndex = index;
1389 emit q->currentIndexChanged();
1392bool QQuickMenuPrivate::activateNextItem()
1394 int index = currentIndex;
1395 int count = contentModel->count();
1396 while (++index < count) {
1397 QQuickItem *item = itemAt(index);
1398 if (!item || !item->activeFocusOnTab() || !item->isEnabled())
1400 setCurrentIndex(index, Qt::TabFocusReason);
1406bool QQuickMenuPrivate::activatePreviousItem()
1408 int index = currentIndex;
1409 while (--index >= 0) {
1410 QQuickItem *item = itemAt(index);
1411 if (!item || !item->activeFocusOnTab() || !item->isEnabled())
1413 setCurrentIndex(index, Qt::BacktabFocusReason);
1419QQuickMenuItem *QQuickMenuPrivate::firstEnabledMenuItem()
const
1421 for (
int i = 0; i < contentModel->count(); ++i) {
1422 QQuickItem *item = itemAt(i);
1423 if (!item || !item->isEnabled())
1426 QQuickMenuItem *menuItem = qobject_cast<QQuickMenuItem *>(item);
1435void QQuickMenuPrivate::contentData_append(QQmlListProperty<QObject> *prop, QObject *obj)
1437 QQuickMenu *q = qobject_cast<QQuickMenu *>(prop->object);
1438 QQuickMenuPrivate *p = QQuickMenuPrivate::get(q);
1440 QQuickItem *item = qobject_cast<QQuickItem *>(obj);
1442 if (QQuickAction *action = qobject_cast<QQuickAction *>(obj))
1443 item = p->createItem(action);
1444 else if (QQuickMenu *menu = qobject_cast<QQuickMenu *>(obj))
1445 item = p->createItem(menu);
1449 if (QQuickItemPrivate::get(item)->isTransparentForPositioner()) {
1450 QQuickItemPrivate::get(item)->addItemChangeListener(p, QQuickItemPrivate::SiblingOrder);
1451 item->setParentItem(p->contentItem);
1452 }
else if (p->contentModel->indexOf(item,
nullptr) == -1) {
1456 p->contentData.append(obj);
1460qsizetype QQuickMenuPrivate::contentData_count(QQmlListProperty<QObject> *prop)
1462 QQuickMenu *q =
static_cast<QQuickMenu *>(prop->object);
1463 return QQuickMenuPrivate::get(q)->contentData.size();
1466QObject *QQuickMenuPrivate::contentData_at(QQmlListProperty<QObject> *prop, qsizetype index)
1468 QQuickMenu *q =
static_cast<QQuickMenu *>(prop->object);
1469 return QQuickMenuPrivate::get(q)->contentData.value(index);
1472QPalette QQuickMenuPrivate::defaultPalette()
const
1474 return QQuickTheme::palette(QQuickTheme::Menu);
1477void QQuickMenuPrivate::contentData_clear(QQmlListProperty<QObject> *prop)
1479 QQuickMenu *q =
static_cast<QQuickMenu *>(prop->object);
1480 QQuickMenuPrivate::get(q)->contentData.clear();
1483void QQuickMenuPrivate::resetContentItem()
1486 QQuickItemPrivate::get(contentItem)->removeItemChangeListener(
this, QQuickItemPrivate::Children);
1487 QQuickItemPrivate::get(contentItem)->removeItemChangeListener(
this, QQuickItemPrivate::Destroyed);
1488 QQuickItemPrivate::get(contentItem)->removeItemChangeListener(
this, QQuickItemPrivate::Geometry);
1490 const auto children = contentItem->childItems();
1491 for (QQuickItem *child : std::as_const(children))
1492 QQuickItemPrivate::get(child)->removeItemChangeListener(
this, QQuickItemPrivate::SiblingOrder);
1493 contentItem =
nullptr;
1497QQuickMenu::QQuickMenu(QObject *parent)
1498 : QQuickPopup(*(
new QQuickMenuPrivate), parent)
1503 connect(d->contentModel, &QQmlObjectModel::countChanged,
this, &QQuickMenu::countChanged);
1506QQuickMenu::~QQuickMenu()
1509 qCDebug(lcNativeMenus) <<
"destroying" <<
this
1511 << d->contentModel->count()
1512 <<
"native item count:" << d->nativeItems.count();
1516 if (
auto *menuItem = qobject_cast<QQuickMenuItem *>(d->parentItem)) {
1517 if (menuItem->subMenu() ==
this) {
1518 auto *menuItemPriv = QQuickMenuItemPrivate::get(menuItem);
1519 menuItemPriv->setSubMenu(
nullptr);
1527 while (d->contentModel->count() > 0)
1528 d->removeItem(0, d->itemAt(0), QQuickMenuPrivate::DestructionPolicy::Destroy);
1530 d->resetContentItem();
1534
1535
1536
1537
1538QQuickItem *QQuickMenu::itemAt(
int index)
const
1540 Q_D(
const QQuickMenu);
1541 return d->itemAt(index);
1545
1546
1547
1548
1549
1550
1551
1552void QQuickMenu::addItem(QQuickItem *item)
1555 insertItem(d->contentModel->count(), item);
1559
1560
1561
1562
1563
1564
1565
1566void QQuickMenu::insertItem(
int index, QQuickItem *item)
1571 const int count = d->contentModel->count();
1572 if (index < 0 || index > count)
1575 int oldIndex = d->contentModel->indexOf(item,
nullptr);
1576 if (oldIndex != -1) {
1577 if (oldIndex < index)
1579 if (oldIndex != index) {
1580 d->moveItem(oldIndex, index);
1583 d->insertItem(index, item);
1588
1589
1590
1591
1592void QQuickMenu::moveItem(
int from,
int to)
1595 const int count = d->contentModel->count();
1596 if (from < 0 || from > count - 1)
1598 if (to < 0 || to > count - 1)
1602 d->moveItem(from, to);
1606
1607
1608
1609
1610
1611void QQuickMenu::removeItem(QQuickItem *item)
1617 const int index = d->contentModel->indexOf(item,
nullptr);
1621 d->removeItem(index, item, QQuickMenuPrivate::DestructionPolicy::Destroy);
1625
1626
1627
1628
1629
1630
1631
1632QQuickItem *QQuickMenu::takeItem(
int index)
1635 const int count = d->contentModel->count();
1636 if (index < 0 || index >= count)
1639 QQuickItem *item = itemAt(index);
1641 d->removeItem(index, item);
1646
1647
1648
1649
1650
1651
1652QQuickMenu *QQuickMenu::menuAt(
int index)
const
1654 Q_D(
const QQuickMenu);
1655 QQuickMenuItem *item = qobject_cast<QQuickMenuItem *>(d->itemAt(index));
1659 return item->subMenu();
1663
1664
1665
1666
1667
1668
1669void QQuickMenu::addMenu(QQuickMenu *menu)
1672 insertMenu(d->contentModel->count(), menu);
1676
1677
1678
1679
1680
1681
1682void QQuickMenu::insertMenu(
int index, QQuickMenu *menu)
1688 insertItem(index, d->createItem(menu));
1692
1693
1694
1695
1696
1697void QQuickMenu::removeMenu(QQuickMenu *menu)
1703 const int count = d->contentModel->count();
1704 for (
int i = 0; i < count; ++i) {
1705 QQuickMenuItem *item = qobject_cast<QQuickMenuItem *>(d->itemAt(i));
1706 if (!item || item->subMenu() != menu)
1713 menu->deleteLater();
1717
1718
1719
1720
1721
1722
1723
1724
1725QQuickMenu *QQuickMenu::takeMenu(
int index)
1728 QQuickMenuItem *item = qobject_cast<QQuickMenuItem *>(d->itemAt(index));
1732 QQuickMenu *subMenu = item->subMenu();
1736 d->removeItem(index, item);
1737 item->deleteLater();
1743
1744
1745
1746
1747
1748
1749QQuickAction *QQuickMenu::actionAt(
int index)
const
1751 Q_D(
const QQuickMenu);
1752 if (!
const_cast<QQuickMenuPrivate *>(d)->maybeNativeHandle()) {
1753 QQuickAbstractButton *item = qobject_cast<QQuickAbstractButton *>(d->itemAt(index));
1757 return item->action();
1759 if (index < 0 || index >= d->nativeItems.size())
1762 return d->nativeItems.at(index)->action();
1767
1768
1769
1770
1771
1772
1773void QQuickMenu::addAction(QQuickAction *action)
1776 insertAction(d->contentModel->count(), action);
1780
1781
1782
1783
1784
1785
1786void QQuickMenu::insertAction(
int index, QQuickAction *action)
1792 insertItem(index, d->createItem(action));
1796
1797
1798
1799
1800
1801void QQuickMenu::removeAction(QQuickAction *action)
1807 const int count = d->contentModel->count();
1808 for (
int i = 0; i < count; ++i) {
1809 QQuickMenuItem *item = qobject_cast<QQuickMenuItem *>(d->itemAt(i));
1810 if (!item || item->action() != action)
1817 action->deleteLater();
1821
1822
1823
1824
1825
1826
1827
1828
1829QQuickAction *QQuickMenu::takeAction(
int index)
1832 QQuickMenuItem *item = qobject_cast<QQuickMenuItem *>(d->itemAt(index));
1836 QQuickAction *action = item->action();
1840 d->removeItem(index, item);
1841 item->deleteLater();
1845bool QQuickMenu::isVisible()
const
1847 Q_D(
const QQuickMenu);
1848 if (d->maybeNativeHandle())
1850 return QQuickPopup::isVisible();
1853void QQuickMenu::setVisible(
bool visible)
1856 if (visible == d->visible)
1859 auto *window =
this->window();
1860 if (visible && window) {
1863 QQuickWindowPrivate::get(window)->rmbContextMenuEventEnabled =
false;
1868 if (!d->parentMenu) {
1869 QQuickOverlay *overlay = QQuickOverlay::overlay(window, parentItem());
1871 const QList<QQuickPopup *> allPopups = QQuickOverlayPrivate::get(overlay)->allPopups;
1872 for (
auto *popup : allPopups) {
1873 if (popup !=
this && qobject_cast<QQuickMenu *>(popup))
1880 if (visible && ((d->useNativeMenu() && !d->maybeNativeHandle())
1881 || (!d->useNativeMenu() && d->maybeNativeHandle()))) {
1885 qCDebug(lcNativeMenus) <<
"setVisible called - useNativeMenu:" << d->useNativeMenu()
1886 <<
"maybeNativeHandle:" << d->maybeNativeHandle();
1887 d->syncWithUseNativeMenu();
1889 if (d->maybeNativeHandle()) {
1890 d->setNativeMenuVisible(visible);
1895 if (QQuickMenuPrivate::get(d->rootMenu())->menuBar)
1896 d->wmWindowType = QNativeInterface::Private::QXcbWindow::DropDownMenu;
1898 d->wmWindowType = QNativeInterface::Private::QXcbWindow::PopupMenu;
1903 QQuickPopup::setVisible(visible);
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927QVariant QQuickMenu::contentModel()
const
1929 Q_D(
const QQuickMenu);
1930 return QVariant::fromValue(d->contentModel);
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948QQmlListProperty<QObject> QQuickMenu::contentData()
1951 if (!d->contentItem)
1952 QQuickControlPrivate::get(d->popupItem)->executeContentItem();
1953 return QQmlListProperty<QObject>(
this,
nullptr,
1954 QQuickMenuPrivate::contentData_append,
1955 QQuickMenuPrivate::contentData_count,
1956 QQuickMenuPrivate::contentData_at,
1957 QQuickMenuPrivate::contentData_clear);
1961
1962
1963
1964
1965
1966
1967
1968
1969QString QQuickMenu::title()
const
1971 Q_D(
const QQuickMenu);
1975void QQuickMenu::setTitle(
const QString &title)
1978 if (title == d->title)
1982 d->handle->setText(title);
1983 emit titleChanged(title);
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2003QQuickIcon QQuickMenu::icon()
const
2005 Q_D(
const QQuickMenu);
2009void QQuickMenu::setIcon(
const QQuickIcon &icon)
2012 if (icon == d->icon)
2015 d->icon.ensureRelativeSourceResolved(
this);
2016 emit iconChanged(icon);
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044bool QQuickMenu::separatorsCollapsible()
const
2046 Q_D(
const QQuickMenu);
2047 return d->collapsibleSeparators;
2050void QQuickMenu::setSeparatorsCollapsible(
bool collapsible)
2053 if (d->collapsibleSeparators == collapsible)
2055 d->collapsibleSeparators = collapsible;
2056 emit separatorsCollapsibleChanged();
2057 d->updateCollapsedSeparators();
2059 d->handle->syncSeparatorsCollapsible(collapsible);
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078bool QQuickMenu::cascade()
const
2080 Q_D(
const QQuickMenu);
2084void QQuickMenu::setCascade(
bool cascade)
2087 if (d->cascade == cascade)
2089 d->cascade = cascade;
2091 d->resolveParentItem();
2092 emit cascadeChanged(cascade);
2095void QQuickMenu::resetCascade()
2099 setCascade(d->parentMenu->cascade());
2101 setCascade(shouldCascade());
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120qreal QQuickMenu::overlap()
const
2122 Q_D(
const QQuickMenu);
2126void QQuickMenu::setOverlap(qreal overlap)
2129 if (d->overlap == overlap)
2131 d->overlap = overlap;
2132 emit overlapChanged();
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157QQmlComponent *QQuickMenu::delegate()
const
2159 Q_D(
const QQuickMenu);
2163void QQuickMenu::setDelegate(QQmlComponent *delegate)
2166 if (d->delegate == delegate)
2169 d->delegate = delegate;
2170 emit delegateChanged();
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185int QQuickMenu::currentIndex()
const
2187 Q_D(
const QQuickMenu);
2188 return d->currentIndex;
2191void QQuickMenu::setCurrentIndex(
int index)
2194 d->setCurrentIndex(index, Qt::OtherFocusReason);
2198
2199
2200
2201
2202
2203
2204int QQuickMenu::count()
const
2206 Q_D(
const QQuickMenu);
2207 return d->contentModel->count();
2210void QQuickMenuPrivate::popup(QQuickItem *menuItem)
2215 QQmlNullableValue<QPointF> pos;
2216#if QT_CONFIG(cursor)
2217 if (parentItem && QGuiApplicationPrivate::platformIntegration()->hasCapability(QPlatformIntegration::MultipleWindows))
2218 pos = parentItem->mapFromGlobal(QCursor::pos());
2222 if (!pos.isValid() && parentItem)
2223 pos = QPointF((parentItem->width() - q->width()) / 2, (parentItem->height() - q->height()) / 2);
2225 q->popup(pos.isValid() ? pos.value() : QPointF(), menuItem);
2228void QQuickMenu::popup(
const QPointF &position, QQuickItem *menuItem)
2232#if QT_CONFIG(cursor)
2234 offset = d->popupItem->mapFromItem(menuItem, QPointF(0, 0)).y();
2236 setPosition(position - QPointF(0, offset));
2239 d->setCurrentIndex(d->contentModel->indexOf(menuItem,
nullptr), Qt::PopupFocusReason);
2241 d->setCurrentIndex(-1, Qt::PopupFocusReason);
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2291void QQuickMenu::popup(QQuickItem *parent, qreal x, qreal y, QQuickItem *menuItem)
2293 popup(parent, QPointF {x, y}, menuItem);
2296void QQuickMenu::popup(QQuickItem *parent,
const QPointF &position, QQuickItem *menuItem)
2299 if (parent && !d->popupItem->isAncestorOf(parent))
2300 setParentItem(parent);
2301 popup(position, menuItem);
2304void QQuickMenu::popup(QQuickItem *parent, QQuickItem *menuItem)
2307 QQuickItem *parentItem =
nullptr;
2308 if (parent && !d->popupItem->isAncestorOf(parent))
2309 parentItem = parent;
2311 setParentItem(parentItem);
2317void QQuickMenu::popup(QQuickItem *parent)
2320 QQuickItem *menuItem =
nullptr;
2322 if (!d->popupItem->isAncestorOf(parent))
2323 setParentItem(parent);
2324 if (d->popupItem->isAncestorOf(parent))
2330void QQuickMenu::popup(qreal x, qreal y, QQuickItem *menuItem)
2332 popup(QPointF {x, y}, menuItem);
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350void QQuickMenu::dismiss()
2352 QQuickMenu *menu =
this;
2355 menu = QQuickMenuPrivate::get(menu)->parentMenu;
2359void QQuickMenu::componentComplete()
2362 QQuickPopup::componentComplete();
2364 d->updateTextPadding();
2365 d->syncWithUseNativeMenu();
2368void QQuickMenu::contentItemChange(QQuickItem *newItem, QQuickItem *oldItem)
2371 QQuickPopup::contentItemChange(newItem, oldItem);
2374 QQuickItemPrivate::get(oldItem)->removeItemChangeListener(d, QQuickItemPrivate::Children);
2375 QQuickItemPrivate::get(oldItem)->removeItemChangeListener(d, QQuickItemPrivate::Destroyed);
2376 QQuickItemPrivate::get(oldItem)->removeItemChangeListener(d, QQuickItemPrivate::Geometry);
2379 QQuickItemPrivate::get(newItem)->addItemChangeListener(d, QQuickItemPrivate::Children);
2380 QQuickItemPrivate::get(newItem)->addItemChangeListener(d, QQuickItemPrivate::Destroyed);
2381 QQuickItemPrivate::get(newItem)->updateOrAddGeometryChangeListener(d, QQuickGeometryChange::Width);
2384 d->contentItem = newItem;
2387void QQuickMenu::itemChange(QQuickItem::ItemChange change,
const QQuickItem::ItemChangeData &data)
2390 QQuickPopup::itemChange(change, data);
2393 case QQuickItem::ItemVisibleHasChanged:
2394 if (!data.boolValue && d->cascade) {
2397 d->setCurrentIndex(-1, Qt::OtherFocusReason);
2405void QQuickMenu::keyPressEvent(QKeyEvent *event)
2408 QQuickPopup::keyPressEvent(event);
2416 switch (event->key()) {
2418 if (!d->activatePreviousItem())
2419 d->propagateKeyEvent(event);
2423 d->activateNextItem();
2429 if (d->popupItem->isMirrored() == (event->key() == Qt::Key_Right)) {
2430 if (d->parentMenu && d->currentItem) {
2432 d->parentMenu->open();
2437 if (QQuickMenu *subMenu = d->currentSubMenu()) {
2438 auto subMenuPrivate = QQuickMenuPrivate::get(subMenu);
2439 subMenuPrivate->popup(subMenuPrivate->firstEnabledMenuItem());
2443 if (!event->isAccepted())
2444 d->propagateKeyEvent(event);
2447#if QT_CONFIG(shortcut)
2451 if (!QKeySequence::mnemonic(QStringLiteral(
"&A")).isEmpty())
2460#if QT_CONFIG(shortcut)
2461 if (event->modifiers() == Qt::NoModifier) {
2462 for (
int i = 0; i < count(); ++i) {
2463 QQuickAbstractButton *item = qobject_cast<QQuickAbstractButton*>(d->itemAt(i));
2466 const QKeySequence keySequence = QKeySequence::mnemonic(item->text());
2467 if (keySequence.isEmpty())
2469 if (keySequence[0].key() == event->key()) {
2478void QQuickMenu::timerEvent(QTimerEvent *event)
2481 if (event->timerId() == d->hoverTimer) {
2482 if (QQuickMenu *subMenu = d->currentSubMenu())
2484 d->stopHoverTimer();
2487 QQuickPopup::timerEvent(event);
2490QFont QQuickMenu::defaultFont()
const
2492 return QQuickTheme::font(QQuickTheme::Menu);
2495#if QT_CONFIG(accessibility)
2496QAccessible::Role QQuickMenu::accessibleRole()
const
2498 return QAccessible::PopupMenu;
2504#include "moc_qquickmenu_p.cpp"
Combined button and popup list for selecting options.