diff options
author | Volker Hilsheimer <volker.hilsheimer@qt.io> | 2020-07-03 11:06:37 +0200 |
---|---|---|
committer | Volker Hilsheimer <volker.hilsheimer@qt.io> | 2020-08-17 21:54:34 +0200 |
commit | d1111632e29124531d5b4512e0492314caaae396 (patch) | |
tree | 15bdf8c7f348b236a90f5ef59ceec4ce91ee4af1 /src | |
parent | 81159cf3f7c47dbab4c781f3b4ffa9e0a202b1dc (diff) |
Introduce QEvent::isInputEvent
This makes it easier to reliably maintain input-event related states
in widgets, in particluar the state of keyboard modifiers. Instead of
testing for all possible event types, code can just test the flag before
safely static_cast'ing to QInputEvent to access the modifiers.
Simplify the code in QAbstractItemView accordingly.
Task-number: QTBUG-73829
Change-Id: Idc7c08e2f3f1e8844f5c6693c195153038ee6490
Reviewed-by: Shawn Rutledge <shawn.rutledge@qt.io>
Diffstat (limited to 'src')
-rw-r--r-- | src/corelib/kernel/qcoreevent.cpp | 18 | ||||
-rw-r--r-- | src/corelib/kernel/qcoreevent.h | 7 | ||||
-rw-r--r-- | src/gui/kernel/qevent.cpp | 2 | ||||
-rw-r--r-- | src/widgets/itemviews/qabstractitemview.cpp | 24 |
4 files changed, 29 insertions, 22 deletions
diff --git a/src/corelib/kernel/qcoreevent.cpp b/src/corelib/kernel/qcoreevent.cpp index e3326f00d7..34712be47a 100644 --- a/src/corelib/kernel/qcoreevent.cpp +++ b/src/corelib/kernel/qcoreevent.cpp @@ -295,7 +295,7 @@ QT_BEGIN_NAMESPACE Contructs an event object of type \a type. */ QEvent::QEvent(Type type) - : d(nullptr), t(type), posted(false), spont(false), m_accept(true) + : d(nullptr), t(type), posted(false), spont(false), m_accept(true), m_inputEvent(false) { Q_TRACE(QEvent_ctor, this, t); } @@ -319,6 +319,14 @@ QEvent::QEvent(const QEvent &other) /*! \internal + \since 6.0 + \fn QEvent::QEvent(Type type, QEvent::InputEventTag) + + Constructs an event object of type \a type, setting the inputEvent flag to true. +*/ + +/*! + \internal Attempts to copy the \a other event. Copying events is a bad idea, yet some Qt 4 code does it (notably, @@ -407,6 +415,14 @@ QEvent::~QEvent() The return value of this function is not defined for paint events. */ +/*! + \fn bool QEvent::isInputEvent() const + \since 6.0 + + Returns \c true if the event object is a QInputEvent or one of its + subclasses. +*/ + namespace { template <size_t N> struct QBasicAtomicBitField { diff --git a/src/corelib/kernel/qcoreevent.h b/src/corelib/kernel/qcoreevent.h index bbd95f7a96..209ed25d6b 100644 --- a/src/corelib/kernel/qcoreevent.h +++ b/src/corelib/kernel/qcoreevent.h @@ -307,9 +307,13 @@ public: inline void accept() { m_accept = true; } inline void ignore() { m_accept = false; } + inline bool isInputEvent() const noexcept { return m_inputEvent; } + static int registerEventType(int hint = -1) noexcept; protected: + struct InputEventTag { explicit InputEventTag() = default; }; + QEvent(Type type, InputEventTag) : QEvent(type) { m_inputEvent = true; } QEventPrivate *d; ushort t; @@ -317,7 +321,8 @@ private: ushort posted : 1; ushort spont : 1; ushort m_accept : 1; - ushort reserved : 13; + ushort m_inputEvent : 1; + ushort reserved : 12; friend class QCoreApplication; friend class QCoreApplicationPrivate; diff --git a/src/gui/kernel/qevent.cpp b/src/gui/kernel/qevent.cpp index aaf6da85cd..c8f2854514 100644 --- a/src/gui/kernel/qevent.cpp +++ b/src/gui/kernel/qevent.cpp @@ -163,7 +163,7 @@ QEnterEvent::~QEnterEvent() \internal */ QInputEvent::QInputEvent(Type type, const QInputDevice *dev, Qt::KeyboardModifiers modifiers) - : QEvent(type), m_dev(dev), m_modState(modifiers) + : QEvent(type, QEvent::InputEventTag{}), m_dev(dev), m_modState(modifiers) {} /*! diff --git a/src/widgets/itemviews/qabstractitemview.cpp b/src/widgets/itemviews/qabstractitemview.cpp index fc1c55114c..c882f1107a 100644 --- a/src/widgets/itemviews/qabstractitemview.cpp +++ b/src/widgets/itemviews/qabstractitemview.cpp @@ -3917,21 +3917,9 @@ QItemSelectionModel::SelectionFlags QAbstractItemView::selectionCommand(const QM const QEvent *event) const { Q_D(const QAbstractItemView); - Qt::KeyboardModifiers keyModifiers = Qt::NoModifier; - if (event) { - switch (event->type()) { - case QEvent::MouseButtonDblClick: - case QEvent::MouseButtonPress: - case QEvent::MouseButtonRelease: - case QEvent::MouseMove: - case QEvent::KeyPress: - case QEvent::KeyRelease: - keyModifiers = (static_cast<const QInputEvent*>(event))->modifiers(); - break; - default: - break; - } - } + Qt::KeyboardModifiers keyModifiers = event && event->isInputEvent() + ? static_cast<const QInputEvent*>(event)->modifiers() + : Qt::NoModifier; switch (d->selectionMode) { case NoSelection: // Never update selection model return QItemSelectionModel::NoUpdate; @@ -3989,16 +3977,16 @@ QItemSelectionModel::SelectionFlags QAbstractItemViewPrivate::extendedSelectionC { Qt::KeyboardModifiers modifiers = Qt::NoModifier; if (event) { + if (event->isInputEvent()) + modifiers = static_cast<const QInputEvent*>(event)->modifiers(); switch (event->type()) { case QEvent::MouseMove: { // Toggle on MouseMove - modifiers = static_cast<const QMouseEvent*>(event)->modifiers(); if (modifiers & Qt::ControlModifier) return QItemSelectionModel::ToggleCurrent|selectionBehaviorFlags(); break; } case QEvent::MouseButtonPress: { - modifiers = static_cast<const QMouseEvent*>(event)->modifiers(); const Qt::MouseButton button = static_cast<const QMouseEvent*>(event)->button(); const bool rightButtonPressed = button & Qt::RightButton; const bool shiftKeyPressed = modifiers & Qt::ShiftModifier; @@ -4016,7 +4004,6 @@ QItemSelectionModel::SelectionFlags QAbstractItemViewPrivate::extendedSelectionC } case QEvent::MouseButtonRelease: { // ClearAndSelect on MouseButtonRelease if MouseButtonPress on selected item or empty area - modifiers = static_cast<const QMouseEvent*>(event)->modifiers(); const Qt::MouseButton button = static_cast<const QMouseEvent*>(event)->button(); const bool rightButtonPressed = button & Qt::RightButton; const bool shiftKeyPressed = modifiers & Qt::ShiftModifier; @@ -4029,7 +4016,6 @@ QItemSelectionModel::SelectionFlags QAbstractItemViewPrivate::extendedSelectionC } case QEvent::KeyPress: { // NoUpdate on Key movement and Ctrl - modifiers = static_cast<const QKeyEvent*>(event)->modifiers(); switch (static_cast<const QKeyEvent*>(event)->key()) { case Qt::Key_Backtab: modifiers = modifiers & ~Qt::ShiftModifier; // special case for backtab |