QtTapGestureRecognizer.cpp [plain text]
#include "config.h"
#include "QtTapGestureRecognizer.h"
#include "QtWebPageEventHandler.h"
#include <QLineF>
#include <QTouchEvent>
namespace WebKit {
QtTapGestureRecognizer::QtTapGestureRecognizer(QtWebPageEventHandler* eventHandler)
: QtGestureRecognizer(eventHandler)
, m_candidate(Invalid)
{
}
bool QtTapGestureRecognizer::withinDistance(const QTouchEvent::TouchPoint& touchPoint, int distance)
{
return QLineF(touchPoint.screenPos(), m_lastTouchPoint.screenPos()).length() < distance;
}
bool QtTapGestureRecognizer::update(QEvent::Type eventType, const QTouchEvent::TouchPoint& touchPoint)
{
ASSERT(m_eventHandler);
switch (eventType) {
case QEvent::TouchBegin:
m_doubleTapTimer.stop();
ASSERT(!m_tapAndHoldTimer.isActive());
m_tapAndHoldTimer.start(tapAndHoldTime, this);
if (m_lastTouchPoint.id() != -1 && withinDistance(touchPoint, maxDoubleTapDistance))
m_candidate = DoubleTapCandidate;
else {
m_candidate = SingleTapCandidate;
m_highlightTimer.start(highlightDelay, this);
m_lastTouchPoint = touchPoint;
m_doubleTapTimer.start(maxDoubleTapInterval, this);
}
break;
case QEvent::TouchUpdate:
if (m_candidate != Invalid && !withinDistance(touchPoint, maxPanDistance))
reset();
break;
case QEvent::TouchEnd:
m_tapAndHoldTimer.stop();
if (m_candidate == Invalid)
break;
if (m_candidate == DoubleTapCandidate) {
m_eventHandler->handlePotentialSingleTapEvent(QTouchEvent::TouchPoint());
m_eventHandler->handleDoubleTapEvent(touchPoint);
reset();
}
break;
default:
break;
}
return false;
}
void QtTapGestureRecognizer::cancel()
{
if (m_candidate == Invalid)
return;
reset();
}
void QtTapGestureRecognizer::highlightTimeout()
{
m_highlightTimer.stop();
if (m_candidate != SingleTapCandidate)
return;
ASSERT(m_lastTouchPoint.id() != -1);
m_eventHandler->handlePotentialSingleTapEvent(m_lastTouchPoint);
}
void QtTapGestureRecognizer::singleTapTimeout()
{
m_doubleTapTimer.stop();
if (m_tapAndHoldTimer.isActive())
return;
ASSERT(m_lastTouchPoint.id() != -1);
if (m_candidate == SingleTapCandidate) {
m_eventHandler->handlePotentialSingleTapEvent(QTouchEvent::TouchPoint());
m_eventHandler->handleSingleTapEvent(m_lastTouchPoint);
}
reset();
}
void QtTapGestureRecognizer::tapAndHoldTimeout()
{
m_tapAndHoldTimer.stop();
ASSERT(m_lastTouchPoint.id() != -1);
#if 0 // No support for synthetic context menus in WK2 yet.
m_eventHandler->handlePotentialSingleTapEvent(QTouchEvent::TouchPoint());
m_eventHandler->handleTapAndHoldEvent(m_lastTouchPoint);
#endif
reset();
}
void QtTapGestureRecognizer::reset()
{
if (m_candidate != Invalid)
m_eventHandler->handlePotentialSingleTapEvent(QTouchEvent::TouchPoint());
m_candidate = Invalid;
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);
}
}