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