From 82a252afdd0f920357b1e543f2ee97f92c34919b Mon Sep 17 00:00:00 2001 From: Laszlo Agocs Date: Thu, 1 Mar 2012 13:45:29 +0200 Subject: [PATCH] Use velocity from touch events only when they are valid Add the capability flags to the extended mouse events. Otherwise it is not possible to tell if the velocity is valid. While the original version is fine if velocity is guaranteed to be available whenever QT_TRANSLATE_TOUCH_TO_MOUSE is set, some platform and driver combinations, e.g. the evdevtouch plugin that comes with Qt, do not provide velocity data in touch events. The touch-only mode of QML may be very useful in these cases too, we just need to fall back to the built-in velocity calculation. Change-Id: Iec5e7632a66380dc04c9435b09f5c173107bbe00 Reviewed-by: Martin Jones --- src/quick/items/qquickcanvas.cpp | 4 ++++ src/quick/items/qquickevents_p_p.h | 14 ++++++++++++-- src/quick/items/qquickflickable.cpp | 17 +++++++++++------ 3 files changed, 27 insertions(+), 8 deletions(-) diff --git a/src/quick/items/qquickcanvas.cpp b/src/quick/items/qquickcanvas.cpp index 670baa8..f135975 100644 --- a/src/quick/items/qquickcanvas.cpp +++ b/src/quick/items/qquickcanvas.cpp @@ -378,6 +378,7 @@ void QQuickCanvasPrivate::translateTouchToMouse(QTouchEvent *event) QQuickMouseEventEx me = touchToMouseEvent(QEvent::MouseButtonDblClick, p); me.setTimestamp(event->timestamp()); me.setAccepted(false); + me.setCapabilities(event->device()->capabilities()); if (!mouseGrabberItem) { if (deliverInitialMousePressEvent(rootItem, &me)) { touchMouseId = p.id(); @@ -394,6 +395,7 @@ void QQuickCanvasPrivate::translateTouchToMouse(QTouchEvent *event) QQuickMouseEventEx me = touchToMouseEvent(QEvent::MouseButtonPress, p); me.setTimestamp(event->timestamp()); me.setAccepted(false); + me.setCapabilities(event->device()->capabilities()); deliverMouseEvent(&me); if (me.isAccepted()) { touchMouseId = p.id(); @@ -405,6 +407,7 @@ void QQuickCanvasPrivate::translateTouchToMouse(QTouchEvent *event) if (p.state() & Qt::TouchPointMoved) { QQuickMouseEventEx me = touchToMouseEvent(QEvent::MouseMove, p); me.setTimestamp(event->timestamp()); + me.setCapabilities(event->device()->capabilities()); if (!mouseGrabberItem) { if (lastMousePosition.isNull()) lastMousePosition = me.windowPos(); @@ -428,6 +431,7 @@ void QQuickCanvasPrivate::translateTouchToMouse(QTouchEvent *event) return; QQuickMouseEventEx me = touchToMouseEvent(QEvent::MouseButtonRelease, p); me.setTimestamp(event->timestamp()); + me.setCapabilities(event->device()->capabilities()); deliverMouseEvent(&me); mouseGrabberItem = 0; } diff --git a/src/quick/items/qquickevents_p_p.h b/src/quick/items/qquickevents_p_p.h index 1d13a19..7ff4835 100644 --- a/src/quick/items/qquickevents_p_p.h +++ b/src/quick/items/qquickevents_p_p.h @@ -163,8 +163,11 @@ public: QQuickMouseEventEx(const QMouseEvent &event) : QMouseEvent(event) { - if (extended(&event)) - setVelocity(extended(&event)->velocity()); + const QQuickMouseEventEx *eventEx = extended(&event); + if (eventEx) { + setVelocity(eventEx->velocity()); + setCapabilities(eventEx->capabilities()); + } } static const QQuickMouseEventEx *extended(const QMouseEvent *e) { @@ -186,8 +189,15 @@ public: } QVector2D velocity() const { return _velocity; } + void setCapabilities(QTouchDevice::Capabilities caps) { + setExtended(); + _capabilities = caps; + } + QTouchDevice::Capabilities capabilities() const { return _capabilities; } + private: QVector2D _velocity; + QTouchDevice::Capabilities _capabilities; }; diff --git a/src/quick/items/qquickflickable.cpp b/src/quick/items/qquickflickable.cpp index 43aaf92..0e8097a 100644 --- a/src/quick/items/qquickflickable.cpp +++ b/src/quick/items/qquickflickable.cpp @@ -975,7 +975,7 @@ void QQuickFlickablePrivate::handleMouseMoveEvent(QMouseEvent *event) lastPosTime = currentTimestamp; QQuickMouseEventEx *extended = QQuickMouseEventEx::extended(event); if (q->yflick() && !rejectY) { - if (extended) { + if (extended && extended->capabilities().testFlag(QTouchDevice::Velocity)) { vData.addVelocitySample(extended->velocity().y(), maxVelocity); } else { qreal dy = event->localPos().y()-lastPos.y(); @@ -983,7 +983,7 @@ void QQuickFlickablePrivate::handleMouseMoveEvent(QMouseEvent *event) } } if (q->xflick() && !rejectX) { - if (extended) { + if (extended && extended->capabilities().testFlag(QTouchDevice::Velocity)) { hData.addVelocitySample(extended->velocity().x(), maxVelocity); } else { qreal dx = event->localPos().x()-lastPos.x(); @@ -1020,7 +1020,8 @@ void QQuickFlickablePrivate::handleMouseReleaseEvent(QMouseEvent *event) qreal vVelocity = 0; if (elapsed < 100 && vData.velocity != 0.) { QQuickMouseEventEx *extended = QQuickMouseEventEx::extended(event); - vVelocity = extended ? extended->velocity().y() : vData.velocity; + vVelocity = (extended && extended->capabilities().testFlag(QTouchDevice::Velocity)) + ? extended->velocity().y() : vData.velocity; } if (vData.atBeginning || vData.atEnd) { vVelocity /= 2; @@ -1035,7 +1036,8 @@ void QQuickFlickablePrivate::handleMouseReleaseEvent(QMouseEvent *event) qreal hVelocity = 0; if (elapsed < 100 && hData.velocity != 0.) { QQuickMouseEventEx *extended = QQuickMouseEventEx::extended(event); - hVelocity = extended ? extended->velocity().x() : hData.velocity; + hVelocity = (extended && extended->capabilities().testFlag(QTouchDevice::Velocity)) + ? extended->velocity().x() : hData.velocity; } if (hData.atBeginning || hData.atEnd) { hVelocity /= 2; @@ -1769,8 +1771,11 @@ bool QQuickFlickable::sendMouseEvent(QMouseEvent *event) QQuickMouseEventEx mouseEvent(event->type(), mapFromScene(event->windowPos()), event->windowPos(), event->screenPos(), event->button(), event->buttons(), event->modifiers()); - if (QQuickMouseEventEx::extended(event)) - mouseEvent.setVelocity(QQuickMouseEventEx::extended(event)->velocity()); + QQuickMouseEventEx *eventEx = QQuickMouseEventEx::extended(event); + if (eventEx) { + mouseEvent.setVelocity(eventEx->velocity()); + mouseEvent.setCapabilities(eventEx->capabilities()); + } mouseEvent.setTimestamp(event->timestamp()); mouseEvent.setAccepted(false); -- 2.7.4