2 * Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies)
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.
26 #include "QtTapGestureRecognizer.h"
28 #include "QtWebPageEventHandler.h"
30 #include <QTouchEvent>
34 QtTapGestureRecognizer::QtTapGestureRecognizer(QtWebPageEventHandler* eventHandler)
35 : QtGestureRecognizer(eventHandler)
40 bool QtTapGestureRecognizer::recognize(const QTouchEvent* event, qint64 eventTimestampMillis)
42 if (event->touchPoints().size() != 1) {
47 switch (event->type()) {
48 case QEvent::TouchBegin:
49 ASSERT(m_tapState == NoTap);
50 ASSERT(!m_tapAndHoldTimer.isActive());
52 m_tapAndHoldTimer.start(tapAndHoldTime, this);
54 if (m_doubleTapTimer.isActive()) {
55 // Might be double tap.
56 ASSERT(m_touchBeginEventForTap);
57 m_doubleTapTimer.stop();
58 QPointF lastPosition = m_touchBeginEventForTap->touchPoints().first().screenPos();
59 QPointF newPosition = event->touchPoints().first().screenPos();
60 if (QLineF(lastPosition, newPosition).length() < maxDoubleTapDistance)
61 m_tapState = DoubleTapCandidate;
63 // Received a new tap, that is unrelated to the previous one.
65 m_tapState = SingleTapStarted;
68 m_tapState = SingleTapStarted;
69 m_touchBeginEventForTap = adoptPtr(new QTouchEvent(*event));
71 if (m_tapState == SingleTapStarted) {
72 const QTouchEvent::TouchPoint& touchPoint = event->touchPoints().first();
73 m_eventHandler->handlePotentialSingleTapEvent(touchPoint);
76 case QEvent::TouchUpdate:
77 // If the touch point moves further than the threshold, we cancel the tap gesture.
78 if (m_tapState == SingleTapStarted) {
79 const QTouchEvent::TouchPoint& touchPoint = event->touchPoints().first();
80 QPointF offset(touchPoint.scenePos() - m_touchBeginEventForTap->touchPoints().first().scenePos());
81 const qreal distX = qAbs(offset.x());
82 const qreal distY = qAbs(offset.y());
83 if (distX > initialTriggerDistanceThreshold || distY > initialTriggerDistanceThreshold)
87 case QEvent::TouchEnd:
88 m_tapAndHoldTimer.stop();
89 m_eventHandler->handlePotentialSingleTapEvent(QTouchEvent::TouchPoint());
92 case DoubleTapCandidate:
94 ASSERT(!m_doubleTapTimer.isActive());
97 const QTouchEvent::TouchPoint& touchPoint = event->touchPoints().first();
98 QPointF startPosition = touchPoint.startScreenPos();
99 QPointF endPosition = touchPoint.screenPos();
100 if (QLineF(endPosition, startPosition).length() < maxDoubleTapDistance && m_eventHandler)
101 m_eventHandler->handleDoubleTapEvent(touchPoint);
104 case SingleTapStarted:
105 ASSERT(!m_doubleTapTimer.isActive());
106 m_doubleTapTimer.start(doubleClickInterval, this);
123 void QtTapGestureRecognizer::tapTimeout()
125 m_doubleTapTimer.stop();
126 m_eventHandler->handleSingleTapEvent(m_touchBeginEventForTap->touchPoints().at(0));
127 m_touchBeginEventForTap.clear();
130 void QtTapGestureRecognizer::tapAndHoldTimeout()
132 ASSERT(m_touchBeginEventForTap);
133 m_tapAndHoldTimer.stop();
134 #if 0 // No support for synthetic context menus in WK2 yet.
135 QTouchEvent::TouchPoint tapPoint = m_touchBeginEventForTap->touchPoints().at(0);
136 WebGestureEvent gesture(WebEvent::GestureTapAndHold, tapPoint.pos().toPoint(), tapPoint.screenPos().toPoint(), WebEvent::Modifiers(0), 0);
138 m_webPageProxy->handleGestureEvent(gesture);
140 m_touchBeginEventForTap.clear();
141 m_tapState = TapAndHold;
143 ASSERT(!m_doubleTapTimer.isActive());
144 m_doubleTapTimer.stop();
147 void QtTapGestureRecognizer::reset()
149 m_eventHandler->handlePotentialSingleTapEvent(QTouchEvent::TouchPoint());
152 m_touchBeginEventForTap.clear();
153 m_tapAndHoldTimer.stop();
155 QtGestureRecognizer::reset();
158 void QtTapGestureRecognizer::timerEvent(QTimerEvent* ev)
160 int timerId = ev->timerId();
161 if (timerId == m_doubleTapTimer.timerId())
163 else if (timerId == m_tapAndHoldTimer.timerId())
166 QObject::timerEvent(ev);
169 } // namespace WebKit