diff options
| author | David Faure <david.faure@kdab.com> | 2024-10-16 12:29:17 +0200 |
|---|---|---|
| committer | Axel Spoerl <axel.spoerl@qt.io> | 2025-03-12 03:39:01 +0000 |
| commit | ada885dfd55d5c386d04039babee43e49ccab5c5 (patch) | |
| tree | 63e09bf5b867493e198c35cf7b0783daae6d6c54 /src/gui/kernel/qkeysequence.cpp | |
| parent | 67e9e2a7e2ee6a7687a5aa01214e70dffb6eb688 (diff) | |
QKeySequence: tolerate spaces when parsing a key sequence string
Fixes: QTBUG-130063
Pick-to: 6.9 6.8
Change-Id: I0f0aff0ca0824cd1c9297cd18a1f5cf9a2a44951
Reviewed-by: Axel Spoerl <axel.spoerl@qt.io>
Reviewed-by: Shawn Rutledge <shawn.rutledge@qt.io>
Diffstat (limited to 'src/gui/kernel/qkeysequence.cpp')
| -rw-r--r-- | src/gui/kernel/qkeysequence.cpp | 48 |
1 files changed, 35 insertions, 13 deletions
diff --git a/src/gui/kernel/qkeysequence.cpp b/src/gui/kernel/qkeysequence.cpp index e514c8a1a07..b9d2855fecf 100644 --- a/src/gui/kernel/qkeysequence.cpp +++ b/src/gui/kernel/qkeysequence.cpp @@ -1102,10 +1102,16 @@ QKeyCombination QKeySequencePrivate::decodeString(QString accel, QKeySequence::S return Qt::Key_unknown; #endif + int singlePlus = -1; qsizetype i = 0; qsizetype lastI = 0; while ((i = sl.indexOf(u'+', i + 1)) != -1) { - const QStringView sub = QStringView{sl}.mid(lastI, i - lastI + 1); + QStringView sub = QStringView{ sl }.mid(lastI, i - lastI + 1); + while (sub.size() > 1 && sub.at(0) == QLatin1Char(' ')) { + sub = sub.mid(1); + ++lastI; + } + // If we get here the shortcuts contains at least one '+'. We break up // along the following strategy: // Meta+Ctrl++ ( "Meta+", "Ctrl+", "+" ) @@ -1117,33 +1123,49 @@ QKeyCombination QKeySequencePrivate::decodeString(QString accel, QKeySequence::S // Only '+' can have length 1. if (sub.size() == 1) { // Make sure we only encounter a single '+' at the end of the accel - if (accel.lastIndexOf(u'+') != accel.size()-1) + if (singlePlus >= 0) return Qt::Key_unknown; + singlePlus = lastI; } else { - // Identify the modifier - bool validModifier = false; - for (int j = 0; j < modifs.size(); ++j) { - const QModifKeyName &mkf = modifs.at(j); - if (sub == mkf.name) { - ret |= mkf.qt_key; - validModifier = true; - break; // Shortcut, since if we find an other it would/should just be a dup + + const auto identifyModifier = [&](QStringView sub) { + for (int j = 0; j < modifs.size(); ++j) { + const QModifKeyName &mkf = modifs.at(j); + if (sub == mkf.name) { + ret |= mkf.qt_key; + return true; // Shortcut, since if we find another it would/should just be a dup + } } - } + return false; + }; + bool validModifier = identifyModifier(sub); + + if (!validModifier) { + // Try harder with slower code that trims spaces + const QString cleanedSub = sub.toString().remove(QLatin1Char(' ')); + validModifier = identifyModifier(cleanedSub); + } if (!validModifier) return Qt::Key_unknown; } lastI = i + 1; } - qsizetype p = accel.lastIndexOf(u'+', accel.size() - 2); // -2 so that Ctrl++ works + qsizetype p = accel.lastIndexOf(u'+', singlePlus > 0 ? singlePlus - 1 : accel.size() - 1); QStringView accelRef(accel); if (p > 0) accelRef = accelRef.mid(p + 1); + while (accelRef.size() > 1 && accelRef.at(0) == QLatin1Char(' ')) + accelRef = accelRef.mid(1); + while (accelRef.size() > 1 && accelRef.endsWith(QLatin1Char(' '))) + accelRef.chop(1); + int fnum = 0; - if (accelRef.size() == 1) { + if (accelRef.isEmpty()) + return Qt::Key_unknown; + else if (accelRef.size() == 1) { #if defined(Q_OS_APPLE) int qtKey = qtkeyForAppleSymbol(accelRef.at(0)); if (qtKey != -1) { |
