|
1 /**************************************************************************** |
|
2 ** |
|
3 ** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies). |
|
4 ** All rights reserved. |
|
5 ** Contact: Nokia Corporation (qt-info@nokia.com) |
|
6 ** |
|
7 ** This file is part of the documentation 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 /*! |
|
43 \example gestures/imagegestures |
|
44 \title Image Gestures Example |
|
45 |
|
46 This example shows how to enable gestures for a widget and use gesture input |
|
47 to perform actions. |
|
48 |
|
49 \image imagegestures-example.png Screenshot of the Image Gestures example. |
|
50 |
|
51 We use two classes to create the user interface for the application: \c MainWidget |
|
52 and \c ImageWidget. The \c MainWidget class is simply used as a container for the |
|
53 \c ImageWidget class, which we will configure to accept gesture input. Since we |
|
54 are interested in the way gestures are used, we will concentrate on the |
|
55 implementation of the \c ImageWidget class. |
|
56 |
|
57 \section1 ImageWidget Class Definition |
|
58 |
|
59 The \c ImageWidget class is a simple QWidget subclass that reimplements the general |
|
60 QWidget::event() handler function in addition to several more specific event handlers: |
|
61 |
|
62 \snippet examples/gestures/imagegestures/imagewidget.h class definition begin |
|
63 \dots |
|
64 \snippet examples/gestures/imagegestures/imagewidget.h class definition end |
|
65 |
|
66 We also implement a private helper function, \c gestureEvent(), to help manage |
|
67 gesture events delivered to the widget, and three functions to perform actions |
|
68 based on gestures: \c panTriggered(), \c pinchTriggered() and \c swipeTriggered(). |
|
69 |
|
70 \section1 ImageWidget Class Implementation |
|
71 |
|
72 In the widget's constructor, we begin by setting up various parameters that will |
|
73 be used to control the way images are displayed. |
|
74 |
|
75 \snippet examples/gestures/imagegestures/imagewidget.cpp constructor |
|
76 |
|
77 We enable three of the standard gestures for the widget by calling QWidget::grabGesture() |
|
78 with the types of gesture we need. These will be recognized by the application's |
|
79 default gesture recognizer, and events will be delivered to our widget. |
|
80 |
|
81 Since QWidget does not define a specific event handler for gestures, the widget |
|
82 needs to reimplement the general QWidget::event() to receive gesture events. |
|
83 |
|
84 \snippet examples/gestures/imagegestures/imagewidget.cpp event handler |
|
85 |
|
86 We implement the event handler to delegate gesture events to a private function |
|
87 specifically written for the task, and pass all other events to QWidget's |
|
88 implementation. |
|
89 |
|
90 The \c gestureHandler() function examines the gestures supplied by the |
|
91 newly-delivered QGestureEvent. Since only one gesture of a given type can be |
|
92 used on a widget at any particular time, we can check for each gesture type |
|
93 using the QGestureEvent::gesture() function: |
|
94 |
|
95 \snippet examples/gestures/imagegestures/imagewidget.cpp gesture event handler |
|
96 |
|
97 If a QGesture object is supplied for a certain type of gesture, we call a special |
|
98 purpose function to deal with it, casting the gesture object to the appropriate |
|
99 QGesture subclass. |
|
100 |
|
101 To illustrate how a standard gesture can be interpreted by an application, we |
|
102 show the implementation of the \c swipeTriggered() function, which handles the |
|
103 gesture associated with a brushing or swiping motion on the user's display or |
|
104 input device: |
|
105 |
|
106 \snippet examples/gestures/imagegestures/imagewidget.cpp swipe function |
|
107 |
|
108 The QSwipeGesture class provides specialized functions and defines a enum |
|
109 to make it more convenient for developers to discover which direction, if |
|
110 any, the user swiped the display. Here, we simply navigate to the previous |
|
111 image in the collection if the user swiped upwards or to the left; otherwise |
|
112 we navigate to the next image in the collection. |
|
113 |
|
114 The other gestures are also handled by special purpose functions, but use |
|
115 the values of properties held by the QGesture object passed to them. |
|
116 */ |