author | Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com> |
Wed, 18 Aug 2010 10:37:55 +0300 | |
changeset 33 | 3e2da88830cd |
parent 30 | 5dc02b23752f |
child 37 | 758a864f9613 |
permissions | -rw-r--r-- |
0 | 1 |
/**************************************************************************** |
2 |
** |
|
18
2f34d5167611
Revision: 201011
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
3 |
** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies). |
0 | 4 |
** All rights reserved. |
5 |
** Contact: Nokia Corporation (qt-info@nokia.com) |
|
6 |
** |
|
7 |
** This file is part of the QtGui module of the Qt Toolkit. |
|
8 |
** |
|
9 |
** $QT_BEGIN_LICENSE:LGPL$ |
|
10 |
** No Commercial Usage |
|
11 |
** This file contains pre-release code and may not be distributed. |
|
12 |
** You may use this file in accordance with the terms and conditions |
|
13 |
** contained in the Technology Preview License Agreement accompanying |
|
14 |
** this package. |
|
15 |
** |
|
16 |
** GNU Lesser General Public License Usage |
|
17 |
** Alternatively, this file may be used under the terms of the GNU Lesser |
|
18 |
** General Public License version 2.1 as published by the Free Software |
|
19 |
** Foundation and appearing in the file LICENSE.LGPL included in the |
|
20 |
** packaging of this file. Please review the following information to |
|
21 |
** ensure the GNU Lesser General Public License version 2.1 requirements |
|
22 |
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. |
|
23 |
** |
|
24 |
** In addition, as a special exception, Nokia gives you certain additional |
|
25 |
** rights. These rights are described in the Nokia Qt LGPL Exception |
|
26 |
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. |
|
27 |
** |
|
28 |
** If you have questions regarding the use of this file, please contact |
|
29 |
** Nokia at qt-info@nokia.com. |
|
30 |
** |
|
31 |
** |
|
32 |
** |
|
33 |
** |
|
34 |
** |
|
35 |
** |
|
36 |
** |
|
37 |
** |
|
38 |
** $QT_END_LICENSE$ |
|
39 |
** |
|
40 |
****************************************************************************/ |
|
41 |
||
42 |
#include "qgesturerecognizer.h" |
|
43 |
||
44 |
#include "private/qgesture_p.h" |
|
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
45 |
#include "private/qgesturemanager_p.h" |
0 | 46 |
|
33
3e2da88830cd
Revision: 201031
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
30
diff
changeset
|
47 |
#ifndef QT_NO_GESTURES |
3e2da88830cd
Revision: 201031
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
30
diff
changeset
|
48 |
|
0 | 49 |
QT_BEGIN_NAMESPACE |
50 |
||
51 |
/*! |
|
52 |
\class QGestureRecognizer |
|
53 |
\since 4.6 |
|
54 |
\brief The QGestureRecognizer class provides the infrastructure for gesture recognition. |
|
55 |
\ingroup gestures |
|
56 |
||
57 |
Gesture recognizers are responsible for creating and managing QGesture objects and |
|
58 |
monitoring input events sent to QWidget and QGraphicsObject subclasses. |
|
59 |
QGestureRecognizer is the base class for implementing custom gestures. |
|
60 |
||
61 |
Developers that only need to provide gesture recognition for standard gestures do not |
|
62 |
need to use this class directly. Instances will be created behind the scenes by the |
|
63 |
framework. |
|
64 |
||
65 |
\section1 Recognizing Gestures |
|
66 |
||
67 |
The process of recognizing gestures involves filtering input events sent to specific |
|
68 |
objects, and modifying the associated QGesture objects to include relevant information |
|
69 |
about the user's input. |
|
70 |
||
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
71 |
Gestures are created when the framework calls create() to handle user input |
0 | 72 |
for a particular instance of a QWidget or QGraphicsObject subclass. A QGesture object |
73 |
is created for each widget or item that is configured to use gestures. |
|
74 |
||
75 |
Once a QGesture has been created for a target object, the gesture recognizer will |
|
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
76 |
receive events for it in its recognize() handler function. |
0 | 77 |
|
78 |
When a gesture is canceled, the reset() function is called, giving the recognizer the |
|
79 |
chance to update the appropriate properties in the corresponding QGesture object. |
|
80 |
||
81 |
\section1 Supporting New Gestures |
|
82 |
||
83 |
To add support for new gestures, you need to derive from QGestureRecognizer to create |
|
84 |
a custom recognizer class, construct an instance of this class, and register it with |
|
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
85 |
the application by calling QGestureRecognizer::registerRecognizer(). You can also |
0 | 86 |
subclass QGesture to create a custom gesture class, or rely on dynamic properties |
87 |
to express specific details of the gesture you want to handle. |
|
88 |
||
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
89 |
Your custom QGestureRecognizer subclass needs to reimplement the recognize() |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
90 |
function to handle and filter the incoming input events for QWidget and |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
91 |
QGraphicsObject subclasses. Although the logic for gesture recognition is |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
92 |
implemented in this function, you can store persistent information about the |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
93 |
state of the recognition process in the QGesture object supplied. The |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
94 |
recognize() function must return a value of QGestureRecognizer::Result that |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
95 |
indicates the state of recognition for a given gesture and target object. |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
96 |
This determines whether or not a gesture event will be delivered to a target |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
97 |
object. |
0 | 98 |
|
99 |
If you choose to represent a gesture by a custom QGesture subclass, you will need to |
|
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
100 |
reimplement the create() function to construct instances of your gesture class. |
0 | 101 |
Similarly, you may need to reimplement the reset() function if your custom gesture |
102 |
objects need to be specially handled when a gesture is canceled. |
|
103 |
||
104 |
\sa QGesture |
|
105 |
*/ |
|
106 |
||
107 |
/*! |
|
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
108 |
\enum QGestureRecognizer::ResultFlag |
0 | 109 |
|
110 |
This enum describes the result of the current event filtering step in |
|
111 |
a gesture recognizer state machine. |
|
112 |
||
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
113 |
The result consists of a state value (one of Ignore, MayBeGesture, |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
114 |
TriggerGesture, FinishGesture, CancelGesture) and an optional hint |
0 | 115 |
(ConsumeEventHint). |
116 |
||
117 |
\value Ignore The event does not change the state of the recognizer. |
|
118 |
||
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
119 |
\value MayBeGesture The event changed the internal state of the recognizer, |
0 | 120 |
but it isn't clear yet if it is a gesture or not. The recognizer needs to |
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
121 |
filter more events to decide. Gesture recognizers in the MayBeGesture state |
0 | 122 |
may be reset automatically if they take too long to recognize gestures. |
123 |
||
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
124 |
\value TriggerGesture The gesture has been triggered and the appropriate |
0 | 125 |
QGesture object will be delivered to the target as a part of a |
126 |
QGestureEvent. |
|
127 |
||
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
128 |
\value FinishGesture The gesture has been finished successfully and the |
0 | 129 |
appropriate QGesture object will be delivered to the target as a part of a |
130 |
QGestureEvent. |
|
131 |
||
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
132 |
\value CancelGesture The event made it clear that it is not a gesture. If |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
133 |
the gesture recognizer was in GestureTriggered state before, then the |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
134 |
gesture is canceled and the appropriate QGesture object will be delivered |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
135 |
to the target as a part of a QGestureEvent. |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
136 |
|
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
137 |
\value ConsumeEventHint This hint specifies that the gesture framework |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
138 |
should consume the filtered event and not deliver it to the receiver. |
0 | 139 |
|
140 |
\omitvalue ResultState_Mask |
|
141 |
\omitvalue ResultHint_Mask |
|
142 |
||
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
143 |
\sa QGestureRecognizer::recognize() |
0 | 144 |
*/ |
145 |
||
146 |
/*! |
|
147 |
Constructs a new gesture recognizer object. |
|
148 |
*/ |
|
149 |
QGestureRecognizer::QGestureRecognizer() |
|
150 |
{ |
|
151 |
} |
|
152 |
||
153 |
/*! |
|
154 |
Destroys the gesture recognizer. |
|
155 |
*/ |
|
156 |
QGestureRecognizer::~QGestureRecognizer() |
|
157 |
{ |
|
158 |
} |
|
159 |
||
160 |
/*! |
|
161 |
This function is called by Qt to create a new QGesture object for the |
|
162 |
given \a target (QWidget or QGraphicsObject). |
|
163 |
||
164 |
Reimplement this function to create a custom QGesture-derived gesture |
|
165 |
object if necessary. |
|
30
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
166 |
|
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
167 |
The application takes ownership of the created gesture object. |
0 | 168 |
*/ |
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
169 |
QGesture *QGestureRecognizer::create(QObject *target) |
0 | 170 |
{ |
171 |
Q_UNUSED(target); |
|
172 |
return new QGesture; |
|
173 |
} |
|
174 |
||
175 |
/*! |
|
176 |
This function is called by the framework to reset a given \a gesture. |
|
177 |
||
178 |
Reimplement this function to implement additional requirements for custom QGesture |
|
179 |
objects. This may be necessary if you implement a custom QGesture whose properties |
|
180 |
need special handling when the gesture is reset. |
|
181 |
*/ |
|
182 |
void QGestureRecognizer::reset(QGesture *gesture) |
|
183 |
{ |
|
184 |
if (gesture) { |
|
185 |
QGesturePrivate *d = gesture->d_func(); |
|
186 |
d->state = Qt::NoGesture; |
|
187 |
d->hotSpot = QPointF(); |
|
30
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
188 |
d->sceneHotSpot = QPointF(); |
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
189 |
d->isHotSpotSet = false; |
0 | 190 |
} |
191 |
} |
|
192 |
||
193 |
/*! |
|
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
194 |
\fn QGestureRecognizer::recognize(QGesture *gesture, QObject *watched, QEvent *event) |
0 | 195 |
|
196 |
Handles the given \a event for the \a watched object, updating the state of the \a gesture |
|
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
197 |
object as required, and returns a suitable result for the current recognition step. |
0 | 198 |
|
199 |
This function is called by the framework to allow the recognizer to filter input events |
|
200 |
dispatched to QWidget or QGraphicsObject instances that it is monitoring. |
|
201 |
||
202 |
The result reflects how much of the gesture has been recognized. The state of the |
|
203 |
\a gesture object is set depending on the result. |
|
204 |
||
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
205 |
\sa QGestureRecognizer::Result |
0 | 206 |
*/ |
207 |
||
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
208 |
/*! |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
209 |
Registers the given \a recognizer in the gesture framework and returns a gesture ID |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
210 |
for it. |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
211 |
|
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
212 |
The application takes ownership of the \a recognizer and returns the gesture type |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
213 |
ID associated with it. For gesture recognizers which handle custom QGesture |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
214 |
objects (i.e., those which return Qt::CustomGesture in a QGesture::gestureType() |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
215 |
function) the return value is a generated gesture ID with the Qt::CustomGesture |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
216 |
flag set. |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
217 |
|
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
218 |
\sa unregisterRecognizer(), QGestureRecognizer::create(), QGesture |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
219 |
*/ |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
220 |
Qt::GestureType QGestureRecognizer::registerRecognizer(QGestureRecognizer *recognizer) |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
221 |
{ |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
222 |
return QGestureManager::instance()->registerGestureRecognizer(recognizer); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
223 |
} |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
224 |
|
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
225 |
/*! |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
226 |
Unregisters all gesture recognizers of the specified \a type. |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
227 |
|
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
228 |
\sa registerRecognizer() |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
229 |
*/ |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
230 |
void QGestureRecognizer::unregisterRecognizer(Qt::GestureType type) |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
231 |
{ |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
232 |
QGestureManager::instance()->unregisterGestureRecognizer(type); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
233 |
} |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
234 |
|
0 | 235 |
QT_END_NAMESPACE |
33
3e2da88830cd
Revision: 201031
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
30
diff
changeset
|
236 |
|
3e2da88830cd
Revision: 201031
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
30
diff
changeset
|
237 |
#endif // QT_NO_GESTURES |