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

source code of qtbase/src/widgets/kernel/qgesture.cpp