QtTapGestureRecognizer.cpp [plain text]
#include "config.h"
#include "QtTapGestureRecognizer.h"
#include "QtWebPageEventHandler.h"
#include <QLineF>
namespace WebKit {
static const int panDistanceThreshold = 10;
static const int maxDoubleTapDistance = 120;
static const int tapAndHoldTime = 1000;
static const int maxDoubleTapInterval = 500;
static const int highlightDelay = 100;
QtTapGestureRecognizer::QtTapGestureRecognizer(QtWebPageEventHandler* eventHandler)
: QtGestureRecognizer(eventHandler)
, m_candidate(SingleTapCandidate)
{
}
bool QtTapGestureRecognizer::withinDistance(const QTouchEvent::TouchPoint& touchPoint, int distance)
{
ASSERT(m_lastTouchPoint.id() != -1);
return QLineF(touchPoint.screenPos(), m_lastTouchPoint.screenPos()).length() < distance;
}
void QtTapGestureRecognizer::update(const QTouchEvent::TouchPoint& touchPoint)
{
switch (m_state) {
case NoGesture:
m_doubleTapTimer.stop(); m_state = GestureRecognitionStarted;
ASSERT(!m_tapAndHoldTimer.isActive());
m_tapAndHoldTimer.start(tapAndHoldTime, this);
if (m_candidate == DoubleTapCandidate && withinDistance(touchPoint, maxDoubleTapDistance))
return;
m_candidate = SingleTapCandidate;
m_lastTouchPoint = touchPoint;
m_highlightTimer.start(highlightDelay, this);
m_doubleTapTimer.start(maxDoubleTapInterval, this);
break;
case GestureRecognitionStarted:
if (!withinDistance(touchPoint, panDistanceThreshold))
reset();
break;
default:
ASSERT_NOT_REACHED();
break;
}
}
void QtTapGestureRecognizer::finish(const QTouchEvent::TouchPoint& touchPoint)
{
ASSERT(m_eventHandler);
m_tapAndHoldTimer.stop();
if (m_state == NoGesture)
return;
m_state = NoGesture;
if (m_candidate == SingleTapCandidate) {
if (m_doubleTapTimer.isActive()) {
m_candidate = DoubleTapCandidate;
m_lastTouchPoint = touchPoint;
return;
}
m_eventHandler->handleSingleTapEvent(touchPoint);
} else m_eventHandler->handleDoubleTapEvent(touchPoint);
reset();
}
void QtTapGestureRecognizer::cancel()
{
if (m_lastTouchPoint.id() == -1)
return;
reset();
}
void QtTapGestureRecognizer::highlightTimeout()
{
m_highlightTimer.stop();
if (m_lastTouchPoint.id() == -1)
return;
m_eventHandler->activateTapHighlight(m_lastTouchPoint);
}
void QtTapGestureRecognizer::singleTapTimeout()
{
m_doubleTapTimer.stop();
if (m_tapAndHoldTimer.isActive() || m_lastTouchPoint.id() == -1)
return;
m_eventHandler->handleSingleTapEvent(m_lastTouchPoint);
reset();
}
void QtTapGestureRecognizer::tapAndHoldTimeout()
{
m_tapAndHoldTimer.stop();
if (m_lastTouchPoint.id() == -1)
return;
#if 0 // No support for synthetic context menus in WK2 yet.
m_eventHandler->handleTapAndHoldEvent(m_lastTouchPoint);
#endif
reset();
}
void QtTapGestureRecognizer::reset()
{
m_eventHandler->deactivateTapHighlight();
m_candidate = SingleTapCandidate;
m_lastTouchPoint.setId(-1);
m_highlightTimer.stop();
m_doubleTapTimer.stop();
m_tapAndHoldTimer.stop();
QtGestureRecognizer::reset();
}
void QtTapGestureRecognizer::timerEvent(QTimerEvent* ev)
{
int timerId = ev->timerId();
if (timerId == m_highlightTimer.timerId())
highlightTimeout();
else if (timerId == m_doubleTapTimer.timerId())
singleTapTimeout();
else if (timerId == m_tapAndHoldTimer.timerId())
tapAndHoldTimeout();
else
QObject::timerEvent(ev);
}
}