src/gui/kernel/qmacgesturerecognizer_mac.mm
changeset 0 1918ee327afb
child 3 41300fa6a67c
equal deleted inserted replaced
-1:000000000000 0:1918ee327afb
       
     1 /****************************************************************************
       
     2 **
       
     3 ** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
       
     4 ** All rights reserved.
       
     5 ** Contact: Nokia Corporation (qt-info@nokia.com)
       
     6 **
       
     7 ** This file is part of the QtGui module of the Qt Toolkit.
       
     8 **
       
     9 ** $QT_BEGIN_LICENSE:LGPL$
       
    10 ** No Commercial Usage
       
    11 ** This file contains pre-release code and may not be distributed.
       
    12 ** You may use this file in accordance with the terms and conditions
       
    13 ** contained in the Technology Preview License Agreement accompanying
       
    14 ** this package.
       
    15 **
       
    16 ** GNU Lesser General Public License Usage
       
    17 ** Alternatively, this file may be used under the terms of the GNU Lesser
       
    18 ** General Public License version 2.1 as published by the Free Software
       
    19 ** Foundation and appearing in the file LICENSE.LGPL included in the
       
    20 ** packaging of this file.  Please review the following information to
       
    21 ** ensure the GNU Lesser General Public License version 2.1 requirements
       
    22 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
       
    23 **
       
    24 ** In addition, as a special exception, Nokia gives you certain additional
       
    25 ** rights.  These rights are described in the Nokia Qt LGPL Exception
       
    26 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
       
    27 **
       
    28 ** If you have questions regarding the use of this file, please contact
       
    29 ** Nokia at qt-info@nokia.com.
       
    30 **
       
    31 **
       
    32 **
       
    33 **
       
    34 **
       
    35 **
       
    36 **
       
    37 **
       
    38 ** $QT_END_LICENSE$
       
    39 **
       
    40 ****************************************************************************/
       
    41 
       
    42 #include "qmacgesturerecognizer_mac_p.h"
       
    43 #include "qgesture.h"
       
    44 #include "qgesture_p.h"
       
    45 #include "qevent.h"
       
    46 #include "qevent_p.h"
       
    47 #include "qwidget.h"
       
    48 #include "qdebug.h"
       
    49 
       
    50 QT_BEGIN_NAMESPACE
       
    51 
       
    52 QMacSwipeGestureRecognizer::QMacSwipeGestureRecognizer()
       
    53 {
       
    54 }
       
    55 
       
    56 QGesture *QMacSwipeGestureRecognizer::createGesture(QObject * /*target*/)
       
    57 {
       
    58     return new QSwipeGesture;
       
    59 }
       
    60 
       
    61 QGestureRecognizer::Result
       
    62 QMacSwipeGestureRecognizer::filterEvent(QGesture *gesture, QObject *obj, QEvent *event)
       
    63 {
       
    64     if (event->type() == QEvent::NativeGesture && obj->isWidgetType()) {
       
    65         QNativeGestureEvent *ev = static_cast<QNativeGestureEvent*>(event);
       
    66         switch (ev->gestureType) {
       
    67             case QNativeGestureEvent::Swipe: {
       
    68                 QSwipeGesture *g = static_cast<QSwipeGesture *>(gesture);
       
    69                 g->setSwipeAngle(ev->angle);
       
    70                 return QGestureRecognizer::GestureTriggered | QGestureRecognizer::ConsumeEventHint;
       
    71                 break; }
       
    72             default:
       
    73                 break;
       
    74         }
       
    75     }
       
    76 
       
    77     return QGestureRecognizer::Ignore;
       
    78 }
       
    79 
       
    80 void QMacSwipeGestureRecognizer::reset(QGesture *gesture)
       
    81 {
       
    82     QSwipeGesture *g = static_cast<QSwipeGesture *>(gesture);
       
    83     g->setSwipeAngle(0);
       
    84     QGestureRecognizer::reset(gesture);
       
    85 }
       
    86 
       
    87 ////////////////////////////////////////////////////////////////////////
       
    88 
       
    89 QMacPinchGestureRecognizer::QMacPinchGestureRecognizer()
       
    90 {
       
    91 }
       
    92 
       
    93 QGesture *QMacPinchGestureRecognizer::createGesture(QObject * /*target*/)
       
    94 {
       
    95     return new QPinchGesture;
       
    96 }
       
    97 
       
    98 QGestureRecognizer::Result
       
    99 QMacPinchGestureRecognizer::filterEvent(QGesture *gesture, QObject *obj, QEvent *event)
       
   100 {
       
   101     if (event->type() == QEvent::NativeGesture && obj->isWidgetType()) {
       
   102         QPinchGesture *g = static_cast<QPinchGesture *>(gesture);
       
   103         QNativeGestureEvent *ev = static_cast<QNativeGestureEvent*>(event);
       
   104         switch(ev->gestureType) {
       
   105         case QNativeGestureEvent::GestureBegin:
       
   106             reset(gesture);
       
   107             g->setStartCenterPoint(static_cast<QWidget*>(obj)->mapFromGlobal(ev->position));
       
   108             g->setCenterPoint(g->startCenterPoint());
       
   109             g->setWhatChanged(QPinchGesture::CenterPointChanged);
       
   110             return QGestureRecognizer::MaybeGesture | QGestureRecognizer::ConsumeEventHint;
       
   111         case QNativeGestureEvent::Rotate: {
       
   112             g->setLastScaleFactor(g->scaleFactor());
       
   113             g->setLastRotationAngle(g->rotationAngle());
       
   114             g->setRotationAngle(g->rotationAngle() + ev->percentage);
       
   115             g->setWhatChanged(QPinchGesture::RotationAngleChanged);
       
   116             return QGestureRecognizer::GestureTriggered | QGestureRecognizer::ConsumeEventHint;
       
   117             break;
       
   118         }
       
   119         case QNativeGestureEvent::Zoom:
       
   120             g->setLastScaleFactor(g->scaleFactor());
       
   121             g->setLastRotationAngle(g->rotationAngle());
       
   122             g->setScaleFactor(g->scaleFactor() + ev->percentage);
       
   123             g->setWhatChanged(QPinchGesture::ScaleFactorChanged);
       
   124             return QGestureRecognizer::GestureTriggered | QGestureRecognizer::ConsumeEventHint;
       
   125             break;
       
   126         case QNativeGestureEvent::GestureEnd:
       
   127             return QGestureRecognizer::GestureFinished | QGestureRecognizer::ConsumeEventHint;
       
   128             break;
       
   129         default:
       
   130             break;
       
   131         }
       
   132     }
       
   133 
       
   134     return QGestureRecognizer::Ignore;
       
   135 }
       
   136 
       
   137 void QMacPinchGestureRecognizer::reset(QGesture *gesture)
       
   138 {
       
   139     QPinchGesture *g = static_cast<QPinchGesture *>(gesture);
       
   140     g->setWhatChanged(0);
       
   141     g->setScaleFactor(1.0f);
       
   142     g->setTotalScaleFactor(1.0f);
       
   143     g->setLastScaleFactor(1.0f);
       
   144     g->setRotationAngle(0.0f);
       
   145     g->setTotalRotationAngle(0.0f);
       
   146     g->setLastRotationAngle(0.0f);
       
   147     g->setCenterPoint(QPointF());
       
   148     g->setStartCenterPoint(QPointF());
       
   149     g->setLastCenterPoint(QPointF());
       
   150     QGestureRecognizer::reset(gesture);
       
   151 }
       
   152 
       
   153 ////////////////////////////////////////////////////////////////////////
       
   154 
       
   155 #if defined(QT_MAC_USE_COCOA)
       
   156 
       
   157 QMacPanGestureRecognizer::QMacPanGestureRecognizer() : _panCanceled(true)
       
   158 {
       
   159 }
       
   160 
       
   161 QGesture *QMacPanGestureRecognizer::createGesture(QObject *target)
       
   162 {
       
   163     if (!target)
       
   164         return new QPanGesture;
       
   165 
       
   166     if (QWidget *w = qobject_cast<QWidget *>(target)) {
       
   167         w->setAttribute(Qt::WA_AcceptTouchEvents);
       
   168         w->setAttribute(Qt::WA_TouchPadAcceptSingleTouchEvents);
       
   169         return new QPanGesture;
       
   170     }
       
   171     return 0;
       
   172 }
       
   173 
       
   174 QGestureRecognizer::Result
       
   175 QMacPanGestureRecognizer::filterEvent(QGesture *gesture, QObject *target, QEvent *event)
       
   176 {
       
   177     const int panBeginDelay = 300;
       
   178     const int panBeginRadius = 3;
       
   179 
       
   180     QPanGesture *g = static_cast<QPanGesture *>(gesture);
       
   181 
       
   182     switch (event->type()) {
       
   183     case QEvent::TouchBegin: {
       
   184         const QTouchEvent *ev = static_cast<const QTouchEvent*>(event);
       
   185         if (ev->touchPoints().size() == 1) {
       
   186             reset(gesture);
       
   187             _startPos = QCursor::pos();
       
   188             _lastPos = _startPos;
       
   189             _panTimer.start(panBeginDelay, target);
       
   190             _panCanceled = false;
       
   191             return QGestureRecognizer::MaybeGesture;
       
   192         }
       
   193         break;}
       
   194     case QEvent::TouchEnd: {
       
   195         if (_panCanceled)
       
   196             break;
       
   197 
       
   198         const QTouchEvent *ev = static_cast<const QTouchEvent*>(event);
       
   199         if (ev->touchPoints().size() == 1)
       
   200             return QGestureRecognizer::GestureFinished;
       
   201         break;}
       
   202     case QEvent::TouchUpdate: {
       
   203         if (_panCanceled)
       
   204             break;
       
   205 
       
   206         const QTouchEvent *ev = static_cast<const QTouchEvent*>(event);
       
   207         if (ev->touchPoints().size() == 1) {
       
   208             if (_panTimer.isActive()) {
       
   209                 // INVARIANT: Still in maybeGesture. Check if the user
       
   210                 // moved his finger so much that it makes sense to cancel the pan:
       
   211                 const QPointF p = QCursor::pos();
       
   212                 if ((p - _startPos).manhattanLength() > panBeginRadius) {
       
   213                     _panCanceled = true;
       
   214                     _panTimer.stop();
       
   215                     return QGestureRecognizer::NotGesture;
       
   216                 }
       
   217             } else {
       
   218                 const QPointF p = QCursor::pos();
       
   219                 const QPointF posOffset = p - _lastPos;
       
   220                 g->setLastOffset(g->offset());
       
   221                 g->setOffset(QPointF(posOffset.x(), posOffset.y()));
       
   222                 g->setTotalOffset(g->lastOffset() + g->offset());
       
   223                 _lastPos = p;
       
   224                 return QGestureRecognizer::GestureTriggered;
       
   225             }
       
   226         } else if (_panTimer.isActive()) {
       
   227             // I only want to cancel the pan if the user is pressing
       
   228             // more than one finger, and the pan hasn't started yet:
       
   229             _panCanceled = true;
       
   230             _panTimer.stop();
       
   231             return QGestureRecognizer::NotGesture;
       
   232         }
       
   233         break;}
       
   234     case QEvent::Timer: {
       
   235         QTimerEvent *ev = static_cast<QTimerEvent *>(event);
       
   236         if (ev->timerId() == _panTimer.timerId()) {
       
   237             _panTimer.stop();
       
   238             if (_panCanceled)
       
   239                 break;
       
   240             // Begin new pan session!
       
   241             _startPos = QCursor::pos();
       
   242             _lastPos = _startPos;
       
   243             return QGestureRecognizer::GestureTriggered | QGestureRecognizer::ConsumeEventHint;
       
   244         }
       
   245         break; }
       
   246     default:
       
   247         break;
       
   248     }
       
   249 
       
   250     return QGestureRecognizer::Ignore;
       
   251 }
       
   252 
       
   253 void QMacPanGestureRecognizer::reset(QGesture *gesture)
       
   254 {
       
   255     QPanGesture *g = static_cast<QPanGesture *>(gesture);
       
   256     _startPos = QPointF();
       
   257     _lastPos = QPointF();
       
   258     _panCanceled = true;
       
   259     g->setOffset(QPointF(0, 0));
       
   260     g->setLastOffset(QPointF(0, 0));
       
   261     g->setTotalOffset(QPointF(0, 0));
       
   262     g->setAcceleration(qreal(1));
       
   263     QGestureRecognizer::reset(gesture);
       
   264 }
       
   265 #endif // QT_MAC_USE_COCOA
       
   266 
       
   267 QT_END_NAMESPACE