aboutsummaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorJ-P Nurmi <jpnurmi@qt.io>2017-07-12 15:13:26 +0200
committerJ-P Nurmi <jpnurmi@qt.io>2017-07-13 11:00:02 +0000
commit01dbd3a8ed6187ec9a7ff2bb61031c3291b24d97 (patch)
tree1bffd776e375946050dfd395197ef58203f86f98 /src
parent141fdff2c59eac4ded9581dd7c6b606bb91197b3 (diff)
QQuickMenu: resolve the parent item
A cascading sub-menu is parented to the menu item in its parent menu, but a non-cascading sub-menu is parented to the parent menu's parent item to be able to have the sub-menu visible while the parent menu is hidden (non-cascading menus are opened and closed one by one). Based on these conditions, resolve the parent item automatically instead of doing it by hand in openSubMenu(). Change-Id: I5dd38ee0978604cd604dabf6f40ac4fb5f3eebbe Reviewed-by: Mitch Curtis <mitch.curtis@qt.io>
Diffstat (limited to 'src')
-rw-r--r--src/quicktemplates2/qquickmenu.cpp39
-rw-r--r--src/quicktemplates2/qquickmenu_p_p.h1
2 files changed, 33 insertions, 7 deletions
diff --git a/src/quicktemplates2/qquickmenu.cpp b/src/quicktemplates2/qquickmenu.cpp
index 68607d33..77327e55 100644
--- a/src/quicktemplates2/qquickmenu.cpp
+++ b/src/quicktemplates2/qquickmenu.cpp
@@ -476,12 +476,8 @@ void QQuickMenuPrivate::openSubMenu(QQuickMenuItem *item, bool activate)
if (!subMenu)
return;
- if (cascade) {
- subMenu->setParentItem(item);
- } else {
+ if (!cascade)
q->close();
- subMenu->setParentItem(parentItem);
- }
if (activate)
QQuickMenuPrivate::get(subMenu)->setCurrentIndex(0, Qt::PopupFocusReason);
@@ -494,13 +490,40 @@ void QQuickMenuPrivate::setParentMenu(QQuickMenu *parent)
if (parentMenu == parent)
return;
- if (parentMenu)
+ if (parentMenu) {
QObject::disconnect(parentMenu.data(), &QQuickMenu::cascadeChanged, q, &QQuickMenu::setCascade);
- if (parent)
+ disconnect(parentMenu.data(), &QQuickMenu::parentChanged, this, &QQuickMenuPrivate::resolveParentItem);
+ }
+ if (parent) {
QObject::connect(parent, &QQuickMenu::cascadeChanged, q, &QQuickMenu::setCascade);
+ connect(parent, &QQuickMenu::parentChanged, this, &QQuickMenuPrivate::resolveParentItem);
+ }
parentMenu = parent;
q->resetCascade();
+ resolveParentItem();
+}
+
+static QQuickItem *findParentMenuItem(QQuickMenu *subMenu)
+{
+ QQuickMenu *menu = QQuickMenuPrivate::get(subMenu)->parentMenu;
+ for (int i = 0; i < QQuickMenuPrivate::get(menu)->contentModel->count(); ++i) {
+ QQuickMenuItem *item = qobject_cast<QQuickMenuItem *>(menu->itemAt(i));
+ if (item && item->subMenu() == subMenu)
+ return item;
+ }
+ return nullptr;
+}
+
+void QQuickMenuPrivate::resolveParentItem()
+{
+ Q_Q(QQuickMenu);
+ if (!parentMenu)
+ q->resetParentItem();
+ else if (!cascade)
+ q->setParentItem(parentMenu->parentItem());
+ else
+ q->setParentItem(findParentMenuItem(q));
}
void QQuickMenuPrivate::startHoverTimer()
@@ -1047,6 +1070,8 @@ void QQuickMenu::setCascade(bool cascade)
if (d->cascade == cascade)
return;
d->cascade = cascade;
+ if (d->parentMenu)
+ d->resolveParentItem();
emit cascadeChanged(cascade);
}
diff --git a/src/quicktemplates2/qquickmenu_p_p.h b/src/quicktemplates2/qquickmenu_p_p.h
index 7a1c631f..b8da1764 100644
--- a/src/quicktemplates2/qquickmenu_p_p.h
+++ b/src/quicktemplates2/qquickmenu_p_p.h
@@ -104,6 +104,7 @@ public:
void openSubMenu(QQuickMenuItem *item, bool activate);
void setParentMenu(QQuickMenu *parent);
+ void resolveParentItem();
void startHoverTimer();
void stopHoverTimer();