diff options
author | Shawn Rutledge <shawn.rutledge@qt.io> | 2020-08-06 17:28:06 +0200 |
---|---|---|
committer | Shawn Rutledge <shawn.rutledge@qt.io> | 2020-08-24 13:50:55 +0200 |
commit | 38806273e567a39bd7ca4ced5e6e38dfd7426f27 (patch) | |
tree | f2336bf4de830b806903b347238d1c7c6054f3b5 /src/gui/kernel/qevent.cpp | |
parent | 0c52e600b3b5c83d128e6b4068d523829357fef0 (diff) |
Remove QEventPoint::event() in favor of device()
event()->device() was the most common use case anyway.
The idea that the "parent" of a QEventPoint is the QPointerEvent
interferes with the ability to copy and move event objects: the parent
pointers are dangling unless we use the QPointerEvent subclass
destructors to set the points' parents to null. Since there is no move
constructor, even returning a QEventPoint from a function by value
results in destroying the temporary instance and copying it to the
caller's space. So the parent pointer is often useless, unless we do
even more work to maintain it when the event moves.
If we optimize to avoid copying QEventPoints too much (and perhaps
enable exposing _mutable_ points to QML) by storing reusable instances in
QPointingDevice (which is the current plan), then the actual parent will
no longer be the event. Events are usually stack-allocated, thus
temporary and intended to be movable.
Change-Id: I24b648dcc046fc79d2401c781f1fda6cb00f47b0
Reviewed-by: Volker Hilsheimer <volker.hilsheimer@qt.io>
Diffstat (limited to 'src/gui/kernel/qevent.cpp')
-rw-r--r-- | src/gui/kernel/qevent.cpp | 23 |
1 files changed, 11 insertions, 12 deletions
diff --git a/src/gui/kernel/qevent.cpp b/src/gui/kernel/qevent.cpp index df8a18beb0..2414a0d951 100644 --- a/src/gui/kernel/qevent.cpp +++ b/src/gui/kernel/qevent.cpp @@ -65,9 +65,7 @@ Q_LOGGING_CATEGORY(lcPointerGrab, "qt.pointer.grab") static const QString pointDeviceName(const QEventPoint *point) { - if (!point->event()) - return {}; - auto device = point->event()->device(); + const auto device = point->device(); QString deviceName = (device ? device->name() : QLatin1String("null device")); deviceName.resize(16, u' '); // shorten, and align in case of sequential output return deviceName; @@ -228,13 +226,14 @@ QInputEvent::~QInputEvent() /*! \internal - Constructs an invalid event point with the given \a id and \a parent. + Constructs an invalid event point with the given \a id and the \a device + from which it originated. This acts as a default constructor in usages like QMap<int, QEventPoint>, as in qgraphicsscene_p.h. */ -QEventPoint::QEventPoint(int id, const QPointerEvent *parent) - : m_parent(parent), m_pointId(id), m_state(State::Unknown), m_accept(false), m_stationaryWithModifiedProperty(false), m_reserved(0) +QEventPoint::QEventPoint(int id, const QPointingDevice *device) + : m_device(device), m_pointId(id), m_state(State::Unknown), m_accept(false), m_stationaryWithModifiedProperty(false), m_reserved(0) { } @@ -323,7 +322,7 @@ void QEventPoint::clearPassiveGrabbers() QPointF QEventPoint::normalizedPos() const { - auto geom = event()->device()->availableVirtualGeometry(); + auto geom = m_device->availableVirtualGeometry(); if (geom.isNull()) return QPointF(); return (globalPosition() - geom.topLeft()) / geom.width(); @@ -331,7 +330,7 @@ QPointF QEventPoint::normalizedPos() const QPointF QEventPoint::startNormalizedPos() const { - auto geom = event()->device()->availableVirtualGeometry(); + auto geom = m_device->availableVirtualGeometry(); if (geom.isNull()) return QPointF(); return (globalPressPosition() - geom.topLeft()) / geom.width(); @@ -339,7 +338,7 @@ QPointF QEventPoint::startNormalizedPos() const QPointF QEventPoint::lastNormalizedPos() const { - auto geom = event()->device()->availableVirtualGeometry(); + auto geom = m_device->availableVirtualGeometry(); if (geom.isNull()) return QPointF(); return (globalLastPosition() - geom.topLeft()) / geom.width(); @@ -369,7 +368,7 @@ const QPointingDevice *QPointerEvent::pointingDevice() const QSinglePointEvent::QSinglePointEvent(QEvent::Type type, const QPointingDevice *dev, const QPointF &localPos, const QPointF &scenePos, const QPointF &globalPos, Qt::MouseButton button, Qt::MouseButtons buttons, Qt::KeyboardModifiers modifiers) : QPointerEvent(type, dev, modifiers), - m_point(0, this), + m_point(0, dev), m_button(button), m_mouseState(buttons), m_source(Qt::MouseEventNotSynthesized), @@ -4243,7 +4242,7 @@ QTouchEvent::QTouchEvent(QEvent::Type eventType, { for (QEventPoint &point : m_touchPoints) { m_touchPointStates |= point.state(); - QMutableEventPoint::from(point).setParent(this); + QMutableEventPoint::from(point).setDevice(device); } } @@ -4266,7 +4265,7 @@ QTouchEvent::QTouchEvent(QEvent::Type eventType, m_touchPoints(touchPoints) { for (QEventPoint &point : m_touchPoints) - QMutableEventPoint::from(point).setParent(this); + QMutableEventPoint::from(point).setDevice(device); } /*! |