/doc/src/examples/symbianvibration.qdoc

https://bitbucket.org/ultra_iter/qt-vtl · Unknown · 192 lines · 140 code · 52 blank · 0 comment · 0 complexity · 085c97d4f2e922377ff3837110875d27 MD5 · raw file

  1. /****************************************************************************
  2. **
  3. ** Copyright (C) 2012 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:FDL$
  10. ** GNU Free Documentation License
  11. ** Alternatively, this file may be used under the terms of the GNU Free
  12. ** Documentation License version 1.3 as published by the Free Software
  13. ** Foundation and appearing in the file included in the packaging of
  14. ** this file.
  15. **
  16. ** Other Usage
  17. ** Alternatively, this file may be used in accordance with the terms
  18. ** and conditions contained in a signed written agreement between you
  19. ** and Nokia.
  20. **
  21. **
  22. **
  23. **
  24. ** $QT_END_LICENSE$
  25. **
  26. ****************************************************************************/
  27. /*! \example widgets/symbianvibration
  28. \group all-examples
  29. \title Symbian Vibration Example
  30. \brief The Symbian Vibrator example shows how to get fine-grained vibration
  31. control on Symbian devices.
  32. Native Symbian APIs have to be used to enable vibration, since QtMobility
  33. doesn't provide an interface for it yet. It is, however, planned to be
  34. included in a future release. In anticipation for that, we make use of the
  35. \c XQVibra class that was a part of the Mobile Extensions Technology Preview
  36. API for Qt for Symbian. The pre-compiled libraries are no longer compatible
  37. with Qt 4.6, but we can include the source code itself with the project.
  38. \image symbianvibration-example.png Screenshot of the Symbian Vibration example
  39. The example application divides the window into rectangles, which can be
  40. pressed to make the device vibrate. Pressing different rectangles make the
  41. device vibrate with different intensities. Each rectangle has a different
  42. color and its intensity number is drawn on top of it. Moving the cursor
  43. from one rectangle to another changes the vibration intensity to that of
  44. the new one. Vibration stops when the mouse button has been released. It
  45. is also possible to launch a short burst of vibration through the menu.
  46. The example consists of four classes:
  47. \list
  48. \o \c XQVibra is the vibration interface class taken from the Mobile
  49. Extensions for Qt for Symbian.
  50. \o \c XQVibraPrivate is the Symbian specific private implementation of the
  51. vibration implementation.
  52. \o \c VibrationSurface is a custom widget that uses a XQVibra instance to
  53. vibrate the device depending on where the user presses.
  54. \o \c MainWindow inherits from QMainWindow and contains a \c VibrationSurface
  55. as its central widget, and also has a menu from which it is possible to
  56. make the phone vibrate.
  57. \endlist
  58. \section1 XQVibra Class Definition
  59. The \c XQVibra class uses the pimpl-idiom to hide the platform specific
  60. implementation behind a common interface. Technically it would be possible
  61. to support more target platforms, with only the addition of a private
  62. implementation. The rest of the code would work the same, since only the
  63. common interface is used.
  64. \snippet examples/widgets/symbianvibration/xqvibra.h 0
  65. \c XQVibra provides a very simple interface for us to use. The interesting
  66. part are the three slots \c start(), \c stop() and \c setIntensity(). Calling the start
  67. method initiates vibration for the specified duration. Calling it while the
  68. device is already vibrating causes it to stop the current one and start the
  69. new one, even if the intensities are the same. The \c setIntensity() method
  70. should be called before starting vibration.
  71. \section1 VibrationSurface Class Definition
  72. \c VibrationSurface inherits from QWidget and acts like a controller for a
  73. \c XQVibra object. It responds to mouse events and performs custom painting.
  74. \snippet examples/widgets/symbianvibration/vibrationsurface.h 0
  75. The virtual event methods are reimplemented from QWidget. As can be seen,
  76. there is no public programmable interface beyond what QWidget provides.
  77. \section1 VibrationSurface Class Implementation
  78. Mouse events control the intensity of the vibration.
  79. \snippet examples/widgets/symbianvibration/vibrationsurface.cpp 0
  80. \codeline
  81. \snippet examples/widgets/symbianvibration/vibrationsurface.cpp 1
  82. \codeline
  83. \snippet examples/widgets/symbianvibration/vibrationsurface.cpp 2
  84. Presses starts the vibration, movement changes the intensity and releases
  85. stops the vibration. To set the right amount of vibration, the private
  86. method \c applyIntensity() is used. It sets the vibration intensity according to
  87. which rectangle the mouse currently resides in.
  88. \snippet examples/widgets/symbianvibration/vibrationsurface.cpp 3
  89. We make sure only to change the intensity if it is different than last
  90. time, so that the vibrator isn't stopped and restarted unnecessarily.
  91. The range of vibration intensity ranges from 0 to XQVibra::MaxIntensity. We
  92. divide this range into a set of levels. The number of levels and the intensity
  93. increase for each level are stored in two constants.
  94. \snippet examples/widgets/symbianvibration/vibrationsurface.cpp 4
  95. Each rectangle has an intensity of one \c IntensityPerLevel more than the
  96. previous one.
  97. \snippet examples/widgets/symbianvibration/vibrationsurface.cpp 5
  98. The rectangles are either put in a row, if the widget's width is greater
  99. than its height (landscape), otherwise they are put in a column (portrait).
  100. Each rectangle's size is thus dependent on the length of the width or the
  101. height of the widget, whichever is longer. The length is then divided by
  102. the number of levels, which gets us either the height or the width of each
  103. rectangle. The dx and dy specify the distance from one rectangle to the
  104. next, which is the same as either the width or height of the rectangle.
  105. \snippet examples/widgets/symbianvibration/vibrationsurface.cpp 6
  106. For each level of intensity, we draw a rectangle with increasing
  107. brightness. On top of the rectangle a text label is drawn, specifying the
  108. intesity of this level. We use the rectangle rect as a template for
  109. drawing, and move it down or right at each iteration.
  110. The intensity is calculated by dividing the greater of the width and height
  111. into \c NumberOfLevels slices.
  112. \snippet examples/widgets/symbianvibration/vibrationsurface.cpp 7
  113. In case the widget's geometry is too small to fit all the levels, the user
  114. interface will not work. For simplicity, we just return 0.
  115. When we know the axis along which the rectangles lie, we can find the one
  116. in which the mouse cursor lie.
  117. \snippet examples/widgets/symbianvibration/vibrationsurface.cpp 8
  118. The final clamp of the intensity value at the end is necessary in case the
  119. mouse coordinate lies outside the widget's geometry.
  120. \section1 MainWindow Class Definition
  121. Here's the definition of the \c MainWindow class:
  122. \snippet examples/widgets/symbianvibration/mainwindow.h 0
  123. \c MainWindow is a top level window that uses a \c XQVibra and a
  124. \c VibrationSurface. It also adds a menu option to the menu bar which can
  125. start a short vibration.
  126. \section1 MainWindow Class Implementation
  127. In the \c MainWindow constructor the \c XQVibra and the \c VibrationSurface
  128. are created. An action is added to the menu and is connected to the vibrate
  129. slot.
  130. \snippet examples/widgets/symbianvibration/mainwindow.cpp 0
  131. The \c vibrate() slot offers a way to invoke the vibration in case no
  132. mouse is present on the device.
  133. \snippet examples/widgets/symbianvibration/mainwindow.cpp 1
  134. \section1 Symbian Vibration Library
  135. The \c XQVibra class requires a platform library to be included. It is
  136. included in the \c .pro file for the symbian target.
  137. \quotefromfile examples/widgets/symbianvibration/symbianvibration.pro
  138. \skipto /^symbian \{/
  139. \printuntil /^\}/
  140. */