2 * Copyright (C) 2011 Benjamin Poulain <benjamin@webkit.org>
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
13 * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
14 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
15 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
17 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
18 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
19 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
20 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
21 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
22 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
23 * THE POSSIBILITY OF SUCH DAMAGE.
27 #include "QtPinchGestureRecognizer.h"
29 #include "ViewportInteractionEngine.h"
30 #include <QtCore/QLineF>
34 const qreal pinchInitialTriggerDistanceThreshold = 5.;
36 static inline int findTouchPointIndex(const QList<QTouchEvent::TouchPoint>& touchPoints, const QtPinchGestureRecognizer::TouchPointInformation& pointInformation)
38 const int touchCount = touchPoints.size();
39 for (int i = 0; i < touchCount; ++i) {
40 const QTouchEvent::TouchPoint& touchPoint = touchPoints.at(i);
41 if (touchPoint.id() == pointInformation.id)
47 static inline QPointF computeTouchCenter(const QTouchEvent::TouchPoint& point1, const QTouchEvent::TouchPoint& point2)
49 return (point1.pos() + point2.pos()) / 2.0f;
52 QtPinchGestureRecognizer::QtPinchGestureRecognizer(ViewportInteractionEngine* interactionEngine)
53 : QtGestureRecognizer(interactionEngine)
58 bool QtPinchGestureRecognizer::recognize(const QTouchEvent* event)
60 const QList<QTouchEvent::TouchPoint>& touchPoints = event->touchPoints();
61 if (touchPoints.size() < 2) {
62 if (m_state == GestureRecognized)
63 m_viewportInteractionEngine->pinchGestureEnded();
68 switch (event->type()) {
69 case QEvent::TouchBegin:
70 case QEvent::TouchUpdate:
73 initializeGesture(touchPoints);
75 case GestureRecognitionStarted:
76 case GestureRecognized:
77 ASSERT(m_point1.isValid());
78 ASSERT(m_point2.isValid());
80 const int point1Index = findTouchPointIndex(touchPoints, m_point1);
81 if (point1Index < 0) {
85 const int point2Index = findTouchPointIndex(touchPoints, m_point2);
86 if (point2Index < 0) {
91 const QTouchEvent::TouchPoint& point1 = touchPoints.at(point1Index);
92 const QTouchEvent::TouchPoint& point2 = touchPoints.at(point2Index);
93 if (m_state == GestureRecognitionStarted) {
94 // FIXME: The gesture should only start if the touch events were not accepted at the start of the touch sequence.
95 const qreal pinchDistance = qAbs(QLineF(point1.screenPos(), point2.screenPos()).length() - QLineF(m_point1.initialScreenPosition, m_point2.initialScreenPosition).length());
96 if (pinchDistance < pinchInitialTriggerDistanceThreshold)
98 m_state = GestureRecognized;
99 m_viewportInteractionEngine->pinchGestureStarted();
101 // We reset the initial position to the previous position in order to avoid the jump caused
102 // by skipping all the events between the beginning and when the threshold is hit.
103 m_point1.initialPosition = point1.lastPos();
104 m_point1.initialScreenPosition = point1.lastScreenPos();
105 m_point2.initialPosition = point2.lastPos();
106 m_point2.initialScreenPosition = point2.lastScreenPos();
108 ASSERT(m_state == GestureRecognized);
109 const qreal currentSpanDistance = QLineF(point1.screenPos(), point2.screenPos()).length();
110 const qreal initialSpanDistance = QLineF(m_point1.initialScreenPosition, m_point2.initialScreenPosition).length();
111 const qreal totalScaleFactor = currentSpanDistance / initialSpanDistance;
112 const QPointF touchCenterInPageViewCoordinate = computeTouchCenter(point1, point2);
113 m_viewportInteractionEngine->pinchGestureRequestUpdate(touchCenterInPageViewCoordinate, totalScaleFactor);
118 case QEvent::TouchEnd:
119 if (m_state == GestureRecognized) {
120 m_viewportInteractionEngine->pinchGestureEnded();
127 ASSERT_NOT_REACHED();
133 void QtPinchGestureRecognizer::reset()
135 QtGestureRecognizer::reset();
136 m_point1 = TouchPointInformation();
137 m_point2 = TouchPointInformation();
140 void QtPinchGestureRecognizer::initializeGesture(const QList<QTouchEvent::TouchPoint>& touchPoints)
142 ASSERT(!m_point1.isValid());
143 ASSERT(!m_point2.isValid());
145 m_state = GestureRecognitionStarted;
147 m_point1 = TouchPointInformation(touchPoints.at(0));
148 m_point2 = TouchPointInformation(touchPoints.at(1));
150 ASSERT(m_point1.isValid());
151 ASSERT(m_point2.isValid());