summaryrefslogtreecommitdiffstats
path: root/src/corelib/global/qnamespace.h
diff options
context:
space:
mode:
authorGiuseppe D'Angelo <giuseppe.dangelo@kdab.com>2020-04-19 19:56:18 +0200
committerGiuseppe D'Angelo <giuseppe.dangelo@kdab.com>2020-09-03 07:00:31 +0200
commit25351dcc549f1daddf5e2ae8a242191174342a3e (patch)
tree97436200219470e7eee4096038e4da6e8df06835 /src/corelib/global/qnamespace.h
parentf03b2f7711e3a0e90fb21672273959a2a9ed1c38 (diff)
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 <volker.hilsheimer@qt.io>
Diffstat (limited to 'src/corelib/global/qnamespace.h')
-rw-r--r--src/corelib/global/qnamespace.h156
1 files changed, 156 insertions, 0 deletions
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 <giuseppe.dangelo@kdab.com>
** 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