src/hbcore/gestures/hbswipegesturelogic_p.cpp
changeset 2 06ff229162e9
parent 1 f7ac710697a9
child 3 11d3954df52a
equal deleted inserted replaced
1:f7ac710697a9 2:06ff229162e9
    21 ** If you have questions regarding the use of this file, please contact
    21 ** If you have questions regarding the use of this file, please contact
    22 ** Nokia at developer.feedback@nokia.com.
    22 ** Nokia at developer.feedback@nokia.com.
    23 **
    23 **
    24 ****************************************************************************/
    24 ****************************************************************************/
    25 
    25 
       
    26 #include "hbswipegesture.h"
       
    27 #include "hbswipegesture_p.h"
       
    28 #include "hbswipegesturelogic_p.h"
       
    29 #include "hbpointrecorder_p.h"
       
    30 #include "hbvelocitycalculator_p.h"
       
    31 
    26 #include <QEvent>
    32 #include <QEvent>
    27 #include <QGestureRecognizer>
    33 #include <QGestureRecognizer>
    28 #include <QGraphicsView>
    34 #include <QGraphicsView>
    29 #include <QMouseEvent>
    35 #include <QMouseEvent>
    30 
    36 
    31 #include "hbswipegesture.h"
       
    32 #include "hbswipegesture_p.h"
       
    33 #include "hbswipegesturelogic_p.h"
       
    34 
       
    35 /*!
    37 /*!
    36    @hbcore
    38    @hbcore
    37    \internal
    39    \internal
    38    \class HbSwipeGestureLogic
    40    \class HbSwipeGestureLogic
    39 
    41 
    98 {
   100 {
    99     // Just ignore situations that are not interesting at all.
   101     // Just ignore situations that are not interesting at all.
   100     if (!(gestureState == Qt::NoGesture && me->button() == Qt::LeftButton)) {
   102     if (!(gestureState == Qt::NoGesture && me->button() == Qt::LeftButton)) {
   101         return QGestureRecognizer::Ignore;
   103         return QGestureRecognizer::Ignore;
   102     }
   104     }
   103     gesture->d_func()->mStartTime = QTime::currentTime();  
   105     gesture->d_func()->mStartTime = mCurrentTime;
   104 
   106 
   105     gesture->d_func()->mStartPos = me->globalPos();
   107     gesture->d_func()->mStartPos = me->globalPos();
   106     gesture->d_func()->mSceneStartPos = HbGestureUtils::mapToScene(watched, me->globalPos());
   108     gesture->d_func()->mSceneStartPos = HbGestureUtils::mapToScene(watched, me->globalPos());
   107 
   109 
   108     gesture->setHotSpot(me->globalPos());
   110     gesture->setHotSpot(me->globalPos());
       
   111     gesture->d_ptr->mAxisX.clear();
       
   112     gesture->d_ptr->mAxisY.clear();
       
   113     gesture->d_ptr->mAxisX.record(me->globalPos().x(), mCurrentTime);
       
   114     gesture->d_ptr->mAxisY.record(me->globalPos().y(), mCurrentTime);
       
   115 
       
   116     return QGestureRecognizer::MayBeGesture;
       
   117 }
       
   118 
       
   119 /*!
       
   120     \internal
       
   121     \brief
       
   122     \return
       
   123 
       
   124 */
       
   125 QGestureRecognizer::Result HbSwipeGestureLogic::handleMouseMove(
       
   126         Qt::GestureState gestureState,
       
   127         HbSwipeGesture *gesture,
       
   128         QObject *watched,
       
   129         QMouseEvent *me )
       
   130 {
       
   131     Q_UNUSED(watched);
       
   132     Q_UNUSED(gestureState);
       
   133 
       
   134     if (!me->buttons().testFlag(Qt::LeftButton)){
       
   135         return QGestureRecognizer::Ignore;
       
   136     }
       
   137 
       
   138     gesture->d_ptr->mAxisX.record(me->globalPos().x(), mCurrentTime);
       
   139     gesture->d_ptr->mAxisY.record(me->globalPos().y(), mCurrentTime);
   109 
   140 
   110     return QGestureRecognizer::MayBeGesture;
   141     return QGestureRecognizer::MayBeGesture;
   111 }
   142 }
   112 
   143 
   113 /*!
   144 /*!
   124 {   
   155 {   
   125     Q_UNUSED(gesture);
   156     Q_UNUSED(gesture);
   126     Q_UNUSED(watched);
   157     Q_UNUSED(watched);
   127     Q_UNUSED(gestureState);
   158     Q_UNUSED(gestureState);
   128 
   159 
       
   160     if (me->button() != Qt::LeftButton) {
       
   161         return QGestureRecognizer::Ignore;
       
   162     }
       
   163 
   129     QPointF totalOffset = me->globalPos() - gesture->d_func()->mStartPos.toPoint();
   164     QPointF totalOffset = me->globalPos() - gesture->d_func()->mStartPos.toPoint();
   130 
   165 
   131     QPointF velocity = totalOffset / gesture->d_func()->mStartTime.elapsed();
   166     int deltaTime = gesture->d_func()->mStartTime.msecsTo(mCurrentTime);
       
   167     QPointF velocity = deltaTime != 0 ? totalOffset / deltaTime : QPointF(0,0);
   132 
   168 
   133     gesture->setSwipeAngle(QLineF(gesture->d_func()->mStartPos, me->globalPos()).angle());
   169     gesture->setSwipeAngle(QLineF(gesture->d_func()->mStartPos, me->globalPos()).angle());
   134     gesture->setSceneSwipeAngle(QLineF(gesture->d_func()->mSceneStartPos, HbGestureUtils::mapToScene(watched, me->globalPos())).angle());
   170     gesture->setSceneSwipeAngle(QLineF(gesture->d_func()->mSceneStartPos, HbGestureUtils::mapToScene(watched, me->globalPos())).angle());
   135 
   171 
   136     if (totalOffset.manhattanLength() >= HbSwipeMinOffset && velocity.manhattanLength() >= HbSwipeMinSpeed && me->button() == Qt::LeftButton) {
   172     bool movedEnough = totalOffset.manhattanLength() >= HbSwipeMinOffset;
       
   173     bool fastEnough = velocity.manhattanLength() >= HbSwipeMinSpeed;
       
   174     bool notStoppedAtEnd = HbVelocityCalculator(gesture->d_ptr->mAxisX, gesture->d_ptr->mAxisY).velocity(mCurrentTime) != QPointF(0,0);
       
   175 
       
   176     if (movedEnough && fastEnough && notStoppedAtEnd) {
   137         return QGestureRecognizer::FinishGesture;
   177         return QGestureRecognizer::FinishGesture;
   138     } else {
   178     } else {
   139         return QGestureRecognizer::Ignore;
   179         return QGestureRecognizer::Ignore;
   140     }
   180     }
   141 }
   181 }
   148 */
   188 */
   149 QGestureRecognizer::Result HbSwipeGestureLogic::recognize(
   189 QGestureRecognizer::Result HbSwipeGestureLogic::recognize(
   150         Qt::GestureState gestureState,
   190         Qt::GestureState gestureState,
   151         HbSwipeGesture *gesture,
   191         HbSwipeGesture *gesture,
   152         QObject *watched,
   192         QObject *watched,
   153         QEvent *event )
   193         QEvent *event,
       
   194         QTime currentTime)
   154 {
   195 {
   155     // Record the time right away.
   196     // Record the time right away.
   156     mCurrentTime = QTime::currentTime();
   197     mCurrentTime = currentTime;
   157     
   198     
   158     if ( isMouseEvent(event->type()) )
   199     if ( isMouseEvent(event->type()) )
   159     {
   200     {
   160         QMouseEvent* me = static_cast<QMouseEvent*>(event);
   201         QMouseEvent* me = static_cast<QMouseEvent*>(event);
   161         switch(event->type())
   202         switch(event->type())
   163         case QEvent::MouseButtonDblClick:
   204         case QEvent::MouseButtonDblClick:
   164         case QEvent::MouseButtonPress:
   205         case QEvent::MouseButtonPress:
   165             return handleMousePress(gestureState, gesture, watched, me);
   206             return handleMousePress(gestureState, gesture, watched, me);
   166 
   207 
   167         case QEvent::MouseMove:
   208         case QEvent::MouseMove:
   168             if (me->buttons().testFlag(Qt::LeftButton))
   209             return handleMouseMove(gestureState, gesture, watched, me);
   169                 return QGestureRecognizer::MayBeGesture;
   210 
   170             else
       
   171                 return QGestureRecognizer::Ignore;
       
   172         case QEvent::MouseButtonRelease:
   211         case QEvent::MouseButtonRelease:
   173             return handleMouseRelease(gestureState, gesture, watched, me);
   212             return handleMouseRelease(gestureState, gesture, watched, me);
   174 
   213 
   175         default: break;
   214         default: break;
   176         }
   215         }