aboutsummaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorAndreas Buhr <andreas@andreasbuhr.de>2021-01-13 15:29:32 +0100
committerQt Cherry-pick Bot <cherrypick_bot@qt-project.org>2021-01-15 16:21:25 +0000
commit5a2c0b364a3a5830dd6d5069b5f8d3921aeac73a (patch)
tree8a200c73e97217560e29e30aa8a0f21abc0c9b13 /src
parent0593616fd9ea17f2ae058e58c8edc8aaca302ab9 (diff)
Change QQuickShortcut::setSequences to bind to all sequences
When binding a Shortcut to a standard key sequence like QKeySequence::FullScreen, it binds only to one key sequence, even though there might be multiple key sequences associated. This patch changes the code to emit a warning in this case and allows to bind to multiple key sequences using 'sequences: [ <key> ]'. Fixes: QTBUG-88682 Change-Id: I88998aa8858d8f2c0c86e46bae94afd7ceb15b66 Reviewed-by: Fabian Kosmale <fabian.kosmale@qt.io> (cherry picked from commit 6511b17038627ac30cb6622b13c7d46d9877bac5) Reviewed-by: Qt Cherry-pick Bot <cherrypick_bot@qt-project.org>
Diffstat (limited to 'src')
-rw-r--r--src/quick/util/qquickshortcut.cpp83
1 files changed, 60 insertions, 23 deletions
diff --git a/src/quick/util/qquickshortcut.cpp b/src/quick/util/qquickshortcut.cpp
index 2905e36d00..fdd846adb6 100644
--- a/src/quick/util/qquickshortcut.cpp
+++ b/src/quick/util/qquickshortcut.cpp
@@ -44,6 +44,7 @@
#include <QtQuick/qquickrendercontrol.h>
#include <QtQuick/private/qtquickglobal_p.h>
#include <QtGui/private/qguiapplication_p.h>
+#include <QtQml/qqmlinfo.h>
/*!
\qmltype Shortcut
@@ -124,13 +125,35 @@ Q_QUICK_PRIVATE_EXPORT void qt_quick_set_shortcut_context_matcher(ContextMatcher
QT_BEGIN_NAMESPACE
-static QKeySequence valueToKeySequence(const QVariant &value)
+static QKeySequence valueToKeySequence(const QVariant &value, const QQuickShortcut *const shortcut)
{
- if (value.userType() == QMetaType::Int)
- return QKeySequence(static_cast<QKeySequence::StandardKey>(value.toInt()));
+ if (value.userType() == QMetaType::Int) {
+ const QVector<QKeySequence> s =
+ QKeySequence::keyBindings(static_cast<QKeySequence::StandardKey>(value.toInt()));
+ if (s.size() > 1) {
+ const QString templateString = QString::fromUtf16(
+ u"Shortcut: Only binding to one of multiple key bindings associated with %1. "
+ u"Use 'sequences: [ <key> ]' to bind to all of them.");
+ qmlWarning(shortcut)
+ << templateString.arg(static_cast<QKeySequence::StandardKey>(value.toInt()));
+ }
+ return s.size() > 0 ? s[0] : QKeySequence {};
+ }
+
return QKeySequence::fromString(value.toString());
}
+static QList<QKeySequence> valueToKeySequences(const QVariant &value)
+{
+ if (value.userType() == QMetaType::Int) {
+ return QKeySequence::keyBindings(static_cast<QKeySequence::StandardKey>(value.toInt()));
+ } else {
+ QList<QKeySequence> result;
+ result.push_back(QKeySequence::fromString(value.toString()));
+ return result;
+ }
+}
+
QQuickShortcut::QQuickShortcut(QObject *parent) : QObject(parent),
m_enabled(true), m_completed(false), m_autorepeat(true), m_context(Qt::WindowShortcut)
{
@@ -172,7 +195,7 @@ void QQuickShortcut::setSequence(const QVariant &value)
if (value == m_shortcut.userValue)
return;
- QKeySequence keySequence = valueToKeySequence(value);
+ QKeySequence keySequence = valueToKeySequence(value, this);
ungrabShortcut(m_shortcut);
m_shortcut.userValue = value;
@@ -207,28 +230,42 @@ QVariantList QQuickShortcut::sequences() const
void QQuickShortcut::setSequences(const QVariantList &values)
{
- QVector<Shortcut> remainder = m_shortcuts.mid(values.count());
- m_shortcuts.resize(values.count());
-
- bool changed = !remainder.isEmpty();
- for (int i = 0; i < values.count(); ++i) {
- const QVariant &value = values.at(i);
- Shortcut& shortcut = m_shortcuts[i];
- if (value == shortcut.userValue)
- continue;
-
- QKeySequence keySequence = valueToKeySequence(value);
-
- ungrabShortcut(shortcut);
- shortcut.userValue = value;
- shortcut.keySequence = keySequence;
- grabShortcut(shortcut, m_context);
+ // convert QVariantList to QVector<QKeySequence>
+ QVector<Shortcut> requestedShortcuts;
+ for (const QVariant &v : values) {
+ const QVector<QKeySequence> list = valueToKeySequences(v);
+ for (const QKeySequence &s : list) {
+ Shortcut sc;
+ sc.userValue = v;
+ sc.keySequence = s;
+ requestedShortcuts.push_back(sc);
+ }
+ }
- changed = true;
+ // if nothing has changed, just return:
+ if (m_shortcuts.size() == requestedShortcuts.size()) {
+ bool changed = false;
+ for (int i = 0; i < requestedShortcuts.count(); ++i) {
+ const Shortcut &requestedShortcut = requestedShortcuts[i];
+ const Shortcut &shortcut = m_shortcuts[i];
+ if (!(requestedShortcut.userValue == shortcut.userValue
+ && requestedShortcut.keySequence == shortcut.keySequence)) {
+ changed = true;
+ break;
+ }
+ }
+ if (!changed) {
+ return;
+ }
}
- if (changed)
- emit sequencesChanged();
+ for (Shortcut &s : m_shortcuts)
+ ungrabShortcut(s);
+ m_shortcuts = requestedShortcuts;
+ for (Shortcut &s : m_shortcuts)
+ grabShortcut(s, m_context);
+
+ emit sequencesChanged();
}
/*!