From 4eb3400843cafcedffa47fb8a272704bca4e9473 Mon Sep 17 00:00:00 2001 From: Gabriel de Dietrich Date: Thu, 5 Sep 2013 17:29:10 +0200 Subject: Menu: Enable mnemonic menu navigation We also added a new mnemonic specific shortcut context matcher. This prevents two menu items with the same mnemonic but within different menus to be reported as ambiguous. Task-number: QTBUG-33030 ChangeLog: Added mnemonic navigation for menus Change-Id: I192c9aacba4d15851fe65bf9201251962fe976d5 Reviewed-by: J-P Nurmi --- src/controls/qquickaction.cpp | 37 ++++++++++++++++++++++++++++++++++++- 1 file changed, 36 insertions(+), 1 deletion(-) (limited to 'src/controls/qquickaction.cpp') diff --git a/src/controls/qquickaction.cpp b/src/controls/qquickaction.cpp index e240d111..6a031404 100644 --- a/src/controls/qquickaction.cpp +++ b/src/controls/qquickaction.cpp @@ -41,6 +41,7 @@ #include "qquickaction_p.h" #include "qquickexclusivegroup_p.h" +#include "qquickmenuitem_p.h" #include #include @@ -203,6 +204,8 @@ void QQuickAction::setText(const QString &text) emit textChanged(); } +namespace { + bool qShortcutContextMatcher(QObject *o, Qt::ShortcutContext context) { switch (context) { @@ -226,6 +229,38 @@ bool qShortcutContextMatcher(QObject *o, Qt::ShortcutContext context) return false; } +bool qMnemonicContextMatcher(QObject *o, Qt::ShortcutContext context) +{ + switch (context) { + case Qt::ApplicationShortcut: + return true; + case Qt::WindowShortcut: { + QObject *w = o; + while (w && !w->isWindowType()) { + w = w->parent(); + if (QQuickItem * item = qobject_cast(w)) + w = item->window(); + else if (QQuickMenuBase *mb = qobject_cast(w)) { + QQuickItem *vi = mb->visualItem(); + if (vi && vi->isVisible()) + w = vi->window(); + else + break; // Non visible menu objects don't get mnemonic match + } + } + if (w && w == QGuiApplication::focusWindow()) + return true; + } + case Qt::WidgetShortcut: + case Qt::WidgetWithChildrenShortcut: + break; + } + + return false; +} + +} // namespace + QString QQuickAction::shortcut() const { return m_shortcut.toString(QKeySequence::NativeText); @@ -262,7 +297,7 @@ void QQuickAction::setMnemonicFromText(const QString &text) if (!m_mnemonic.isEmpty()) { Qt::ShortcutContext context = Qt::WindowShortcut; - QGuiApplicationPrivate::instance()->shortcutMap.addShortcut(this, m_mnemonic, context, qShortcutContextMatcher); + QGuiApplicationPrivate::instance()->shortcutMap.addShortcut(this, m_mnemonic, context, qMnemonicContextMatcher); } } -- cgit v1.2.1