Absolute File Name: | /home/qt/qt5_coco/qt5/qtbase/src/widgets/kernel/qstandardgestures.cpp |
Source code | Switch to Preprocessed file |
Line | Source | Count | ||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
1 | /**************************************************************************** | - | ||||||||||||
2 | ** | - | ||||||||||||
3 | ** Copyright (C) 2015 The Qt Company Ltd. | - | ||||||||||||
4 | ** Contact: http://www.qt.io/licensing/ | - | ||||||||||||
5 | ** | - | ||||||||||||
6 | ** This file is part of the QtWidgets module of the Qt Toolkit. | - | ||||||||||||
7 | ** | - | ||||||||||||
8 | ** $QT_BEGIN_LICENSE:LGPL21$ | - | ||||||||||||
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 The Qt Company. For licensing terms | - | ||||||||||||
14 | ** and conditions see http://www.qt.io/terms-conditions. For further | - | ||||||||||||
15 | ** information use the contact form at http://www.qt.io/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 or version 3 as published by the Free | - | ||||||||||||
20 | ** Software Foundation and appearing in the file LICENSE.LGPLv21 and | - | ||||||||||||
21 | ** LICENSE.LGPLv3 included in the packaging of this file. Please review the | - | ||||||||||||
22 | ** following information to ensure the GNU Lesser General Public License | - | ||||||||||||
23 | ** requirements will be met: https://www.gnu.org/licenses/lgpl.html and | - | ||||||||||||
24 | ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. | - | ||||||||||||
25 | ** | - | ||||||||||||
26 | ** As a special exception, The Qt Company gives you certain additional | - | ||||||||||||
27 | ** rights. These rights are described in The Qt Company LGPL Exception | - | ||||||||||||
28 | ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. | - | ||||||||||||
29 | ** | - | ||||||||||||
30 | ** $QT_END_LICENSE$ | - | ||||||||||||
31 | ** | - | ||||||||||||
32 | ****************************************************************************/ | - | ||||||||||||
33 | - | |||||||||||||
34 | #include "qstandardgestures_p.h" | - | ||||||||||||
35 | #include "qgesture.h" | - | ||||||||||||
36 | #include "qgesture_p.h" | - | ||||||||||||
37 | #include "qevent.h" | - | ||||||||||||
38 | #include "qwidget.h" | - | ||||||||||||
39 | #include "qabstractscrollarea.h" | - | ||||||||||||
40 | #include <qgraphicssceneevent.h> | - | ||||||||||||
41 | #include "qdebug.h" | - | ||||||||||||
42 | - | |||||||||||||
43 | #ifndef QT_NO_GESTURES | - | ||||||||||||
44 | - | |||||||||||||
45 | QT_BEGIN_NAMESPACE | - | ||||||||||||
46 | - | |||||||||||||
47 | // If the change in scale for a single touch event is out of this range, | - | ||||||||||||
48 | // we consider it to be spurious. | - | ||||||||||||
49 | static const qreal kSingleStepScaleMax = 2.0; | - | ||||||||||||
50 | static const qreal kSingleStepScaleMin = 0.1; | - | ||||||||||||
51 | - | |||||||||||||
52 | QGesture *QPanGestureRecognizer::create(QObject *target) | - | ||||||||||||
53 | { | - | ||||||||||||
54 | if (target && target->isWidgetType()) {
| 0 | ||||||||||||
55 | #if (defined(Q_OS_MACX) || defined(Q_OS_WIN)) && !defined(QT_NO_NATIVE_GESTURES) | - | ||||||||||||
56 | // for scroll areas on Windows and OS X we want to use native gestures instead | - | ||||||||||||
57 | if (!qobject_cast<QAbstractScrollArea *>(target->parent())) | - | ||||||||||||
58 | static_cast<QWidget *>(target)->setAttribute(Qt::WA_AcceptTouchEvents); | - | ||||||||||||
59 | #else | - | ||||||||||||
60 | static_cast<QWidget *>(target)->setAttribute(Qt::WA_AcceptTouchEvents); | - | ||||||||||||
61 | #endif | - | ||||||||||||
62 | } never executed: end of block | 0 | ||||||||||||
63 | return new QPanGesture; never executed: return new QPanGesture; | 0 | ||||||||||||
64 | } | - | ||||||||||||
65 | - | |||||||||||||
66 | static QPointF panOffset(const QList<QTouchEvent::TouchPoint> &touchPoints, int maxCount) | - | ||||||||||||
67 | { | - | ||||||||||||
68 | QPointF result; | - | ||||||||||||
69 | const int count = qMin(touchPoints.size(), maxCount); | - | ||||||||||||
70 | for (int p = 0; p < count; ++p)
| 0 | ||||||||||||
71 | result += touchPoints.at(p).pos() - touchPoints.at(p).startPos(); never executed: result += touchPoints.at(p).pos() - touchPoints.at(p).startPos(); | 0 | ||||||||||||
72 | return result / qreal(count); never executed: return result / qreal(count); | 0 | ||||||||||||
73 | } | - | ||||||||||||
74 | - | |||||||||||||
75 | QGestureRecognizer::Result QPanGestureRecognizer::recognize(QGesture *state, | - | ||||||||||||
76 | QObject *, | - | ||||||||||||
77 | QEvent *event) | - | ||||||||||||
78 | { | - | ||||||||||||
79 | QPanGesture *q = static_cast<QPanGesture *>(state); | - | ||||||||||||
80 | QPanGesturePrivate *d = q->d_func(); | - | ||||||||||||
81 | - | |||||||||||||
82 | QGestureRecognizer::Result result = QGestureRecognizer::Ignore; | - | ||||||||||||
83 | switch (event->type()) { | - | ||||||||||||
84 | case QEvent::TouchBegin: { never executed: case QEvent::TouchBegin: | 0 | ||||||||||||
85 | const QTouchEvent *ev = static_cast<const QTouchEvent *>(event); | - | ||||||||||||
86 | result = QGestureRecognizer::MayBeGesture; | - | ||||||||||||
87 | QTouchEvent::TouchPoint p = ev->touchPoints().at(0); | - | ||||||||||||
88 | d->lastOffset = d->offset = QPointF(); | - | ||||||||||||
89 | d->pointCount = m_pointCount; | - | ||||||||||||
90 | break; never executed: break; | 0 | ||||||||||||
91 | } | - | ||||||||||||
92 | case QEvent::TouchEnd: { never executed: case QEvent::TouchEnd: | 0 | ||||||||||||
93 | if (q->state() != Qt::NoGesture) {
| 0 | ||||||||||||
94 | const QTouchEvent *ev = static_cast<const QTouchEvent *>(event); | - | ||||||||||||
95 | if (ev->touchPoints().size() == d->pointCount) {
| 0 | ||||||||||||
96 | d->lastOffset = d->offset; | - | ||||||||||||
97 | d->offset = panOffset(ev->touchPoints(), d->pointCount); | - | ||||||||||||
98 | } never executed: end of block | 0 | ||||||||||||
99 | result = QGestureRecognizer::FinishGesture; | - | ||||||||||||
100 | } else { never executed: end of block | 0 | ||||||||||||
101 | result = QGestureRecognizer::CancelGesture; | - | ||||||||||||
102 | } never executed: end of block | 0 | ||||||||||||
103 | break; never executed: break; | 0 | ||||||||||||
104 | } | - | ||||||||||||
105 | case QEvent::TouchUpdate: { never executed: case QEvent::TouchUpdate: | 0 | ||||||||||||
106 | const QTouchEvent *ev = static_cast<const QTouchEvent *>(event); | - | ||||||||||||
107 | if (ev->touchPoints().size() >= d->pointCount) {
| 0 | ||||||||||||
108 | d->lastOffset = d->offset; | - | ||||||||||||
109 | d->offset = panOffset(ev->touchPoints(), d->pointCount); | - | ||||||||||||
110 | if (d->offset.x() > 10 || d->offset.y() > 10 ||
| 0 | ||||||||||||
111 | d->offset.x() < -10 || d->offset.y() < -10) {
| 0 | ||||||||||||
112 | q->setHotSpot(ev->touchPoints().first().startScreenPos()); | - | ||||||||||||
113 | result = QGestureRecognizer::TriggerGesture; | - | ||||||||||||
114 | } else { never executed: end of block | 0 | ||||||||||||
115 | result = QGestureRecognizer::MayBeGesture; | - | ||||||||||||
116 | } never executed: end of block | 0 | ||||||||||||
117 | } | - | ||||||||||||
118 | break; never executed: break; | 0 | ||||||||||||
119 | } | - | ||||||||||||
120 | default: never executed: default: | 0 | ||||||||||||
121 | break; never executed: break; | 0 | ||||||||||||
122 | } | - | ||||||||||||
123 | return result; never executed: return result; | 0 | ||||||||||||
124 | } | - | ||||||||||||
125 | - | |||||||||||||
126 | void QPanGestureRecognizer::reset(QGesture *state) | - | ||||||||||||
127 | { | - | ||||||||||||
128 | QPanGesture *pan = static_cast<QPanGesture*>(state); | - | ||||||||||||
129 | QPanGesturePrivate *d = pan->d_func(); | - | ||||||||||||
130 | - | |||||||||||||
131 | d->lastOffset = d->offset = QPointF(); | - | ||||||||||||
132 | d->acceleration = 0; | - | ||||||||||||
133 | - | |||||||||||||
134 | QGestureRecognizer::reset(state); | - | ||||||||||||
135 | } never executed: end of block | 0 | ||||||||||||
136 | - | |||||||||||||
137 | - | |||||||||||||
138 | // | - | ||||||||||||
139 | // QPinchGestureRecognizer | - | ||||||||||||
140 | // | - | ||||||||||||
141 | - | |||||||||||||
142 | QPinchGestureRecognizer::QPinchGestureRecognizer() | - | ||||||||||||
143 | { | - | ||||||||||||
144 | } | - | ||||||||||||
145 | - | |||||||||||||
146 | QGesture *QPinchGestureRecognizer::create(QObject *target) | - | ||||||||||||
147 | { | - | ||||||||||||
148 | if (target && target->isWidgetType()) {
| 0 | ||||||||||||
149 | static_cast<QWidget *>(target)->setAttribute(Qt::WA_AcceptTouchEvents); | - | ||||||||||||
150 | } never executed: end of block | 0 | ||||||||||||
151 | return new QPinchGesture; never executed: return new QPinchGesture; | 0 | ||||||||||||
152 | } | - | ||||||||||||
153 | - | |||||||||||||
154 | QGestureRecognizer::Result QPinchGestureRecognizer::recognize(QGesture *state, | - | ||||||||||||
155 | QObject *, | - | ||||||||||||
156 | QEvent *event) | - | ||||||||||||
157 | { | - | ||||||||||||
158 | QPinchGesture *q = static_cast<QPinchGesture *>(state); | - | ||||||||||||
159 | QPinchGesturePrivate *d = q->d_func(); | - | ||||||||||||
160 | - | |||||||||||||
161 | QGestureRecognizer::Result result = QGestureRecognizer::Ignore; | - | ||||||||||||
162 | - | |||||||||||||
163 | switch (event->type()) { | - | ||||||||||||
164 | case QEvent::TouchBegin: { never executed: case QEvent::TouchBegin: | 0 | ||||||||||||
165 | result = QGestureRecognizer::MayBeGesture; | - | ||||||||||||
166 | break; never executed: break; | 0 | ||||||||||||
167 | } | - | ||||||||||||
168 | case QEvent::TouchEnd: { never executed: case QEvent::TouchEnd: | 0 | ||||||||||||
169 | if (q->state() != Qt::NoGesture) {
| 0 | ||||||||||||
170 | result = QGestureRecognizer::FinishGesture; | - | ||||||||||||
171 | } else { never executed: end of block | 0 | ||||||||||||
172 | result = QGestureRecognizer::CancelGesture; | - | ||||||||||||
173 | } never executed: end of block | 0 | ||||||||||||
174 | break; never executed: break; | 0 | ||||||||||||
175 | } | - | ||||||||||||
176 | case QEvent::TouchUpdate: { never executed: case QEvent::TouchUpdate: | 0 | ||||||||||||
177 | const QTouchEvent *ev = static_cast<const QTouchEvent *>(event); | - | ||||||||||||
178 | d->changeFlags = 0; | - | ||||||||||||
179 | if (ev->touchPoints().size() == 2) {
| 0 | ||||||||||||
180 | QTouchEvent::TouchPoint p1 = ev->touchPoints().at(0); | - | ||||||||||||
181 | QTouchEvent::TouchPoint p2 = ev->touchPoints().at(1); | - | ||||||||||||
182 | - | |||||||||||||
183 | d->hotSpot = p1.screenPos(); | - | ||||||||||||
184 | d->isHotSpotSet = true; | - | ||||||||||||
185 | - | |||||||||||||
186 | QPointF centerPoint = (p1.screenPos() + p2.screenPos()) / 2.0; | - | ||||||||||||
187 | if (d->isNewSequence) {
| 0 | ||||||||||||
188 | d->startPosition[0] = p1.screenPos(); | - | ||||||||||||
189 | d->startPosition[1] = p2.screenPos(); | - | ||||||||||||
190 | d->lastCenterPoint = centerPoint; | - | ||||||||||||
191 | } else { never executed: end of block | 0 | ||||||||||||
192 | d->lastCenterPoint = d->centerPoint; | - | ||||||||||||
193 | } never executed: end of block | 0 | ||||||||||||
194 | d->centerPoint = centerPoint; | - | ||||||||||||
195 | - | |||||||||||||
196 | d->changeFlags |= QPinchGesture::CenterPointChanged; | - | ||||||||||||
197 | - | |||||||||||||
198 | if (d->isNewSequence) {
| 0 | ||||||||||||
199 | d->scaleFactor = 1.0; | - | ||||||||||||
200 | d->lastScaleFactor = 1.0; | - | ||||||||||||
201 | } else { never executed: end of block | 0 | ||||||||||||
202 | d->lastScaleFactor = d->scaleFactor; | - | ||||||||||||
203 | QLineF line(p1.screenPos(), p2.screenPos()); | - | ||||||||||||
204 | QLineF lastLine(p1.lastScreenPos(), p2.lastScreenPos()); | - | ||||||||||||
205 | qreal newScaleFactor = line.length() / lastLine.length(); | - | ||||||||||||
206 | if (newScaleFactor > kSingleStepScaleMax || newScaleFactor < kSingleStepScaleMin)
| 0 | ||||||||||||
207 | return QGestureRecognizer::Ignore; never executed: return QGestureRecognizer::Ignore; | 0 | ||||||||||||
208 | d->scaleFactor = newScaleFactor; | - | ||||||||||||
209 | } never executed: end of block | 0 | ||||||||||||
210 | d->totalScaleFactor = d->totalScaleFactor * d->scaleFactor; | - | ||||||||||||
211 | d->changeFlags |= QPinchGesture::ScaleFactorChanged; | - | ||||||||||||
212 | - | |||||||||||||
213 | qreal angle = QLineF(p1.screenPos(), p2.screenPos()).angle(); | - | ||||||||||||
214 | if (angle > 180)
| 0 | ||||||||||||
215 | angle -= 360; never executed: angle -= 360; | 0 | ||||||||||||
216 | qreal startAngle = QLineF(p1.startScreenPos(), p2.startScreenPos()).angle(); | - | ||||||||||||
217 | if (startAngle > 180)
| 0 | ||||||||||||
218 | startAngle -= 360; never executed: startAngle -= 360; | 0 | ||||||||||||
219 | const qreal rotationAngle = startAngle - angle; | - | ||||||||||||
220 | if (d->isNewSequence)
| 0 | ||||||||||||
221 | d->lastRotationAngle = 0.0; never executed: d->lastRotationAngle = 0.0; | 0 | ||||||||||||
222 | else | - | ||||||||||||
223 | d->lastRotationAngle = d->rotationAngle; never executed: d->lastRotationAngle = d->rotationAngle; | 0 | ||||||||||||
224 | d->rotationAngle = rotationAngle; | - | ||||||||||||
225 | d->totalRotationAngle += d->rotationAngle - d->lastRotationAngle; | - | ||||||||||||
226 | d->changeFlags |= QPinchGesture::RotationAngleChanged; | - | ||||||||||||
227 | - | |||||||||||||
228 | d->totalChangeFlags |= d->changeFlags; | - | ||||||||||||
229 | d->isNewSequence = false; | - | ||||||||||||
230 | result = QGestureRecognizer::TriggerGesture; | - | ||||||||||||
231 | } else { never executed: end of block | 0 | ||||||||||||
232 | d->isNewSequence = true; | - | ||||||||||||
233 | if (q->state() == Qt::NoGesture)
| 0 | ||||||||||||
234 | result = QGestureRecognizer::Ignore; never executed: result = QGestureRecognizer::Ignore; | 0 | ||||||||||||
235 | else | - | ||||||||||||
236 | result = QGestureRecognizer::FinishGesture; never executed: result = QGestureRecognizer::FinishGesture; | 0 | ||||||||||||
237 | } | - | ||||||||||||
238 | break; never executed: break; | 0 | ||||||||||||
239 | } | - | ||||||||||||
240 | default: never executed: default: | 0 | ||||||||||||
241 | break; never executed: break; | 0 | ||||||||||||
242 | } | - | ||||||||||||
243 | return result; never executed: return result; | 0 | ||||||||||||
244 | } | - | ||||||||||||
245 | - | |||||||||||||
246 | void QPinchGestureRecognizer::reset(QGesture *state) | - | ||||||||||||
247 | { | - | ||||||||||||
248 | QPinchGesture *pinch = static_cast<QPinchGesture *>(state); | - | ||||||||||||
249 | QPinchGesturePrivate *d = pinch->d_func(); | - | ||||||||||||
250 | - | |||||||||||||
251 | d->totalChangeFlags = d->changeFlags = 0; | - | ||||||||||||
252 | - | |||||||||||||
253 | d->startCenterPoint = d->lastCenterPoint = d->centerPoint = QPointF(); | - | ||||||||||||
254 | d->totalScaleFactor = d->lastScaleFactor = d->scaleFactor = 1; | - | ||||||||||||
255 | d->totalRotationAngle = d->lastRotationAngle = d->rotationAngle = 0; | - | ||||||||||||
256 | - | |||||||||||||
257 | d->isNewSequence = true; | - | ||||||||||||
258 | d->startPosition[0] = d->startPosition[1] = QPointF(); | - | ||||||||||||
259 | - | |||||||||||||
260 | QGestureRecognizer::reset(state); | - | ||||||||||||
261 | } never executed: end of block | 0 | ||||||||||||
262 | - | |||||||||||||
263 | // | - | ||||||||||||
264 | // QSwipeGestureRecognizer | - | ||||||||||||
265 | // | - | ||||||||||||
266 | - | |||||||||||||
267 | QSwipeGestureRecognizer::QSwipeGestureRecognizer() | - | ||||||||||||
268 | { | - | ||||||||||||
269 | } | - | ||||||||||||
270 | - | |||||||||||||
271 | QGesture *QSwipeGestureRecognizer::create(QObject *target) | - | ||||||||||||
272 | { | - | ||||||||||||
273 | if (target && target->isWidgetType()) {
| 0 | ||||||||||||
274 | static_cast<QWidget *>(target)->setAttribute(Qt::WA_AcceptTouchEvents); | - | ||||||||||||
275 | } never executed: end of block | 0 | ||||||||||||
276 | return new QSwipeGesture; never executed: return new QSwipeGesture; | 0 | ||||||||||||
277 | } | - | ||||||||||||
278 | - | |||||||||||||
279 | QGestureRecognizer::Result QSwipeGestureRecognizer::recognize(QGesture *state, | - | ||||||||||||
280 | QObject *, | - | ||||||||||||
281 | QEvent *event) | - | ||||||||||||
282 | { | - | ||||||||||||
283 | QSwipeGesture *q = static_cast<QSwipeGesture *>(state); | - | ||||||||||||
284 | QSwipeGesturePrivate *d = q->d_func(); | - | ||||||||||||
285 | - | |||||||||||||
286 | QGestureRecognizer::Result result = QGestureRecognizer::Ignore; | - | ||||||||||||
287 | - | |||||||||||||
288 | switch (event->type()) { | - | ||||||||||||
289 | case QEvent::TouchBegin: { never executed: case QEvent::TouchBegin: | 0 | ||||||||||||
290 | d->velocityValue = 1; | - | ||||||||||||
291 | d->time.start(); | - | ||||||||||||
292 | d->state = QSwipeGesturePrivate::Started; | - | ||||||||||||
293 | result = QGestureRecognizer::MayBeGesture; | - | ||||||||||||
294 | break; never executed: break; | 0 | ||||||||||||
295 | } | - | ||||||||||||
296 | case QEvent::TouchEnd: { never executed: case QEvent::TouchEnd: | 0 | ||||||||||||
297 | if (q->state() != Qt::NoGesture) {
| 0 | ||||||||||||
298 | result = QGestureRecognizer::FinishGesture; | - | ||||||||||||
299 | } else { never executed: end of block | 0 | ||||||||||||
300 | result = QGestureRecognizer::CancelGesture; | - | ||||||||||||
301 | } never executed: end of block | 0 | ||||||||||||
302 | break; never executed: break; | 0 | ||||||||||||
303 | } | - | ||||||||||||
304 | case QEvent::TouchUpdate: { never executed: case QEvent::TouchUpdate: | 0 | ||||||||||||
305 | const QTouchEvent *ev = static_cast<const QTouchEvent *>(event); | - | ||||||||||||
306 | if (d->state == QSwipeGesturePrivate::NoGesture)
| 0 | ||||||||||||
307 | result = QGestureRecognizer::CancelGesture; never executed: result = QGestureRecognizer::CancelGesture; | 0 | ||||||||||||
308 | else if (ev->touchPoints().size() == 3) {
| 0 | ||||||||||||
309 | d->state = QSwipeGesturePrivate::ThreePointsReached; | - | ||||||||||||
310 | QTouchEvent::TouchPoint p1 = ev->touchPoints().at(0); | - | ||||||||||||
311 | QTouchEvent::TouchPoint p2 = ev->touchPoints().at(1); | - | ||||||||||||
312 | QTouchEvent::TouchPoint p3 = ev->touchPoints().at(2); | - | ||||||||||||
313 | - | |||||||||||||
314 | if (d->lastPositions[0].isNull()) {
| 0 | ||||||||||||
315 | d->lastPositions[0] = p1.startScreenPos().toPoint(); | - | ||||||||||||
316 | d->lastPositions[1] = p2.startScreenPos().toPoint(); | - | ||||||||||||
317 | d->lastPositions[2] = p3.startScreenPos().toPoint(); | - | ||||||||||||
318 | } never executed: end of block | 0 | ||||||||||||
319 | d->hotSpot = p1.screenPos(); | - | ||||||||||||
320 | d->isHotSpotSet = true; | - | ||||||||||||
321 | - | |||||||||||||
322 | int xDistance = (p1.screenPos().x() - d->lastPositions[0].x() + | - | ||||||||||||
323 | p2.screenPos().x() - d->lastPositions[1].x() + | - | ||||||||||||
324 | p3.screenPos().x() - d->lastPositions[2].x()) / 3; | - | ||||||||||||
325 | int yDistance = (p1.screenPos().y() - d->lastPositions[0].y() + | - | ||||||||||||
326 | p2.screenPos().y() - d->lastPositions[1].y() + | - | ||||||||||||
327 | p3.screenPos().y() - d->lastPositions[2].y()) / 3; | - | ||||||||||||
328 | - | |||||||||||||
329 | const int distance = xDistance >= yDistance ? xDistance : yDistance;
| 0 | ||||||||||||
330 | int elapsedTime = d->time.restart(); | - | ||||||||||||
331 | if (!elapsedTime)
| 0 | ||||||||||||
332 | elapsedTime = 1; never executed: elapsedTime = 1; | 0 | ||||||||||||
333 | d->velocityValue = 0.9 * d->velocityValue + (qreal) distance / elapsedTime; | - | ||||||||||||
334 | d->swipeAngle = QLineF(p1.startScreenPos(), p1.screenPos()).angle(); | - | ||||||||||||
335 | - | |||||||||||||
336 | static const int MoveThreshold = 50; | - | ||||||||||||
337 | static const int directionChangeThreshold = MoveThreshold / 8; | - | ||||||||||||
338 | if (qAbs(xDistance) > MoveThreshold || qAbs(yDistance) > MoveThreshold) {
| 0 | ||||||||||||
339 | // measure the distance to check if the direction changed | - | ||||||||||||
340 | d->lastPositions[0] = p1.screenPos().toPoint(); | - | ||||||||||||
341 | d->lastPositions[1] = p2.screenPos().toPoint(); | - | ||||||||||||
342 | d->lastPositions[2] = p3.screenPos().toPoint(); | - | ||||||||||||
343 | result = QGestureRecognizer::TriggerGesture; | - | ||||||||||||
344 | // QTBUG-46195, small changes in direction should not cause the gesture to be canceled. | - | ||||||||||||
345 | if (d->verticalDirection == QSwipeGesture::NoDirection || qAbs(yDistance) > directionChangeThreshold) {
| 0 | ||||||||||||
346 | const QSwipeGesture::SwipeDirection vertical = yDistance > 0
| 0 | ||||||||||||
347 | ? QSwipeGesture::Down : QSwipeGesture::Up; | - | ||||||||||||
348 | if (d->verticalDirection != QSwipeGesture::NoDirection && d->verticalDirection != vertical)
| 0 | ||||||||||||
349 | result = QGestureRecognizer::CancelGesture; never executed: result = QGestureRecognizer::CancelGesture; | 0 | ||||||||||||
350 | d->verticalDirection = vertical; | - | ||||||||||||
351 | } never executed: end of block | 0 | ||||||||||||
352 | if (d->horizontalDirection == QSwipeGesture::NoDirection || qAbs(xDistance) > directionChangeThreshold) {
| 0 | ||||||||||||
353 | const QSwipeGesture::SwipeDirection horizontal = xDistance > 0
| 0 | ||||||||||||
354 | ? QSwipeGesture::Right : QSwipeGesture::Left; | - | ||||||||||||
355 | if (d->horizontalDirection != QSwipeGesture::NoDirection && d->horizontalDirection != horizontal)
| 0 | ||||||||||||
356 | result = QGestureRecognizer::CancelGesture; never executed: result = QGestureRecognizer::CancelGesture; | 0 | ||||||||||||
357 | d->horizontalDirection = horizontal; | - | ||||||||||||
358 | } never executed: end of block | 0 | ||||||||||||
359 | } else { never executed: end of block | 0 | ||||||||||||
360 | if (q->state() != Qt::NoGesture)
| 0 | ||||||||||||
361 | result = QGestureRecognizer::TriggerGesture; never executed: result = QGestureRecognizer::TriggerGesture; | 0 | ||||||||||||
362 | else | - | ||||||||||||
363 | result = QGestureRecognizer::MayBeGesture; never executed: result = QGestureRecognizer::MayBeGesture; | 0 | ||||||||||||
364 | } | - | ||||||||||||
365 | } else if (ev->touchPoints().size() > 3) {
| 0 | ||||||||||||
366 | result = QGestureRecognizer::CancelGesture; | - | ||||||||||||
367 | } else { // less than 3 touch points never executed: end of block | 0 | ||||||||||||
368 | switch (d->state) { | - | ||||||||||||
369 | case QSwipeGesturePrivate::NoGesture: never executed: case QSwipeGesturePrivate::NoGesture: | 0 | ||||||||||||
370 | result = QGestureRecognizer::MayBeGesture; | - | ||||||||||||
371 | break; never executed: break; | 0 | ||||||||||||
372 | case QSwipeGesturePrivate::Started: never executed: case QSwipeGesturePrivate::Started: | 0 | ||||||||||||
373 | result = QGestureRecognizer::Ignore; | - | ||||||||||||
374 | break; never executed: break; | 0 | ||||||||||||
375 | case QSwipeGesturePrivate::ThreePointsReached: never executed: case QSwipeGesturePrivate::ThreePointsReached: | 0 | ||||||||||||
376 | result = (ev->touchPointStates() & Qt::TouchPointPressed)
| 0 | ||||||||||||
377 | ? QGestureRecognizer::CancelGesture : QGestureRecognizer::Ignore; | - | ||||||||||||
378 | break; never executed: break; | 0 | ||||||||||||
379 | } | - | ||||||||||||
380 | } never executed: end of block | 0 | ||||||||||||
381 | break; never executed: break; | 0 | ||||||||||||
382 | } | - | ||||||||||||
383 | default: never executed: default: | 0 | ||||||||||||
384 | break; never executed: break; | 0 | ||||||||||||
385 | } | - | ||||||||||||
386 | return result; never executed: return result; | 0 | ||||||||||||
387 | } | - | ||||||||||||
388 | - | |||||||||||||
389 | void QSwipeGestureRecognizer::reset(QGesture *state) | - | ||||||||||||
390 | { | - | ||||||||||||
391 | QSwipeGesture *q = static_cast<QSwipeGesture *>(state); | - | ||||||||||||
392 | QSwipeGesturePrivate *d = q->d_func(); | - | ||||||||||||
393 | - | |||||||||||||
394 | d->verticalDirection = d->horizontalDirection = QSwipeGesture::NoDirection; | - | ||||||||||||
395 | d->swipeAngle = 0; | - | ||||||||||||
396 | - | |||||||||||||
397 | d->lastPositions[0] = d->lastPositions[1] = d->lastPositions[2] = QPoint(); | - | ||||||||||||
398 | d->state = QSwipeGesturePrivate::NoGesture; | - | ||||||||||||
399 | d->velocityValue = 0; | - | ||||||||||||
400 | d->time.invalidate(); | - | ||||||||||||
401 | - | |||||||||||||
402 | QGestureRecognizer::reset(state); | - | ||||||||||||
403 | } never executed: end of block | 0 | ||||||||||||
404 | - | |||||||||||||
405 | // | - | ||||||||||||
406 | // QTapGestureRecognizer | - | ||||||||||||
407 | // | - | ||||||||||||
408 | - | |||||||||||||
409 | QTapGestureRecognizer::QTapGestureRecognizer() | - | ||||||||||||
410 | { | - | ||||||||||||
411 | } | - | ||||||||||||
412 | - | |||||||||||||
413 | QGesture *QTapGestureRecognizer::create(QObject *target) | - | ||||||||||||
414 | { | - | ||||||||||||
415 | if (target && target->isWidgetType()) {
| 0 | ||||||||||||
416 | static_cast<QWidget *>(target)->setAttribute(Qt::WA_AcceptTouchEvents); | - | ||||||||||||
417 | } never executed: end of block | 0 | ||||||||||||
418 | return new QTapGesture; never executed: return new QTapGesture; | 0 | ||||||||||||
419 | } | - | ||||||||||||
420 | - | |||||||||||||
421 | QGestureRecognizer::Result QTapGestureRecognizer::recognize(QGesture *state, | - | ||||||||||||
422 | QObject *, | - | ||||||||||||
423 | QEvent *event) | - | ||||||||||||
424 | { | - | ||||||||||||
425 | QTapGesture *q = static_cast<QTapGesture *>(state); | - | ||||||||||||
426 | QTapGesturePrivate *d = q->d_func(); | - | ||||||||||||
427 | - | |||||||||||||
428 | const QTouchEvent *ev = static_cast<const QTouchEvent *>(event); | - | ||||||||||||
429 | - | |||||||||||||
430 | QGestureRecognizer::Result result = QGestureRecognizer::CancelGesture; | - | ||||||||||||
431 | - | |||||||||||||
432 | switch (event->type()) { | - | ||||||||||||
433 | case QEvent::TouchBegin: { never executed: case QEvent::TouchBegin: | 0 | ||||||||||||
434 | d->position = ev->touchPoints().at(0).pos(); | - | ||||||||||||
435 | q->setHotSpot(ev->touchPoints().at(0).screenPos()); | - | ||||||||||||
436 | result = QGestureRecognizer::TriggerGesture; | - | ||||||||||||
437 | break; never executed: break; | 0 | ||||||||||||
438 | } | - | ||||||||||||
439 | case QEvent::TouchUpdate: never executed: case QEvent::TouchUpdate: | 0 | ||||||||||||
440 | case QEvent::TouchEnd: { never executed: case QEvent::TouchEnd: | 0 | ||||||||||||
441 | if (q->state() != Qt::NoGesture && ev->touchPoints().size() == 1) {
| 0 | ||||||||||||
442 | QTouchEvent::TouchPoint p = ev->touchPoints().at(0); | - | ||||||||||||
443 | QPoint delta = p.pos().toPoint() - p.startPos().toPoint(); | - | ||||||||||||
444 | enum { TapRadius = 40 }; | - | ||||||||||||
445 | if (delta.manhattanLength() <= TapRadius) {
| 0 | ||||||||||||
446 | if (event->type() == QEvent::TouchEnd)
| 0 | ||||||||||||
447 | result = QGestureRecognizer::FinishGesture; never executed: result = QGestureRecognizer::FinishGesture; | 0 | ||||||||||||
448 | else | - | ||||||||||||
449 | result = QGestureRecognizer::TriggerGesture; never executed: result = QGestureRecognizer::TriggerGesture; | 0 | ||||||||||||
450 | } | - | ||||||||||||
451 | } never executed: end of block | 0 | ||||||||||||
452 | break; never executed: break; | 0 | ||||||||||||
453 | } | - | ||||||||||||
454 | case QEvent::MouseButtonPress: never executed: case QEvent::MouseButtonPress: | 0 | ||||||||||||
455 | case QEvent::MouseMove: never executed: case QEvent::MouseMove: | 0 | ||||||||||||
456 | case QEvent::MouseButtonRelease: never executed: case QEvent::MouseButtonRelease: | 0 | ||||||||||||
457 | result = QGestureRecognizer::Ignore; | - | ||||||||||||
458 | break; never executed: break; | 0 | ||||||||||||
459 | default: never executed: default: | 0 | ||||||||||||
460 | result = QGestureRecognizer::Ignore; | - | ||||||||||||
461 | break; never executed: break; | 0 | ||||||||||||
462 | } | - | ||||||||||||
463 | return result; never executed: return result; | 0 | ||||||||||||
464 | } | - | ||||||||||||
465 | - | |||||||||||||
466 | void QTapGestureRecognizer::reset(QGesture *state) | - | ||||||||||||
467 | { | - | ||||||||||||
468 | QTapGesture *q = static_cast<QTapGesture *>(state); | - | ||||||||||||
469 | QTapGesturePrivate *d = q->d_func(); | - | ||||||||||||
470 | - | |||||||||||||
471 | d->position = QPointF(); | - | ||||||||||||
472 | - | |||||||||||||
473 | QGestureRecognizer::reset(state); | - | ||||||||||||
474 | } never executed: end of block | 0 | ||||||||||||
475 | - | |||||||||||||
476 | // | - | ||||||||||||
477 | // QTapAndHoldGestureRecognizer | - | ||||||||||||
478 | // | - | ||||||||||||
479 | - | |||||||||||||
480 | QTapAndHoldGestureRecognizer::QTapAndHoldGestureRecognizer() | - | ||||||||||||
481 | { | - | ||||||||||||
482 | } | - | ||||||||||||
483 | - | |||||||||||||
484 | QGesture *QTapAndHoldGestureRecognizer::create(QObject *target) | - | ||||||||||||
485 | { | - | ||||||||||||
486 | if (target && target->isWidgetType()) {
| 0 | ||||||||||||
487 | static_cast<QWidget *>(target)->setAttribute(Qt::WA_AcceptTouchEvents); | - | ||||||||||||
488 | } never executed: end of block | 0 | ||||||||||||
489 | return new QTapAndHoldGesture; never executed: return new QTapAndHoldGesture; | 0 | ||||||||||||
490 | } | - | ||||||||||||
491 | - | |||||||||||||
492 | QGestureRecognizer::Result | - | ||||||||||||
493 | QTapAndHoldGestureRecognizer::recognize(QGesture *state, QObject *object, | - | ||||||||||||
494 | QEvent *event) | - | ||||||||||||
495 | { | - | ||||||||||||
496 | QTapAndHoldGesture *q = static_cast<QTapAndHoldGesture *>(state); | - | ||||||||||||
497 | QTapAndHoldGesturePrivate *d = q->d_func(); | - | ||||||||||||
498 | - | |||||||||||||
499 | if (object == state && event->type() == QEvent::Timer) {
| 0 | ||||||||||||
500 | q->killTimer(d->timerId); | - | ||||||||||||
501 | d->timerId = 0; | - | ||||||||||||
502 | return QGestureRecognizer::FinishGesture | QGestureRecognizer::ConsumeEventHint; never executed: return QGestureRecognizer::FinishGesture | QGestureRecognizer::ConsumeEventHint; | 0 | ||||||||||||
503 | } | - | ||||||||||||
504 | - | |||||||||||||
505 | enum { TapRadius = 40 }; | - | ||||||||||||
506 | - | |||||||||||||
507 | switch (event->type()) { | - | ||||||||||||
508 | #ifndef QT_NO_GRAPHICSVIEW | - | ||||||||||||
509 | case QEvent::GraphicsSceneMousePress: { never executed: case QEvent::GraphicsSceneMousePress: | 0 | ||||||||||||
510 | const QGraphicsSceneMouseEvent *gsme = static_cast<const QGraphicsSceneMouseEvent *>(event); | - | ||||||||||||
511 | d->position = gsme->screenPos(); | - | ||||||||||||
512 | q->setHotSpot(d->position); | - | ||||||||||||
513 | if (d->timerId)
| 0 | ||||||||||||
514 | q->killTimer(d->timerId); never executed: q->killTimer(d->timerId); | 0 | ||||||||||||
515 | d->timerId = q->startTimer(QTapAndHoldGesturePrivate::Timeout); | - | ||||||||||||
516 | return QGestureRecognizer::MayBeGesture; // we don't show a sign of life until the timeout never executed: return QGestureRecognizer::MayBeGesture; | 0 | ||||||||||||
517 | } | - | ||||||||||||
518 | #endif | - | ||||||||||||
519 | case QEvent::MouseButtonPress: { never executed: case QEvent::MouseButtonPress: | 0 | ||||||||||||
520 | const QMouseEvent *me = static_cast<const QMouseEvent *>(event); | - | ||||||||||||
521 | d->position = me->globalPos(); | - | ||||||||||||
522 | q->setHotSpot(d->position); | - | ||||||||||||
523 | if (d->timerId)
| 0 | ||||||||||||
524 | q->killTimer(d->timerId); never executed: q->killTimer(d->timerId); | 0 | ||||||||||||
525 | d->timerId = q->startTimer(QTapAndHoldGesturePrivate::Timeout); | - | ||||||||||||
526 | return QGestureRecognizer::MayBeGesture; // we don't show a sign of life until the timeout never executed: return QGestureRecognizer::MayBeGesture; | 0 | ||||||||||||
527 | } | - | ||||||||||||
528 | case QEvent::TouchBegin: { never executed: case QEvent::TouchBegin: | 0 | ||||||||||||
529 | const QTouchEvent *ev = static_cast<const QTouchEvent *>(event); | - | ||||||||||||
530 | d->position = ev->touchPoints().at(0).startScreenPos(); | - | ||||||||||||
531 | q->setHotSpot(d->position); | - | ||||||||||||
532 | if (d->timerId)
| 0 | ||||||||||||
533 | q->killTimer(d->timerId); never executed: q->killTimer(d->timerId); | 0 | ||||||||||||
534 | d->timerId = q->startTimer(QTapAndHoldGesturePrivate::Timeout); | - | ||||||||||||
535 | return QGestureRecognizer::MayBeGesture; // we don't show a sign of life until the timeout never executed: return QGestureRecognizer::MayBeGesture; | 0 | ||||||||||||
536 | } | - | ||||||||||||
537 | #ifndef QT_NO_GRAPHICSVIEW | - | ||||||||||||
538 | case QEvent::GraphicsSceneMouseRelease: never executed: case QEvent::GraphicsSceneMouseRelease: | 0 | ||||||||||||
539 | #endif | - | ||||||||||||
540 | case QEvent::MouseButtonRelease: never executed: case QEvent::MouseButtonRelease: | 0 | ||||||||||||
541 | case QEvent::TouchEnd: never executed: case QEvent::TouchEnd: | 0 | ||||||||||||
542 | return QGestureRecognizer::CancelGesture; // get out of the MayBeGesture state never executed: return QGestureRecognizer::CancelGesture; | 0 | ||||||||||||
543 | case QEvent::TouchUpdate: { never executed: case QEvent::TouchUpdate: | 0 | ||||||||||||
544 | const QTouchEvent *ev = static_cast<const QTouchEvent *>(event); | - | ||||||||||||
545 | if (d->timerId && ev->touchPoints().size() == 1) {
| 0 | ||||||||||||
546 | QTouchEvent::TouchPoint p = ev->touchPoints().at(0); | - | ||||||||||||
547 | QPoint delta = p.pos().toPoint() - p.startPos().toPoint(); | - | ||||||||||||
548 | if (delta.manhattanLength() <= TapRadius)
| 0 | ||||||||||||
549 | return QGestureRecognizer::MayBeGesture; never executed: return QGestureRecognizer::MayBeGesture; | 0 | ||||||||||||
550 | } never executed: end of block | 0 | ||||||||||||
551 | return QGestureRecognizer::CancelGesture; never executed: return QGestureRecognizer::CancelGesture; | 0 | ||||||||||||
552 | } | - | ||||||||||||
553 | case QEvent::MouseMove: { never executed: case QEvent::MouseMove: | 0 | ||||||||||||
554 | const QMouseEvent *me = static_cast<const QMouseEvent *>(event); | - | ||||||||||||
555 | QPoint delta = me->globalPos() - d->position.toPoint(); | - | ||||||||||||
556 | if (d->timerId && delta.manhattanLength() <= TapRadius)
| 0 | ||||||||||||
557 | return QGestureRecognizer::MayBeGesture; never executed: return QGestureRecognizer::MayBeGesture; | 0 | ||||||||||||
558 | return QGestureRecognizer::CancelGesture; never executed: return QGestureRecognizer::CancelGesture; | 0 | ||||||||||||
559 | } | - | ||||||||||||
560 | #ifndef QT_NO_GRAPHICSVIEW | - | ||||||||||||
561 | case QEvent::GraphicsSceneMouseMove: { never executed: case QEvent::GraphicsSceneMouseMove: | 0 | ||||||||||||
562 | const QGraphicsSceneMouseEvent *gsme = static_cast<const QGraphicsSceneMouseEvent *>(event); | - | ||||||||||||
563 | QPoint delta = gsme->screenPos() - d->position.toPoint(); | - | ||||||||||||
564 | if (d->timerId && delta.manhattanLength() <= TapRadius)
| 0 | ||||||||||||
565 | return QGestureRecognizer::MayBeGesture; never executed: return QGestureRecognizer::MayBeGesture; | 0 | ||||||||||||
566 | return QGestureRecognizer::CancelGesture; never executed: return QGestureRecognizer::CancelGesture; | 0 | ||||||||||||
567 | } | - | ||||||||||||
568 | #endif | - | ||||||||||||
569 | default: never executed: default: | 0 | ||||||||||||
570 | return QGestureRecognizer::Ignore; never executed: return QGestureRecognizer::Ignore; | 0 | ||||||||||||
571 | } | - | ||||||||||||
572 | } | - | ||||||||||||
573 | - | |||||||||||||
574 | void QTapAndHoldGestureRecognizer::reset(QGesture *state) | - | ||||||||||||
575 | { | - | ||||||||||||
576 | QTapAndHoldGesture *q = static_cast<QTapAndHoldGesture *>(state); | - | ||||||||||||
577 | QTapAndHoldGesturePrivate *d = q->d_func(); | - | ||||||||||||
578 | - | |||||||||||||
579 | d->position = QPointF(); | - | ||||||||||||
580 | if (d->timerId)
| 0 | ||||||||||||
581 | q->killTimer(d->timerId); never executed: q->killTimer(d->timerId); | 0 | ||||||||||||
582 | d->timerId = 0; | - | ||||||||||||
583 | - | |||||||||||||
584 | QGestureRecognizer::reset(state); | - | ||||||||||||
585 | } never executed: end of block | 0 | ||||||||||||
586 | - | |||||||||||||
587 | QT_END_NAMESPACE | - | ||||||||||||
588 | - | |||||||||||||
589 | #endif // QT_NO_GESTURES | - | ||||||||||||
Source code | Switch to Preprocessed file |