Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
qgesture.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// Qt-Security score:significant reason:default
4
5#include "qgesture.h"
6#include "private/qgesture_p.h"
7#include "private/qstandardgestures_p.h"
8#if QT_CONFIG(graphicsview)
9#include "qgraphicsview.h"
10#endif
11
12#include <private/qdebug_p.h>
13#ifndef QT_NO_GESTURES
14
15using namespace std::chrono_literals;
16
17QT_BEGIN_NAMESPACE
18
19QT_IMPL_METATYPE_EXTERN_TAGGED(Qt::GestureState, Qt__GestureState)
20QT_IMPL_METATYPE_EXTERN_TAGGED(Qt::GestureType, Qt__GestureType)
21QT_IMPL_METATYPE_EXTERN_TAGGED(QPinchGesture::ChangeFlags,
22 QPinchGesture__ChangeFlags)
23QT_IMPL_METATYPE_EXTERN_TAGGED(QGesture::GestureCancelPolicy,
24 QGesture__GestureCancelPolicy)
25
26 /*!
27 \class QGesture
28 \since 4.6
29 \ingroup gestures
30 \inmodule QtWidgets
31
32 \brief The QGesture class represents a gesture, containing properties that
33 describe the corresponding user input.
34
35 Gesture objects are not constructed directly by developers. They are created by
36 the QGestureRecognizer object that is registered with the application; see
37 QGestureRecognizer::registerRecognizer().
38
39 For an overview of gesture handling in Qt and information on using gestures
40 in your applications, see the \l{Gestures in Widgets and Graphics View} document.
41
42 \section1 Gesture Properties
43
44 The class has a list of properties that can be queried by the user to get
45 some gesture-specific arguments. For example, the pinch gesture has a scale
46 factor that is exposed as a property.
47
48 Developers of custom gesture recognizers can add additional properties in
49 order to provide additional information about a gesture. This can be done
50 by adding new dynamic properties to a QGesture object, or by subclassing
51 the QGesture class (or one of its subclasses).
52
53 \section1 Lifecycle of a Gesture Object
54
55 A QGesture instance is implicitly created when needed and is owned by Qt.
56 Developers should never destroy them or store them for later use as Qt may
57 destroy particular instances of them and create new ones to replace them.
58
59 The registered gesture recognizer monitors the input events for the target
60 object via its \l{QGestureRecognizer::}{recognize()} function, updating the
61 properties of the gesture object as required.
62
63 The gesture object may be delivered to the target object in a QGestureEvent if
64 the corresponding gesture is active or has just been canceled. Each event that
65 is delivered contains a list of gesture objects, since support for more than
66 one gesture may be enabled for the target object. Due to the way events are
67 handled in Qt, gesture events may be filtered by other objects.
68
69 \sa QGestureEvent, QGestureRecognizer
70*/
71
72/*!
73 Constructs a new gesture object with the given \a parent.
74
75 QGesture objects are created by gesture recognizers in the
76 QGestureRecognizer::create() function.
77*/
78QGesture::QGesture(QObject *parent)
79 : QObject(*new QGesturePrivate, parent)
80{
81 d_func()->gestureType = Qt::CustomGesture;
82}
83
84/*!
85 \internal
86*/
87QGesture::QGesture(QGesturePrivate &dd, QObject *parent)
88 : QObject(dd, parent)
89{
90}
91
92/*!
93 Destroys the gesture object.
94*/
95QGesture::~QGesture()
96{
97}
98
99/*!
100 \property QGesture::state
101 \brief the current state of the gesture
102*/
103
104/*!
105 \property QGesture::gestureType
106 \brief the type of the gesture
107*/
108
109/*!
110 \property QGesture::hotSpot
111
112 \brief The point that is used to find the receiver for the gesture event.
113
114 The hot-spot is a point in the global coordinate system, use
115 QWidget::mapFromGlobal() or QGestureEvent::mapToGraphicsScene() to get a
116 local hot-spot.
117
118 The hot-spot should be set by the gesture recognizer to allow gesture event
119 delivery to a QGraphicsObject.
120*/
121
122/*!
123 \property QGesture::hasHotSpot
124 \brief whether the gesture has a hot-spot
125*/
126
127Qt::GestureType QGesture::gestureType() const
128{
129 return d_func()->gestureType;
130}
131
132Qt::GestureState QGesture::state() const
133{
134 return d_func()->state;
135}
136
137QPointF QGesture::hotSpot() const
138{
139 return d_func()->hotSpot;
140}
141
142void QGesture::setHotSpot(const QPointF &value)
143{
144 Q_D(QGesture);
145 d->hotSpot = value;
146 d->isHotSpotSet = true;
147}
148
149bool QGesture::hasHotSpot() const
150{
151 return d_func()->isHotSpotSet;
152}
153
154void QGesture::unsetHotSpot()
155{
156 d_func()->isHotSpotSet = false;
157}
158
159/*!
160 \property QGesture::gestureCancelPolicy
161 \brief the policy for deciding what happens on accepting a gesture
162
163 On accepting one gesture Qt can automatically cancel other gestures
164 that belong to other targets. The policy is normally set to not cancel
165 any other gestures and can be set to cancel all active gestures in the
166 context. For example for all child widgets.
167*/
168
169/*!
170 \enum QGesture::GestureCancelPolicy
171
172 This enum describes how accepting a gesture can cancel other gestures
173 automatically.
174
175 \value CancelNone On accepting this gesture no other gestures will be affected.
176
177 \value CancelAllInContext On accepting this gesture all gestures that are
178 active in the context (respecting the Qt::GestureFlag that were specified
179 when subscribed to the gesture) will be cancelled.
180*/
181
182void QGesture::setGestureCancelPolicy(GestureCancelPolicy policy)
183{
184 Q_D(QGesture);
185 d->gestureCancelPolicy = static_cast<uint>(policy);
186}
187
188QGesture::GestureCancelPolicy QGesture::gestureCancelPolicy() const
189{
190 Q_D(const QGesture);
191 return static_cast<GestureCancelPolicy>(d->gestureCancelPolicy);
192}
193
194/*!
195 \class QPanGesture
196 \since 4.6
197 \brief The QPanGesture class describes a panning gesture made by the user.
198 \ingroup gestures
199 \inmodule QtWidgets
200
201 \image pangesture.png {Demonstration of moving an image with pan gesture}
202
203 For an overview of gesture handling in Qt and information on using gestures
204 in your applications, see the \l{Gestures in Widgets and Graphics View} document.
205
206 \sa QPinchGesture, QSwipeGesture
207*/
208
209/*!
210 \property QPanGesture::lastOffset
211 \brief the last offset recorded for this gesture
212
213 The last offset contains the change in position of the user's input as
214 reported in the \l offset property when a previous gesture event was
215 delivered for this gesture.
216
217 If no previous event was delivered with information about this gesture
218 (i.e., this gesture object contains information about the first movement
219 in the gesture) then this property contains a zero size.
220*/
221
222/*!
223 \property QPanGesture::offset
224 \brief the total offset from the first input position to the current input
225 position
226
227 The offset measures the total change in position of the user's input
228 covered by the gesture on the input device.
229*/
230
231/*!
232 \property QPanGesture::delta
233 \brief the offset from the previous input position to the current input
234
235 This is essentially the same as the difference between offset() and
236 lastOffset().
237*/
238
239/*!
240 \property QPanGesture::acceleration
241 \brief the acceleration in the motion of the touch point for this gesture
242*/
243
244/*!
245 \property QPanGesture::horizontalVelocity
246 \brief the horizontal component of the motion of the touch point for this
247 gesture
248 \since 4.7.1
249 \internal
250
251 \sa verticalVelocity, acceleration
252*/
253
254/*!
255 \property QPanGesture::verticalVelocity
256 \brief the vertical component of the motion of the touch point for this
257 gesture
258 \since 4.7.1
259 \internal
260
261 \sa horizontalVelocity, acceleration
262*/
263
264/*!
265 \internal
266*/
267QPanGesture::QPanGesture(QObject *parent)
268 : QGesture(*new QPanGesturePrivate, parent)
269{
270 d_func()->gestureType = Qt::PanGesture;
271}
272
273/*!
274 Destructor.
275*/
276QPanGesture::~QPanGesture()
277{
278}
279
280QPointF QPanGesture::lastOffset() const
281{
282 return d_func()->lastOffset;
283}
284
285QPointF QPanGesture::offset() const
286{
287 return d_func()->offset;
288}
289
290QPointF QPanGesture::delta() const
291{
292 Q_D(const QPanGesture);
293 return d->offset - d->lastOffset;
294}
295
296qreal QPanGesture::acceleration() const
297{
298 return d_func()->acceleration;
299}
300
301void QPanGesture::setLastOffset(const QPointF &value)
302{
303 d_func()->lastOffset = value;
304}
305
306void QPanGesture::setOffset(const QPointF &value)
307{
308 d_func()->offset = value;
309}
310
311void QPanGesture::setAcceleration(qreal value)
312{
313 d_func()->acceleration = value;
314}
315
316/*!
317 \class QPinchGesture
318 \since 4.6
319 \brief The QPinchGesture class describes a pinch gesture made by the user.
320 \ingroup touch
321 \ingroup gestures
322 \inmodule QtWidgets
323
324 A pinch gesture is a form of touch user input in which the user typically
325 touches two points on the input device with a thumb and finger, before moving
326 them closer together or further apart to change the scale factor, zoom, or level
327 of detail of the user interface.
328
329 For an overview of gesture handling in Qt and information on using gestures
330 in your applications, see the \l{Gestures in Widgets and Graphics View} document.
331
332 \image pinchgesture.png {Demonstration of pinch gesture with two fingers}
333
334 Instead of repeatedly applying the same pinching gesture, the user may
335 continue to touch the input device in one place, and apply a second touch
336 to a new point, continuing the gesture. When this occurs, gesture events
337 will continue to be delivered to the target object, containing an instance
338 of QPinchGesture in the Qt::GestureUpdated state.
339
340 \sa QPanGesture, QSwipeGesture
341*/
342
343/*!
344 \enum QPinchGesture::ChangeFlag
345
346 This enum describes the changes that can occur to the properties of
347 the gesture object.
348
349 \value ScaleFactorChanged The scale factor held by scaleFactor changed.
350 \value RotationAngleChanged The rotation angle held by rotationAngle changed.
351 \value CenterPointChanged The center point held by centerPoint changed.
352
353 \sa changeFlags, totalChangeFlags
354*/
355
356/*!
357 \property QPinchGesture::totalChangeFlags
358 \brief the property of the gesture that has change
359
360 This property indicates which of the other properties has changed since the
361 gesture has started. You can use this information to determine which aspect
362 of your user interface needs to be updated.
363
364 \sa changeFlags, scaleFactor, rotationAngle, centerPoint
365*/
366
367/*!
368 \property QPinchGesture::changeFlags
369 \brief the property of the gesture that has changed in the current step
370
371 This property indicates which of the other properties has changed since
372 the previous gesture event included information about this gesture. You
373 can use this information to determine which aspect of your user interface
374 needs to be updated.
375
376 \sa totalChangeFlags, scaleFactor, rotationAngle, centerPoint
377*/
378
379/*!
380 \property QPinchGesture::totalScaleFactor
381 \brief the total scale factor
382
383 The total scale factor measures the total change in scale factor from the
384 original value to the current scale factor.
385
386 \sa scaleFactor, lastScaleFactor
387*/
388/*!
389 \property QPinchGesture::lastScaleFactor
390 \brief the last scale factor recorded for this gesture
391
392 The last scale factor contains the scale factor reported in the
393 \l scaleFactor property when a previous gesture event included
394 information about this gesture.
395
396 If no previous event was delivered with information about this gesture
397 (i.e., this gesture object contains information about the first movement
398 in the gesture) then this property contains zero.
399
400 \sa scaleFactor, totalScaleFactor
401*/
402/*!
403 \property QPinchGesture::scaleFactor
404 \brief the current scale factor
405
406 The scale factor measures the scale factor associated with the distance
407 between two of the user's inputs on a touch device.
408
409 \sa totalScaleFactor, lastScaleFactor
410*/
411
412/*!
413 \property QPinchGesture::totalRotationAngle
414 \brief the total angle covered by the gesture
415
416 This total angle measures the complete angle covered by the gesture. Usually, this
417 is equal to the value held by the \l rotationAngle property, except in the case where
418 the user performs multiple rotations by removing and repositioning one of the touch
419 points, as described above. In this case, the total angle will be the sum of the
420 rotation angles for the multiple stages of the gesture.
421
422 \sa rotationAngle, lastRotationAngle
423*/
424/*!
425 \property QPinchGesture::lastRotationAngle
426 \brief the last reported angle covered by the gesture motion
427
428 The last rotation angle is the angle as reported in the \l rotationAngle property
429 when a previous gesture event was delivered for this gesture.
430
431 \sa rotationAngle, totalRotationAngle
432*/
433/*!
434 \property QPinchGesture::rotationAngle
435 \brief the angle covered by the gesture motion
436
437 \sa totalRotationAngle, lastRotationAngle
438*/
439
440/*!
441 \property QPinchGesture::startCenterPoint
442 \brief the starting position of the center point
443
444 \sa centerPoint, lastCenterPoint
445*/
446/*!
447 \property QPinchGesture::lastCenterPoint
448 \brief the last position of the center point recorded for this gesture
449
450 \sa centerPoint, startCenterPoint
451*/
452/*!
453 \property QPinchGesture::centerPoint
454 \brief the current center point
455
456 The center point is the midpoint between the two input points in the gesture.
457
458 \sa startCenterPoint, lastCenterPoint
459*/
460
461/*!
462 \internal
463*/
464QPinchGesture::QPinchGesture(QObject *parent)
465 : QGesture(*new QPinchGesturePrivate, parent)
466{
467 d_func()->gestureType = Qt::PinchGesture;
468}
469
470/*!
471 Destructor.
472*/
473QPinchGesture::~QPinchGesture()
474{
475}
476
477QPinchGesture::ChangeFlags QPinchGesture::totalChangeFlags() const
478{
479 return d_func()->totalChangeFlags;
480}
481
482void QPinchGesture::setTotalChangeFlags(QPinchGesture::ChangeFlags value)
483{
484 d_func()->totalChangeFlags = value;
485}
486
487QPinchGesture::ChangeFlags QPinchGesture::changeFlags() const
488{
489 return d_func()->changeFlags;
490}
491
492void QPinchGesture::setChangeFlags(QPinchGesture::ChangeFlags value)
493{
494 d_func()->changeFlags = value;
495}
496
497QPointF QPinchGesture::startCenterPoint() const
498{
499 return d_func()->startCenterPoint;
500}
501
502QPointF QPinchGesture::lastCenterPoint() const
503{
504 return d_func()->lastCenterPoint;
505}
506
507QPointF QPinchGesture::centerPoint() const
508{
509 return d_func()->centerPoint;
510}
511
512void QPinchGesture::setStartCenterPoint(const QPointF &value)
513{
514 d_func()->startCenterPoint = value;
515}
516
517void QPinchGesture::setLastCenterPoint(const QPointF &value)
518{
519 d_func()->lastCenterPoint = value;
520}
521
522void QPinchGesture::setCenterPoint(const QPointF &value)
523{
524 d_func()->centerPoint = value;
525}
526
527
528qreal QPinchGesture::totalScaleFactor() const
529{
530 return d_func()->totalScaleFactor;
531}
532
533qreal QPinchGesture::lastScaleFactor() const
534{
535 return d_func()->lastScaleFactor;
536}
537
538qreal QPinchGesture::scaleFactor() const
539{
540 return d_func()->scaleFactor;
541}
542
543void QPinchGesture::setTotalScaleFactor(qreal value)
544{
545 d_func()->totalScaleFactor = value;
546}
547
548void QPinchGesture::setLastScaleFactor(qreal value)
549{
550 d_func()->lastScaleFactor = value;
551}
552
553void QPinchGesture::setScaleFactor(qreal value)
554{
555 d_func()->scaleFactor = value;
556}
557
558
559qreal QPinchGesture::totalRotationAngle() const
560{
561 return d_func()->totalRotationAngle;
562}
563
564qreal QPinchGesture::lastRotationAngle() const
565{
566 return d_func()->lastRotationAngle;
567}
568
569qreal QPinchGesture::rotationAngle() const
570{
571 return d_func()->rotationAngle;
572}
573
574void QPinchGesture::setTotalRotationAngle(qreal value)
575{
576 d_func()->totalRotationAngle = value;
577}
578
579void QPinchGesture::setLastRotationAngle(qreal value)
580{
581 d_func()->lastRotationAngle = value;
582}
583
584void QPinchGesture::setRotationAngle(qreal value)
585{
586 d_func()->rotationAngle = value;
587}
588
589/*!
590 \class QSwipeGesture
591 \since 4.6
592 \brief The QSwipeGesture class describes a swipe gesture made by the user.
593 \ingroup gestures
594 \inmodule QtWidgets
595
596 \image swipegesture.png {Demonstration of swipe gesture}
597
598 For an overview of gesture handling in Qt and information on using gestures
599 in your applications, see the \l{Gestures in Widgets and Graphics View} document.
600
601 \sa QPanGesture, QPinchGesture
602*/
603
604/*!
605 \enum QSwipeGesture::SwipeDirection
606
607 This enum describes the possible directions for the gesture's motion
608 along the horizontal and vertical axes.
609
610 \value NoDirection The gesture had no motion associated with it on a particular axis.
611 \value Left The gesture involved a horizontal motion to the left.
612 \value Right The gesture involved a horizontal motion to the right.
613 \value Up The gesture involved an upward vertical motion.
614 \value Down The gesture involved a downward vertical motion.
615*/
616
617/*!
618 \property QSwipeGesture::horizontalDirection
619 \brief the horizontal direction of the gesture
620
621 If the gesture has a horizontal component, the horizontal direction
622 is either Left or Right; otherwise, it is NoDirection.
623
624 \sa verticalDirection, swipeAngle
625*/
626
627/*!
628 \property QSwipeGesture::verticalDirection
629 \brief the vertical direction of the gesture
630
631 If the gesture has a vertical component, the vertical direction
632 is either Up or Down; otherwise, it is NoDirection.
633
634 \sa horizontalDirection, swipeAngle
635*/
636
637/*!
638 \property QSwipeGesture::swipeAngle
639 \brief the angle of the motion associated with the gesture
640
641 If the gesture has either a horizontal or vertical component, the
642 swipe angle describes the angle between the direction of motion and the
643 x-axis as defined using the standard widget
644 \l{Coordinate System}{coordinate system}.
645
646 \sa horizontalDirection, verticalDirection
647*/
648
649/*!
650 \property QSwipeGesture::velocity
651 \since 4.7.1
652 \internal
653*/
654
655/*!
656 \internal
657*/
658QSwipeGesture::QSwipeGesture(QObject *parent)
659 : QGesture(*new QSwipeGesturePrivate, parent)
660{
661 d_func()->gestureType = Qt::SwipeGesture;
662}
663
664/*!
665 Destructor.
666*/
667QSwipeGesture::~QSwipeGesture()
668{
669}
670
671QSwipeGesture::SwipeDirection QSwipeGesture::horizontalDirection() const
672{
673 Q_D(const QSwipeGesture);
674 if (d->swipeAngle < 0 || d->swipeAngle == 90 || d->swipeAngle == 270)
675 return QSwipeGesture::NoDirection;
676 else if (d->swipeAngle < 90 || d->swipeAngle > 270)
677 return QSwipeGesture::Right;
678 else
679 return QSwipeGesture::Left;
680}
681
682QSwipeGesture::SwipeDirection QSwipeGesture::verticalDirection() const
683{
684 Q_D(const QSwipeGesture);
685 if (d->swipeAngle <= 0 || d->swipeAngle == 180)
686 return QSwipeGesture::NoDirection;
687 else if (d->swipeAngle < 180)
688 return QSwipeGesture::Up;
689 else
690 return QSwipeGesture::Down;
691}
692
693qreal QSwipeGesture::swipeAngle() const
694{
695 return d_func()->swipeAngle;
696}
697
698void QSwipeGesture::setSwipeAngle(qreal value)
699{
700 d_func()->swipeAngle = value;
701}
702
703/*!
704 \class QTapGesture
705 \since 4.6
706 \brief The QTapGesture class describes a tap gesture made by the user.
707 \ingroup gestures
708 \inmodule QtWidgets
709
710 For an overview of gesture handling in Qt and information on using gestures
711 in your applications, see the \l{Gestures in Widgets and Graphics View} document.
712
713 \sa QPanGesture, QPinchGesture
714*/
715
716/*!
717 \property QTapGesture::position
718 \brief the position of the tap
719*/
720
721/*!
722 \internal
723*/
724QTapGesture::QTapGesture(QObject *parent)
725 : QGesture(*new QTapGesturePrivate, parent)
726{
727 d_func()->gestureType = Qt::TapGesture;
728}
729
730/*!
731 Destructor.
732*/
733QTapGesture::~QTapGesture()
734{
735}
736
737QPointF QTapGesture::position() const
738{
739 return d_func()->position;
740}
741
742void QTapGesture::setPosition(const QPointF &value)
743{
744 d_func()->position = value;
745}
746/*!
747 \class QTapAndHoldGesture
748 \since 4.6
749 \brief The QTapAndHoldGesture class describes a tap-and-hold (aka LongTap)
750 gesture made by the user.
751 \ingroup gestures
752 \inmodule QtWidgets
753
754 For an overview of gesture handling in Qt and information on using gestures
755 in your applications, see the \l{Gestures in Widgets and Graphics View} document.
756
757 \sa QPanGesture, QPinchGesture
758*/
759
760/*!
761 \property QTapAndHoldGesture::position
762 \brief the position of the tap
763*/
764
765/*!
766 \internal
767*/
768QTapAndHoldGesture::QTapAndHoldGesture(QObject *parent)
769 : QGesture(*new QTapAndHoldGesturePrivate, parent)
770{
771 d_func()->gestureType = Qt::TapAndHoldGesture;
772}
773
774/*!
775 Destructor.
776*/
777QTapAndHoldGesture::~QTapAndHoldGesture()
778{
779}
780
781QPointF QTapAndHoldGesture::position() const
782{
783 return d_func()->position;
784}
785
786void QTapAndHoldGesture::setPosition(const QPointF &value)
787{
788 d_func()->position = value;
789}
790
791/*!
792 Set the timeout, in milliseconds, before the gesture triggers.
793
794 The recognizer will detect a touch down and if \a msecs
795 later the touch is still down, it will trigger the QTapAndHoldGesture.
796 The default value is 700 milliseconds.
797*/
798void QTapAndHoldGesture::setTimeout(int msecs)
799{
800 QTapAndHoldGesturePrivate::Timeout = std::chrono::milliseconds(msecs);
801}
802
803/*!
804 Set the timeout before the gesture triggers.
805
806 The recognizer will detect a touch down and if \a msecs
807 later the touch is still down, it will trigger the QTapAndHoldGesture.
808 The default value is 700 milliseconds.
809 \since 6.13
810*/
811void QTapAndHoldGesture::setTimeout(std::chrono::milliseconds msecs)
812{
813 QTapAndHoldGesturePrivate::Timeout = msecs;
814}
815
816/*!
817 Gets the timeout, in milliseconds, before the gesture triggers.
818
819 The recognizer will detect a touch down and if timeout()
820 later the touch is still down, it will trigger the QTapAndHoldGesture.
821 The default value is 700 milliseconds.
822*/
823int QTapAndHoldGesture::timeout()
824{
825 return QTapAndHoldGesturePrivate::Timeout.count();
826}
827
828/*!
829 Gets the timeout before the gesture triggers.
830
831 The recognizer will detect a touch down and if timeoutAsDuration()
832 later the touch is still down, it will trigger the QTapAndHoldGesture.
833 The default value is 700 milliseconds.
834
835 \sa timeout(), setTimeout()
836 \since 6.13
837*/
838std::chrono::milliseconds QTapAndHoldGesture::timeoutAsDuration()
839{
840 return QTapAndHoldGesturePrivate::Timeout;
841}
842
843std::chrono::milliseconds QTapAndHoldGesturePrivate::Timeout = 700ms;
844
845
846/*!
847 \class QGestureEvent
848 \since 4.6
849 \ingroup events
850 \ingroup gestures
851 \inmodule QtWidgets
852
853 \brief The QGestureEvent class provides the description of triggered gestures.
854
855 The QGestureEvent class contains a list of gestures, which can be obtained using the
856 gestures() function.
857
858 The gestures are either active or canceled. A list of those that are currently being
859 executed can be obtained using the activeGestures() function. A list of those which
860 were previously active and have been canceled can be accessed using the
861 canceledGestures() function. A gesture might be canceled if the current window loses
862 focus, for example, or because of a timeout, or for other reasons.
863
864 If the event handler does not accept the event by calling the generic
865 QEvent::accept() function, all individual QGesture object that were not
866 accepted and in the Qt::GestureStarted state will be propagated up the
867 parent widget chain until a widget accepts them individually, by calling
868 QGestureEvent::accept() for each of them, or an event filter consumes the
869 event.
870
871 \section1 Further Reading
872
873 For an overview of gesture handling in Qt and information on using gestures
874 in your applications, see the \l{Gestures in Widgets and Graphics View} document.
875
876 \sa QGesture, QGestureRecognizer,
877 QWidget::grabGesture(), QGraphicsObject::grabGesture()
878*/
879
880/*!
881 Creates new QGestureEvent containing a list of \a gestures.
882*/
883QGestureEvent::QGestureEvent(const QList<QGesture *> &gestures)
884 : QEvent(QEvent::Gesture), m_gestures(gestures), m_widget(nullptr)
885
886{
887}
888
889/*!
890 Destroys QGestureEvent.
891*/
892QGestureEvent::~QGestureEvent()
893{
894}
895
896/*!
897 Returns all gestures that are delivered in the event.
898*/
899QList<QGesture *> QGestureEvent::gestures() const
900{
901 return m_gestures;
902}
903
904/*!
905 Returns a gesture object by \a type.
906*/
907QGesture *QGestureEvent::gesture(Qt::GestureType type) const
908{
909 for (int i = 0; i < m_gestures.size(); ++i)
910 if (m_gestures.at(i)->gestureType() == type)
911 return m_gestures.at(i);
912 return nullptr;
913}
914
915/*!
916 Returns a list of active (not canceled) gestures.
917*/
918QList<QGesture *> QGestureEvent::activeGestures() const
919{
920 QList<QGesture *> gestures;
921 for (QGesture *gesture : m_gestures) {
922 if (gesture->state() != Qt::GestureCanceled)
923 gestures.append(gesture);
924 }
925 return gestures;
926}
927
928/*!
929 Returns a list of canceled gestures.
930*/
931QList<QGesture *> QGestureEvent::canceledGestures() const
932{
933 QList<QGesture *> gestures;
934 for (QGesture *gesture : m_gestures) {
935 if (gesture->state() == Qt::GestureCanceled)
936 gestures.append(gesture);
937 }
938 return gestures;
939}
940
941/*!
942 Sets the accept flag of the given \a gesture object to the specified \a value.
943
944 Setting the accept flag indicates that the event receiver wants the \a gesture.
945 Unwanted gestures may be propagated to the parent widget.
946
947 By default, gestures in events of type QEvent::Gesture are accepted, and
948 gestures in QEvent::GestureOverride events are ignored.
949
950 For convenience, the accept flag can also be set with
951 \l{QGestureEvent::accept()}{accept(gesture)}, and cleared with
952 \l{QGestureEvent::ignore()}{ignore(gesture)}.
953*/
954void QGestureEvent::setAccepted(QGesture *gesture, bool value)
955{
956 if (gesture)
957 setAccepted(gesture->gestureType(), value);
958}
959
960/*!
961 Sets the accept flag of the given \a gesture object, the equivalent of calling
962 \l{QGestureEvent::setAccepted()}{setAccepted(gesture, true)}.
963
964 Setting the accept flag indicates that the event receiver wants the
965 gesture. Unwanted gestures may be propagated to the parent widget.
966
967 \sa QGestureEvent::ignore()
968*/
969void QGestureEvent::accept(QGesture *gesture)
970{
971 if (gesture)
972 setAccepted(gesture->gestureType(), true);
973}
974
975/*!
976 Clears the accept flag parameter of the given \a gesture object, the equivalent
977 of calling \l{QGestureEvent::setAccepted()}{setAccepted(gesture, false)}.
978
979 Clearing the accept flag indicates that the event receiver does not
980 want the gesture. Unwanted gestures may be propagated to the parent widget.
981
982 \sa QGestureEvent::accept()
983*/
984void QGestureEvent::ignore(QGesture *gesture)
985{
986 if (gesture)
987 setAccepted(gesture->gestureType(), false);
988}
989
990/*!
991 Returns \c true if the \a gesture is accepted; otherwise returns \c false.
992*/
993bool QGestureEvent::isAccepted(QGesture *gesture) const
994{
995 return gesture ? isAccepted(gesture->gestureType()) : false;
996}
997
998/*!
999 Sets the accept flag of the given \a gestureType object to the specified
1000 \a value.
1001
1002 Setting the accept flag indicates that the event receiver wants to receive
1003 gestures of the specified type, \a gestureType. Unwanted gestures may be
1004 propagated to the parent widget.
1005
1006 By default, gestures in events of type QEvent::Gesture are accepted, and
1007 gestures in QEvent::GestureOverride events are ignored.
1008
1009 For convenience, the accept flag can also be set with
1010 \l{QGestureEvent::accept()}{accept(gestureType)}, and cleared with
1011 \l{QGestureEvent::ignore()}{ignore(gestureType)}.
1012*/
1013void QGestureEvent::setAccepted(Qt::GestureType gestureType, bool value)
1014{
1015 setAccepted(false);
1016 m_accepted[gestureType] = value;
1017}
1018
1019/*!
1020 Sets the accept flag of the given \a gestureType, the equivalent of calling
1021 \l{QGestureEvent::setAccepted()}{setAccepted(gestureType, true)}.
1022
1023 Setting the accept flag indicates that the event receiver wants the
1024 gesture. Unwanted gestures may be propagated to the parent widget.
1025
1026 \sa QGestureEvent::ignore()
1027*/
1028void QGestureEvent::accept(Qt::GestureType gestureType)
1029{
1030 setAccepted(gestureType, true);
1031}
1032
1033/*!
1034 Clears the accept flag parameter of the given \a gestureType, the equivalent
1035 of calling \l{QGestureEvent::setAccepted()}{setAccepted(gesture, false)}.
1036
1037 Clearing the accept flag indicates that the event receiver does not
1038 want the gesture. Unwanted gestures may be propagated to the parent widget.
1039
1040 \sa QGestureEvent::accept()
1041*/
1042void QGestureEvent::ignore(Qt::GestureType gestureType)
1043{
1044 setAccepted(gestureType, false);
1045}
1046
1047/*!
1048 Returns \c true if the gesture of type \a gestureType is accepted; otherwise
1049 returns \c false.
1050*/
1051bool QGestureEvent::isAccepted(Qt::GestureType gestureType) const
1052{
1053 return m_accepted.value(gestureType, true);
1054}
1055
1056/*!
1057 \internal
1058
1059 Sets the widget for this event to the \a widget specified.
1060*/
1061void QGestureEvent::setWidget(QWidget *widget)
1062{
1063 m_widget = widget;
1064}
1065
1066/*!
1067 Returns the widget on which the event occurred.
1068*/
1069QWidget *QGestureEvent::widget() const
1070{
1071 return m_widget;
1072}
1073
1074#if QT_CONFIG(graphicsview)
1075/*!
1076 Returns the scene-local coordinates if the \a gesturePoint is inside a
1077 graphics view.
1078
1079 This functional might be useful when the gesture event is delivered to a
1080 QGraphicsObject to translate a point in screen coordinates to scene-local
1081 coordinates.
1082
1083 \sa QPointF::isNull()
1084*/
1085QPointF QGestureEvent::mapToGraphicsScene(const QPointF &gesturePoint) const
1086{
1087 QWidget *w = widget();
1088 if (w) // we get the viewport as widget, not the graphics view
1089 w = w->parentWidget();
1090 QGraphicsView *view = qobject_cast<QGraphicsView*>(w);
1091 if (view)
1092 return view->mapToScene(view->mapFromGlobal(gesturePoint).toPoint());
1093 return QPointF();
1094}
1095#endif // QT_CONFIG(graphicsview)
1096
1097#ifndef QT_NO_DEBUG_STREAM
1098
1099static void formatGestureHeader(QDebug d, const char *className, const QGesture *gesture)
1100{
1101 d << className << "(state=";
1102 QtDebugUtils::formatQEnum(d, gesture->state());
1103 if (gesture->hasHotSpot()) {
1104 d << ",hotSpot=";
1105 QtDebugUtils::formatQPoint(d, gesture->hotSpot());
1106 }
1107}
1108
1109Q_WIDGETS_EXPORT QDebug operator<<(QDebug d, const QGesture *gesture)
1110{
1111 QDebugStateSaver saver(d);
1112 d.nospace();
1113
1114 if (!gesture)
1115 return d << "QGesture(0x0)";
1116
1117 switch (gesture->gestureType()) {
1118 case Qt::TapGesture:
1119 formatGestureHeader(d, "QTapGesture", gesture);
1120 d << ",position=";
1121 QtDebugUtils::formatQPoint(d, static_cast<const QTapGesture*>(gesture)->position());
1122 d << ')';
1123 break;
1124 case Qt::TapAndHoldGesture: {
1125 const QTapAndHoldGesture *tap = static_cast<const QTapAndHoldGesture*>(gesture);
1126 formatGestureHeader(d, "QTapAndHoldGesture", tap);
1127 d << ",position=";
1128 QtDebugUtils::formatQPoint(d, tap->position());
1129 d << ",timeout=" << tap->timeout() << ')';
1130 }
1131 break;
1132 case Qt::PanGesture: {
1133 const QPanGesture *pan = static_cast<const QPanGesture*>(gesture);
1134 formatGestureHeader(d, "QPanGesture", pan);
1135 d << ",lastOffset=";
1136 QtDebugUtils::formatQPoint(d, pan->lastOffset());
1137 d << pan->lastOffset();
1138 d << ",offset=";
1139 QtDebugUtils::formatQPoint(d, pan->offset());
1140 d << ",acceleration=" << pan->acceleration() << ",delta=";
1141 QtDebugUtils::formatQPoint(d, pan->delta());
1142 d << ')';
1143 }
1144 break;
1145 case Qt::PinchGesture: {
1146 const QPinchGesture *pinch = static_cast<const QPinchGesture*>(gesture);
1147 formatGestureHeader(d, "QPinchGesture", pinch);
1148 d << ",totalChangeFlags=" << pinch->totalChangeFlags()
1149 << ",changeFlags=" << pinch->changeFlags() << ",startCenterPoint=";
1150 QtDebugUtils::formatQPoint(d, pinch->startCenterPoint());
1151 d << ",lastCenterPoint=";
1152 QtDebugUtils::formatQPoint(d, pinch->lastCenterPoint());
1153 d << ",centerPoint=";
1154 QtDebugUtils::formatQPoint(d, pinch->centerPoint());
1155 d << ",totalScaleFactor=" << pinch->totalScaleFactor()
1156 << ",lastScaleFactor=" << pinch->lastScaleFactor()
1157 << ",scaleFactor=" << pinch->scaleFactor()
1158 << ",totalRotationAngle=" << pinch->totalRotationAngle()
1159 << ",lastRotationAngle=" << pinch->lastRotationAngle()
1160 << ",rotationAngle=" << pinch->rotationAngle() << ')';
1161 }
1162 break;
1163 case Qt::SwipeGesture: {
1164 const QSwipeGesture *swipe = static_cast<const QSwipeGesture*>(gesture);
1165 formatGestureHeader(d, "QSwipeGesture", swipe);
1166 d << ",horizontalDirection=";
1167 QtDebugUtils::formatQEnum(d, swipe->horizontalDirection());
1168 d << ",verticalDirection=";
1169 QtDebugUtils::formatQEnum(d, swipe->verticalDirection());
1170 d << ",swipeAngle=" << swipe->swipeAngle() << ')';
1171 }
1172 break;
1173 default:
1174 formatGestureHeader(d, "Custom gesture", gesture);
1175 d << ",type=" << gesture->gestureType() << ')';
1176 break;
1177 }
1178 return d;
1179}
1180
1181Q_WIDGETS_EXPORT QDebug operator<<(QDebug d, const QGestureEvent *gestureEvent)
1182{
1183 QDebugStateSaver saver(d);
1184 d.nospace();
1185 d << "QGestureEvent(";
1186 if (gestureEvent)
1187 d << gestureEvent->gestures();
1188 else
1189 d << "0x0";
1190 return d << ')';
1191}
1192
1193#endif // !QT_NO_DEBUG_STREAM
1194QT_END_NAMESPACE
1195
1196#include <moc_qgesture.cpp>
1197
1198#endif // QT_NO_GESTURES
Q_CORE_EXPORT QDebug operator<<(QDebug debug, QDir::Filters filters)
Definition qdir.cpp:2620
static void formatGestureHeader(QDebug d, const char *className, const QGesture *gesture)