From 25351dcc549f1daddf5e2ae8a242191174342a3e Mon Sep 17 00:00:00 2001 From: Giuseppe D'Angelo Date: Sun, 19 Apr 2020 19:56:18 +0200 Subject: Long live QKeyCombination! C++20 via P1120 is deprecating arithmetic operations between unrelated enumeration types, and GCC 10 is already complaining. Hence, these operations might become illegal in C++23 or C++26 at the latest. A case of this that affects Qt is in key combinations: a QKeySequence can be constructed by summing / ORing modifiers and a key, for instance: Qt::CTRL + Qt::Key_A Qt::SHIFT | Qt::CTRL | Qt::Key_G (recommended, see below) The problem is that the modifiers and the key belong to different enumerations (and there's 2 enumerations for the modifier, and one for the key). To solve this: add a dedicated class to represent a combination of keys, and operators between those enumerations to build instances of this class. I would've simply defined operator|, but again docs and pre-existing code use operator+ as well, so added both to at least tackle simple cases (modifier + key). Multiple modifiers create a problem: operator+ between them yields int, not the corresponding flags type (because operator+ is not overloaded for this use case): Qt::CTRL + Qt::SHIFT + Qt::Key_A \__________________/ / int / \______________/ int Not only this loses track of the datatypes involved, but it would also then "add" the key (with NO warnings, now its int + enum, so it's not mixing enums!) and yielding int again. I don't want to special-case this; the point of the class is that int is the wrong datatype. Everything works just fine when using operator| instead: Qt::CTRL | Qt::SHIFT | Qt::Key_A \__________________/ / Qt::Modifiers / \______________/ QKeyCombination So I'm defining operator+ so that the simple cases still work, but also deprecating it. Port some code around Qt to the new class. In certain cases, it's a huge win for clarity. In some others, I've just added the necessary casts to make it still compile without warnings, without attempting refactorings. [ChangeLog][QtCore][QKeyCombination] New class to represent a combination of a key and zero or more modifiers, to be used when defining shortcuts or similar. [ChangeLog][Potentially Source-Incompatible Changes] A keyboard modifier (such as Qt::CTRL, Qt::AltModifier, etc.) should be combined with a key (such as Qt::Key_A, Qt::Key_F1, etc.) by using operator|, not operator+. The result is now an object of type QKeyCombination, that stores the key and the modifiers. Change-Id: I657a3a328232f059023fff69c5031ee31cc91dd6 Reviewed-by: Volker Hilsheimer --- src/corelib/global/qnamespace.h | 156 ++++++++++++++++ src/corelib/global/qnamespace.qdoc | 199 +++++++++++++++++++++ src/corelib/io/qdebug.h | 11 ++ src/corelib/serialization/qdatastream.h | 14 ++ src/corelib/tools/qhashfunctions.h | 2 + .../snippets/code/src_gui_kernel_qkeysequence.cpp | 6 +- .../doc/snippets/code/src_gui_kernel_qshortcut.cpp | 2 +- src/gui/kernel/qevent.cpp | 9 + src/gui/kernel/qevent.h | 4 + src/gui/kernel/qguivariant.cpp | 3 +- src/gui/kernel/qkeymapper.cpp | 25 ++- src/gui/kernel/qkeysequence.cpp | 25 ++- src/gui/kernel/qkeysequence.h | 8 +- src/gui/kernel/qkeysequence_p.h | 2 +- src/gui/kernel/qplatformtheme.cpp | 2 +- src/gui/kernel/qshortcutmap.cpp | 14 +- src/gui/platform/unix/dbusmenu/qdbusmenutypes.cpp | 2 +- src/gui/platform/unix/qxkbcommon.cpp | 4 +- src/testlib/qtestkeyboard.h | 8 +- src/widgets/dialogs/qfiledialog.cpp | 2 +- src/widgets/dialogs/qmessagebox.cpp | 2 +- src/widgets/kernel/qwhatsthis.cpp | 2 +- src/widgets/widgets/qkeysequenceedit.cpp | 6 +- src/widgets/widgets/qkeysequenceedit_p.h | 2 +- src/widgets/widgets/qmenu.cpp | 2 +- 25 files changed, 461 insertions(+), 51 deletions(-) (limited to 'src') diff --git a/src/corelib/global/qnamespace.h b/src/corelib/global/qnamespace.h index a531bc627b..0c4b4fc8bf 100644 --- a/src/corelib/global/qnamespace.h +++ b/src/corelib/global/qnamespace.h @@ -1,6 +1,7 @@ /**************************************************************************** ** ** Copyright (C) 2020 The Qt Company Ltd. +** Copyright (C) 2020 Klaralvdalens Datakonsult AB, a KDAB Group company, info@kdab.com, author Giuseppe D'Angelo ** Contact: https://www.qt.io/licensing/ ** ** This file is part of the QtCore module of the Qt Toolkit. @@ -1094,6 +1095,8 @@ namespace Qt { ALT = Qt::AltModifier, MODIFIER_MASK = KeyboardModifierMask, }; + Q_DECLARE_FLAGS(Modifiers, Modifier) + Q_DECLARE_OPERATORS_FOR_FLAGS(Modifiers) enum ArrowType { NoArrow, @@ -1785,6 +1788,9 @@ namespace Qt { Q_ENUM_NS(SortOrder) Q_ENUM_NS(CaseSensitivity) Q_FLAG_NS(MatchFlags) + Q_ENUM_NS(Modifier) + Q_FLAG_NS(Modifiers) + Q_ENUM_NS(KeyboardModifier) Q_FLAG_NS(KeyboardModifiers) Q_FLAG_NS(MouseButtons) Q_ENUM_NS(WindowType) @@ -1863,6 +1869,156 @@ public: static bool activateCallbacks(Callback, void **); }; +class QKeyCombination +{ + int combination; + +public: + constexpr /* implicit */ QKeyCombination(Qt::Key key = Qt::Key_unknown) noexcept + : combination(int(key)) + {} + + constexpr explicit QKeyCombination(Qt::Modifiers modifiers, Qt::Key key = Qt::Key_unknown) noexcept + : combination(int(modifiers) | int(key)) + {} + + constexpr explicit QKeyCombination(Qt::KeyboardModifiers modifiers, Qt::Key key = Qt::Key_unknown) noexcept + : combination(int(modifiers) | int(key)) + {} + + constexpr Qt::KeyboardModifiers keyboardModifiers() const noexcept + { + return Qt::KeyboardModifiers(combination & Qt::KeyboardModifierMask); + } + + constexpr Qt::Key key() const noexcept + { + return Qt::Key(combination & ~Qt::KeyboardModifierMask); + } + + static constexpr QKeyCombination fromCombined(int combined) + { + QKeyCombination result; + result.combination = combined; + return result; + } + + constexpr int toCombined() const noexcept + { + return combination; + } + +#if QT_DEPRECATED_SINCE(6, 0) + QT_DEPRECATED_VERSION_X(6, 0, "Use QKeyCombination instead of int") + constexpr /* implicit */ operator int() const noexcept + { + return combination; + } +#endif + + friend constexpr bool operator==(QKeyCombination lhs, QKeyCombination rhs) noexcept + { + return lhs.combination == rhs.combination; + } + + friend constexpr bool operator!=(QKeyCombination lhs, QKeyCombination rhs) noexcept + { + return lhs.combination != rhs.combination; + } +}; + +Q_DECLARE_TYPEINFO(QKeyCombination, Q_MOVABLE_TYPE); + +constexpr QKeyCombination operator|(Qt::Modifier modifier, Qt::Key key) noexcept +{ + return QKeyCombination(modifier, key); +} + +constexpr QKeyCombination operator|(Qt::Modifiers modifiers, Qt::Key key) noexcept +{ + return QKeyCombination(modifiers, key); +} + +constexpr QKeyCombination operator|(Qt::KeyboardModifier modifier, Qt::Key key) noexcept +{ + return QKeyCombination(modifier, key); +} + +constexpr QKeyCombination operator|(Qt::KeyboardModifiers modifiers, Qt::Key key) noexcept +{ + return QKeyCombination(modifiers, key); +} + +constexpr QKeyCombination operator|(Qt::Key key, Qt::Modifier modifier) noexcept +{ + return QKeyCombination(modifier, key); +} + +constexpr QKeyCombination operator|(Qt::Key key, Qt::Modifiers modifiers) noexcept +{ + return QKeyCombination(modifiers, key); +} + +constexpr QKeyCombination operator|(Qt::Key key, Qt::KeyboardModifier modifier) noexcept +{ + return QKeyCombination(modifier, key); +} + +constexpr QKeyCombination operator|(Qt::Key key, Qt::KeyboardModifiers modifiers) noexcept +{ + return QKeyCombination(modifiers, key); +} + +#if QT_DEPRECATED_SINCE(6, 0) +QT_DEPRECATED_VERSION_X(6, 0, "Use operator| instead") +constexpr QKeyCombination operator+(Qt::Modifier modifier, Qt::Key key) noexcept +{ + return QKeyCombination(modifier, key); +} + +QT_DEPRECATED_VERSION_X(6, 0, "Use operator| instead") +constexpr QKeyCombination operator+(Qt::Modifiers modifiers, Qt::Key key) noexcept +{ + return QKeyCombination(modifiers, key); +} + +QT_DEPRECATED_VERSION_X(6, 0, "Use operator| instead") +constexpr QKeyCombination operator+(Qt::KeyboardModifier modifier, Qt::Key key) noexcept +{ + return QKeyCombination(modifier, key); +} + +QT_DEPRECATED_VERSION_X(6, 0, "Use operator| instead") +constexpr QKeyCombination operator+(Qt::KeyboardModifiers modifiers, Qt::Key key) noexcept +{ + return QKeyCombination(modifiers, key); +} + +QT_DEPRECATED_VERSION_X(6, 0, "Use operator| instead") +constexpr QKeyCombination operator+(Qt::Key key, Qt::Modifier modifier) noexcept +{ + return QKeyCombination(modifier, key); +} + +QT_DEPRECATED_VERSION_X(6, 0, "Use operator| instead") +constexpr QKeyCombination operator+(Qt::Key key, Qt::Modifiers modifiers) noexcept +{ + return QKeyCombination(modifiers, key); +} + +QT_DEPRECATED_VERSION_X(6, 0, "Use operator| instead") +constexpr QKeyCombination operator+(Qt::Key key, Qt::KeyboardModifier modifier) noexcept +{ + return QKeyCombination(modifier, key); +} + +QT_DEPRECATED_VERSION_X(6, 0, "Use operator| instead") +constexpr QKeyCombination operator+(Qt::Key key, Qt::KeyboardModifiers modifiers) noexcept +{ + return QKeyCombination(modifiers, key); +} +#endif + QT_END_NAMESPACE #endif // QNAMESPACE_H diff --git a/src/corelib/global/qnamespace.qdoc b/src/corelib/global/qnamespace.qdoc index 95a73c4439..b5d44cb1cb 100644 --- a/src/corelib/global/qnamespace.qdoc +++ b/src/corelib/global/qnamespace.qdoc @@ -1,6 +1,7 @@ /**************************************************************************** ** ** Copyright (C) 2020 The Qt Company Ltd. +** Copyright (C) 2020 Klaralvdalens Datakonsult AB, a KDAB Group company, info@kdab.com, author Giuseppe D'Angelo ** Contact: https://www.qt.io/licensing/ ** ** This file is part of the documentation of the Qt Toolkit. @@ -3246,3 +3247,201 @@ \sa QLabel::picture() \sa QLabel::pixmap() */ + +/*! + \class QKeyCombination + \inmodule QtCore + \since 6.0 + \brief The QKeyCombination class stores a combination of a key with optional modifiers. + + The QKeyCombination class can be used to represent a combination of a key + with zero or more keyboard modifiers. + + \sa QKeySequence +*/ + +/*! + \fn QKeyCombination::QKeyCombination(Qt::Key key = Qt::Key_unknown) noexcept + + Constructs a QKeyCombination object that represents the key \a key + and no modifiers. + + \sa key() +*/ + +/*! + \fn QKeyCombination::QKeyCombination(Qt::Modifiers modifiers, Qt::Key key = Qt::Key_unknown) noexcept + + Constructs a QKeyCombination object that represents the combination + of \a key with the modifiers \a modifiers. + + \sa key(), keyboardModifiers() +*/ + +/*! + \fn QKeyCombination::QKeyCombination(Qt::KeyboardModifiers modifiers, Qt::Key key = Qt::Key_unknown) noexcept + + Constructs a QKeyCombination object that represents the combination + of \a key with the modifiers \a modifiers. + + \sa key(), keyboardModifiers() +*/ + +/*! + \fn Qt::KeyboardModifiers QKeyCombination::keyboardModifiers() const noexcept + + Returns the keyboard modifiers represented by this QKeyCombination object. + + \sa key() +*/ + +/*! + \fn Qt::Key QKeyCombination::key() const noexcept + + Returns the key represented by this QKeyCombination object. + + \sa keyboardModifiers() +*/ + +/*! + \fn QKeyCombination QKeyCombination::fromCombined(int combined) + + Constructs a QKeyCombination object by extracting the key and the + modifiers out of \a combined, which must be the result of a bitwise + OR between a value of type Qt::Key and value of type + Qt::KeyboardModifiers. toCombined() can be used in order to produce + valid values for \a combined. + + \sa toCombined() +*/ + +/*! + \fn int QKeyCombination::toCombined() const noexcept + + Returns an integer value obtained by applying a bitwise OR between + the values of key() and keyboardModifiers() represented + by this object. A QKeyCombination object can be created from the + returned integer value by using fromCombined(). + + \sa fromCombined(), key(), keyboardModifiers() +*/ + +#if QT_DEPRECATED_SINCE(6, 0) +/*! + \fn QKeyCombination::operator int() const noexcept + \obsolete + + Use toCombined() instead. +*/ +#endif + +/*! + \fn bool operator==(QKeyCombination lhs, QKeyCombination rhs) noexcept + \relates QKeyCombination + + Returns \c true if \a lhs and \a rhs have the same combination + of key and modifiers, and \c false otherwise. +*/ + +/*! + \fn bool operator!=(QKeyCombination lhs, QKeyCombination rhs) noexcept + \relates QKeyCombination + + Returns \c true if \a lhs and \a rhs have different combinations + of key and modifiers, otherwise \c false. +*/ + +/*! + \fn QKeyCombination operator|(Qt::Modifier modifier, Qt::Key key) noexcept + \fn QKeyCombination operator|(Qt::KeyboardModifier modifier, Qt::Key key) noexcept + \fn QKeyCombination operator|(Qt::Key key, Qt::Modifier modifier) noexcept + \fn QKeyCombination operator|(Qt::Key key, Qt::KeyboardModifier modifier) noexcept + + \relates QKeyCombination + + Returns a QKeyCombination object that represents the combination + of \a key with the modifier \a modifier. +*/ + +/*! + \fn QKeyCombination operator|(Qt::Modifiers modifiers, Qt::Key key) noexcept + \fn QKeyCombination operator|(Qt::KeyboardModifiers modifiers, Qt::Key key) noexcept + \fn QKeyCombination operator|(Qt::Key key, Qt::Modifiers modifiers) noexcept + \fn QKeyCombination operator|(Qt::Key key, Qt::KeyboardModifiers modifiers) noexcept + + \relates QKeyCombination + + Returns a QKeyCombination object that represents the combination + of \a key with the modifiers \a modifiers. +*/ + +/*! + \fn QKeyCombination operator+(Qt::Modifier modifier, Qt::Key key) noexcept + \fn QKeyCombination operator+(Qt::KeyboardModifier modifier, Qt::Key key) noexcept + \fn QKeyCombination operator+(Qt::Key key, Qt::Modifier modifier) noexcept + \fn QKeyCombination operator+(Qt::Key key, Qt::KeyboardModifier modifier) noexcept + + \relates QKeyCombination + \obsolete + + Use operator| instead. + + Returns a QKeyCombination object that represents the combination + of \a key with the modifier \a modifier. +*/ + +/*! + \fn QKeyCombination operator+(Qt::Modifiers modifiers, Qt::Key key) noexcept + \fn QKeyCombination operator+(Qt::KeyboardModifiers modifiers, Qt::Key key) noexcept + \fn QKeyCombination operator+(Qt::Key key, Qt::Modifiers modifiers) noexcept + \fn QKeyCombination operator+(Qt::Key key, Qt::KeyboardModifiers modifiers) noexcept + + \relates QKeyCombination + \obsolete + + Use operator| instead. + + Returns a QKeyCombination object that represents the combination + of \a key with the modifiers \a modifiers. +*/ + +/*! + \fn size_t qHash(QKeyCombination key, size_t seed = 0) noexcept + \relates QKeyCombination + + Returns the hash value for the \a key, using \a seed to seed the calculation. +*/ + +#ifndef QT_NO_DEBUG_STREAM +/*! + \fn QDebug operator<<(QDebug debug, QKeyCombination combination) + \relates QKeyCombination + + Writes the combination \a combination into the debug object \a debug for + debugging purposes. + + \sa {Debugging Techniques} +*/ +#endif + +#ifndef QT_NO_DATASTREAM +/*! + \fn QDataStream &operator<<(QDataStream &out, QKeyCombination combination) + \relates QKeyCombination + + Writes the combination \a combination into the stream \a out. + Returns \a out. + + \sa {Serializing Qt Data Types} +*/ + +/*! + \fn QDataStream &operator>>(QDataStream &in, QKeyCombination &combination) + \relates QKeyCombination + + Reads the combination \a combination from the stream \a in. + Returns \a in. + + \sa {Serializing Qt Data Types} +*/ +#endif diff --git a/src/corelib/io/qdebug.h b/src/corelib/io/qdebug.h index 2463095743..b3787f9e78 100644 --- a/src/corelib/io/qdebug.h +++ b/src/corelib/io/qdebug.h @@ -446,6 +446,17 @@ inline QDebug operator<<(QDebug debug, const QFlags &flags) return qt_QMetaEnum_flagDebugOperator_helper(debug, flags); } +inline QDebug operator<<(QDebug debug, QKeyCombination combination) +{ + QDebugStateSaver saver(debug); + debug.nospace() << "QKeyCombination(" + << combination.keyboardModifiers() + << ", " + << combination.key() + << ")"; + return debug; +} + #ifdef Q_OS_MAC // We provide QDebug stream operators for commonly used Core Foundation diff --git a/src/corelib/serialization/qdatastream.h b/src/corelib/serialization/qdatastream.h index 19188c3d91..32ccf7d6cc 100644 --- a/src/corelib/serialization/qdatastream.h +++ b/src/corelib/serialization/qdatastream.h @@ -43,6 +43,7 @@ #include #include #include +#include #ifdef Status #error qdatastream.h must be included before any header file that defines Status @@ -510,6 +511,19 @@ inline QDataStreamIfHasOStreamOperators operator<<(QDataStream& s, const } #endif +inline QDataStream &operator>>(QDataStream &s, QKeyCombination &combination) +{ + int combined; + s >> combined; + combination = QKeyCombination::fromCombined(combined); + return s; +} + +inline QDataStream &operator<<(QDataStream &s, QKeyCombination combination) +{ + return s << combination.toCombined(); +} + #endif // QT_NO_DATASTREAM QT_END_NAMESPACE diff --git a/src/corelib/tools/qhashfunctions.h b/src/corelib/tools/qhashfunctions.h index 716b81e186..94c6e13b1c 100644 --- a/src/corelib/tools/qhashfunctions.h +++ b/src/corelib/tools/qhashfunctions.h @@ -162,6 +162,8 @@ inline Q_DECL_PURE_FUNCTION size_t qHash(const QString &key, size_t seed = 0) no #endif Q_CORE_EXPORT Q_DECL_PURE_FUNCTION size_t qHash(const QBitArray &key, size_t seed = 0) noexcept; Q_CORE_EXPORT Q_DECL_PURE_FUNCTION size_t qHash(QLatin1String key, size_t seed = 0) noexcept; +Q_DECL_CONST_FUNCTION constexpr inline size_t qHash(QKeyCombination key, size_t seed = 0) noexcept +{ return qHash(key.toCombined(), seed); } Q_CORE_EXPORT Q_DECL_PURE_FUNCTION uint qt_hash(QStringView key, uint chained = 0) noexcept; template inline size_t qHash(const T &t, size_t seed) diff --git a/src/gui/doc/snippets/code/src_gui_kernel_qkeysequence.cpp b/src/gui/doc/snippets/code/src_gui_kernel_qkeysequence.cpp index 2994bb60cf..4be214ad64 100644 --- a/src/gui/doc/snippets/code/src_gui_kernel_qkeysequence.cpp +++ b/src/gui/doc/snippets/code/src_gui_kernel_qkeysequence.cpp @@ -62,13 +62,15 @@ struct Wrapper : public QWidget QKeySequence(QKeySequence::Print); QKeySequence(tr("Ctrl+P")); QKeySequence(tr("Ctrl+p")); -QKeySequence(Qt::CTRL + Qt::Key_P); +QKeySequence(Qt::CTRL | Qt::Key_P); +QKeySequence(Qt::CTRL + Qt::Key_P); // deprecated //! [0] //! [1] QKeySequence(tr("Ctrl+X, Ctrl+C")); -QKeySequence(Qt::CTRL + Qt::Key_X, Qt::CTRL + Qt::Key_C); +QKeySequence(Qt::CTRL | Qt::Key_X, Qt::CTRL | Qt::Key_C); +QKeySequence(Qt::CTRL + Qt::Key_X, Qt::CTRL + Qt::Key_C); // deprecated //! [1] */ // Wrap non-compilable code snippet diff --git a/src/gui/doc/snippets/code/src_gui_kernel_qshortcut.cpp b/src/gui/doc/snippets/code/src_gui_kernel_qshortcut.cpp index bac264fcfc..370afd0679 100644 --- a/src/gui/doc/snippets/code/src_gui_kernel_qshortcut.cpp +++ b/src/gui/doc/snippets/code/src_gui_kernel_qshortcut.cpp @@ -60,6 +60,6 @@ setKey(QKeySequence()); // no signal emitted setKey(0x3b1); // Greek letter alpha setKey(Qt::Key_D); // 'd', e.g. to delete setKey('q'); // 'q', e.g. to quit -setKey(Qt::CTRL + Qt::Key_P); // Ctrl+P, e.g. to print document +setKey(Qt::CTRL | Qt::Key_P); // Ctrl+P, e.g. to print document setKey("Ctrl+P"); // Ctrl+P, e.g. to print document //! [1] diff --git a/src/gui/kernel/qevent.cpp b/src/gui/kernel/qevent.cpp index 1a6a43a4cb..b4880eff53 100644 --- a/src/gui/kernel/qevent.cpp +++ b/src/gui/kernel/qevent.cpp @@ -1285,6 +1285,15 @@ Qt::KeyboardModifiers QKeyEvent::modifiers() const return QInputEvent::modifiers(); } +/*! + \fn QKeyCombination QKeyEvent::keyCombination() const + + Returns a QKeyCombination object containing both the key() and + the modifiers() carried by this event. + + \since 6.0 +*/ + #if QT_CONFIG(shortcut) /*! \fn bool QKeyEvent::matches(QKeySequence::StandardKey key) const diff --git a/src/gui/kernel/qevent.h b/src/gui/kernel/qevent.h index 2c0325296d..c8402b6632 100644 --- a/src/gui/kernel/qevent.h +++ b/src/gui/kernel/qevent.h @@ -480,6 +480,10 @@ public: bool matches(QKeySequence::StandardKey key) const; #endif Qt::KeyboardModifiers modifiers() const; + QKeyCombination keyCombination() const + { + return QKeyCombination(modifiers(), Qt::Key(m_key)); + } inline QString text() const { return m_text; } inline bool isAutoRepeat() const { return m_autoRepeat; } inline int count() const { return int(m_count); } diff --git a/src/gui/kernel/qguivariant.cpp b/src/gui/kernel/qguivariant.cpp index ebb180dc8e..1af0692173 100644 --- a/src/gui/kernel/qguivariant.cpp +++ b/src/gui/kernel/qguivariant.cpp @@ -99,7 +99,6 @@ struct GuiTypesFilter { }; }; - static const struct : QMetaTypeModuleHelper { #define QT_IMPL_METATYPEINTERFACE_GUI_TYPES(MetaTypeName, MetaTypeId, RealName) \ @@ -146,7 +145,7 @@ static const struct : QMetaTypeModuleHelper ); QMETATYPE_CONVERTER(QKeySequence, QString, result = source; return true;); QMETATYPE_CONVERTER(Int, QKeySequence, - result = source.isEmpty() ? 0 : source[0]; + result = source.isEmpty() ? 0 : source[0].toCombined(); return true; ); QMETATYPE_CONVERTER(QKeySequence, Int, result = source; return true;); diff --git a/src/gui/kernel/qkeymapper.cpp b/src/gui/kernel/qkeymapper.cpp index f0ea66dc79..89eacc944a 100644 --- a/src/gui/kernel/qkeymapper.cpp +++ b/src/gui/kernel/qkeymapper.cpp @@ -71,17 +71,20 @@ QKeyMapper::~QKeyMapper() { } -QList QKeyMapper::possibleKeys(QKeyEvent *e) +static QList extractKeyFromEvent(QKeyEvent *e) { QList result; + if (e->key() && (e->key() != Qt::Key_unknown)) + result << e->keyCombination().toCombined(); + else if (!e->text().isEmpty()) + result << int(e->text().at(0).unicode() + (int)e->modifiers()); + return result; +} - if (!e->nativeScanCode()) { - if (e->key() && (e->key() != Qt::Key_unknown)) - result << int(e->key() + e->modifiers()); - else if (!e->text().isEmpty()) - result << int(e->text().at(0).unicode() + e->modifiers()); - return result; - } +QList QKeyMapper::possibleKeys(QKeyEvent *e) +{ + if (!e->nativeScanCode()) + return extractKeyFromEvent(e); return instance()->d_func()->possibleKeys(e); } @@ -132,11 +135,7 @@ QList QKeyMapperPrivate::possibleKeys(QKeyEvent *e) if (!result.isEmpty()) return result; - if (e->key() && (e->key() != Qt::Key_unknown)) - result << int(e->key() + e->modifiers()); - else if (!e->text().isEmpty()) - result << int(e->text().at(0).unicode() + e->modifiers()); - return result; + return extractKeyFromEvent(e); } QT_END_NAMESPACE diff --git a/src/gui/kernel/qkeysequence.cpp b/src/gui/kernel/qkeysequence.cpp index 13e6a058cb..5d52ba139d 100644 --- a/src/gui/kernel/qkeysequence.cpp +++ b/src/gui/kernel/qkeysequence.cpp @@ -869,6 +869,17 @@ QKeySequence::QKeySequence(int k1, int k2, int k3, int k4) d->key[3] = k4; } +/*! + Constructs a key sequence with up to 4 keys \a k1, \a k2, + \a k3 and \a k4. + + \sa QKeyCombination +*/ +QKeySequence::QKeySequence(QKeyCombination k1, QKeyCombination k2, QKeyCombination k3, QKeyCombination k4) + : QKeySequence(k1.toCombined(), k2.toCombined(), k3.toCombined(), k4.toCombined()) +{ +} + /*! Copy constructor. Makes a copy of \a keysequence. */ @@ -908,11 +919,11 @@ QKeySequence::~QKeySequence() delivery. */ -void QKeySequence::setKey(int key, int index) +void QKeySequence::setKey(QKeyCombination key, int index) { Q_ASSERT_X(index >= 0 && index < QKeySequencePrivate::MaxKeyCount, "QKeySequence::setKey", "index out of range"); qAtomicDetach(d); - d->key[index] = key; + d->key[index] = key.toCombined(); } static_assert(QKeySequencePrivate::MaxKeyCount == 4, "Change docs below"); @@ -967,7 +978,7 @@ QKeySequence QKeySequence::mnemonic(const QString &text) if (c.isPrint()) { if (!found) { c = c.toUpper(); - ret = QKeySequence(c.unicode() + Qt::ALT); + ret = QKeySequence(QKeyCombination(Qt::ALT, Qt::Key(c.unicode()))); #ifdef QT_NO_DEBUG return ret; #else @@ -1377,8 +1388,8 @@ QKeySequence::SequenceMatch QKeySequence::matches(const QKeySequence &seq) const SequenceMatch match = (userN == seqN ? ExactMatch : PartialMatch); for (uint i = 0; i < userN; ++i) { - int userKey = (*this)[i], - sequenceKey = seq[i]; + QKeyCombination userKey = (*this)[i], + sequenceKey = seq[i]; if (userKey != sequenceKey) return NoMatch; } @@ -1397,10 +1408,10 @@ QKeySequence::operator QVariant() const Returns a reference to the element at position \a index in the key sequence. This can only be used to read an element. */ -int QKeySequence::operator[](uint index) const +QKeyCombination QKeySequence::operator[](uint index) const { Q_ASSERT_X(index < QKeySequencePrivate::MaxKeyCount, "QKeySequence::operator[]", "index out of range"); - return d->key[index]; + return QKeyCombination::fromCombined(d->key[index]); } diff --git a/src/gui/kernel/qkeysequence.h b/src/gui/kernel/qkeysequence.h index 8b7ff12bed..880d0a581a 100644 --- a/src/gui/kernel/qkeysequence.h +++ b/src/gui/kernel/qkeysequence.h @@ -155,6 +155,10 @@ public: QKeySequence(); QKeySequence(const QString &key, SequenceFormat format = NativeText); QKeySequence(int k1, int k2 = 0, int k3 = 0, int k4 = 0); + QKeySequence(QKeyCombination k1, + QKeyCombination k2 = QKeyCombination::fromCombined(0), + QKeyCombination k3 = QKeyCombination::fromCombined(0), + QKeyCombination k4 = QKeyCombination::fromCombined(0)); QKeySequence(const QKeySequence &ks); QKeySequence(StandardKey key); ~QKeySequence(); @@ -179,7 +183,7 @@ public: static QList keyBindings(StandardKey key); operator QVariant() const; - int operator[](uint i) const; + QKeyCombination operator[](uint i) const; QKeySequence &operator=(const QKeySequence &other); QKeySequence &operator=(QKeySequence &&other) noexcept { swap(other); return *this; } void swap(QKeySequence &other) noexcept { qSwap(d, other.d); } @@ -201,7 +205,7 @@ private: static QString encodeString(int key); int assign(const QString &str); int assign(const QString &str, SequenceFormat format); - void setKey(int key, int index); + void setKey(QKeyCombination key, int index); QKeySequencePrivate *d; diff --git a/src/gui/kernel/qkeysequence_p.h b/src/gui/kernel/qkeysequence_p.h index 8c59505561..99d17e98d1 100644 --- a/src/gui/kernel/qkeysequence_p.h +++ b/src/gui/kernel/qkeysequence_p.h @@ -64,7 +64,7 @@ struct QKeyBinding { QKeySequence::StandardKey standardKey; uchar priority; - uint shortcut; + QKeyCombination shortcut; uint platform; }; diff --git a/src/gui/kernel/qplatformtheme.cpp b/src/gui/kernel/qplatformtheme.cpp index 81c897def7..5de1c98127 100644 --- a/src/gui/kernel/qplatformtheme.cpp +++ b/src/gui/kernel/qplatformtheme.cpp @@ -645,7 +645,7 @@ QList QPlatformTheme::keyBindings(QKeySequence::StandardKey key) c if (!(it->platform & platform)) continue; - uint shortcut = it->shortcut; + uint shortcut = it->shortcut.toCombined(); if (it->priority > 0) list.prepend(QKeySequence(shortcut)); diff --git a/src/gui/kernel/qshortcutmap.cpp b/src/gui/kernel/qshortcutmap.cpp index 4b6368a749..e79aaacd1a 100644 --- a/src/gui/kernel/qshortcutmap.cpp +++ b/src/gui/kernel/qshortcutmap.cpp @@ -543,12 +543,12 @@ void QShortcutMap::createNewSequences(QKeyEvent *e, QList &ksl, in curKsl.setKey(curSeq[2], 2); curKsl.setKey(curSeq[3], 3); } else { - curKsl.setKey(0, 0); - curKsl.setKey(0, 1); - curKsl.setKey(0, 2); - curKsl.setKey(0, 3); + curKsl.setKey(QKeyCombination::fromCombined(0), 0); + curKsl.setKey(QKeyCombination::fromCombined(0), 1); + curKsl.setKey(QKeyCombination::fromCombined(0), 2); + curKsl.setKey(QKeyCombination::fromCombined(0), 3); } - curKsl.setKey(possibleKeys.at(pkNum) & ~ignoredModifiers, index); + curKsl.setKey(QKeyCombination::fromCombined(possibleKeys.at(pkNum) & ~ignoredModifiers), index); } } } @@ -574,8 +574,8 @@ QKeySequence::SequenceMatch QShortcutMap::matches(const QKeySequence &seq1, : QKeySequence::PartialMatch); for (uint i = 0; i < userN; ++i) { - int userKey = seq1[i], - sequenceKey = seq2[i]; + int userKey = seq1[i].toCombined(), + sequenceKey = seq2[i].toCombined(); if ((userKey & Qt::Key_unknown) == Qt::Key_hyphen) userKey = (userKey & Qt::KeyboardModifierMask) | Qt::Key_Minus; if ((sequenceKey & Qt::Key_unknown) == Qt::Key_hyphen) diff --git a/src/gui/platform/unix/dbusmenu/qdbusmenutypes.cpp b/src/gui/platform/unix/dbusmenu/qdbusmenutypes.cpp index 73ff0aee69..c5bc4a1889 100644 --- a/src/gui/platform/unix/dbusmenu/qdbusmenutypes.cpp +++ b/src/gui/platform/unix/dbusmenu/qdbusmenutypes.cpp @@ -241,7 +241,7 @@ QDBusMenuShortcut QDBusMenuItem::convertKeySequence(const QKeySequence &sequence QDBusMenuShortcut shortcut; for (int i = 0; i < sequence.count(); ++i) { QStringList tokens; - int key = sequence[i]; + int key = sequence[i].toCombined(); if (key & Qt::MetaModifier) tokens << QStringLiteral("Super"); if (key & Qt::ControlModifier) diff --git a/src/gui/platform/unix/qxkbcommon.cpp b/src/gui/platform/unix/qxkbcommon.cpp index ff19ebd5d5..71f1cfd24a 100644 --- a/src/gui/platform/unix/qxkbcommon.cpp +++ b/src/gui/platform/unix/qxkbcommon.cpp @@ -656,7 +656,7 @@ QList QXkbCommon::possibleKeys(xkb_state *state, const QKeyEvent *event, int baseQtKey = keysymToQtKey_internal(sym, modifiers, queryState, keycode, superAsMeta, hyperAsMeta); if (baseQtKey) - result += (baseQtKey + modifiers); + result += (baseQtKey + int(modifiers)); xkb_mod_index_t shiftMod = xkb_keymap_mod_get_index(keymap, "Shift"); xkb_mod_index_t altMod = xkb_keymap_mod_get_index(keymap, "Alt"); @@ -711,7 +711,7 @@ QList QXkbCommon::possibleKeys(xkb_state *state, const QKeyEvent *event, if (ambiguous) continue; - result += (qtKey + mods); + result += (qtKey + int(mods)); } } diff --git a/src/testlib/qtestkeyboard.h b/src/testlib/qtestkeyboard.h index 1882c858c1..deee6da753 100644 --- a/src/testlib/qtestkeyboard.h +++ b/src/testlib/qtestkeyboard.h @@ -172,8 +172,8 @@ namespace QTest Q_DECL_UNUSED inline static void keySequence(QWindow *window, const QKeySequence &keySequence) { for (int i = 0; i < keySequence.count(); ++i) { - const Qt::Key key = Qt::Key(keySequence[i] & ~Qt::KeyboardModifierMask); - const Qt::KeyboardModifiers modifiers = Qt::KeyboardModifiers(keySequence[i] & Qt::KeyboardModifierMask); + const Qt::Key key = keySequence[i].key(); + const Qt::KeyboardModifiers modifiers = keySequence[i].keyboardModifiers(); keyClick(window, key, modifiers); } } @@ -313,8 +313,8 @@ namespace QTest inline static void keySequence(QWidget *widget, const QKeySequence &keySequence) { for (int i = 0; i < keySequence.count(); ++i) { - const Qt::Key key = Qt::Key(keySequence[i] & ~Qt::KeyboardModifierMask); - const Qt::KeyboardModifiers modifiers = Qt::KeyboardModifiers(keySequence[i] & Qt::KeyboardModifierMask); + const Qt::Key key = keySequence[i].key(); + const Qt::KeyboardModifiers modifiers = keySequence[i].keyboardModifiers(); keyClick(widget, key, modifiers); } } diff --git a/src/widgets/dialogs/qfiledialog.cpp b/src/widgets/dialogs/qfiledialog.cpp index 81e182c352..c4df58b6aa 100644 --- a/src/widgets/dialogs/qfiledialog.cpp +++ b/src/widgets/dialogs/qfiledialog.cpp @@ -3287,7 +3287,7 @@ void QFileDialogPrivate::createMenuActions() QAction *goHomeAction = new QAction(q); #ifndef QT_NO_SHORTCUT - goHomeAction->setShortcut(Qt::CTRL + Qt::Key_H + Qt::SHIFT); + goHomeAction->setShortcut(Qt::CTRL | Qt::SHIFT | Qt::Key_H); #endif QObject::connect(goHomeAction, SIGNAL(triggered()), q, SLOT(_q_goHome())); q->addAction(goHomeAction); diff --git a/src/widgets/dialogs/qmessagebox.cpp b/src/widgets/dialogs/qmessagebox.cpp index 4183098274..566263f1e0 100644 --- a/src/widgets/dialogs/qmessagebox.cpp +++ b/src/widgets/dialogs/qmessagebox.cpp @@ -1522,7 +1522,7 @@ void QMessageBox::keyPressEvent(QKeyEvent *e) const QList buttons = d->buttonBox->buttons(); for (auto *pb : buttons) { QKeySequence shortcut = pb->shortcut(); - if (!shortcut.isEmpty() && key == int(shortcut[0] & ~Qt::MODIFIER_MASK)) { + if (!shortcut.isEmpty() && key == shortcut[0].key()) { pb->animateClick(); return; } diff --git a/src/widgets/kernel/qwhatsthis.cpp b/src/widgets/kernel/qwhatsthis.cpp index 7ca50f9dab..b483655a71 100644 --- a/src/widgets/kernel/qwhatsthis.cpp +++ b/src/widgets/kernel/qwhatsthis.cpp @@ -510,7 +510,7 @@ QWhatsThisAction::QWhatsThisAction(QObject *parent) : QAction(tr("What's This?") setCheckable(true); connect(this, SIGNAL(triggered()), this, SLOT(actionTriggered())); #ifndef QT_NO_SHORTCUT - setShortcut(Qt::ShiftModifier + Qt::Key_F1); + setShortcut(Qt::ShiftModifier | Qt::Key_F1); #endif } diff --git a/src/widgets/widgets/qkeysequenceedit.cpp b/src/widgets/widgets/qkeysequenceedit.cpp index a38250c195..97ee915037 100644 --- a/src/widgets/widgets/qkeysequenceedit.cpp +++ b/src/widgets/widgets/qkeysequenceedit.cpp @@ -63,7 +63,7 @@ void QKeySequenceEditPrivate::init() layout->setContentsMargins(0, 0, 0, 0); layout->addWidget(lineEdit); - key[0] = key[1] = key[2] = key[3] = 0; + std::fill_n(key, QKeySequencePrivate::MaxKeyCount, QKeyCombination::fromCombined(0)); lineEdit->setFocusProxy(q); lineEdit->installEventFilter(q); @@ -186,7 +186,7 @@ void QKeySequenceEdit::setKeySequence(const QKeySequence &keySequence) d->keySequence = keySequence; - d->key[0] = d->key[1] = d->key[2] = d->key[3] = 0; + d->key[0] = d->key[1] = d->key[2] = d->key[3] = QKeyCombination::fromCombined(0); d->keyNum = keySequence.count(); for (int i = 0; i < d->keyNum; ++i) d->key[i] = keySequence[i]; @@ -286,7 +286,7 @@ void QKeySequenceEdit::keyPressEvent(QKeyEvent *e) } - d->key[d->keyNum] = nextKey; + d->key[d->keyNum] = QKeyCombination::fromCombined(nextKey); d->keyNum++; QKeySequence key(d->key[0], d->key[1], d->key[2], d->key[3]); diff --git a/src/widgets/widgets/qkeysequenceedit_p.h b/src/widgets/widgets/qkeysequenceedit_p.h index 7af034e735..8cd58de68f 100644 --- a/src/widgets/widgets/qkeysequenceedit_p.h +++ b/src/widgets/widgets/qkeysequenceedit_p.h @@ -76,7 +76,7 @@ public: QLineEdit *lineEdit; QKeySequence keySequence; int keyNum; - int key[QKeySequencePrivate::MaxKeyCount]; + QKeyCombination key[QKeySequencePrivate::MaxKeyCount]; int prevKey; int releaseTimer; }; diff --git a/src/widgets/widgets/qmenu.cpp b/src/widgets/widgets/qmenu.cpp index c2ef383d28..9007d2af78 100644 --- a/src/widgets/widgets/qmenu.cpp +++ b/src/widgets/widgets/qmenu.cpp @@ -3406,7 +3406,7 @@ void QMenu::keyPressEvent(QKeyEvent *e) continue; QAction *act = d->actions.at(i); QKeySequence sequence = QKeySequence::mnemonic(act->text()); - int key = sequence[0] & 0xffff; + int key = sequence[0].toCombined() & 0xffff; // suspicious if (key == c.unicode()) { clashCount++; if (!first) -- cgit v1.2.3