Qt
Internal/Contributor docs for the Qt SDK. <b>Note:</b> These are NOT official API docs; those are found <a href='https://doc.qt.io/'>here</a>.
Loading...
Searching...
No Matches
qmacgesturerecognizer.cpp
Go to the documentation of this file.
1// Copyright (C) 2016 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
3
5#include "qgesture.h"
6#include "qgesture_p.h"
7#include "qevent.h"
8#include "qwidget.h"
9#include "qdebug.h"
10#include <QtCore/qcoreapplication.h>
11
12#ifndef QT_NO_GESTURES
13
15
19
24
25QGestureRecognizer::Result
27{
28 if (event->type() == QEvent::NativeGesture && obj->isWidgetType()) {
30 switch (ev->gestureType()) {
32 QSwipeGesture *g = static_cast<QSwipeGesture *>(gesture);
33 g->setSwipeAngle(ev->value());
34 g->setHotSpot(ev->globalPosition());
36 break; }
37 default:
38 break;
39 }
40 }
41
43}
44
46{
47 QSwipeGesture *g = static_cast<QSwipeGesture *>(gesture);
48 g->setSwipeAngle(0);
50}
51
53
57
62
63QGestureRecognizer::Result
65{
66 if (event->type() == QEvent::NativeGesture && obj->isWidgetType()) {
67 QPinchGesture *g = static_cast<QPinchGesture *>(gesture);
69 switch (ev->gestureType()) {
71 reset(gesture);
72 g->setStartCenterPoint(static_cast<QWidget*>(obj)->mapFromGlobal(ev->globalPosition().toPoint()));
73 g->setCenterPoint(g->startCenterPoint());
74 g->setChangeFlags(QPinchGesture::CenterPointChanged);
75 g->setTotalChangeFlags(g->totalChangeFlags() | g->changeFlags());
76 g->setHotSpot(ev->globalPosition());
79 g->setLastScaleFactor(g->scaleFactor());
80 g->setLastRotationAngle(g->rotationAngle());
81 g->setRotationAngle(g->rotationAngle() + ev->value());
83 g->setTotalChangeFlags(g->totalChangeFlags() | g->changeFlags());
84 g->setHotSpot(ev->globalPosition());
87 g->setLastScaleFactor(g->scaleFactor());
88 g->setLastRotationAngle(g->rotationAngle());
89 g->setScaleFactor(1 + ev->value());
90 g->setTotalScaleFactor(g->totalScaleFactor() * g->scaleFactor());
91 g->setChangeFlags(QPinchGesture::ScaleFactorChanged);
92 g->setTotalChangeFlags(g->totalChangeFlags() | g->changeFlags());
93 g->setHotSpot(ev->globalPosition());
96 g->setLastScaleFactor(g->scaleFactor());
97 g->setLastRotationAngle(g->rotationAngle());
98 g->setScaleFactor(ev->value() ? 1.7f : 1.0f);
99 g->setChangeFlags(QPinchGesture::ScaleFactorChanged);
100 g->setTotalChangeFlags(g->totalChangeFlags() | g->changeFlags());
101 g->setHotSpot(ev->globalPosition());
105 default:
106 break;
107 }
108 }
109
111}
112
114{
115 QPinchGesture *g = static_cast<QPinchGesture *>(gesture);
116 g->setChangeFlags({});
117 g->setTotalChangeFlags({});
118 g->setScaleFactor(1.0f);
119 g->setTotalScaleFactor(1.0f);
120 g->setLastScaleFactor(1.0f);
121 g->setRotationAngle(0.0f);
122 g->setTotalRotationAngle(0.0f);
123 g->setLastRotationAngle(0.0f);
124 g->setCenterPoint(QPointF());
125 g->setStartCenterPoint(QPointF());
126 g->setLastCenterPoint(QPointF());
128}
129
131
135
137{
138 if (!target)
139 return new QPanGesture;
140
141 if (QWidget *w = qobject_cast<QWidget *>(target)) {
142 w->setAttribute(Qt::WA_AcceptTouchEvents);
144 return new QPanGesture;
145 }
146 return nullptr;
147}
148
150{
151 if (ev->timerId() == _panTimer.timerId()) {
152 if (_panTimer.isActive())
153 _panTimer.stop();
154 if (_target)
155 QCoreApplication::sendEvent(_target, ev);
156 }
157}
158
159QGestureRecognizer::Result
161{
162 const int panBeginDelay = 300;
163 const int panBeginRadius = 3;
164
165 QPanGesture *g = static_cast<QPanGesture *>(gesture);
166
167 switch (event->type()) {
168 case QEvent::TouchBegin: {
169 const QTouchEvent *ev = static_cast<const QTouchEvent*>(event);
170 if (ev->points().size() == 1) {
171 reset(gesture);
172 _startPos = QCursor::pos();
173 _target = target;
174 _panTimer.start(panBeginDelay, this);
175 _panCanceled = false;
177 }
178 break;}
179 case QEvent::TouchEnd: {
180 if (_panCanceled)
181 break;
182
183 const QTouchEvent *ev = static_cast<const QTouchEvent*>(event);
184 if (ev->points().size() == 1)
186 break;}
187 case QEvent::TouchUpdate: {
188 if (_panCanceled)
189 break;
190
191 const QTouchEvent *ev = static_cast<const QTouchEvent*>(event);
192 if (ev->points().size() == 1) {
193 if (_panTimer.isActive()) {
194 // INVARIANT: Still in maybeGesture. Check if the user
195 // moved his finger so much that it makes sense to cancel the pan:
196 const QPointF p = QCursor::pos();
197 if ((p - _startPos).manhattanLength() > panBeginRadius) {
198 _panCanceled = true;
199 _panTimer.stop();
201 }
202 } else {
203 const QPointF p = QCursor::pos();
204 const QPointF posOffset = p - _startPos;
205 g->setLastOffset(g->offset());
206 g->setOffset(QPointF(posOffset.x(), posOffset.y()));
207 g->setHotSpot(_startPos);
209 }
210 } else if (_panTimer.isActive()) {
211 // I only want to cancel the pan if the user is pressing
212 // more than one finger, and the pan hasn't started yet:
213 _panCanceled = true;
214 _panTimer.stop();
216 }
217 break;}
218 case QEvent::Timer: {
219 QTimerEvent *ev = static_cast<QTimerEvent *>(event);
220 if (ev->timerId() == _panTimer.timerId()) {
221 if (_panCanceled)
222 break;
223 // Begin new pan session!
224 _startPos = QCursor::pos();
225 g->setHotSpot(_startPos);
227 }
228 break; }
229 default:
230 break;
231 }
232
234}
235
237{
238 QPanGesture *g = static_cast<QPanGesture *>(gesture);
239 _startPos = QPointF();
240 _panCanceled = true;
241 _panTimer.stop();
242 g->setOffset(QPointF(0, 0));
243 g->setLastOffset(QPointF(0, 0));
244 g->setAcceleration(qreal(1));
246}
247
249
250#endif // QT_NO_GESTURES
void start(int msec, QObject *obj)
\obsolete Use chrono overload instead.
int timerId() const noexcept
Returns the timer's ID.
Definition qbasictimer.h:35
void stop()
Stops the timer.
bool isActive() const noexcept
Returns true if the timer is running and has not been stopped; otherwise returns false.
Definition qbasictimer.h:34
static bool sendEvent(QObject *receiver, QEvent *event)
Sends event event directly to receiver receiver, using the notify() function.
static QPoint pos()
Returns the position of the cursor (hot spot) of the primary screen in global screen coordinates.
Definition qcursor.cpp:188
\inmodule QtCore
Definition qcoreevent.h:45
@ NativeGesture
Definition qcoreevent.h:246
@ TouchUpdate
Definition qcoreevent.h:242
@ TouchBegin
Definition qcoreevent.h:241
virtual void reset(QGesture *state)
This function is called by the framework to reset a given gesture.
The QGesture class represents a gesture, containing properties that describe the corresponding user i...
Definition qgesture.h:29
void reset(QGesture *gesture) override
This function is called by the framework to reset a given gesture.
QGestureRecognizer::Result recognize(QGesture *gesture, QObject *watched, QEvent *event) override
Handles the given event for the watched object, updating the state of the gesture object as required,...
QGesture * create(QObject *target) override
This function is called by Qt to create a new QGesture object for the given target (QWidget or QGraph...
void timerEvent(QTimerEvent *ev) override
This event handler can be reimplemented in a subclass to receive timer events for the object.
void reset(QGesture *gesture) override
This function is called by the framework to reset a given gesture.
QGestureRecognizer::Result recognize(QGesture *gesture, QObject *watched, QEvent *event) override
Handles the given event for the watched object, updating the state of the gesture object as required,...
QGesture * create(QObject *target) override
This function is called by Qt to create a new QGesture object for the given target (QWidget or QGraph...
QGestureRecognizer::Result recognize(QGesture *gesture, QObject *watched, QEvent *event) override
Handles the given event for the watched object, updating the state of the gesture object as required,...
void reset(QGesture *gesture) override
This function is called by the framework to reset a given gesture.
QGesture * create(QObject *target) override
This function is called by Qt to create a new QGesture object for the given target (QWidget or QGraph...
The QNativeGestureEvent class contains parameters that describe a gesture event. \inmodule QtGui.
\inmodule QtCore
Definition qobject.h:103
The QPanGesture class describes a panning gesture made by the user.\inmodule QtWidgets.
Definition qgesture.h:73
The QPinchGesture class describes a pinch gesture made by the user.\inmodule QtWidgets.
Definition qgesture.h:103
@ RotationAngleChanged
Definition qgesture.h:110
void setChangeFlags(ChangeFlags value)
Definition qgesture.cpp:489
\inmodule QtCore\reentrant
Definition qpoint.h:217
The QSwipeGesture class describes a swipe gesture made by the user.\inmodule QtWidgets.
Definition qgesture.h:177
void setSwipeAngle(qreal value)
Definition qgesture.cpp:695
\inmodule QtCore
Definition qcoreevent.h:366
The QTouchEvent class contains parameters that describe a touch event.
Definition qevent.h:917
The QWidget class is the base class of all user interface objects.
Definition qwidget.h:99
Combined button and popup list for selecting options.
@ WA_AcceptTouchEvents
Definition qnamespace.h:404
@ WA_TouchPadAcceptSingleTouchEvents
Definition qnamespace.h:406
@ RotateNativeGesture
@ ZoomNativeGesture
@ BeginNativeGesture
@ EndNativeGesture
@ SwipeNativeGesture
@ SmartZoomNativeGesture
GLfloat GLfloat GLfloat w
[0]
GLenum target
GLboolean GLboolean g
struct _cl_event * event
GLhandleARB obj
[2]
GLboolean reset
GLfloat GLfloat p
[1]
double qreal
Definition qtypes.h:187