From ff844b63b9656e5527af126d41a300ecfcc303ff Mon Sep 17 00:00:00 2001 From: Frederik Gladhorn Date: Thu, 7 Jul 2016 22:35:54 +0200 Subject: Add QQuickPointerEvent::syntheticMouseEvent It makes it easier to send synthetic mouse events for touch. Change-Id: Ibb8e2737e3245ae7438708aa170ec1f888e770d8 Reviewed-by: Frederik Gladhorn --- src/quick/items/qquickevents.cpp | 47 ++++++++++++++++++++++++++++++++++++++ src/quick/items/qquickevents_p_p.h | 6 ++++- 2 files changed, 52 insertions(+), 1 deletion(-) (limited to 'src/quick/items') diff --git a/src/quick/items/qquickevents.cpp b/src/quick/items/qquickevents.cpp index ad5a007b37..d09294b515 100644 --- a/src/quick/items/qquickevents.cpp +++ b/src/quick/items/qquickevents.cpp @@ -38,6 +38,7 @@ ****************************************************************************/ #include "qquickevents_p_p.h" +#include #include #include @@ -572,6 +573,52 @@ const QQuickEventPoint *QQuickPointerEvent::point(int i) const { return nullptr; } +/*! + \internal + Populate the reusable synth-mouse event from one touchpoint. + It's required that isTouchEvent() be true when this is called. + If the touchpoint cannot be found, this returns nullptr. + Ownership of the event is NOT transferred to the caller. +*/ +QMouseEvent *QQuickPointerEvent::syntheticMouseEvent(int pointID, QQuickItem *relativeTo) const { + const QTouchEvent::TouchPoint *p = touchPointById(pointID); + if (!p) + return nullptr; + QEvent::Type type; + Qt::MouseButton buttons = Qt::LeftButton; + switch (p->state()) { + case Qt::TouchPointPressed: + type = QEvent::MouseButtonPress; + break; + case Qt::TouchPointMoved: + case Qt::TouchPointStationary: + type = QEvent::MouseMove; + break; + case Qt::TouchPointReleased: + type = QEvent::MouseButtonRelease; + buttons = Qt::NoButton; + break; + default: + Q_ASSERT(false); + return nullptr; + } + m_synthMouseEvent = QMouseEvent(type, relativeTo->mapFromScene(p->scenePos()), + p->scenePos(), p->screenPos(), Qt::LeftButton, buttons, m_event->modifiers()); + m_synthMouseEvent.setAccepted(true); + m_synthMouseEvent.setTimestamp(m_event->timestamp()); + // In the future we will try to always have valid velocity in every QQuickEventPoint. + // QQuickFlickablePrivate::handleMouseMoveEvent() checks for QTouchDevice::Velocity + // and if it is set, then it does not need to do its own velocity calculations. + // That's probably the only usecase for this, so far. Some day Flickable should handle + // pointer events, and then passing touchpoint velocity via QMouseEvent will be obsolete. + // Conveniently (by design), QTouchDevice::Velocity == QQuickPointerDevice.Velocity + // so that we don't need to convert m_device->capabilities(). + if (m_device) + QGuiApplicationPrivate::setMouseEventCapsAndVelocity(&m_synthMouseEvent, m_device->capabilities(), p->velocity()); + QGuiApplicationPrivate::setMouseEventSource(&m_synthMouseEvent, Qt::MouseEventSynthesizedByQt); + return &m_synthMouseEvent; +} + /*! \internal Returns a pointer to the original TouchPoint which has the same diff --git a/src/quick/items/qquickevents_p_p.h b/src/quick/items/qquickevents_p_p.h index 0a306e8b46..65a9ae190a 100644 --- a/src/quick/items/qquickevents_p_p.h +++ b/src/quick/items/qquickevents_p_p.h @@ -396,7 +396,8 @@ public: , m_button(Qt::NoButton) , m_pressedButtons(Qt::NoButton) , m_pointCount(0) - , m_mousePoint(nullptr) { } + , m_mousePoint(nullptr) + , m_synthMouseEvent(QEvent::MouseMove, QPointF(), Qt::NoButton, Qt::NoButton, Qt::NoModifier) { } public: // property accessors const QQuickPointerDevice *device() const { return m_device; } @@ -422,6 +423,8 @@ public: // helpers for C++ only (during event delivery) QTouchEvent *touchEventForItem(const QList &newPoints, QQuickItem *relativeTo) const; + QMouseEvent *syntheticMouseEvent(int pointID, QQuickItem *relativeTo) const; + private: void initFromMouse(QMouseEvent *ev); void initFromTouch(QTouchEvent *ev); @@ -434,6 +437,7 @@ private: int m_pointCount; QVector m_touchPoints; QQuickEventPoint *m_mousePoint; + mutable QMouseEvent m_synthMouseEvent; Q_DISABLE_COPY(QQuickPointerEvent) }; -- cgit v1.2.3