aboutsummaryrefslogtreecommitdiffstats
path: root/src/quick/items
diff options
context:
space:
mode:
authorShawn Rutledge <shawn.rutledge@qt.io>2016-07-11 16:19:52 +0200
committerShawn Rutledge <shawn.rutledge@qt.io>2016-07-12 12:43:06 +0000
commit14e8e05ce3511e46d30710a680b0b7e987a569c7 (patch)
treee25ad4a4dd0a0637ae6819c86b8c801ecc3515dc /src/quick/items
parentccad62088a80fa33cedba58adb633d27055e87b6 (diff)
add QQuickPointerEvent::touchEventForItem
It's similar to QQuickWindowPrivate::touchEventWithPoints(), and is used in the same way when delivering a QQuickPointerEvent by sending a QTouchEvent to the item's event() method. Change-Id: I0d181eaf924bfdad2db2de9d8acf85d0345aec3e Reviewed-by: Frederik Gladhorn <frederik.gladhorn@qt.io>
Diffstat (limited to 'src/quick/items')
-rw-r--r--src/quick/items/qquickevents.cpp66
-rw-r--r--src/quick/items/qquickevents_p_p.h10
2 files changed, 75 insertions, 1 deletions
diff --git a/src/quick/items/qquickevents.cpp b/src/quick/items/qquickevents.cpp
index 520e5d2704..50714518b1 100644
--- a/src/quick/items/qquickevents.cpp
+++ b/src/quick/items/qquickevents.cpp
@@ -38,6 +38,7 @@
****************************************************************************/
#include "qquickevents_p_p.h"
+#include <QtQuick/private/qquickitem_p.h>
#include <QtQuick/private/qquickwindow_p.h>
QT_BEGIN_NAMESPACE
@@ -535,4 +536,69 @@ bool QQuickPointerEvent::isTabletEvent() const
}
}
+/*
+ \internal
+
+ make a new QTouchEvent, giving it a subset of the original touch points
+*/
+QTouchEvent *QQuickPointerEvent::touchEventForItem(const QList<const QQuickEventPoint *> &newPoints, QQuickItem *relativeTo) const
+{
+ QList<QTouchEvent::TouchPoint> touchPoints;
+ Qt::TouchPointStates eventStates;
+ // TODO maybe add QQuickItem::mapVector2DFromScene(QVector2D) to avoid needing QQuickItemPrivate here
+ // Or else just document that velocity is always scene-relative and is not scaled and rotated with the item
+ // but that would require changing tst_qquickwindow::touchEvent_velocity(): it expects transformed velocity
+ QMatrix4x4 transformMatrix(QQuickItemPrivate::get(relativeTo)->windowToItemTransform());
+ for (const QQuickEventPoint * p : newPoints) {
+ const QTouchEvent::TouchPoint *tp = touchPointById(p->pointId());
+ if (tp) {
+ eventStates |= tp->state();
+ QTouchEvent::TouchPoint tpCopy = *tp;
+ tpCopy.setPos(relativeTo->mapFromScene(tpCopy.scenePos()));
+ tpCopy.setLastPos(relativeTo->mapFromScene(tpCopy.lastPos()));
+ tpCopy.setStartPos(relativeTo->mapFromScene(tpCopy.startScenePos()));
+ tpCopy.setRect(relativeTo->mapRectFromScene(tpCopy.sceneRect()));
+ tpCopy.setVelocity(transformMatrix.mapVector(tpCopy.velocity()).toVector2D());
+ touchPoints << tpCopy;
+ }
+ }
+
+ // if all points have the same state, set the event type accordingly
+ const QTouchEvent &event = *asTouchEvent();
+ QEvent::Type eventType = event.type();
+ switch (eventStates) {
+ case Qt::TouchPointPressed:
+ eventType = QEvent::TouchBegin;
+ break;
+ case Qt::TouchPointReleased:
+ eventType = QEvent::TouchEnd;
+ break;
+ default:
+ eventType = QEvent::TouchUpdate;
+ break;
+ }
+
+ QTouchEvent *touchEvent = new QTouchEvent(eventType);
+ touchEvent->setWindow(event.window());
+ touchEvent->setTarget(relativeTo);
+ touchEvent->setDevice(event.device());
+ touchEvent->setModifiers(event.modifiers());
+ touchEvent->setTouchPoints(touchPoints);
+ touchEvent->setTouchPointStates(eventStates);
+ touchEvent->setTimestamp(event.timestamp());
+ touchEvent->accept();
+ return touchEvent;
+}
+
+const QTouchEvent::TouchPoint *QQuickPointerEvent::touchPointById(int pointId) const {
+ const QTouchEvent *ev = asTouchEvent();
+ if (!ev)
+ return nullptr;
+ const QList<QTouchEvent::TouchPoint> &tps = ev->touchPoints();
+ auto it = std::find_if(tps.constBegin(), tps.constEnd(),
+ [&pointId](QTouchEvent::TouchPoint const& tp) { return tp.id() == pointId; } );
+ // return the pointer to the actual TP in QTouchEvent::_touchPoints
+ return (it == tps.end() ? nullptr : it.operator->());
+}
+
QT_END_NAMESPACE
diff --git a/src/quick/items/qquickevents_p_p.h b/src/quick/items/qquickevents_p_p.h
index c4c4daf830..af3e385ce6 100644
--- a/src/quick/items/qquickevents_p_p.h
+++ b/src/quick/items/qquickevents_p_p.h
@@ -378,6 +378,8 @@ private:
QPointerUniqueId m_uniqueId;
};
+class QQuickItem;
+
class Q_QUICK_PRIVATE_EXPORT QQuickPointerEvent : public QObject
{
Q_OBJECT
@@ -406,6 +408,9 @@ public:
Qt::MouseButton button() const { return m_button; }
Qt::MouseButtons buttons() const { return m_pressedButtons; }
+ // ----------------------------------------------------
+ // helpers for C++ event delivery, not for QML properties
+
/** Returns the original touch event. */
QTouchEvent *asTouchEvent() const;
@@ -414,7 +419,6 @@ public:
* Returns nullptr in case the original event was not a mouse event. */
QMouseEvent *asMouseEvent() const;
- // helpers for C++ event delivery, not for QML properties
int pointCount() const { return asTouchEvent() ? m_touchPoints.count() : 1; }
const QQuickEventPoint *point(int i) const {
if (asTouchEvent())
@@ -422,6 +426,10 @@ public:
return i == 0 ? m_mousePoint : nullptr;
}
+ const QTouchEvent::TouchPoint *touchPointById(int pointId) const;
+
+ QTouchEvent *touchEventForItem(const QList<const QQuickEventPoint *> &newPoints, QQuickItem *relativeTo) const;
+
protected:
bool isValid() const { return m_event != nullptr; }