| 1 |
/**************************************************************************** |
| 2 |
** |
| 3 |
** Copyright (C) 2017 The Qt Company Ltd. |
| 4 |
** Contact: https://www.qt.io/licensing/ |
| 5 |
** |
| 6 |
** This file is part of the documentation of the Qt Toolkit. |
| 7 |
** |
| 8 |
** $QT_BEGIN_LICENSE:FDL$ |
| 9 |
** Commercial License Usage |
| 10 |
** Licensees holding valid commercial Qt licenses may use this file in |
| 11 |
** accordance with the commercial license agreement provided with the |
| 12 |
** Software or, alternatively, in accordance with the terms contained in |
| 13 |
** a written agreement between you and The Qt Company. For licensing terms |
| 14 |
** and conditions see https://www.qt.io/terms-conditions. For further |
| 15 |
** information use the contact form at https://www.qt.io/contact-us. |
| 16 |
** |
| 17 |
** GNU Free Documentation License Usage |
| 18 |
** Alternatively, this file may be used under the terms of the GNU Free |
| 19 |
** Documentation License version 1.3 as published by the Free Software |
| 20 |
** Foundation and appearing in the file included in the packaging of |
| 21 |
** this file. Please review the following information to ensure |
| 22 |
** the GNU Free Documentation License version 1.3 requirements |
| 23 |
** will be met: https://www.gnu.org/licenses/fdl-1.3.html. |
| 24 |
** $QT_END_LICENSE$ |
| 25 |
** |
| 26 |
****************************************************************************/ |
| 27 |
|
| 28 |
/*! |
| 29 |
\example hapticsquare |
| 30 |
\title Haptic Square Example |
| 31 |
|
| 32 |
\section1 Overview |
| 33 |
|
| 34 |
This example shows how to use simple haptic effects in an application via |
| 35 |
the \l{Feedback}{Qt Feedback API}. |
| 36 |
|
| 37 |
It provides an example of how to use the Qt Feedback |
| 38 |
\list |
| 39 |
\li play "system theme" haptic effects corresponding to certain predefined events |
| 40 |
\li play a custom effect, single or repeating |
| 41 |
\endlist |
| 42 |
|
| 43 |
\section2 Use Case |
| 44 |
|
| 45 |
Compelling applications attempt to immerse the user in the application |
| 46 |
experience. There are many elements to an immersive experience, including |
| 47 |
a consistent and beautiful graphical user interface design, unobtrusive yet |
| 48 |
informative sound design, and intuitive program flow. Another important |
| 49 |
aspect of immersive applications is tactile feedback and haptic effects. |
| 50 |
|
| 51 |
The \l{Feedback}{QtFeedback API} allows application developers to |
| 52 |
include tactile feedback into their application via a simple to use and |
| 53 |
extensible API. Some common uses for tactile feedback are: |
| 54 |
|
| 55 |
\list |
| 56 |
\li maintain consistency with system theme for tactile feedback about interface events (button clicks, scrolling, etc) |
| 57 |
\li notify the user of an application-specific event (invalid operation, status change, etc) |
| 58 |
\li multisensory user interface (status can be "read" by touching the screen, tactile interfaces, etc) |
| 59 |
\li immersive gaming experiences (explosions, impacts, collisions, etc) |
| 60 |
\endlist |
| 61 |
|
| 62 |
This example application provides some short snippets which illustrate how |
| 63 |
the first two of those use cases may be fulfilled. |
| 64 |
|
| 65 |
\section1 The Application |
| 66 |
|
| 67 |
The application is designed to work on desktop and mobile platforms with |
| 68 |
minimal differences in code between the platforms. The interface consists |
| 69 |
of four buttons arranged into a square, each of which causes a different |
| 70 |
tactile effect to be played by the default tactile effect provider plugin |
| 71 |
on the platform. |
| 72 |
|
| 73 |
\image hapticsquare-example.png |
| 74 |
|
| 75 |
\list |
| 76 |
\li "Rumble!" plays a non-repeating effect with symmetric attack and decay |
| 77 |
\li "Ocean" is a toggle button which plays a repeating ocean wave-like effect |
| 78 |
\li "Click" plays the system theme effect for a basic button click |
| 79 |
\li "Oops!" plays the system theme effect for a negative or invalid response |
| 80 |
\endlist |
| 81 |
|
| 82 |
The example implements two classes: |
| 83 |
|
| 84 |
\list |
| 85 |
\li \c {HapticButton}: Implementation of a button. It |
| 86 |
inherits QWidget and sends signals for button clicks. |
| 87 |
\li \c {Dialog}: A QDialog subclass that displays the four |
| 88 |
\c{HapticButton}s mentioned above, |
| 89 |
connects them to its slots, and implements the functionality to |
| 90 |
play the haptic effects. |
| 91 |
\endlist |
| 92 |
|
| 93 |
\section2 The Dialog Class |
| 94 |
|
| 95 |
We will now go through the code for the \c Dialog class. Here is its |
| 96 |
definition: |
| 97 |
|
| 98 |
\snippet examples/hapticsquare/hapticsquare.h 0 |
| 99 |
|
| 100 |
The buttons are connected to the slots, which play the effects. We will now |
| 101 |
go through the implementation of \c Dialog. |
| 102 |
|
| 103 |
The constructor starts by setting up the non-repeating haptic effect, which |
| 104 |
is played by clicking the \gui {Rumble! Button}. |
| 105 |
|
| 106 |
\snippet examples/hapticsquare/hapticsquare.cpp 0 |
| 107 |
|
| 108 |
Custom haptics effects are created by setting up a QFeedbackHapticsEffect. |
| 109 |
|
| 110 |
A haptics effect provides a fade-in of the effect's |
| 111 |
\l{QFeedbackHapticsEffect::}{intensity()}. With vibration, you can think of |
| 112 |
the intensity as how hard the device will vibrate. The effect will start at |
| 113 |
\l{QFeedbackHapticsEffect::}{attackIntensity()} and interpolate to |
| 114 |
\l{QFeedbackHapticsEffect::}{intensity()} in |
| 115 |
\l{QFeedbackHapticsEffect::}{attackTime()} milliseconds. When the effect |
| 116 |
ends, we have a similar fade-out, where the haptics effect's intensity will |
| 117 |
interpolate from \l{QFeedbackHapticsEffect::}{intensity()} to |
| 118 |
\l{QFeedbackHapticsEffect::}{fadeTime()} in |
| 119 |
\l{QFeedbackHapticsEffect::}{fadeTime()} milliseconds. |
| 120 |
The effect will last for a total duration of |
| 121 |
\l{QFeedbackHapticsEffect::}{duration()} milliseconds. |
| 122 |
|
| 123 |
We next set up the effect for the \gui {Ocean Button}. |
| 124 |
|
| 125 |
\snippet examples/hapticsquare/hapticsquare.cpp 1 |
| 126 |
|
| 127 |
The \c m_ocean is a periodic effect, i.e., it repeats after |
| 128 |
\l{QFeedbackHapticsEffect::}{period()} milliseconds. Note that the |
| 129 |
\l{QFeedbackHapticsEffect::}{duration()} must be greater than the period |
| 130 |
in order for the periodicity of the effect to be discernable. |
| 131 |
|
| 132 |
We then set up the GUI and connects the buttons to slots that will play the |
| 133 |
effects. |
| 134 |
|
| 135 |
\snippet examples/hapticsquare/hapticsquare.cpp 2 |
| 136 |
|
| 137 |
Let's look at the slots to see how the effects are played. |
| 138 |
|
| 139 |
\snippet examples/hapticsquare/hapticsquare.cpp 3 |
| 140 |
|
| 141 |
With the \c m_rumble, we only have to call \l{QFeedbackEffect::}{start()}. It |
| 142 |
will stop when the effect has finished, and can be played again by calling |
| 143 |
\l{QFeedbackEffect::}{start()} again. |
| 144 |
|
| 145 |
The periodic \c m_ocean effect is started the same way as the \c m_rumble |
| 146 |
effect, and may be stopped with the \l{QFeedbackEffect::}{stop()} function. |
| 147 |
It will start playing from the beginning again when |
| 148 |
\l{QFeedbackEffect::}{start()} is called. We could also have paused the |
| 149 |
effect with \l{QFeedbackEffect::}{pause()}. |
| 150 |
|
| 151 |
\snippet examples/hapticsquare/hapticsquare.cpp 4 |
| 152 |
|
| 153 |
System theme effects are played with the static |
| 154 |
QFeedbackEffect::playThemeEffect() function. Theme effects cannot be stopped |
| 155 |
or paused. There is no guarantee that the backend can play the effect; |
| 156 |
\l{QFeedbackEffect::}{playThemeEffect()} will return false if the effect |
| 157 |
could not be played. |
| 158 |
|
| 159 |
\section1 Known Issues |
| 160 |
|
| 161 |
The example is not intended to exercise the entire API. Instead, it is a |
| 162 |
simple example which illustrates some simple uses of the API. |
| 163 |
Also, the example will not work correctly on platforms which do not have a |
| 164 |
QFeedbackHapticInterface (haptic effect provider) plugin loaded. On such |
| 165 |
platforms, clicking the buttons will have no effect. On Maemo5, periodic |
| 166 |
effects do not support attack or fade, and so the ocean effect is not |
| 167 |
smooth. |
| 168 |
*/ |
| 169 |
|