Qt 4.8
qmacgesturerecognizer_mac.mm
Go to the documentation of this file.
1 /****************************************************************************
2 **
3 ** Copyright (C) 2014 Digia Plc and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/legal
5 **
6 ** This file is part of the QtGui module of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:LGPL$
9 ** Commercial License Usage
10 ** Licensees holding valid commercial Qt licenses may use this file in
11 ** accordance with the commercial license agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and Digia. For licensing terms and
14 ** conditions see http://qt.digia.com/licensing. For further information
15 ** use the contact form at http://qt.digia.com/contact-us.
16 **
17 ** GNU Lesser General Public License Usage
18 ** Alternatively, this file may be used under the terms of the GNU Lesser
19 ** General Public License version 2.1 as published by the Free Software
20 ** Foundation and appearing in the file LICENSE.LGPL included in the
21 ** packaging of this file. Please review the following information to
22 ** ensure the GNU Lesser General Public License version 2.1 requirements
23 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
24 **
25 ** In addition, as a special exception, Digia gives you certain additional
26 ** rights. These rights are described in the Digia Qt LGPL Exception
27 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
28 **
29 ** GNU General Public License Usage
30 ** Alternatively, this file may be used under the terms of the GNU
31 ** General Public License version 3.0 as published by the Free Software
32 ** Foundation and appearing in the file LICENSE.GPL included in the
33 ** packaging of this file. Please review the following information to
34 ** ensure the GNU General Public License version 3.0 requirements will be
35 ** met: http://www.gnu.org/copyleft/gpl.html.
36 **
37 **
38 ** $QT_END_LICENSE$
39 **
40 ****************************************************************************/
41 
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 
53 
55 {
56 }
57 
59 {
60  return new QSwipeGesture;
61 }
62 
63 QGestureRecognizer::Result
65 {
66  if (event->type() == QEvent::NativeGesture && obj->isWidgetType()) {
67  QNativeGestureEvent *ev = static_cast<QNativeGestureEvent*>(event);
68  switch (ev->gestureType) {
70  QSwipeGesture *g = static_cast<QSwipeGesture *>(gesture);
71  g->setSwipeAngle(ev->angle);
72  g->setHotSpot(ev->position);
74  break; }
75  default:
76  break;
77  }
78  }
79 
81 }
82 
84 {
85  QSwipeGesture *g = static_cast<QSwipeGesture *>(gesture);
86  g->setSwipeAngle(0);
88 }
89 
91 
93 {
94 }
95 
97 {
98  return new QPinchGesture;
99 }
100 
101 QGestureRecognizer::Result
103 {
104  if (event->type() == QEvent::NativeGesture && obj->isWidgetType()) {
105  QPinchGesture *g = static_cast<QPinchGesture *>(gesture);
106  QNativeGestureEvent *ev = static_cast<QNativeGestureEvent*>(event);
107  switch(ev->gestureType) {
109  reset(gesture);
110  g->setStartCenterPoint(static_cast<QWidget*>(obj)->mapFromGlobal(ev->position));
114  g->setHotSpot(ev->position);
122  g->setHotSpot(ev->position);
124  }
128  g->setScaleFactor(g->scaleFactor() * (1 + ev->percentage));
131  g->setHotSpot(ev->position);
135  default:
136  break;
137  }
138  }
139 
141 }
142 
144 {
145  QPinchGesture *g = static_cast<QPinchGesture *>(gesture);
146  g->setChangeFlags(0);
147  g->setTotalChangeFlags(0);
148  g->setScaleFactor(1.0f);
149  g->setTotalScaleFactor(1.0f);
150  g->setLastScaleFactor(1.0f);
151  g->setRotationAngle(0.0f);
152  g->setTotalRotationAngle(0.0f);
153  g->setLastRotationAngle(0.0f);
154  g->setCenterPoint(QPointF());
157  QGestureRecognizer::reset(gesture);
158 }
159 
161 
162 #if defined(QT_MAC_USE_COCOA)
163 
164 QMacPanGestureRecognizer::QMacPanGestureRecognizer() : _panCanceled(true)
165 {
166 }
167 
169 {
170  if (!target)
171  return new QPanGesture;
172 
173  if (QWidget *w = qobject_cast<QWidget *>(target)) {
174  w->setAttribute(Qt::WA_AcceptTouchEvents);
176  return new QPanGesture;
177  }
178  return 0;
179 }
180 
181 QGestureRecognizer::Result
182 QMacPanGestureRecognizer::recognize(QGesture *gesture, QObject *target, QEvent *event)
183 {
184  const int panBeginDelay = 300;
185  const int panBeginRadius = 3;
186 
187  QPanGesture *g = static_cast<QPanGesture *>(gesture);
188 
189  switch (event->type()) {
190  case QEvent::TouchBegin: {
191  const QTouchEvent *ev = static_cast<const QTouchEvent*>(event);
192  if (ev->touchPoints().size() == 1) {
193  reset(gesture);
194  _startPos = QCursor::pos();
195  _panTimer.start(panBeginDelay, target);
196  _panCanceled = false;
198  }
199  break;}
200  case QEvent::TouchEnd: {
201  if (_panCanceled)
202  break;
203 
204  const QTouchEvent *ev = static_cast<const QTouchEvent*>(event);
205  if (ev->touchPoints().size() == 1)
207  break;}
208  case QEvent::TouchUpdate: {
209  if (_panCanceled)
210  break;
211 
212  const QTouchEvent *ev = static_cast<const QTouchEvent*>(event);
213  if (ev->touchPoints().size() == 1) {
214  if (_panTimer.isActive()) {
215  // INVARIANT: Still in maybeGesture. Check if the user
216  // moved his finger so much that it makes sense to cancel the pan:
217  const QPointF p = QCursor::pos();
218  if ((p - _startPos).manhattanLength() > panBeginRadius) {
219  _panCanceled = true;
220  _panTimer.stop();
222  }
223  } else {
224  const QPointF p = QCursor::pos();
225  const QPointF posOffset = p - _startPos;
226  g->setLastOffset(g->offset());
227  g->setOffset(QPointF(posOffset.x(), posOffset.y()));
228  g->setHotSpot(_startPos);
230  }
231  } else if (_panTimer.isActive()) {
232  // I only want to cancel the pan if the user is pressing
233  // more than one finger, and the pan hasn't started yet:
234  _panCanceled = true;
235  _panTimer.stop();
237  }
238  break;}
239  case QEvent::Timer: {
240  QTimerEvent *ev = static_cast<QTimerEvent *>(event);
241  if (ev->timerId() == _panTimer.timerId()) {
242  _panTimer.stop();
243  if (_panCanceled)
244  break;
245  // Begin new pan session!
246  _startPos = QCursor::pos();
247  g->setHotSpot(_startPos);
249  }
250  break; }
251  default:
252  break;
253  }
254 
256 }
257 
259 {
260  QPanGesture *g = static_cast<QPanGesture *>(gesture);
261  _startPos = QPointF();
262  _panCanceled = true;
263  g->setOffset(QPointF(0, 0));
264  g->setLastOffset(QPointF(0, 0));
265  g->setAcceleration(qreal(1));
266  QGestureRecognizer::reset(gesture);
267 }
268 #endif // QT_MAC_USE_COCOA
269 
271 
272 #endif // QT_NO_GESTURES
qreal rotationAngle
the angle covered by the gesture motion
Definition: qgesture.h:159
The QPanGesture class describes a panning gesture made by the user.
Definition: qgesture.h:107
double qreal
Definition: qglobal.h:1193
#define QT_END_NAMESPACE
This macro expands to.
Definition: qglobal.h:90
void setCenterPoint(const QPointF &value)
Definition: qgesture.cpp:611
EventRef event
void setLastScaleFactor(qreal value)
Definition: qgesture.cpp:637
static Expression::Ptr create(Expression *const expr, const YYLTYPE &sourceLocator, const ParserContext *const parseInfo)
Q_CORE_EXPORT QTextStream & reset(QTextStream &s)
The QPointF class defines a point in the plane using floating point precision.
Definition: qpoint.h:214
QGesture * create(QObject *target)
This function is called by Qt to create a new QGesture object for the given target (QWidget or QGraph...
The QWidget class is the base class of all user interface objects.
Definition: qwidget.h:150
void setChangeFlags(ChangeFlags value)
Definition: qgesture.cpp:581
The QObject class is the base class of all Qt objects.
Definition: qobject.h:111
qreal x() const
Returns the x-coordinate of this point.
Definition: qpoint.h:282
QPointF offset
the total offset from the first input position to the current input position
Definition: qgesture.h:113
qreal scaleFactor
the current scale factor
Definition: qgesture.h:155
void setTotalScaleFactor(qreal value)
Definition: qgesture.cpp:632
#define QT_BEGIN_NAMESPACE
This macro expands to.
Definition: qglobal.h:89
virtual void reset(QGesture *state)
This function is called by the framework to reset a given gesture.
QGestureRecognizer::Result recognize(QGesture *gesture, QObject *watched, QEvent *event)
Handles the given event for the watched object, updating the state of the gesture object as required...
void setLastRotationAngle(qreal value)
Definition: qgesture.cpp:668
The QPinchGesture class describes a pinch gesture made by the user.
Definition: qgesture.h:136
void setScaleFactor(qreal value)
Definition: qgesture.cpp:642
int timerId() const
Returns the unique timer identifier, which is the same identifier as returned from QObject::startTime...
Definition: qcoreevent.h:346
QGestureRecognizer::Result recognize(QGesture *gesture, QObject *watched, QEvent *event)
Handles the given event for the watched object, updating the state of the gesture object as required...
void setOffset(const QPointF &value)
Definition: qgesture.cpp:364
QPointF startCenterPoint
the starting position of the center point
Definition: qgesture.h:161
ChangeFlags totalChangeFlags
the property of the gesture that has change
Definition: qgesture.h:150
bool isWidgetType() const
Returns true if the object is a widget; otherwise returns false.
Definition: qobject.h:146
The QTimerEvent class contains parameters that describe a timer event.
Definition: qcoreevent.h:341
void setLastOffset(const QPointF &value)
Definition: qgesture.cpp:359
The QGesture class represents a gesture, containing properties that describe the corresponding user i...
Definition: qgesture.h:64
void setHotSpot(const QPointF &value)
Definition: qgesture.cpp:180
void reset(QGesture *gesture)
This function is called by the framework to reset a given gesture.
const QList< QTouchEvent::TouchPoint > & touchPoints() const
Returns the list of touch points contained in the touch event.
Definition: qevent.h:820
int size() const
Returns the number of items in the list.
Definition: qlist.h:137
ChangeFlags changeFlags
the property of the gesture that has changed in the current step
Definition: qgesture.h:151
QGesture * create(QObject *target)
This function is called by Qt to create a new QGesture object for the given target (QWidget or QGraph...
qreal y() const
Returns the y-coordinate of this point.
Definition: qpoint.h:287
The QTouchEvent class contains parameters that describe a touch event.
Definition: qevent.h:741
void reset(QGesture *gesture)
This function is called by the framework to reset a given gesture.
void setTotalRotationAngle(qreal value)
Definition: qgesture.cpp:663
void setSwipeAngle(qreal value)
Definition: qgesture.cpp:794
void setLastCenterPoint(const QPointF &value)
Definition: qgesture.cpp:606
The QSwipeGesture class describes a swipe gesture made by the user.
Definition: qgesture.h:207
The QEvent class is the base class of all event classes.
Definition: qcoreevent.h:56
Type type() const
Returns the event type.
Definition: qcoreevent.h:303
void setAcceleration(qreal value)
Definition: qgesture.cpp:369
void setStartCenterPoint(const QPointF &value)
Definition: qgesture.cpp:601
void setTotalChangeFlags(ChangeFlags value)
Definition: qgesture.cpp:571
static QPoint pos()
Returns the position of the cursor (hot spot) in global screen coordinates.
Definition: qcursor_mac.mm:310
void setRotationAngle(qreal value)
Definition: qgesture.cpp:673