diff options
author | Frederik Gladhorn <frederik.gladhorn@theqtcompany.com> | 2016-07-07 22:35:54 +0200 |
---|---|---|
committer | Frederik Gladhorn <frederik.gladhorn@qt.io> | 2016-07-15 08:44:03 +0000 |
commit | ff844b63b9656e5527af126d41a300ecfcc303ff (patch) | |
tree | 4d5756b70124def5b81fc413f82effaf6cc1b5a4 /src/quick/items | |
parent | 62a90bcd8304fde9072c96f7f81cb09b4fa878dc (diff) |
Add QQuickPointerEvent::syntheticMouseEvent
It makes it easier to send synthetic mouse events for touch.
Change-Id: Ibb8e2737e3245ae7438708aa170ec1f888e770d8
Reviewed-by: Frederik Gladhorn <frederik.gladhorn@qt.io>
Diffstat (limited to 'src/quick/items')
-rw-r--r-- | src/quick/items/qquickevents.cpp | 47 | ||||
-rw-r--r-- | src/quick/items/qquickevents_p_p.h | 6 |
2 files changed, 52 insertions, 1 deletions
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 <QtGui/private/qguiapplication_p.h> #include <QtQuick/private/qquickitem_p.h> #include <QtQuick/private/qquickwindow_p.h> @@ -574,6 +575,52 @@ const QQuickEventPoint *QQuickPointerEvent::point(int i) const { /*! \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 \l {QTouchEvent::TouchPoint::id}{id} as \a pointId, if the original event is a QTouchEvent, and if that point is found. Otherwise, returns nullptr. 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<const QQuickEventPoint *> &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<QQuickEventTouchPoint *> m_touchPoints; QQuickEventPoint *m_mousePoint; + mutable QMouseEvent m_synthMouseEvent; Q_DISABLE_COPY(QQuickPointerEvent) }; |