diff options
author | Giuseppe D'Angelo <giuseppe.dangelo@kdab.com> | 2020-04-19 19:56:18 +0200 |
---|---|---|
committer | Giuseppe D'Angelo <giuseppe.dangelo@kdab.com> | 2020-09-03 07:00:31 +0200 |
commit | 25351dcc549f1daddf5e2ae8a242191174342a3e (patch) | |
tree | 97436200219470e7eee4096038e4da6e8df06835 /src/widgets/widgets | |
parent | f03b2f7711e3a0e90fb21672273959a2a9ed1c38 (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/widgets/widgets')
-rw-r--r-- | src/widgets/widgets/qkeysequenceedit.cpp | 6 | ||||
-rw-r--r-- | src/widgets/widgets/qkeysequenceedit_p.h | 2 | ||||
-rw-r--r-- | src/widgets/widgets/qmenu.cpp | 2 |
3 files changed, 5 insertions, 5 deletions
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) |