src/hbcore/gestures/hbswipegesturelogic_p.cpp
changeset 30 80e4d18b72f5
parent 23 e6ad4ef83b23
equal deleted inserted replaced
28:b7da29130b0e 30:80e4d18b72f5
    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 "hbswipegesturelogic_p.h"
    26 #include "hbgestures_p.h"
    28 #include "hbgestures_p.h"
    27 #include "hbswipegesture.h"
    29 
    28 #include "hbswipegesture_p.h"
       
    29 #include "hbswipegesturelogic_p.h"
       
    30 #include "hbpointrecorder_p.h"
    30 #include "hbpointrecorder_p.h"
    31 #include "hbvelocitycalculator_p.h"
    31 #include "hbvelocitycalculator_p.h"
    32 
    32 
    33 #include <hbdeviceprofile.h>
    33 #include <hbdeviceprofile.h>
    34 
    34 
    53     \return
    53     \return
    54 
    54 
    55 */
    55 */
    56 HbSwipeGestureLogic::HbSwipeGestureLogic()
    56 HbSwipeGestureLogic::HbSwipeGestureLogic()
    57 {
    57 {
    58     mCurrentTime = QTime();
       
    59 }
    58 }
    60 
    59 
    61 HbSwipeGestureLogic::~HbSwipeGestureLogic() {}
    60 HbSwipeGestureLogic::~HbSwipeGestureLogic() {}
    62 
    61 
    63 /*!
    62 /*!
    97 */
    96 */
    98 QGestureRecognizer::Result HbSwipeGestureLogic::handleMousePress(
    97 QGestureRecognizer::Result HbSwipeGestureLogic::handleMousePress(
    99         Qt::GestureState gestureState,
    98         Qt::GestureState gestureState,
   100         HbSwipeGesture *gesture,
    99         HbSwipeGesture *gesture,
   101         QObject *watched,
   100         QObject *watched,
   102         QMouseEvent *me )
   101         QMouseEvent *me,
       
   102         qint64 currentTime)
   103 {
   103 {
   104     // Just ignore situations that are not interesting at all.
   104     // Just ignore situations that are not interesting at all.
   105     if (!(gestureState == Qt::NoGesture && me->button() == Qt::LeftButton)) {
   105     if (!(gestureState == Qt::NoGesture && me->button() == Qt::LeftButton)) {
   106         return QGestureRecognizer::Ignore;
   106         return QGestureRecognizer::Ignore;
   107     }
   107     }
   108     gesture->d_func()->mStartTime = mCurrentTime;
   108     gesture->d_func()->mStartTime = currentTime;
   109 
   109 
   110     gesture->d_func()->mStartPos = me->globalPos();
   110     gesture->d_func()->mStartPos = me->globalPos();
   111     gesture->d_func()->mSceneStartPos = HbGestureUtils::mapToScene(watched, me->globalPos());
   111     gesture->d_func()->mSceneStartPos = HbGestureUtils::mapToScene(watched, me->globalPos());
   112 
   112 
   113     gesture->setHotSpot(me->globalPos());
   113     gesture->setHotSpot(me->globalPos());
   114 
   114 
   115     qreal velocityThreshold = HbPanVelocityUpdateThreshold * HbDeviceProfile::current().ppmValue();
   115     qreal velocityThreshold = HbPanVelocityUpdateThreshold * HbDeviceProfile::current().ppmValue();
   116 
   116 
   117     gesture->d_ptr->mAxisX.resetRecorder(velocityThreshold);
   117     gesture->d_ptr->mAxisX.resetRecorder(velocityThreshold);
   118     gesture->d_ptr->mAxisY.resetRecorder(velocityThreshold);
   118     gesture->d_ptr->mAxisY.resetRecorder(velocityThreshold);
   119     gesture->d_ptr->mAxisX.record(me->globalPos().x(), mCurrentTime);
   119     gesture->d_ptr->mAxisX.record(me->globalPos().x(), currentTime);
   120     gesture->d_ptr->mAxisY.record(me->globalPos().y(), mCurrentTime);
   120     gesture->d_ptr->mAxisY.record(me->globalPos().y(), currentTime);
   121 
   121 
   122     return QGestureRecognizer::MayBeGesture;
   122     return QGestureRecognizer::MayBeGesture;
   123 }
   123 }
   124 
   124 
   125 /*!
   125 /*!
   130 */
   130 */
   131 QGestureRecognizer::Result HbSwipeGestureLogic::handleMouseMove(
   131 QGestureRecognizer::Result HbSwipeGestureLogic::handleMouseMove(
   132         Qt::GestureState gestureState,
   132         Qt::GestureState gestureState,
   133         HbSwipeGesture *gesture,
   133         HbSwipeGesture *gesture,
   134         QObject *watched,
   134         QObject *watched,
   135         QMouseEvent *me )
   135         QMouseEvent *me,
       
   136         qint64 currentTime )
   136 {
   137 {
   137     Q_UNUSED(watched);
   138     Q_UNUSED(watched);
   138     Q_UNUSED(gestureState);
   139     Q_UNUSED(gestureState);
   139 
   140 
   140     if (!me->buttons().testFlag(Qt::LeftButton)){
   141     if (!me->buttons().testFlag(Qt::LeftButton)){
   141         return QGestureRecognizer::Ignore;
   142         return QGestureRecognizer::Ignore;
   142     }
   143     }
   143 
   144 
   144     gesture->d_ptr->mAxisX.record(me->globalPos().x(), mCurrentTime);
   145     gesture->d_ptr->mAxisX.record(me->globalPos().x(), currentTime);
   145     gesture->d_ptr->mAxisY.record(me->globalPos().y(), mCurrentTime);
   146     gesture->d_ptr->mAxisY.record(me->globalPos().y(), currentTime);
   146 
   147 
   147     return QGestureRecognizer::MayBeGesture;
   148     return QGestureRecognizer::MayBeGesture;
   148 }
   149 }
   149 
   150 
   150 /*!
   151 /*!
   156 #include <QDebug>
   157 #include <QDebug>
   157 QGestureRecognizer::Result HbSwipeGestureLogic::handleMouseRelease(
   158 QGestureRecognizer::Result HbSwipeGestureLogic::handleMouseRelease(
   158         Qt::GestureState gestureState,
   159         Qt::GestureState gestureState,
   159         HbSwipeGesture *gesture,
   160         HbSwipeGesture *gesture,
   160         QObject *watched,
   161         QObject *watched,
   161         QMouseEvent *me )
   162         QMouseEvent *me,
       
   163         qint64 currentTime )
   162 {   
   164 {   
   163     Q_UNUSED(gesture);
   165     Q_UNUSED(gesture);
   164     Q_UNUSED(watched);
   166     Q_UNUSED(watched);
   165     Q_UNUSED(gestureState);
   167     Q_UNUSED(gestureState);
   166 
   168 
   168         return QGestureRecognizer::Ignore;
   170         return QGestureRecognizer::Ignore;
   169     }
   171     }
   170 
   172 
   171     QPoint totalOffset = me->globalPos() - gesture->d_func()->mStartPos.toPoint();
   173     QPoint totalOffset = me->globalPos() - gesture->d_func()->mStartPos.toPoint();
   172 
   174 
   173     int deltaTime = gesture->d_func()->mStartTime.msecsTo(mCurrentTime);
   175     qint64 deltaTime = currentTime - gesture->d_func()->mStartTime;
   174     QPointF velocity = deltaTime != 0 ? totalOffset / deltaTime : QPointF(0,0);
   176     QPointF velocity = deltaTime != 0 ? totalOffset / deltaTime : QPointF(0,0);
   175 
   177 
   176     gesture->setSwipeAngle(QLineF(gesture->d_func()->mStartPos, me->globalPos()).angle());
   178     gesture->setSwipeAngle(QLineF(gesture->d_func()->mStartPos, me->globalPos()).angle());
   177     gesture->setSceneSwipeAngle(QLineF(gesture->d_func()->mSceneStartPos, HbGestureUtils::mapToScene(watched, me->globalPos())).angle());    
   179     gesture->setSceneSwipeAngle(QLineF(gesture->d_func()->mSceneStartPos, HbGestureUtils::mapToScene(watched, me->globalPos())).angle());    
   178     bool movedEnough = totalOffset.manhattanLength() >= (int)(HbSwipeMinOffset * HbDeviceProfile::current().ppmValue());
   180     bool movedEnough = totalOffset.manhattanLength() >= (int)(HbSwipeMinOffset * HbDeviceProfile::current().ppmValue());
   179     bool fastEnough = velocity.manhattanLength() >= HbSwipeMinSpeed * HbDeviceProfile::current().ppmValue();
   181     bool fastEnough = velocity.manhattanLength() >= HbSwipeMinSpeed * HbDeviceProfile::current().ppmValue();
   180     bool notStoppedAtEnd = HbVelocityCalculator(gesture->d_ptr->mAxisX, gesture->d_ptr->mAxisY).velocity(mCurrentTime) != QPointF(0,0);
   182     bool notStoppedAtEnd = HbVelocityCalculator(gesture->d_func()->mAxisX, gesture->d_func()->mAxisY).velocity(currentTime) != QPointF(0,0);
   181 
   183 
   182     if (movedEnough && fastEnough && notStoppedAtEnd) {
   184     if (movedEnough && fastEnough && notStoppedAtEnd) {
   183         return QGestureRecognizer::FinishGesture;
   185         return QGestureRecognizer::FinishGesture;
   184     } else {
   186     } else {
   185         return QGestureRecognizer::Ignore;
   187         return QGestureRecognizer::Ignore;
   195 QGestureRecognizer::Result HbSwipeGestureLogic::recognize(
   197 QGestureRecognizer::Result HbSwipeGestureLogic::recognize(
   196         Qt::GestureState gestureState,
   198         Qt::GestureState gestureState,
   197         HbSwipeGesture *gesture,
   199         HbSwipeGesture *gesture,
   198         QObject *watched,
   200         QObject *watched,
   199         QEvent *event,
   201         QEvent *event,
   200         QTime currentTime)
   202         qint64 currentTime)
   201 {
   203 {    
   202     // Record the time right away.
       
   203     mCurrentTime = currentTime;
       
   204     
       
   205     if ( isMouseEvent(event->type()) )
   204     if ( isMouseEvent(event->type()) )
   206     {
   205     {
   207         QMouseEvent* me = static_cast<QMouseEvent*>(event);
   206         QMouseEvent* me = static_cast<QMouseEvent*>(event);
   208         switch(event->type())
   207         switch(event->type())
   209         {
   208         {
   210         case QEvent::MouseButtonDblClick:
   209         case QEvent::MouseButtonDblClick:
   211         case QEvent::MouseButtonPress:
   210         case QEvent::MouseButtonPress:
   212             return handleMousePress(gestureState, gesture, watched, me);
   211             return handleMousePress(gestureState, gesture, watched, me, currentTime);
   213 
   212 
   214         case QEvent::MouseMove:
   213         case QEvent::MouseMove:
   215             return handleMouseMove(gestureState, gesture, watched, me);
   214             return handleMouseMove(gestureState, gesture, watched, me, currentTime);
   216 
   215 
   217         case QEvent::MouseButtonRelease:
   216         case QEvent::MouseButtonRelease:
   218             return handleMouseRelease(gestureState, gesture, watched, me);
   217             return handleMouseRelease(gestureState, gesture, watched, me, currentTime);
   219 
   218 
   220         default: break;
   219         default: break;
   221         }
   220         }
   222     }
   221     }
   223     return QGestureRecognizer::Ignore;
   222     return QGestureRecognizer::Ignore;