diff options
author | J-P Nurmi <jpnurmi@qt.io> | 2017-07-10 17:26:50 +0200 |
---|---|---|
committer | J-P Nurmi <jpnurmi@qt.io> | 2017-07-11 16:37:44 +0000 |
commit | b9637d71b391c165b8e6b1177e1ca1dc81557d76 (patch) | |
tree | 35452821fdcfa3593dfe0a071164ac5f00577209 /src/quicktemplates2/qquickmenu.cpp | |
parent | f0efd6b5f49703f64b97287ec7ecabc777536d65 (diff) |
Add QQuickMenu::dismiss()
[ChangeLog][Controls][Menu] Added a dismiss() method. Unlike close()
that only closes a menu and its sub-menus, dismiss() closes the whole
hierarchy of menus, including the parent menus.
Change-Id: Ibde2102bbb542a5deefdf17843496b7b80021db4
Reviewed-by: Mitch Curtis <mitch.curtis@qt.io>
Diffstat (limited to 'src/quicktemplates2/qquickmenu.cpp')
-rw-r--r-- | src/quicktemplates2/qquickmenu.cpp | 89 |
1 files changed, 54 insertions, 35 deletions
diff --git a/src/quicktemplates2/qquickmenu.cpp b/src/quicktemplates2/qquickmenu.cpp index c87573a1..78afeac2 100644 --- a/src/quicktemplates2/qquickmenu.cpp +++ b/src/quicktemplates2/qquickmenu.cpp @@ -360,6 +360,22 @@ void QQuickMenuPrivate::itemGeometryChanged(QQuickItem *, QQuickGeometryChange, resizeItems(); } +bool QQuickMenuPrivate::prepareExitTransition() +{ + if (!QQuickPopupPrivate::prepareExitTransition()) + return false; + + if (currentItem) { + QQuickMenu *subMenu = currentItem->subMenu(); + while (subMenu) { + QPointer<QQuickMenuItem> currentSubMenuItem = QQuickMenuPrivate::get(subMenu)->currentItem; + subMenu->close(); + subMenu = currentSubMenuItem ? currentSubMenuItem->subMenu() : nullptr; + } + } + return true; +} + bool QQuickMenuPrivate::blockInput(QQuickItem *item, const QPointF &point) const { // keep the parent menu open when a cascading sub-menu (this menu) is interacted with @@ -379,8 +395,11 @@ void QQuickMenuPrivate::onItemHovered() if (index != -1) { setCurrentIndex(index, Qt::OtherFocusReason); if (oldCurrentItem != currentItem) { - if (oldCurrentItem) - closeSubMenu(oldCurrentItem->subMenu()); + if (oldCurrentItem) { + QQuickMenu *subMenu = oldCurrentItem->subMenu(); + if (subMenu) + subMenu->close(); + } if (currentItem) { QQuickMenu *subMenu = currentItem->menu(); if (subMenu && subMenu->cascade()) @@ -397,16 +416,10 @@ void QQuickMenuPrivate::onItemTriggered() if (!item) return; - if (item->subMenu()) { + if (item->subMenu()) openSubMenu(item, true); - } else { - // close the whole chain of menus - QQuickMenu *menu = q; - while (menu) { - menu->close(); - menu = QQuickMenuPrivate::get(menu)->parentMenu; - } - } + else + q->dismiss(); } void QQuickMenuPrivate::onItemActiveFocusChanged() @@ -436,6 +449,7 @@ void QQuickMenuPrivate::openSubMenu(QQuickMenuItem *item, bool activate) else subMenu->setPosition(QPointF(item->width() + q->rightPadding() - subMenu->overlap(), -subMenu->topPadding())); } else { + q->close(); subMenu->setParentItem(parentItem); subMenu->setClosePolicy(defaultMenuClosePolicy); subMenu->setPosition(QPointF(q->x() + (q->width() - subMenu->width()) / 2, @@ -448,27 +462,6 @@ void QQuickMenuPrivate::openSubMenu(QQuickMenuItem *item, bool activate) p->setCurrentIndex(0, Qt::PopupFocusReason); subMenu->setCascade(cascade); subMenu->open(); - - if (!subMenu->cascade()) - q->close(); -} - -void QQuickMenuPrivate::closeSubMenu(QQuickMenu *subMenu) -{ - if (!subMenu || !subMenu->isVisible()) - return; - - // re-open the parent menu of a cascading sub-menu - QQuickMenu *parentMenu = QQuickMenuPrivate::get(subMenu)->parentMenu; - if (parentMenu && !subMenu->cascade()) - parentMenu->open(); - - // close the whole chain of sub-menus - while (subMenu) { - QPointer<QQuickMenuItem> currentSubMenuItem = QQuickMenuPrivate::get(subMenu)->currentItem; - subMenu->close(); - subMenu = currentSubMenuItem ? currentSubMenuItem->subMenu() : nullptr; - } } void QQuickMenuPrivate::startHoverTimer() @@ -1120,7 +1113,7 @@ void QQuickMenu::setCurrentIndex(int index) The menu can be optionally aligned to a specific menu \a item. - \sa Popup::open() + \sa dismiss(), Popup::open() */ void QQuickMenu::popup(QQmlV4Function *args) { @@ -1186,6 +1179,29 @@ void QQuickMenu::popup(QQmlV4Function *args) open(); } +/*! + \since QtQuick.Controls 2.3 (Qt 5.10) + \qmlmethod void QtQuick.Controls::Menu::dismiss() + + Closes all menus in the hierarchy that this menu belongs to. + + \note Unlike \l {Popup::}{close()} that only closes a menu and its sub-menus, + \c dismiss() closes the whole hierarchy of menus, including the parent menus. + In practice, \c close() is suitable e.g. for implementing navigation in a + hierarchy of menus, and \c dismiss() is the appropriate method for closing + the whole hierarchy of menus. + + \sa popup(), Popup::close() +*/ +void QQuickMenu::dismiss() +{ + QQuickMenu *menu = this; + while (menu) { + menu->close(); + menu = QQuickMenuPrivate::get(menu)->parentMenu; + } +} + void QQuickMenu::componentComplete() { Q_D(QQuickMenu); @@ -1245,8 +1261,11 @@ void QQuickMenu::keyPressEvent(QKeyEvent *event) case Qt::Key_Left: case Qt::Key_Right: if (d->popupItem->isMirrored() == (event->key() == Qt::Key_Right)) { - if (d->parentMenu && d->currentItem) - d->closeSubMenu(this); + if (d->parentMenu && d->currentItem) { + if (!d->cascade) + d->parentMenu->open(); + close(); + } } else { d->openSubMenu(d->currentItem, true); } |