From 52d35526f256bf4c8155f5e660a214ab8a2efbdf Mon Sep 17 00:00:00 2001 From: Shawn Rutledge Date: Wed, 9 May 2018 20:32:16 +0200 Subject: MultiPointHandler: add a centroid property One of the reasons we were so fond of using SinglePointHandler for the "simple" handlers like TapHandler and DragHandler is that it has the point property, which is easy to bind to in QML. But multi-point handlers tend to use the centroid as the focal point or fulcrum of the transformation, so exposing the centroid as a QQuickHandlerPoint gadget rather than just a QPointF has all the same advantages as the point property in SinglePointHandler, including letting the QQuickHandlerPoint instance store state between events (press position, grab position, last known position etc.) Change-Id: I4a955fa21b54b7ebb50b0ee2c942fb98eeccb560 Reviewed-by: Shawn Rutledge --- src/quick/handlers/qquickmultipointhandler.cpp | 35 +++++++++++--------------- 1 file changed, 14 insertions(+), 21 deletions(-) (limited to 'src/quick/handlers/qquickmultipointhandler.cpp') diff --git a/src/quick/handlers/qquickmultipointhandler.cpp b/src/quick/handlers/qquickmultipointhandler.cpp index d595b4c9b4..89733a4260 100644 --- a/src/quick/handlers/qquickmultipointhandler.cpp +++ b/src/quick/handlers/qquickmultipointhandler.cpp @@ -91,6 +91,20 @@ bool QQuickMultiPointHandler::wantsPointerEvent(QQuickPointerEvent *event) return ret; } +void QQuickMultiPointHandler::handlePointerEventImpl(QQuickPointerEvent *event) +{ + QQuickPointerHandler::handlePointerEventImpl(event); + m_centroid.reset(m_currentPoints); + emit centroidChanged(); +} + +void QQuickMultiPointHandler::onActiveChanged() +{ + if (active()) { + m_centroid.m_sceneGrabPosition = m_centroid.m_scenePosition; + } +} + QVector QQuickMultiPointHandler::eligiblePoints(QQuickPointerEvent *event) { QVector ret; @@ -207,27 +221,6 @@ bool QQuickMultiPointHandler::sameAsCurrentPoints(QQuickPointerEvent *event) return ret; } -// TODO make templates for these functions somehow? -QPointF QQuickMultiPointHandler::touchPointCentroid() -{ - QPointF ret; - if (Q_UNLIKELY(m_currentPoints.size() == 0)) - return ret; - for (QQuickEventPoint *point : qAsConst(m_currentPoints)) - ret += point->scenePosition(); - return ret / m_currentPoints.size(); -} - -QVector2D QQuickMultiPointHandler::touchPointCentroidVelocity() -{ - QVector2D ret; - if (Q_UNLIKELY(m_currentPoints.size() == 0)) - return ret; - for (QQuickEventPoint *point : qAsConst(m_currentPoints)) - ret += point->velocity(); - return ret / m_currentPoints.size(); -} - qreal QQuickMultiPointHandler::averageTouchPointDistance(const QPointF &ref) { qreal ret = 0; -- cgit v1.2.3