diff options
author | Zeno Albisser <zeno.albisser@nokia.com> | 2010-09-07 11:21:41 -0400 |
---|---|---|
committer | Zeno Albisser <zeno.albisser@nokia.com> | 2010-09-08 09:09:03 -0400 |
commit | 9d1881a807a1e1f393d0374b71555601970b59a2 (patch) | |
tree | 59d18d879afe2c1ee4f51949a2e3411bfffd762c | |
parent | 4a9a0c09f3cfb9262f929620fd86609a9b1b3a76 (diff) |
avoid using private headers in QPanGestureRecognizer
Reviewed-by: Denis Dzyubenko
-rw-r--r-- | plugin.cpp | 8 | ||||
-rw-r--r-- | qdeclarativegesturerecognizers.cpp | 144 |
2 files changed, 90 insertions, 62 deletions
@@ -75,11 +75,11 @@ void GestureAreaQmlPlugin::registerTypes(const char *uri) QGestureRecognizer::unregisterRecognizer(Qt::PinchGesture); QGestureRecognizer::unregisterRecognizer(Qt::SwipeGesture); - QGestureRecognizer::registerRecognizer(new QTapGestureRecognizer()); - QGestureRecognizer::registerRecognizer(new QTapAndHoldGestureRecognizer()); +// QGestureRecognizer::registerRecognizer(new QTapGestureRecognizer()); +// QGestureRecognizer::registerRecognizer(new QTapAndHoldGestureRecognizer()); QGestureRecognizer::registerRecognizer(new QPanGestureRecognizer()); - QGestureRecognizer::registerRecognizer(new QPinchGestureRecognizer()); - QGestureRecognizer::registerRecognizer(new QSwipeGestureRecognizer()); +// QGestureRecognizer::registerRecognizer(new QPinchGestureRecognizer()); +// QGestureRecognizer::registerRecognizer(new QSwipeGestureRecognizer()); #endif } diff --git a/qdeclarativegesturerecognizers.cpp b/qdeclarativegesturerecognizers.cpp index a7d260a..2a1a5aa 100644 --- a/qdeclarativegesturerecognizers.cpp +++ b/qdeclarativegesturerecognizers.cpp @@ -45,6 +45,7 @@ #include "qwidget.h" #include "qabstractscrollarea.h" #include <qgraphicssceneevent.h> +#include <QtCore/QElapsedTimer> #include "qdebug.h" #include "qmath.h" @@ -53,6 +54,8 @@ QT_BEGIN_NAMESPACE +Q_DECLARE_METATYPE(QElapsedTimer) + static const int PanGestureMovementThreshold = 30; QPanGestureRecognizer::QPanGestureRecognizer() @@ -80,16 +83,19 @@ QGestureRecognizer::Result QPanGestureRecognizer::recognize(QGesture *state, QEvent *event) { QPanGesture *q = static_cast<QPanGesture *>(state); - QPanGesturePrivate *d = q->d_func(); QGestureRecognizer::Result result; switch (event->type()) { case QEvent::TouchBegin: { const QTouchEvent *ev = static_cast<const QTouchEvent *>(event); - d->gotTouched = true; + q->setProperty("gotTouched", true); QTouchEvent::TouchPoint p = ev->touchPoints().at(0); - d->lastOffset = d->offset = QPointF(); - d->pressTime.start(); + q->setOffset(QPointF()); + q->setLastOffset(QPointF()); + QElapsedTimer pressTime; + pressTime.start(); + q->setProperty("pressTime", QVariant::fromValue(pressTime)); + result = QGestureRecognizer::MayBeGesture; break; } @@ -98,15 +104,15 @@ QGestureRecognizer::Result QPanGestureRecognizer::recognize(QGesture *state, if (q->state() != Qt::NoGesture) { if (ev->deviceType() == QTouchEvent::TouchScreen && ev->touchPoints().size() == 1) { QTouchEvent::TouchPoint p1 = ev->touchPoints().at(0); - d->lastOffset = d->offset; - d->offset = QPointF(p1.pos().x() - p1.startPos().x(), p1.pos().y() - p1.startPos().y()); + q->setLastOffset(q->offset()); + q->setOffset(QPointF(p1.pos().x() - p1.startPos().x(), p1.pos().y() - p1.startPos().y())); } else if (ev->deviceType() == QTouchEvent::TouchPad && ev->touchPoints().size() == 2) { QTouchEvent::TouchPoint p1 = ev->touchPoints().at(0); QTouchEvent::TouchPoint p2 = ev->touchPoints().at(1); - d->lastOffset = d->offset; - d->offset = - QPointF(p1.pos().x() - p1.startPos().x() + p2.pos().x() - p2.startPos().x(), - p1.pos().y() - p1.startPos().y() + p2.pos().y() - p2.startPos().y()) / 2; + q->setLastOffset(q->offset()); + q->setOffset(QPointF(p1.pos().x() - p1.startPos().x() + p2.pos().x() - p2.startPos().x(), + p1.pos().y() - p1.startPos().y() + p2.pos().y() - p2.startPos().y()) / 2); + } result = QGestureRecognizer::FinishGesture; } else { @@ -121,20 +127,20 @@ QGestureRecognizer::Result QPanGestureRecognizer::recognize(QGesture *state, if (ev->deviceType() == QTouchEvent::TouchScreen && ev->touchPoints().size() == 1) { deviceAndTouchPointsOK = true; p1 = ev->touchPoints().at(0); - d->lastOffset = d->offset; - d->offset = QPointF(p1.pos().x() - p1.startPos().x(), p1.pos().y() - p1.startPos().y()); + q->setLastOffset(q->offset()); + q->setOffset(QPointF(p1.pos().x() - p1.startPos().x(), p1.pos().y() - p1.startPos().y())); } else if (ev->deviceType() == QTouchEvent::TouchPad && ev->touchPoints().size() >= 2) { deviceAndTouchPointsOK = true; p1 = ev->touchPoints().at(0); QTouchEvent::TouchPoint p2 = ev->touchPoints().at(1); - d->lastOffset = d->offset; - d->offset = - QPointF(p1.pos().x() - p1.startPos().x() + p2.pos().x() - p2.startPos().x(), - p1.pos().y() - p1.startPos().y() + p2.pos().y() - p2.startPos().y()) / 2; + q->setLastOffset(q->offset()); + q->setOffset(QPointF(p1.pos().x() - p1.startPos().x() + p2.pos().x() - p2.startPos().x(), + p1.pos().y() - p1.startPos().y() + p2.pos().y() - p2.startPos().y()) / 2); } + QPointF offset = q->offset(); if (deviceAndTouchPointsOK && - (d->offset.x() > 10 || d->offset.y() > 10 || - d->offset.x() < -10 || d->offset.y() < -10)) { + (offset.x() > 10 || offset.y() > 10 || + offset.x() < -10 || offset.y() < -10)) { q->setHotSpot(p1.startScreenPos()); result = QGestureRecognizer::TriggerGesture; } else { @@ -144,40 +150,55 @@ QGestureRecognizer::Result QPanGestureRecognizer::recognize(QGesture *state, } case QEvent::MouseButtonPress: - if (!d->gotTouched) { + if (!q->property("gotTouched").toBool()) { QMouseEvent *ev = static_cast<QMouseEvent *>(event); if (ev->buttons() == Qt::LeftButton && !(ev->modifiers() & Qt::ControlModifier)) { - d->startPosition = d->lastPos = ev->globalPos(); - state->setHotSpot(d->startPosition); + QPoint globalPos = ev->globalPos(); + q->setProperty("startPosition", globalPos); + q->setProperty("lastPos", globalPos); + state->setHotSpot(globalPos); state->setHandled(false); - d->pressTime.start(); - d->lastPosTime.start(); + + QElapsedTimer pressTime; + pressTime.start(); + q->setProperty("pressTime",QVariant::fromValue(pressTime)); + q->setProperty("lastPosTime",QVariant::fromValue(pressTime)); + ev->accept(); } } return QGestureRecognizer::MayBeGesture; case QEvent::MouseMove: - if (!d->gotTouched) { + if (!q->property("gotTouched").toBool()) { QMouseEvent *ev = static_cast<QMouseEvent *>(event); - if (!d->startPosition.isNull()) { - QPoint delta = ev->globalPos() - d->startPosition; + QPoint startPosition = q->property("startPosition").toPoint(); + if (!startPosition.isNull()) { + QPoint delta = ev->globalPos() - startPosition; if (state->state() != Qt::GestureStarted || state->state() != Qt::GestureUpdated) { - if (delta.manhattanLength() > PanGestureMovementThreshold || d->pressTime.elapsed() > 200) { + if (delta.manhattanLength() > PanGestureMovementThreshold || q->property("pressTime").value<QElapsedTimer>().elapsed() > 200) { q->setLastOffset(q->offset()); q->setOffset(delta); - qreal elapsed = qreal(d->lastPosTime.restart()) / 1000.; + QPoint lastPos = q->property("lastPos").toPoint(); + QElapsedTimer lastPosTime = q->property("lastPosTime").value<QElapsedTimer>(); + qreal xVelocity = q->horizontalVelocity(); + qreal yVelocity = q->verticalVelocity(); + + qreal elapsed = qreal(lastPosTime.restart()) / 1000.; + q->setProperty("lastPosTime", QVariant::fromValue(lastPosTime)); if (elapsed <= 0) elapsed = 1; - int dx = ev->globalPos().x() - d->lastPos.x(); - d->xVelocity += dx / elapsed; - d->xVelocity /= 2; - int dy = ev->globalPos().y() - d->lastPos.y(); - d->yVelocity += dy / elapsed; - d->yVelocity /= 2; - - d->lastPos = ev->globalPos(); + int dx = ev->globalPos().x() - lastPos.x(); + xVelocity += dx / elapsed; + xVelocity /= 2; + q->setHorizontalVelocity(xVelocity); + int dy = ev->globalPos().y() - lastPos.y(); + yVelocity += dy / elapsed; + yVelocity /= 2; + q->setVerticalVelocity(yVelocity); + + q->setProperty("lastPos",ev->globalPos()); return QGestureRecognizer::TriggerGesture; } } @@ -186,12 +207,13 @@ QGestureRecognizer::Result QPanGestureRecognizer::recognize(QGesture *state, return QGestureRecognizer::Ignore; case QEvent::MouseButtonRelease: - if (!d->gotTouched) { + if (!q->property("gotTouched").toBool()) { if(q->state() != Qt::NoGesture) { QMouseEvent *ev = static_cast<QMouseEvent *>(event); - if (d->lastPosTime.elapsed() > 100) { + if (q->property("lastPosTime").value<QElapsedTimer>().elapsed() > 100) { // if we drag then pause before release we should not cause a flick. - d->xVelocity = d->yVelocity = 0; + q->setHorizontalVelocity(0); + q->setVerticalVelocity(0); } else { // FlickThreshold determines how far the "mouse" must have moved // before we perform a flick. @@ -200,16 +222,18 @@ QGestureRecognizer::Result QPanGestureRecognizer::recognize(QGesture *state, // Really slow flicks can be annoying. static const int minimumFlickVelocity = 200; - if (qAbs(d->xVelocity) > 10 && qAbs(ev->globalPos().x() - d->startPosition.x()) > FlickThreshold) { - if (qAbs(d->xVelocity) < minimumFlickVelocity) - d->xVelocity = d->xVelocity < 0 ? -minimumFlickVelocity : minimumFlickVelocity; + qreal xVelocity = q->horizontalVelocity(); + if (qAbs(xVelocity) > 10 && qAbs(ev->globalPos().x() - q->property("startPosition").toPoint().x()) > FlickThreshold) { + if (qAbs(xVelocity) < minimumFlickVelocity) + q->setHorizontalVelocity(xVelocity < 0 ? -minimumFlickVelocity : minimumFlickVelocity); } - if (qAbs(d->yVelocity) > 10 && qAbs(ev->globalPos().y() - d->startPosition.y()) > FlickThreshold) { - if (qAbs(d->yVelocity) < minimumFlickVelocity) - d->yVelocity = d->yVelocity < 0 ? -minimumFlickVelocity : minimumFlickVelocity; + qreal yVelocity = q->verticalVelocity(); + if (qAbs(yVelocity) > 10 && qAbs(ev->globalPos().y() - q->property("startPosition").toPoint().y()) > FlickThreshold) { + if (qAbs(yVelocity) < minimumFlickVelocity) + q->setVerticalVelocity(yVelocity < 0 ? -minimumFlickVelocity : minimumFlickVelocity); } } - if (!d->startPosition.isNull() && !q->lastOffset().isNull()) { + if (!q->property("startPosition").toPoint().isNull() && !q->lastOffset().isNull()) { return QGestureRecognizer::FinishGesture; } } @@ -228,16 +252,20 @@ void QPanGestureRecognizer::reset(QGesture *state) if (!state) return; QPanGesture *pan = static_cast<QPanGesture*>(state); - QPanGesturePrivate *d = pan->d_func(); - - d->startPosition = QPoint(); - d->lastOffset = d->offset = QPointF(); - d->acceleration = 0; - d->gotTouched = false; - d->pressTime.invalidate(); - d->lastPosTime.invalidate(); - d->lastPos = QPoint(); - d->xVelocity = d->yVelocity = 0; + pan->setProperty("startPosition", QPoint()); + pan->setLastOffset(QPointF()); + pan->setOffset(QPointF()); + pan->setAcceleration(0); + pan->setProperty("gotTouched",false); + QElapsedTimer pressTime = pan->property("pressTime").value<QElapsedTimer>(); + pressTime.invalidate(); + pan->setProperty("pressTime",QVariant::fromValue(pressTime)); + QElapsedTimer lastPosTime = pan->property("lastPosTime").value<QElapsedTimer>(); + lastPosTime.invalidate(); + pan->setProperty("lastPosTime", QVariant::fromValue(lastPosTime)); + pan->setProperty("lastPos",QPoint()); + pan->setHorizontalVelocity(0); + pan->setVerticalVelocity(0); QGestureRecognizer::reset(state); } @@ -246,7 +274,7 @@ void QPanGestureRecognizer::reset(QGesture *state) // // QPinchGestureRecognizer // - +/* QPinchGestureRecognizer::QPinchGestureRecognizer() { } @@ -867,7 +895,7 @@ void QTapAndHoldGestureRecognizer::reset(QGesture *state) d->timerId = 0; QGestureRecognizer::reset(state); -} +}*/ QT_END_NAMESPACE |