1 | /****************************************************************************
|
---|
2 | **
|
---|
3 | ** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
|
---|
4 | ** All rights reserved.
|
---|
5 | ** Contact: Nokia Corporation ([email protected])
|
---|
6 | **
|
---|
7 | ** This file is part of the documentation of the Qt Toolkit.
|
---|
8 | **
|
---|
9 | ** $QT_BEGIN_LICENSE:LGPL$
|
---|
10 | ** Commercial Usage
|
---|
11 | ** Licensees holding valid Qt Commercial licenses may use this file in
|
---|
12 | ** accordance with the Qt Commercial License Agreement provided with the
|
---|
13 | ** Software or, alternatively, in accordance with the terms contained in
|
---|
14 | ** a written agreement between you and Nokia.
|
---|
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 | ** GNU General Public License Usage
|
---|
29 | ** Alternatively, this file may be used under the terms of the GNU
|
---|
30 | ** General Public License version 3.0 as published by the Free Software
|
---|
31 | ** Foundation and appearing in the file LICENSE.GPL included in the
|
---|
32 | ** packaging of this file. Please review the following information to
|
---|
33 | ** ensure the GNU General Public License version 3.0 requirements will be
|
---|
34 | ** met: http://www.gnu.org/copyleft/gpl.html.
|
---|
35 | **
|
---|
36 | ** If you have questions regarding the use of this file, please contact
|
---|
37 | ** Nokia at [email protected].
|
---|
38 | ** $QT_END_LICENSE$
|
---|
39 | **
|
---|
40 | ****************************************************************************/
|
---|
41 |
|
---|
42 | /*!
|
---|
43 | \page gestures-overview.html
|
---|
44 | \title Gestures Programming
|
---|
45 | \ingroup frameworks-technologies
|
---|
46 | \startpage index.html Qt Reference Documentation
|
---|
47 |
|
---|
48 | \brief An overview of the Qt support for Gesture programming.
|
---|
49 |
|
---|
50 | Qt includes a framework for gesture programming that gives has the ability
|
---|
51 | to form gestures from a series of events, independently of the input methods
|
---|
52 | used. A gesture could be a particular movement of a mouse, a touch screen
|
---|
53 | action, or a series of events from some other source. The nature of the input,
|
---|
54 | the interpretation of the gesture and the action taken are the choice of the
|
---|
55 | developer.
|
---|
56 |
|
---|
57 | \tableofcontents
|
---|
58 |
|
---|
59 | \section1 Overview
|
---|
60 |
|
---|
61 | QGesture is the central class in Qt's gesture framework, providing a container
|
---|
62 | for information about gestures performed by the user. QGesture exposes
|
---|
63 | properties that give general information that is common to all gestures, and
|
---|
64 | these can be extended to provide additional gesture-specific information.
|
---|
65 | Common panning, pinching and swiping gestures are represented by specialized
|
---|
66 | classes: QPanGesture, QPinchGesture and QSwipeGesture.
|
---|
67 |
|
---|
68 | Developers can also implement new gestures by subclassing and extending the
|
---|
69 | QGestureRecognizer class. Adding support for a new gesture involves implementing
|
---|
70 | code to recognize the gesture from input events. This is described in the
|
---|
71 | \l{Creating Your Own Gesture Recognizer} section.
|
---|
72 |
|
---|
73 | \section1 Using Standard Gestures with Widgets
|
---|
74 |
|
---|
75 | Gestures can be enabled for instances of QWidget and QGraphicsObject subclasses.
|
---|
76 | An object that accepts gesture input is referred to as a \e{target object}.
|
---|
77 |
|
---|
78 | To enable a gesture for a target object, call its QWidget::grabGesture() or
|
---|
79 | QGraphicsObject::grabGesture() function with an argument describing the
|
---|
80 | required gesture type. The standard types are defined by the Qt::GestureType
|
---|
81 | enum and include many commonly used gestures.
|
---|
82 |
|
---|
83 | \snippet examples/gestures/imagegestures/imagewidget.cpp enable gestures
|
---|
84 |
|
---|
85 | In the above code, the gesture is set up in the constructor of the target object
|
---|
86 | itself.
|
---|
87 |
|
---|
88 | When the user performs a gesture, QGestureEvent events will be delivered to the
|
---|
89 | target object, and these can be handled by reimplementing the QWidget::event()
|
---|
90 | handler function for widgets or QGraphicsItem::sceneEvent() for graphics objects.
|
---|
91 |
|
---|
92 | For convenience, the \l{Image Gestures Example} reimplements the general
|
---|
93 | \l{QWidget::}{event()} handler function and delegates gesture events to a
|
---|
94 | specialized gestureEvent() function:
|
---|
95 |
|
---|
96 | \snippet examples/gestures/imagegestures/imagewidget.cpp event handler
|
---|
97 |
|
---|
98 | The gesture events delivered to the target object can be examined individually
|
---|
99 | and dealt with appropriately:
|
---|
100 |
|
---|
101 | \snippet examples/gestures/imagegestures/imagewidget.cpp gesture event handler
|
---|
102 |
|
---|
103 | Responding to a gesture is simply a matter of obtaining the QGesture object
|
---|
104 | delivered in the QGestureEvent sent to the target object and examining the
|
---|
105 | information it contains.
|
---|
106 |
|
---|
107 | \snippet examples/gestures/imagegestures/imagewidget.cpp swipe function
|
---|
108 |
|
---|
109 | Here, we examine the direction in which the user swiped the widget and modify
|
---|
110 | its contents accordingly.
|
---|
111 |
|
---|
112 |
|
---|
113 | \section1 Creating Your Own Gesture Recognizer
|
---|
114 |
|
---|
115 | Adding support for a new gesture involves creating and registering a new gesture
|
---|
116 | recognizer. Depending on the recognition process for the gesture, it may also
|
---|
117 | involve creating a new gesture object.
|
---|
118 |
|
---|
119 | To create a new recognizer, you need to subclass QGestureRecognizer to create a
|
---|
120 | custom recognizer class. There is one virtual function that you must reimplement
|
---|
121 | and two others that can be reimplemented as required.
|
---|
122 |
|
---|
123 | \section2 Filtering Input Events
|
---|
124 |
|
---|
125 | The \l{QGestureRecognizer::}{recognize()} function must be reimplemented.
|
---|
126 | This function handles and filters the incoming input events for the target objects
|
---|
127 | and determines whether or not they correspond to the gesture the recognizer is
|
---|
128 | looking for.
|
---|
129 |
|
---|
130 | Although the logic for gesture recognition is implemented in this function,
|
---|
131 | possibly using a state machine based on the Qt::GestureState enums, you can store
|
---|
132 | persistent information about the state of the recognition process in the QGesture
|
---|
133 | object supplied.
|
---|
134 |
|
---|
135 | Your \l{QGestureRecognizer::}{recognize()} function must return a value of
|
---|
136 | QGestureRecognizer::Result that indicates the state of recognition for a given gesture and
|
---|
137 | target object. This determines whether or not a gesture event will be delivered
|
---|
138 | to a target object.
|
---|
139 |
|
---|
140 | \section2 Custom Gestures
|
---|
141 |
|
---|
142 | If you choose to represent a gesture by a custom QGesture subclass, you will need to
|
---|
143 | reimplement the \l{QGestureRecognizer::}{create()} function to construct
|
---|
144 | instances of your gesture class instead of standard QGesture instances. Alternatively,
|
---|
145 | you may want to use standard QGesture instances, but add additional dynamic properties
|
---|
146 | to them to express specific details of the gesture you want to handle.
|
---|
147 |
|
---|
148 | \section2 Resetting Gestures
|
---|
149 |
|
---|
150 | If you use custom gesture objects that need to be reset or otherwise specially
|
---|
151 | handled when a gesture is canceled, you need to reimplement the
|
---|
152 | \l{QGestureRecognizer::}{reset()} function to perform these special tasks.
|
---|
153 |
|
---|
154 | Note that QGesture objects are only created once for each combination of target object
|
---|
155 | and gesture type, and they might be reused every time the user attempts to perform the
|
---|
156 | same gesture type on the target object. As a result, it can be useful to reimplement
|
---|
157 | the \l{QGestureRecognizer::}{reset()} function to clean up after each previous attempt
|
---|
158 | at recognizing a gesture.
|
---|
159 |
|
---|
160 |
|
---|
161 | \section1 Using a New Gesture Recognizer
|
---|
162 |
|
---|
163 | To use a gesture recognizer, construct an instance of your QGestureRecognizer
|
---|
164 | subclass, and register it with the application with
|
---|
165 | QGestureRecognizer::registerRecognizer(). A recognizer for a given type of
|
---|
166 | gesture can be removed with QGestureRecognizer::unregisterRecognizer().
|
---|
167 |
|
---|
168 |
|
---|
169 | \section1 Further Reading
|
---|
170 |
|
---|
171 | The \l{gestures/imagegestures}{Image Gestures Example} shows how to enable
|
---|
172 | gestures for a widget in a simple image viewer application.
|
---|
173 | */
|
---|