initial import
[vuplus_webkit] / Source / WebKit2 / UIProcess / qt / QtPinchGestureRecognizer.cpp
1 /*
2  * Copyright (C) 2011 Benjamin Poulain <benjamin@webkit.org>
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
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.
12  *
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.
24  */
25
26 #include "config.h"
27 #include "QtPinchGestureRecognizer.h"
28
29 #include "ViewportInteractionEngine.h"
30 #include <QtCore/QLineF>
31
32 namespace WebKit {
33
34 const qreal pinchInitialTriggerDistanceThreshold = 5.;
35
36 static inline int findTouchPointIndex(const QList<QTouchEvent::TouchPoint>& touchPoints, const QtPinchGestureRecognizer::TouchPointInformation& pointInformation)
37 {
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)
42             return i;
43     }
44     return -1;
45 }
46
47 static inline QPointF computeTouchCenter(const QTouchEvent::TouchPoint& point1, const QTouchEvent::TouchPoint& point2)
48 {
49     return (point1.pos() + point2.pos()) / 2.0f;
50 }
51
52 QtPinchGestureRecognizer::QtPinchGestureRecognizer(ViewportInteractionEngine* interactionEngine)
53     : QtGestureRecognizer(interactionEngine)
54 {
55     reset();
56 }
57
58 bool QtPinchGestureRecognizer::recognize(const QTouchEvent* event)
59 {
60     const QList<QTouchEvent::TouchPoint>& touchPoints = event->touchPoints();
61     if (touchPoints.size() < 2) {
62         if (m_state == GestureRecognized)
63             m_viewportInteractionEngine->pinchGestureEnded();
64         reset();
65         return false;
66     }
67
68     switch (event->type()) {
69     case QEvent::TouchBegin:
70     case QEvent::TouchUpdate:
71         switch (m_state) {
72         case NoGesture:
73             initializeGesture(touchPoints);
74             return false;
75         case GestureRecognitionStarted:
76         case GestureRecognized:
77             ASSERT(m_point1.isValid());
78             ASSERT(m_point2.isValid());
79
80             const int point1Index = findTouchPointIndex(touchPoints, m_point1);
81             if (point1Index < 0) {
82                 reset();
83                 return false;
84             }
85             const int point2Index = findTouchPointIndex(touchPoints, m_point2);
86             if (point2Index < 0) {
87                 reset();
88                 return false;
89             }
90
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)
97                     return false;
98                 m_state = GestureRecognized;
99                 m_viewportInteractionEngine->pinchGestureStarted();
100
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();
107             }
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);
114             return true;
115             break;
116         }
117         break;
118     case QEvent::TouchEnd:
119         if (m_state == GestureRecognized) {
120             m_viewportInteractionEngine->pinchGestureEnded();
121             reset();
122             return true;
123         }
124         reset();
125         break;
126     default:
127         ASSERT_NOT_REACHED();
128     }
129
130     return false;
131 }
132
133 void QtPinchGestureRecognizer::reset()
134 {
135     QtGestureRecognizer::reset();
136     m_point1 = TouchPointInformation();
137     m_point2 = TouchPointInformation();
138 }
139
140 void QtPinchGestureRecognizer::initializeGesture(const QList<QTouchEvent::TouchPoint>& touchPoints)
141 {
142     ASSERT(!m_point1.isValid());
143     ASSERT(!m_point2.isValid());
144
145     m_state = GestureRecognitionStarted;
146
147     m_point1 = TouchPointInformation(touchPoints.at(0));
148     m_point2 = TouchPointInformation(touchPoints.at(1));
149
150     ASSERT(m_point1.isValid());
151     ASSERT(m_point2.isValid());
152 }
153
154 }