tests/manual/gestures/scrollarea/mousepangesturerecognizer.cpp
changeset 3 41300fa6a67c
parent 0 1918ee327afb
child 4 3b1da2848fc7
equal deleted inserted replaced
2:56cd8111b7f7 3:41300fa6a67c
    47 
    47 
    48 MousePanGestureRecognizer::MousePanGestureRecognizer()
    48 MousePanGestureRecognizer::MousePanGestureRecognizer()
    49 {
    49 {
    50 }
    50 }
    51 
    51 
    52 QGesture* MousePanGestureRecognizer::createGesture(QObject *) const
    52 QGesture *MousePanGestureRecognizer::create(QObject *)
    53 {
    53 {
    54     return new QPanGesture;
    54     return new QPanGesture;
    55 }
    55 }
    56 
    56 
    57 QGestureRecognizer::Result MousePanGestureRecognizer::filterEvent(QGesture *state, QObject *, QEvent *event)
    57 QGestureRecognizer::Result MousePanGestureRecognizer::recognize(QGesture *state, QObject *, QEvent *event)
    58 {
    58 {
    59     QPanGesture *g = static_cast<QPanGesture *>(state);
    59     QPanGesture *g = static_cast<QPanGesture *>(state);
       
    60     if (event->type() == QEvent::TouchBegin) {
       
    61         // ignore the following mousepress event
       
    62         g->setProperty("ignoreMousePress", QVariant::fromValue<bool>(true));
       
    63     } else if (event->type() == QEvent::TouchEnd) {
       
    64         g->setProperty("ignoreMousePress", QVariant::fromValue<bool>(false));
       
    65     }
    60     QMouseEvent *me = static_cast<QMouseEvent *>(event);
    66     QMouseEvent *me = static_cast<QMouseEvent *>(event);
    61     if (event->type() == QEvent::MouseButtonPress || event->type() == QEvent::MouseButtonDblClick) {
    67     if (event->type() == QEvent::MouseButtonPress) {
       
    68         if (g->property("ignoreMousePress").toBool())
       
    69             return QGestureRecognizer::Ignore;
    62         g->setHotSpot(me->globalPos());
    70         g->setHotSpot(me->globalPos());
    63         g->setProperty("lastPos", me->globalPos());
    71         g->setProperty("startPos", me->globalPos());
    64         g->setProperty("pressed", QVariant::fromValue<bool>(true));
    72         g->setProperty("pressed", QVariant::fromValue<bool>(true));
    65         return QGestureRecognizer::GestureTriggered | QGestureRecognizer::ConsumeEventHint;
    73         return QGestureRecognizer::TriggerGesture | QGestureRecognizer::ConsumeEventHint;
    66     } else if (event->type() == QEvent::MouseMove) {
    74     } else if (event->type() == QEvent::MouseMove) {
    67         if (g->property("pressed").toBool()) {
    75         if (g->property("pressed").toBool()) {
    68             QPoint pos = me->globalPos();
    76             QPoint offset = me->globalPos() - g->property("startPos").toPoint();
    69             QPoint lastPos = g->property("lastPos").toPoint();
       
    70             g->setLastOffset(g->offset());
    77             g->setLastOffset(g->offset());
    71             lastPos = pos - lastPos;
    78             g->setOffset(QPointF(offset.x(), offset.y()));
    72             g->setOffset(QPointF(lastPos.x(), lastPos.y()));
    79             return QGestureRecognizer::TriggerGesture | QGestureRecognizer::ConsumeEventHint;
    73             g->setTotalOffset(g->totalOffset() + QPointF(lastPos.x(), lastPos.y()));
       
    74             g->setProperty("lastPos", pos);
       
    75             return QGestureRecognizer::GestureTriggered | QGestureRecognizer::ConsumeEventHint;
       
    76         }
    80         }
    77         return QGestureRecognizer::NotGesture;
    81         return QGestureRecognizer::CancelGesture;
    78     } else if (event->type() == QEvent::MouseButtonRelease) {
    82     } else if (event->type() == QEvent::MouseButtonRelease) {
    79         return QGestureRecognizer::GestureFinished | QGestureRecognizer::ConsumeEventHint;
    83         if (g->property("pressed").toBool())
       
    84             return QGestureRecognizer::FinishGesture | QGestureRecognizer::ConsumeEventHint;
    80     }
    85     }
    81     return QGestureRecognizer::Ignore;
    86     return QGestureRecognizer::Ignore;
    82 }
    87 }
    83 
    88 
    84 void MousePanGestureRecognizer::reset(QGesture *state)
    89 void MousePanGestureRecognizer::reset(QGesture *state)
    85 {
    90 {
    86     QPanGesture *g = static_cast<QPanGesture *>(state);
    91     QPanGesture *g = static_cast<QPanGesture *>(state);
    87     g->setTotalOffset(QPointF());
       
    88     g->setLastOffset(QPointF());
    92     g->setLastOffset(QPointF());
    89     g->setOffset(QPointF());
    93     g->setOffset(QPointF());
    90     g->setAcceleration(0);
    94     g->setAcceleration(0);
    91     g->setProperty("lastPos", QVariant());
    95     g->setProperty("startPos", QVariant());
    92     g->setProperty("pressed", QVariant::fromValue<bool>(false));
    96     g->setProperty("pressed", QVariant::fromValue<bool>(false));
       
    97     g->setProperty("ignoreMousePress", QVariant::fromValue<bool>(false));
    93     QGestureRecognizer::reset(state);
    98     QGestureRecognizer::reset(state);
    94 }
    99 }