| 1 | /****************************************************************************
|
|---|
| 2 | **
|
|---|
| 3 | ** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
|
|---|
| 4 | ** All rights reserved.
|
|---|
| 5 | ** Contact: Nokia Corporation ([email protected])
|
|---|
| 6 | **
|
|---|
| 7 | ** This file is part of the QtGui module of the Qt Toolkit.
|
|---|
| 8 | **
|
|---|
| 9 | ** $QT_BEGIN_LICENSE:LGPL$
|
|---|
| 10 | ** Commercial Usage
|
|---|
| 11 | ** Licensees holding valid Qt Commercial licenses may use this file in
|
|---|
| 12 | ** accordance with the Qt Commercial License Agreement provided with the
|
|---|
| 13 | ** Software or, alternatively, in accordance with the terms contained in
|
|---|
| 14 | ** a written agreement between you and Nokia.
|
|---|
| 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 | ** GNU General Public License Usage
|
|---|
| 29 | ** Alternatively, this file may be used under the terms of the GNU
|
|---|
| 30 | ** General Public License version 3.0 as published by the Free Software
|
|---|
| 31 | ** Foundation and appearing in the file LICENSE.GPL included in the
|
|---|
| 32 | ** packaging of this file. Please review the following information to
|
|---|
| 33 | ** ensure the GNU General Public License version 3.0 requirements will be
|
|---|
| 34 | ** met: http://www.gnu.org/copyleft/gpl.html.
|
|---|
| 35 | **
|
|---|
| 36 | ** If you have questions regarding the use of this file, please contact
|
|---|
| 37 | ** Nokia at [email protected].
|
|---|
| 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 | #ifndef QT_NO_GESTURES
|
|---|
| 51 |
|
|---|
| 52 | QT_BEGIN_NAMESPACE
|
|---|
| 53 |
|
|---|
| 54 | QMacSwipeGestureRecognizer::QMacSwipeGestureRecognizer()
|
|---|
| 55 | {
|
|---|
| 56 | }
|
|---|
| 57 |
|
|---|
| 58 | QGesture *QMacSwipeGestureRecognizer::create(QObject * /*target*/)
|
|---|
| 59 | {
|
|---|
| 60 | return new QSwipeGesture;
|
|---|
| 61 | }
|
|---|
| 62 |
|
|---|
| 63 | QGestureRecognizer::Result
|
|---|
| 64 | QMacSwipeGestureRecognizer::recognize(QGesture *gesture, QObject *obj, QEvent *event)
|
|---|
| 65 | {
|
|---|
| 66 | if (event->type() == QEvent::NativeGesture && obj->isWidgetType()) {
|
|---|
| 67 | QNativeGestureEvent *ev = static_cast<QNativeGestureEvent*>(event);
|
|---|
| 68 | switch (ev->gestureType) {
|
|---|
| 69 | case QNativeGestureEvent::Swipe: {
|
|---|
| 70 | QSwipeGesture *g = static_cast<QSwipeGesture *>(gesture);
|
|---|
| 71 | g->setSwipeAngle(ev->angle);
|
|---|
| 72 | return QGestureRecognizer::FinishGesture | QGestureRecognizer::ConsumeEventHint;
|
|---|
| 73 | break; }
|
|---|
| 74 | default:
|
|---|
| 75 | break;
|
|---|
| 76 | }
|
|---|
| 77 | }
|
|---|
| 78 |
|
|---|
| 79 | return QGestureRecognizer::Ignore;
|
|---|
| 80 | }
|
|---|
| 81 |
|
|---|
| 82 | void QMacSwipeGestureRecognizer::reset(QGesture *gesture)
|
|---|
| 83 | {
|
|---|
| 84 | QSwipeGesture *g = static_cast<QSwipeGesture *>(gesture);
|
|---|
| 85 | g->setSwipeAngle(0);
|
|---|
| 86 | QGestureRecognizer::reset(gesture);
|
|---|
| 87 | }
|
|---|
| 88 |
|
|---|
| 89 | ////////////////////////////////////////////////////////////////////////
|
|---|
| 90 |
|
|---|
| 91 | QMacPinchGestureRecognizer::QMacPinchGestureRecognizer()
|
|---|
| 92 | {
|
|---|
| 93 | }
|
|---|
| 94 |
|
|---|
| 95 | QGesture *QMacPinchGestureRecognizer::create(QObject * /*target*/)
|
|---|
| 96 | {
|
|---|
| 97 | return new QPinchGesture;
|
|---|
| 98 | }
|
|---|
| 99 |
|
|---|
| 100 | QGestureRecognizer::Result
|
|---|
| 101 | QMacPinchGestureRecognizer::recognize(QGesture *gesture, QObject *obj, QEvent *event)
|
|---|
| 102 | {
|
|---|
| 103 | if (event->type() == QEvent::NativeGesture && obj->isWidgetType()) {
|
|---|
| 104 | QPinchGesture *g = static_cast<QPinchGesture *>(gesture);
|
|---|
| 105 | QNativeGestureEvent *ev = static_cast<QNativeGestureEvent*>(event);
|
|---|
| 106 | switch(ev->gestureType) {
|
|---|
| 107 | case QNativeGestureEvent::GestureBegin:
|
|---|
| 108 | reset(gesture);
|
|---|
| 109 | g->setStartCenterPoint(static_cast<QWidget*>(obj)->mapFromGlobal(ev->position));
|
|---|
| 110 | g->setCenterPoint(g->startCenterPoint());
|
|---|
| 111 | g->setChangeFlags(QPinchGesture::CenterPointChanged);
|
|---|
| 112 | g->setTotalChangeFlags(g->totalChangeFlags() | g->changeFlags());
|
|---|
| 113 | return QGestureRecognizer::MayBeGesture | QGestureRecognizer::ConsumeEventHint;
|
|---|
| 114 | case QNativeGestureEvent::Rotate: {
|
|---|
| 115 | g->setLastScaleFactor(g->scaleFactor());
|
|---|
| 116 | g->setLastRotationAngle(g->rotationAngle());
|
|---|
| 117 | g->setRotationAngle(g->rotationAngle() + ev->percentage);
|
|---|
| 118 | g->setChangeFlags(QPinchGesture::RotationAngleChanged);
|
|---|
| 119 | g->setTotalChangeFlags(g->totalChangeFlags() | g->changeFlags());
|
|---|
| 120 | return QGestureRecognizer::TriggerGesture | QGestureRecognizer::ConsumeEventHint;
|
|---|
| 121 | }
|
|---|
| 122 | case QNativeGestureEvent::Zoom:
|
|---|
| 123 | g->setLastScaleFactor(g->scaleFactor());
|
|---|
| 124 | g->setLastRotationAngle(g->rotationAngle());
|
|---|
| 125 | g->setScaleFactor(g->scaleFactor() * (1 + ev->percentage));
|
|---|
| 126 | g->setChangeFlags(QPinchGesture::ScaleFactorChanged);
|
|---|
| 127 | g->setTotalChangeFlags(g->totalChangeFlags() | g->changeFlags());
|
|---|
| 128 | return QGestureRecognizer::TriggerGesture | QGestureRecognizer::ConsumeEventHint;
|
|---|
| 129 | case QNativeGestureEvent::GestureEnd:
|
|---|
| 130 | return QGestureRecognizer::FinishGesture | QGestureRecognizer::ConsumeEventHint;
|
|---|
| 131 | default:
|
|---|
| 132 | break;
|
|---|
| 133 | }
|
|---|
| 134 | }
|
|---|
| 135 |
|
|---|
| 136 | return QGestureRecognizer::Ignore;
|
|---|
| 137 | }
|
|---|
| 138 |
|
|---|
| 139 | void QMacPinchGestureRecognizer::reset(QGesture *gesture)
|
|---|
| 140 | {
|
|---|
| 141 | QPinchGesture *g = static_cast<QPinchGesture *>(gesture);
|
|---|
| 142 | g->setChangeFlags(0);
|
|---|
| 143 | g->setTotalChangeFlags(0);
|
|---|
| 144 | g->setScaleFactor(1.0f);
|
|---|
| 145 | g->setTotalScaleFactor(1.0f);
|
|---|
| 146 | g->setLastScaleFactor(1.0f);
|
|---|
| 147 | g->setRotationAngle(0.0f);
|
|---|
| 148 | g->setTotalRotationAngle(0.0f);
|
|---|
| 149 | g->setLastRotationAngle(0.0f);
|
|---|
| 150 | g->setCenterPoint(QPointF());
|
|---|
| 151 | g->setStartCenterPoint(QPointF());
|
|---|
| 152 | g->setLastCenterPoint(QPointF());
|
|---|
| 153 | QGestureRecognizer::reset(gesture);
|
|---|
| 154 | }
|
|---|
| 155 |
|
|---|
| 156 | ////////////////////////////////////////////////////////////////////////
|
|---|
| 157 |
|
|---|
| 158 | #if defined(QT_MAC_USE_COCOA)
|
|---|
| 159 |
|
|---|
| 160 | QMacPanGestureRecognizer::QMacPanGestureRecognizer() : _panCanceled(true)
|
|---|
| 161 | {
|
|---|
| 162 | }
|
|---|
| 163 |
|
|---|
| 164 | QGesture *QMacPanGestureRecognizer::create(QObject *target)
|
|---|
| 165 | {
|
|---|
| 166 | if (!target)
|
|---|
| 167 | return new QPanGesture;
|
|---|
| 168 |
|
|---|
| 169 | if (QWidget *w = qobject_cast<QWidget *>(target)) {
|
|---|
| 170 | w->setAttribute(Qt::WA_AcceptTouchEvents);
|
|---|
| 171 | w->setAttribute(Qt::WA_TouchPadAcceptSingleTouchEvents);
|
|---|
| 172 | return new QPanGesture;
|
|---|
| 173 | }
|
|---|
| 174 | return 0;
|
|---|
| 175 | }
|
|---|
| 176 |
|
|---|
| 177 | QGestureRecognizer::Result
|
|---|
| 178 | QMacPanGestureRecognizer::recognize(QGesture *gesture, QObject *target, QEvent *event)
|
|---|
| 179 | {
|
|---|
| 180 | const int panBeginDelay = 300;
|
|---|
| 181 | const int panBeginRadius = 3;
|
|---|
| 182 |
|
|---|
| 183 | QPanGesture *g = static_cast<QPanGesture *>(gesture);
|
|---|
| 184 |
|
|---|
| 185 | switch (event->type()) {
|
|---|
| 186 | case QEvent::TouchBegin: {
|
|---|
| 187 | const QTouchEvent *ev = static_cast<const QTouchEvent*>(event);
|
|---|
| 188 | if (ev->touchPoints().size() == 1) {
|
|---|
| 189 | reset(gesture);
|
|---|
| 190 | _startPos = QCursor::pos();
|
|---|
| 191 | _panTimer.start(panBeginDelay, target);
|
|---|
| 192 | _panCanceled = false;
|
|---|
| 193 | return QGestureRecognizer::MayBeGesture;
|
|---|
| 194 | }
|
|---|
| 195 | break;}
|
|---|
| 196 | case QEvent::TouchEnd: {
|
|---|
| 197 | if (_panCanceled)
|
|---|
| 198 | break;
|
|---|
| 199 |
|
|---|
| 200 | const QTouchEvent *ev = static_cast<const QTouchEvent*>(event);
|
|---|
| 201 | if (ev->touchPoints().size() == 1)
|
|---|
| 202 | return QGestureRecognizer::FinishGesture;
|
|---|
| 203 | break;}
|
|---|
| 204 | case QEvent::TouchUpdate: {
|
|---|
| 205 | if (_panCanceled)
|
|---|
| 206 | break;
|
|---|
| 207 |
|
|---|
| 208 | const QTouchEvent *ev = static_cast<const QTouchEvent*>(event);
|
|---|
| 209 | if (ev->touchPoints().size() == 1) {
|
|---|
| 210 | if (_panTimer.isActive()) {
|
|---|
| 211 | // INVARIANT: Still in maybeGesture. Check if the user
|
|---|
| 212 | // moved his finger so much that it makes sense to cancel the pan:
|
|---|
| 213 | const QPointF p = QCursor::pos();
|
|---|
| 214 | if ((p - _startPos).manhattanLength() > panBeginRadius) {
|
|---|
| 215 | _panCanceled = true;
|
|---|
| 216 | _panTimer.stop();
|
|---|
| 217 | return QGestureRecognizer::CancelGesture;
|
|---|
| 218 | }
|
|---|
| 219 | } else {
|
|---|
| 220 | const QPointF p = QCursor::pos();
|
|---|
| 221 | const QPointF posOffset = p - _startPos;
|
|---|
| 222 | g->setLastOffset(g->offset());
|
|---|
| 223 | g->setOffset(QPointF(posOffset.x(), posOffset.y()));
|
|---|
| 224 | return QGestureRecognizer::TriggerGesture;
|
|---|
| 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::CancelGesture;
|
|---|
| 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 | return QGestureRecognizer::TriggerGesture | QGestureRecognizer::ConsumeEventHint;
|
|---|
| 243 | }
|
|---|
| 244 | break; }
|
|---|
| 245 | default:
|
|---|
| 246 | break;
|
|---|
| 247 | }
|
|---|
| 248 |
|
|---|
| 249 | return QGestureRecognizer::Ignore;
|
|---|
| 250 | }
|
|---|
| 251 |
|
|---|
| 252 | void QMacPanGestureRecognizer::reset(QGesture *gesture)
|
|---|
| 253 | {
|
|---|
| 254 | QPanGesture *g = static_cast<QPanGesture *>(gesture);
|
|---|
| 255 | _startPos = QPointF();
|
|---|
| 256 | _panCanceled = true;
|
|---|
| 257 | g->setOffset(QPointF(0, 0));
|
|---|
| 258 | g->setLastOffset(QPointF(0, 0));
|
|---|
| 259 | g->setAcceleration(qreal(1));
|
|---|
| 260 | QGestureRecognizer::reset(gesture);
|
|---|
| 261 | }
|
|---|
| 262 | #endif // QT_MAC_USE_COCOA
|
|---|
| 263 |
|
|---|
| 264 | QT_END_NAMESPACE
|
|---|
| 265 |
|
|---|
| 266 | #endif // QT_NO_GESTURES
|
|---|