src/hbcore/gestures/hbswipegesturelogic_p.cpp
changeset 1 f7ac710697a9
child 2 06ff229162e9
equal deleted inserted replaced
0:16d8024aca5e 1:f7ac710697a9
       
     1 /****************************************************************************
       
     2 **
       
     3 ** Copyright (C) 2008-2010 Nokia Corporation and/or its subsidiary(-ies).
       
     4 ** All rights reserved.
       
     5 ** Contact: Nokia Corporation (developer.feedback@nokia.com)
       
     6 **
       
     7 ** This file is part of the HbCore module of the UI Extensions for Mobile.
       
     8 **
       
     9 ** GNU Lesser General Public License Usage
       
    10 ** This file may be used under the terms of the GNU Lesser General Public
       
    11 ** License version 2.1 as published by the Free Software Foundation and
       
    12 ** appearing in the file LICENSE.LGPL included in the packaging of this file.
       
    13 ** Please review the following information to ensure the GNU Lesser General
       
    14 ** Public License version 2.1 requirements will be met:
       
    15 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
       
    16 **
       
    17 ** In addition, as a special exception, Nokia gives you certain additional
       
    18 ** rights.  These rights are described in the Nokia Qt LGPL Exception
       
    19 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
       
    20 **
       
    21 ** If you have questions regarding the use of this file, please contact
       
    22 ** Nokia at developer.feedback@nokia.com.
       
    23 **
       
    24 ****************************************************************************/
       
    25 
       
    26 #include <QEvent>
       
    27 #include <QGestureRecognizer>
       
    28 #include <QGraphicsView>
       
    29 #include <QMouseEvent>
       
    30 
       
    31 #include "hbswipegesture.h"
       
    32 #include "hbswipegesture_p.h"
       
    33 #include "hbswipegesturelogic_p.h"
       
    34 
       
    35 /*!
       
    36    @hbcore
       
    37    \internal
       
    38    \class HbSwipeGestureLogic
       
    39 
       
    40    \brief
       
    41 
       
    42 */
       
    43 
       
    44 
       
    45 /*!
       
    46     \internal
       
    47     \brief
       
    48     \return
       
    49 
       
    50 */
       
    51 HbSwipeGestureLogic::HbSwipeGestureLogic()
       
    52 {
       
    53     mCurrentTime = QTime();
       
    54 }
       
    55 
       
    56 HbSwipeGestureLogic::~HbSwipeGestureLogic() {}
       
    57 
       
    58 /*!
       
    59     \internal
       
    60     \brief
       
    61     \return
       
    62 
       
    63 */
       
    64 bool HbSwipeGestureLogic::isMouseEvent(QEvent::Type eventType)
       
    65 {
       
    66     return eventType == QEvent::MouseButtonPress ||
       
    67            eventType == QEvent::MouseMove ||
       
    68            eventType == QEvent::MouseButtonDblClick ||
       
    69            eventType == QEvent::MouseButtonRelease;
       
    70 }
       
    71 
       
    72 /*!
       
    73     \internal
       
    74     \brief
       
    75     \return
       
    76 
       
    77 */
       
    78 void HbSwipeGestureLogic::resetGesture(HbSwipeGesture *gesture)
       
    79 {   
       
    80     gesture->setSwipeAngle(0);
       
    81     gesture->setSceneSwipeAngle(0);
       
    82 
       
    83     gesture->d_func()->mStartPos = QPointF();
       
    84     gesture->d_func()->mSceneStartPos = QPointF();    
       
    85 }
       
    86 
       
    87 /*!
       
    88     \internal
       
    89     \brief
       
    90     \return
       
    91 
       
    92 */
       
    93 QGestureRecognizer::Result HbSwipeGestureLogic::handleMousePress(
       
    94         Qt::GestureState gestureState,
       
    95         HbSwipeGesture *gesture,
       
    96         QObject *watched,
       
    97         QMouseEvent *me )
       
    98 {
       
    99     // Just ignore situations that are not interesting at all.
       
   100     if (!(gestureState == Qt::NoGesture && me->button() == Qt::LeftButton)) {
       
   101         return QGestureRecognizer::Ignore;
       
   102     }
       
   103     gesture->d_func()->mStartTime = QTime::currentTime();  
       
   104 
       
   105     gesture->d_func()->mStartPos = me->globalPos();
       
   106     gesture->d_func()->mSceneStartPos = HbGestureUtils::mapToScene(watched, me->globalPos());
       
   107 
       
   108     gesture->setHotSpot(me->globalPos());
       
   109 
       
   110     return QGestureRecognizer::MayBeGesture;
       
   111 }
       
   112 
       
   113 /*!
       
   114     \internal
       
   115     \brief
       
   116     \return
       
   117 
       
   118 */
       
   119 QGestureRecognizer::Result HbSwipeGestureLogic::handleMouseRelease(
       
   120         Qt::GestureState gestureState,
       
   121         HbSwipeGesture *gesture,
       
   122         QObject *watched,
       
   123         QMouseEvent *me )
       
   124 {   
       
   125     Q_UNUSED(gesture);
       
   126     Q_UNUSED(watched);
       
   127     Q_UNUSED(gestureState);
       
   128 
       
   129     QPointF totalOffset = me->globalPos() - gesture->d_func()->mStartPos.toPoint();
       
   130 
       
   131     QPointF velocity = totalOffset / gesture->d_func()->mStartTime.elapsed();
       
   132 
       
   133     gesture->setSwipeAngle(QLineF(gesture->d_func()->mStartPos, me->globalPos()).angle());
       
   134     gesture->setSceneSwipeAngle(QLineF(gesture->d_func()->mSceneStartPos, HbGestureUtils::mapToScene(watched, me->globalPos())).angle());
       
   135 
       
   136     if (totalOffset.manhattanLength() >= HbSwipeMinOffset && velocity.manhattanLength() >= HbSwipeMinSpeed && me->button() == Qt::LeftButton) {
       
   137         return QGestureRecognizer::FinishGesture;
       
   138     } else {
       
   139         return QGestureRecognizer::Ignore;
       
   140     }
       
   141 }
       
   142 
       
   143 /*!
       
   144     \internal
       
   145     \brief
       
   146     \return
       
   147 
       
   148 */
       
   149 QGestureRecognizer::Result HbSwipeGestureLogic::recognize(
       
   150         Qt::GestureState gestureState,
       
   151         HbSwipeGesture *gesture,
       
   152         QObject *watched,
       
   153         QEvent *event )
       
   154 {
       
   155     // Record the time right away.
       
   156     mCurrentTime = QTime::currentTime();
       
   157     
       
   158     if ( isMouseEvent(event->type()) )
       
   159     {
       
   160         QMouseEvent* me = static_cast<QMouseEvent*>(event);
       
   161         switch(event->type())
       
   162         {
       
   163         case QEvent::MouseButtonDblClick:
       
   164         case QEvent::MouseButtonPress:
       
   165             return handleMousePress(gestureState, gesture, watched, me);
       
   166 
       
   167         case QEvent::MouseMove:
       
   168             if (me->buttons().testFlag(Qt::LeftButton))
       
   169                 return QGestureRecognizer::MayBeGesture;
       
   170             else
       
   171                 return QGestureRecognizer::Ignore;
       
   172         case QEvent::MouseButtonRelease:
       
   173             return handleMouseRelease(gestureState, gesture, watched, me);
       
   174 
       
   175         default: break;
       
   176         }
       
   177     }
       
   178     return QGestureRecognizer::Ignore;
       
   179 }