From af96b35bf4487279357a76bd02a926c1b7f528bb Mon Sep 17 00:00:00 2001 From: J-P Nurmi Date: Thu, 8 Jun 2017 20:19:50 +0200 Subject: QQuickMenu: fix key navigation Skip non-focusable separators, and use a key focus reason (Qt::TabFocusReason & Qt::BacktabFocusReason) to give the items visual focus. [ChangeLog][Controls][Menu] Fixed key navigation to skip separators. Change-Id: I99affabc50703c7363ab8146e5ced9b45111de00 Reviewed-by: Mitch Curtis --- src/quicktemplates2/qquickmenu.cpp | 36 +++++++++++++++++++++++++++--------- src/quicktemplates2/qquickmenu_p_p.h | 3 +++ 2 files changed, 30 insertions(+), 9 deletions(-) (limited to 'src') diff --git a/src/quicktemplates2/qquickmenu.cpp b/src/quicktemplates2/qquickmenu.cpp index 9e8fabe0..c40c49e1 100644 --- a/src/quicktemplates2/qquickmenu.cpp +++ b/src/quicktemplates2/qquickmenu.cpp @@ -254,6 +254,31 @@ void QQuickMenuPrivate::setCurrentIndex(int index) contentItem->setProperty("currentIndex", index); } +void QQuickMenuPrivate::activateNextItem() +{ + int index = currentIndex(); + int count = contentModel->count(); + while (++index < count) { + QQuickItem *item = itemAt(index); + if (!item || !item->activeFocusOnTab()) + continue; + item->forceActiveFocus(Qt::TabFocusReason); + break; + } +} + +void QQuickMenuPrivate::activatePreviousItem() +{ + int index = currentIndex(); + while (--index >= 0) { + QQuickItem *item = itemAt(index); + if (!item || !item->activeFocusOnTab()) + continue; + item->forceActiveFocus(Qt::BacktabFocusReason); + break; + } +} + void QQuickMenuPrivate::contentData_append(QQmlListProperty *prop, QObject *obj) { QQuickMenuPrivate *p = static_cast(prop->data); @@ -511,23 +536,16 @@ void QQuickMenu::keyReleaseEvent(QKeyEvent *event) // shown at once. switch (event->key()) { case Qt::Key_Up: - if (d->contentItem->metaObject()->indexOfMethod("decrementCurrentIndex()") != -1) - QMetaObject::invokeMethod(d->contentItem, "decrementCurrentIndex"); + d->activatePreviousItem(); break; case Qt::Key_Down: - if (d->contentItem->metaObject()->indexOfMethod("incrementCurrentIndex()") != -1) - QMetaObject::invokeMethod(d->contentItem, "incrementCurrentIndex"); + d->activateNextItem(); break; default: break; } - - int index = d->currentIndex(); - QQuickItem *item = itemAt(index); - if (item) - item->forceActiveFocus(); } QFont QQuickMenu::defaultFont() const diff --git a/src/quicktemplates2/qquickmenu_p_p.h b/src/quicktemplates2/qquickmenu_p_p.h index 504bc74d..583bb41d 100644 --- a/src/quicktemplates2/qquickmenu_p_p.h +++ b/src/quicktemplates2/qquickmenu_p_p.h @@ -84,6 +84,9 @@ public: int currentIndex() const; void setCurrentIndex(int index); + void activateNextItem(); + void activatePreviousItem(); + static void contentData_append(QQmlListProperty *prop, QObject *obj); static int contentData_count(QQmlListProperty *prop); static QObject *contentData_at(QQmlListProperty *prop, int index); -- cgit v1.2.3