[Qt][WK2] Move non-api classes to WebKit namespace at WebKit2/UiProcess/qt
[WebKit-https.git] / 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 "QtViewportInteractionEngine.h"
30 #include "QtWebPageEventHandler.h"
31 #include <QtCore/QLineF>
32
33 namespace WebKit {
34
35 const qreal pinchInitialTriggerDistanceThreshold = 5.;
36
37 static inline QPointF computePinchCenter(const QTouchEvent::TouchPoint& point1, const QTouchEvent::TouchPoint& point2)
38 {
39     return (point1.pos() + point2.pos()) / 2.0f;
40 }
41
42 QtPinchGestureRecognizer::QtPinchGestureRecognizer(QtWebPageEventHandler* eventHandler)
43     : QtGestureRecognizer(eventHandler)
44 {
45     reset();
46 }
47
48 bool QtPinchGestureRecognizer::update(const QTouchEvent::TouchPoint& point1, const QTouchEvent::TouchPoint& point2)
49 {
50     ASSERT(interactionEngine());
51     const qreal currentFingerDistance = QLineF(point1.screenPos(), point2.screenPos()).length();
52     switch (m_state) {
53     case NoGesture:
54         m_initialFingerDistance = currentFingerDistance;
55         m_state = GestureRecognitionStarted;
56         return false;
57     case GestureRecognitionStarted: {
58         const qreal pinchDistance = qAbs(currentFingerDistance - m_initialFingerDistance);
59         if (pinchDistance < pinchInitialTriggerDistanceThreshold)
60             return false;
61         m_state = GestureRecognized;
62         interactionEngine()->pinchGestureStarted(computePinchCenter(point1, point2));
63
64         // We reset the initial span distance to the current distance of the
65         // touch points in order to avoid the jump caused by the events which
66         // were skipped between the recognition start and the actual recognition.
67         m_initialFingerDistance = currentFingerDistance;
68
69         // fall through
70     }
71     case GestureRecognized:
72         const qreal totalScaleFactor = currentFingerDistance / m_initialFingerDistance;
73         const QPointF touchCenterInViewCoordinates = computePinchCenter(point1, point2);
74         interactionEngine()->pinchGestureRequestUpdate(touchCenterInViewCoordinates, totalScaleFactor);
75         return true;
76         break;
77     }
78
79     ASSERT_NOT_REACHED();
80     return false;
81 }
82
83 void QtPinchGestureRecognizer::finish()
84 {
85     if (m_state == NoGesture)
86         return;
87
88     ASSERT(interactionEngine());
89     interactionEngine()->pinchGestureEnded();
90     reset();
91 }
92
93 void QtPinchGestureRecognizer::cancel()
94 {
95     if (m_state == NoGesture)
96         return;
97
98     ASSERT(interactionEngine());
99     interactionEngine()->pinchGestureCancelled();
100     reset();
101 }
102
103 } // namespace WebKit
104