/**************************************************************************** ** ** Copyright (C) 2017 The Qt Company Ltd. ** Contact: https://www.qt.io/licensing/ ** ** This file is part of the documentation of the Qt Toolkit. ** ** $QT_BEGIN_LICENSE:FDL$ ** Commercial License Usage ** Licensees holding valid commercial Qt licenses may use this file in ** accordance with the commercial license agreement provided with the ** Software or, alternatively, in accordance with the terms contained in ** a written agreement between you and The Qt Company. For licensing terms ** and conditions see https://www.qt.io/terms-conditions. For further ** information use the contact form at https://www.qt.io/contact-us. ** ** GNU Free Documentation License Usage ** Alternatively, this file may be used under the terms of the GNU Free ** Documentation License version 1.3 as published by the Free Software ** Foundation and appearing in the file included in the packaging of ** this file. Please review the following information to ensure ** the GNU Free Documentation License version 1.3 requirements ** will be met: https://www.gnu.org/licenses/fdl-1.3.html. ** $QT_END_LICENSE$ ** ****************************************************************************/ /*! \example hapticsquare \title Haptic Square Example \section1 Overview This example shows how to use simple haptic effects in an application via the \l{Feedback}{Qt Feedback API}. It provides an example of how to use the Qt Feedback \list \li play "system theme" haptic effects corresponding to certain predefined events \li play a custom effect, single or repeating \endlist \section2 Use Case Compelling applications attempt to immerse the user in the application experience. There are many elements to an immersive experience, including a consistent and beautiful graphical user interface design, unobtrusive yet informative sound design, and intuitive program flow. Another important aspect of immersive applications is tactile feedback and haptic effects. The \l{Feedback}{QtFeedback API} allows application developers to include tactile feedback into their application via a simple to use and extensible API. Some common uses for tactile feedback are: \list \li maintain consistency with system theme for tactile feedback about interface events (button clicks, scrolling, etc) \li notify the user of an application-specific event (invalid operation, status change, etc) \li multisensory user interface (status can be "read" by touching the screen, tactile interfaces, etc) \li immersive gaming experiences (explosions, impacts, collisions, etc) \endlist This example application provides some short snippets which illustrate how the first two of those use cases may be fulfilled. \section1 The Application The application is designed to work on desktop and mobile platforms with minimal differences in code between the platforms. The interface consists of four buttons arranged into a square, each of which causes a different tactile effect to be played by the default tactile effect provider plugin on the platform. \image hapticsquare-example.png \list \li "Rumble!" plays a non-repeating effect with symmetric attack and decay \li "Ocean" is a toggle button which plays a repeating ocean wave-like effect \li "Click" plays the system theme effect for a basic button click \li "Oops!" plays the system theme effect for a negative or invalid response \endlist The example implements two classes: \list \li \c {HapticButton}: Implementation of a button. It inherits QWidget and sends signals for button clicks. \li \c {Dialog}: A QDialog subclass that displays the four \c{HapticButton}s mentioned above, connects them to its slots, and implements the functionality to play the haptic effects. \endlist \section2 The Dialog Class We will now go through the code for the \c Dialog class. Here is its definition: \snippet examples/hapticsquare/hapticsquare.h 0 The buttons are connected to the slots, which play the effects. We will now go through the implementation of \c Dialog. The constructor starts by setting up the non-repeating haptic effect, which is played by clicking the \gui {Rumble! Button}. \snippet examples/hapticsquare/hapticsquare.cpp 0 Custom haptics effects are created by setting up a QFeedbackHapticsEffect. A haptics effect provides a fade-in of the effect's \l{QFeedbackHapticsEffect::}{intensity()}. With vibration, you can think of the intensity as how hard the device will vibrate. The effect will start at \l{QFeedbackHapticsEffect::}{attackIntensity()} and interpolate to \l{QFeedbackHapticsEffect::}{intensity()} in \l{QFeedbackHapticsEffect::}{attackTime()} milliseconds. When the effect ends, we have a similar fade-out, where the haptics effect's intensity will interpolate from \l{QFeedbackHapticsEffect::}{intensity()} to \l{QFeedbackHapticsEffect::}{fadeTime()} in \l{QFeedbackHapticsEffect::}{fadeTime()} milliseconds. The effect will last for a total duration of \l{QFeedbackHapticsEffect::}{duration()} milliseconds. We next set up the effect for the \gui {Ocean Button}. \snippet examples/hapticsquare/hapticsquare.cpp 1 The \c m_ocean is a periodic effect, i.e., it repeats after \l{QFeedbackHapticsEffect::}{period()} milliseconds. Note that the \l{QFeedbackHapticsEffect::}{duration()} must be greater than the period in order for the periodicity of the effect to be discernable. We then set up the GUI and connects the buttons to slots that will play the effects. \snippet examples/hapticsquare/hapticsquare.cpp 2 Let's look at the slots to see how the effects are played. \snippet examples/hapticsquare/hapticsquare.cpp 3 With the \c m_rumble, we only have to call \l{QFeedbackEffect::}{start()}. It will stop when the effect has finished, and can be played again by calling \l{QFeedbackEffect::}{start()} again. The periodic \c m_ocean effect is started the same way as the \c m_rumble effect, and may be stopped with the \l{QFeedbackEffect::}{stop()} function. It will start playing from the beginning again when \l{QFeedbackEffect::}{start()} is called. We could also have paused the effect with \l{QFeedbackEffect::}{pause()}. \snippet examples/hapticsquare/hapticsquare.cpp 4 System theme effects are played with the static QFeedbackEffect::playThemeEffect() function. Theme effects cannot be stopped or paused. There is no guarantee that the backend can play the effect; \l{QFeedbackEffect::}{playThemeEffect()} will return false if the effect could not be played. \section1 Known Issues The example is not intended to exercise the entire API. Instead, it is a simple example which illustrates some simple uses of the API. Also, the example will not work correctly on platforms which do not have a QFeedbackHapticInterface (haptic effect provider) plugin loaded. On such platforms, clicking the buttons will have no effect. On Maemo5, periodic effects do not support attack or fade, and so the ocean effect is not smooth. */