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
qgesturerecognizer.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
6
7#include "private/qgesture_p.h"
8#include "private/qgesturemanager_p.h"
9#include "private/qapplication_p.h"
10
11#ifndef QT_NO_GESTURES
12
13QT_BEGIN_NAMESPACE
14
15/*!
16 \class QGestureRecognizer
17 \since 4.6
18 \brief The QGestureRecognizer class provides the infrastructure for gesture recognition.
19 \ingroup gestures
20 \inmodule QtWidgets
21
22 Gesture recognizers are responsible for creating and managing QGesture objects and
23 monitoring input events sent to QWidget and QGraphicsObject subclasses.
24 QGestureRecognizer is the base class for implementing custom gestures.
25
26 Developers that only need to provide gesture recognition for standard gestures do not
27 need to use this class directly. Instances will be created behind the scenes by the
28 framework.
29
30 For an overview of gesture handling in Qt and information on using gestures
31 in your applications, see the \l{Gestures in Widgets and Graphics View} document.
32
33 \section1 Recognizing Gestures
34
35 The process of recognizing gestures involves filtering input events sent to specific
36 objects, and modifying the associated QGesture objects to include relevant information
37 about the user's input.
38
39 Gestures are created when the framework calls create() to handle user input
40 for a particular instance of a QWidget or QGraphicsObject subclass. A QGesture object
41 is created for each widget or item that is configured to use gestures.
42
43 Once a QGesture has been created for a target object, the gesture recognizer will
44 receive events for it in its recognize() handler function.
45
46 When a gesture is canceled, the reset() function is called, giving the recognizer the
47 chance to update the appropriate properties in the corresponding QGesture object.
48
49 \section1 Supporting New Gestures
50
51 To add support for new gestures, you need to derive from QGestureRecognizer to create
52 a custom recognizer class, construct an instance of this class, and register it with
53 the application by calling QGestureRecognizer::registerRecognizer(). You can also
54 subclass QGesture to create a custom gesture class, or rely on dynamic properties
55 to express specific details of the gesture you want to handle.
56
57 Your custom QGestureRecognizer subclass needs to reimplement the recognize()
58 function to handle and filter the incoming input events for QWidget and
59 QGraphicsObject subclasses. Although the logic for gesture recognition is
60 implemented in this function, you can store persistent information about the
61 state of the recognition process in the QGesture object supplied. The
62 recognize() function must return a value of QGestureRecognizer::Result that
63 indicates the state of recognition for a given gesture and target object.
64 This determines whether or not a gesture event will be delivered to a target
65 object.
66
67 If you choose to represent a gesture by a custom QGesture subclass, you will need to
68 reimplement the create() function to construct instances of your gesture class.
69 Similarly, you may need to reimplement the reset() function if your custom gesture
70 objects need to be specially handled when a gesture is canceled.
71
72 \sa QGesture
73*/
74
75/*!
76 \enum QGestureRecognizer::ResultFlag
77
78 This enum describes the result of the current event filtering step in
79 a gesture recognizer state machine.
80
81 The result consists of a state value (one of Ignore, MayBeGesture,
82 TriggerGesture, FinishGesture, CancelGesture) and an optional hint
83 (ConsumeEventHint).
84
85 \value Ignore The event does not change the state of the recognizer.
86
87 \value MayBeGesture The event changed the internal state of the recognizer,
88 but it isn't clear yet if it is a gesture or not. The recognizer needs to
89 filter more events to decide. Gesture recognizers in the MayBeGesture state
90 may be reset automatically if they take too long to recognize gestures.
91
92 \value TriggerGesture The gesture has been triggered and the appropriate
93 QGesture object will be delivered to the target as a part of a
94 QGestureEvent.
95
96 \value FinishGesture The gesture has been finished successfully and the
97 appropriate QGesture object will be delivered to the target as a part of a
98 QGestureEvent.
99
100 \value CancelGesture The event made it clear that it is not a gesture. If
101 the gesture recognizer was in GestureTriggered state before, then the
102 gesture is canceled and the appropriate QGesture object will be delivered
103 to the target as a part of a QGestureEvent.
104
105 \value ConsumeEventHint This hint specifies that the gesture framework
106 should consume the filtered event and not deliver it to the receiver.
107
108 \omitvalue ResultState_Mask
109 \omitvalue ResultHint_Mask
110
111 \sa QGestureRecognizer::recognize()
112*/
113
114/*!
115 Constructs a new gesture recognizer object.
116*/
117QGestureRecognizer::QGestureRecognizer()
118{
119}
120
121/*!
122 Destroys the gesture recognizer.
123*/
124QGestureRecognizer::~QGestureRecognizer()
125{
126}
127
128/*!
129 This function is called by Qt to create a new QGesture object for the
130 given \a target (QWidget or QGraphicsObject).
131
132 Reimplement this function to create a custom QGesture-derived gesture
133 object if necessary.
134
135 QApplication takes ownership of the created gesture object.
136*/
137QGesture *QGestureRecognizer::create(QObject *target)
138{
139 Q_UNUSED(target);
140 return new QGesture;
141}
142
143/*!
144 This function is called by Qt to reset a given \a gesture.
145
146 Reimplement this function to implement additional requirements for custom QGesture
147 objects. This may be necessary if you implement a custom QGesture whose properties
148 need special handling when the gesture is reset.
149*/
150void QGestureRecognizer::reset(QGesture *gesture)
151{
152 if (gesture) {
153 QGesturePrivate *d = gesture->d_func();
154 d->state = Qt::NoGesture;
155 d->hotSpot = QPointF();
156 d->sceneHotSpot = QPointF();
157 d->isHotSpotSet = false;
158 }
159}
160
161/*!
162 \fn QGestureRecognizer::Result QGestureRecognizer::recognize(QGesture *gesture, QObject *watched, QEvent *event)
163
164 Handles the given \a event for the \a watched object, updating the state of the \a gesture
165 object as required, and returns a suitable result for the current recognition step.
166
167 This function is called by the framework to allow the recognizer to filter input events
168 dispatched to QWidget or QGraphicsObject instances that it is monitoring.
169
170 The result reflects how much of the gesture has been recognized. The state of the
171 \a gesture object is set depending on the result.
172
173 \sa QGestureRecognizer::Result
174*/
175
176/*!
177 Registers the given \a recognizer in the gesture framework and returns a gesture ID
178 for it.
179
180 QApplication takes ownership of the \a recognizer, and this function
181 returns the gesture type ID associated with it. For gesture recognizers
182 that handle custom QGesture objects (those that return Qt::CustomGesture in
183 a QGesture::gestureType() function), the return value is a generated
184 gesture ID with the Qt::CustomGesture flag set.
185
186 \sa unregisterRecognizer(), QGestureRecognizer::create(), QGesture
187*/
188Qt::GestureType QGestureRecognizer::registerRecognizer(QGestureRecognizer *recognizer)
189{
190 return QGestureManager::instance()->registerGestureRecognizer(recognizer);
191}
192
193/*!
194 Unregisters all gesture recognizers of the specified \a type.
195
196 \sa registerRecognizer()
197*/
198void QGestureRecognizer::unregisterRecognizer(Qt::GestureType type)
199{
200 auto qAppPriv = QApplicationPrivate::instance();
201 if (!qAppPriv)
202 return;
203 if (!qAppPriv->gestureManager)
204 return;
205 QGestureManager::instance()->unregisterGestureRecognizer(type);
206}
207
208QT_END_NAMESPACE
209
210#endif // QT_NO_GESTURES