| 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 QtFeedback module of the Qt Toolkit. |
| 7 | ** |
| 8 | ** $QT_BEGIN_LICENSE:LGPL$ |
| 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 Lesser General Public License Usage |
| 18 | ** Alternatively, this file may be used under the terms of the GNU Lesser |
| 19 | ** General Public License version 3 as published by the Free Software |
| 20 | ** Foundation and appearing in the file LICENSE.LGPL3 included in the |
| 21 | ** packaging of this file. Please review the following information to |
| 22 | ** ensure the GNU Lesser General Public License version 3 requirements |
| 23 | ** will be met: https://www.gnu.org/licenses/lgpl-3.0.html. |
| 24 | ** |
| 25 | ** GNU General Public License Usage |
| 26 | ** Alternatively, this file may be used under the terms of the GNU |
| 27 | ** General Public License version 2.0 or (at your option) the GNU General |
| 28 | ** Public license version 3 or any later version approved by the KDE Free |
| 29 | ** Qt Foundation. The licenses are as published by the Free Software |
| 30 | ** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3 |
| 31 | ** included in the packaging of this file. Please review the following |
| 32 | ** information to ensure the GNU General Public License requirements will |
| 33 | ** be met: https://www.gnu.org/licenses/gpl-2.0.html and |
| 34 | ** https://www.gnu.org/licenses/gpl-3.0.html. |
| 35 | ** |
| 36 | ** $QT_END_LICENSE$ |
| 37 | ** |
| 38 | ****************************************************************************/ |
| 39 | |
| 40 | #include "qfeedbackeffect.h" |
| 41 | #include "qfeedbackeffect_p.h" |
| 42 | #include "qfeedbackplugininterfaces.h" |
| 43 | |
| 44 | #include <QtCore> |
| 45 | #include <QDebug> |
| 46 | |
| 47 | |
| 48 | QT_BEGIN_NAMESPACE |
| 49 | |
| 50 | /*! |
| 51 | \class QFeedbackEffect |
| 52 | \brief The QFeedbackEffect class is the abstract base class for feedback effects. |
| 53 | \ingroup feedback |
| 54 | \inmodule QtFeedback |
| 55 | |
| 56 | It represents an effect to provide feedback to a person (i.e., an effect that |
| 57 | affect human senses). The technology available today usually only |
| 58 | provides haptic effects, which deal with the sense of touch, and audio effects. The |
| 59 | QFeedbackHapticsEffect and QFeedbackFileEffect are implementations |
| 60 | of haptic effects and can be used to control a mobile device's |
| 61 | vibrator. In addition, the QFeedbackFileEffect can also be used to play |
| 62 | audio feedback. |
| 63 | |
| 64 | Feedback effects have a duration, which is measured in |
| 65 | milliseconds. Subclasses reimplement duration() to inform how long |
| 66 | the effect lasts. The duration is the total time the effect will |
| 67 | last, and thus includes any envelope modifiers (attack and fade). |
| 68 | |
| 69 | At any given time, a feedback effect is in one of four states: |
| 70 | Loading, Stopped, Running, or Paused. See the |
| 71 | \l{QFeedbackEffect::}{State} enum documentation for further |
| 72 | details. Subclasses must reimplement state() to report which |
| 73 | state an effect is in, and setState() to receive state change |
| 74 | requests. The start(), pause(), and stop() slots calls |
| 75 | setState() with the corresponding new \l{QFeedbackEffect::}{State}. Changes |
| 76 | in state are reported through the stateChanged() signal and may happen |
| 77 | asynchronously some time after the state change request. |
| 78 | |
| 79 | A system often has a set of standard feedback effects for user |
| 80 | interface interaction (e.g., button clicks). The |
| 81 | \l{QFeedbackEffect::}{Effect} describes the standard effects |
| 82 | that QFeedbackEffect supports. It is named so because the effects |
| 83 | often depend on the theme of the user interface. You can play |
| 84 | these effects using the playThemeEffect() function. |
| 85 | |
| 86 | \code |
| 87 | QFeedbackEffect::playThemeEffect(QFeedbackEffect::Press); |
| 88 | \endcode |
| 89 | |
| 90 | The playThemeEffect() function returns true if the effect was |
| 91 | played successfully. An effect may not be played if the system does |
| 92 | not support it or if an error occurred. |
| 93 | */ |
| 94 | |
| 95 | /*! |
| 96 | \fn void QFeedbackEffect::error(QFeedbackEffect::ErrorType error) const |
| 97 | |
| 98 | This signal is emitted by subclasses if an \a error occurred during |
| 99 | playback of an effect. The \l{QFeedbackEffect::}{ErrorType} enum |
| 100 | describes the errors that can be reported. |
| 101 | */ |
| 102 | |
| 103 | /*! |
| 104 | \fn void QFeedbackEffect::stateChanged() |
| 105 | |
| 106 | This signal is emitted by subclasses when the \l State of the |
| 107 | effect changes. |
| 108 | |
| 109 | \sa state() |
| 110 | */ |
| 111 | |
| 112 | /*! |
| 113 | \enum QFeedbackEffect::State |
| 114 | |
| 115 | This enum describes the state of the effect. An effect will be in |
| 116 | one of these states. |
| 117 | |
| 118 | \value Stopped The effect is not running. This is the initial |
| 119 | state. The state changes to either Loading when loading an effect |
| 120 | or to Running when the effect is started by calling start(). When |
| 121 | an effect has finished playing, it will enter the Stopped state |
| 122 | again. |
| 123 | |
| 124 | \value Paused The effect is paused. Calling start() will resume it. |
| 125 | |
| 126 | \value Running The effect is running. You can control the current state |
| 127 | by calling the stop() or pause() functions. |
| 128 | |
| 129 | \value Loading The effect is loading. That can happen when loading |
| 130 | is done asynchronously. When the effect has loaded, the state will change |
| 131 | to either Running (if start() has been called) or Stopped. |
| 132 | |
| 133 | \sa state() |
| 134 | */ |
| 135 | |
| 136 | |
| 137 | /*! |
| 138 | \enum QFeedbackEffect::ErrorType |
| 139 | |
| 140 | This enum describes the possible errors happening on the effect. |
| 141 | |
| 142 | \value UnknownError An unknown error occurred. |
| 143 | |
| 144 | \value DeviceBusy The feedback could not start because the device is busy. |
| 145 | |
| 146 | \sa error() |
| 147 | */ |
| 148 | |
| 149 | /*! |
| 150 | \enum QFeedbackEffect::Effect |
| 151 | |
| 152 | This enum describes all possible effect types. Effects might |
| 153 | be tactile, or audio or visual. |
| 154 | |
| 155 | Not all platforms and devices have distinct effects for each type. |
| 156 | |
| 157 | \value Effect.Undefined - Undefined feedback. No feedback is given. |
| 158 | \value Effect.Press - Feedback for when the screen is pressed. |
| 159 | \value Effect.Release - Feedback for touch release. |
| 160 | \value Effect.PressWeak - A weak feedback for press. |
| 161 | \value Effect.ReleaseWeak - A weak feedback for release. |
| 162 | \value Effect.PressStrong - A strong feedback for press. |
| 163 | \value Effect.ReleaseStrong - A strong feedback for release. |
| 164 | \value Effect.DragStart - Feedback for when dragging starts. |
| 165 | \value Effect.DragDropInZone - Feedback for when dragging ends and touch is released inside a drop zone. |
| 166 | \value Effect.DragDropOutOfZone - Feedback for when dragging ends and touch is released outside a drop zone. |
| 167 | \value Effect.DragCrossBoundary - Feedback for when crossing a boundary while dragging. |
| 168 | \value Effect.Appear - Feedback for when an item is shown. |
| 169 | \value Effect.Disappear - Feedback for when an item is closed. |
| 170 | \value Effect.Move - Feedback for dragging on screen. |
| 171 | \value NumberOfEffects The number of built-in effects. |
| 172 | \value UserEffect The starting point for any user defined effects, where supported. |
| 173 | */ |
| 174 | |
| 175 | /*! |
| 176 | \enum QFeedbackEffect::Duration |
| 177 | This enum describes the possible effect predefined duration types. Generally a specific |
| 178 | milliseconds value can be supplied instead of one of these values. |
| 179 | |
| 180 | \value Infinite Infinite effect duration |
| 181 | */ |
| 182 | |
| 183 | /*! |
| 184 | \property QFeedbackEffect::state |
| 185 | \brief state of the feedback effect. |
| 186 | |
| 187 | This returns the state of the feedback effect. The \l State enumeration reports |
| 188 | the possible states. |
| 189 | */ |
| 190 | |
| 191 | /*! |
| 192 | \property QFeedbackEffect::duration |
| 193 | \brief duration of the feedback effect, in milliseconds. |
| 194 | |
| 195 | In some cases the duration will be unknown, which will be reported as 0. If the duration |
| 196 | is infinite, QFeedbackEffect::Infinite will be returned. Some subclasses may have |
| 197 | more than one type of duration (for example, \l QFeedbackHapticsEffect), and this |
| 198 | property will return the total duration of the effect. |
| 199 | */ |
| 200 | |
| 201 | /*! |
| 202 | Constructs the QFeedbackEffect base class, and passes \a parent to |
| 203 | QObject's constructor. This is called by the classes that inherit |
| 204 | from this class. |
| 205 | */ |
| 206 | QFeedbackEffect::QFeedbackEffect(QObject *parent) : QObject(parent) |
| 207 | { |
| 208 | } |
| 209 | |
| 210 | /*! |
| 211 | \fn void QFeedbackEffect::start() |
| 212 | |
| 213 | Starts playing the effect. If an error occurs the |
| 214 | error() signal will be emitted. |
| 215 | |
| 216 | \sa stop() |
| 217 | */ |
| 218 | void QFeedbackEffect::start() |
| 219 | { |
| 220 | setState(Running); |
| 221 | } |
| 222 | |
| 223 | /*! |
| 224 | \fn void QFeedbackEffect::stop() |
| 225 | |
| 226 | Stops a playing effect. If an error occurs the |
| 227 | error() signal will be emitted. |
| 228 | |
| 229 | \sa start(), pause(), setState() |
| 230 | */ |
| 231 | void QFeedbackEffect::stop() |
| 232 | { |
| 233 | setState(Stopped); |
| 234 | } |
| 235 | |
| 236 | /*! |
| 237 | \fn void QFeedbackEffect::pause() |
| 238 | |
| 239 | Pauses a playing effect. If an error occurs the |
| 240 | error() signal will be emitted. Not all systems |
| 241 | support pausing an effect during playback. |
| 242 | */ |
| 243 | void QFeedbackEffect::pause() |
| 244 | { |
| 245 | setState(Paused); |
| 246 | } |
| 247 | |
| 248 | /*! |
| 249 | \fn QFeedbackEffect::playThemeEffect(Effect effect) |
| 250 | |
| 251 | This function plays \a effect instantly and returns true if the |
| 252 | effect could be played; otherwise, returns false. |
| 253 | */ |
| 254 | bool QFeedbackEffect::playThemeEffect(Effect effect) |
| 255 | { |
| 256 | if (QFeedbackThemeInterface *iface = QFeedbackThemeInterface::instance()) |
| 257 | return iface->play(effect); |
| 258 | return false; |
| 259 | } |
| 260 | |
| 261 | /*! |
| 262 | \fn QFeedbackEffect::supportsThemeEffect() |
| 263 | |
| 264 | Returns true if playing themed feedback is available. |
| 265 | |
| 266 | */ |
| 267 | bool QFeedbackEffect::supportsThemeEffect() |
| 268 | { |
| 269 | return QFeedbackThemeInterface::instance() != 0; |
| 270 | } |
| 271 | |
| 272 | /*! |
| 273 | \class QFeedbackHapticsEffect |
| 274 | \ingroup feedback |
| 275 | \inmodule QtFeedback |
| 276 | \brief The QFeedbackHapticsEffect class allows you to play a haptics effect. |
| 277 | |
| 278 | A haptics effect is an effect that takes advantage of the sense of |
| 279 | touch. Most mobile devices today supports one such effect, |
| 280 | vibration, which will then be the default when you create a |
| 281 | QFeedbackHapticsEffect. |
| 282 | |
| 283 | A haptics effect has a few parameters that must be set up before |
| 284 | it can be played: |
| 285 | |
| 286 | \list |
| 287 | \li duration(): The total duration of the effect in milliseconds. |
| 288 | \li intensity(): The intensity, e.g., how hard the device will vibrate. |
| 289 | \endlist |
| 290 | |
| 291 | An effect can, for example, be set up as follows: |
| 292 | |
| 293 | \omit I'm right that this is all that is required to be set? \endomit |
| 294 | \code |
| 295 | QFeedbackHapticsEffect rumble; |
| 296 | rumble.setIntensity(1.0); |
| 297 | rumble.setDuration(100); |
| 298 | \endcode |
| 299 | |
| 300 | You can now start() the effect. |
| 301 | |
| 302 | \code |
| 303 | rumble.start(); |
| 304 | \endcode |
| 305 | |
| 306 | At any given time, the effect is in one of four states: |
| 307 | \l{QFeedbackEffect::}{Stopped}, \l{QFeedbackEffect::}{Paused}, |
| 308 | \l{QFeedbackEffect::}{Running}, or \l{QFeedbackEffect::}{Loading}. |
| 309 | You can request a state change by calling start(), pause(), or |
| 310 | stop(). The state is queried with state(). |
| 311 | |
| 312 | The haptics effect also supports a fade-in of the effect. For |
| 313 | vibration, this means that the vibration will grow (or sink) in |
| 314 | intensity from when the effect starts until intensity() is |
| 315 | reached. You can set that up as follows: |
| 316 | |
| 317 | \code |
| 318 | rumble.setAttackIntensity(0.0); |
| 319 | rumble.setAttackTime(250); |
| 320 | \endcode |
| 321 | |
| 322 | Attack intensity is the start intensity and attack time is the |
| 323 | duration of the fade-in. We have a similar fade-out: |
| 324 | |
| 325 | \code |
| 326 | rumble.setFadeTime(250); |
| 327 | rumble.setFadeIntensity(0.0); |
| 328 | \endcode |
| 329 | |
| 330 | When using fade-in and fade-out the total duration of the haptics |
| 331 | effect will be: duration(); the main intensity() will be played for |
| 332 | (duration() - (attackTime() + fadeTime())) milliseconds. |
| 333 | |
| 334 | A QFeedbackHapticsEffect is played on an |
| 335 | \l{QFeedbackHapticsEffect::}{actuator()}, which is the physical component that |
| 336 | performs the effect. You can query if other actuators are |
| 337 | available - see the QFeedbackActuator::actuators() function |
| 338 | documentation for details. |
| 339 | |
| 340 | Errors occurring during playback are notified through the |
| 341 | error() signal. |
| 342 | |
| 343 | \sa QFeedbackActuator |
| 344 | */ |
| 345 | |
| 346 | /*! |
| 347 | \fn virtual void QFeedbackEffect::setState(State state) = 0 |
| 348 | |
| 349 | Requests the effect to change its State to change to the specified \a state. |
| 350 | |
| 351 | Subclasses reimplement this function to handle state change requests |
| 352 | for the effect. |
| 353 | */ |
| 354 | |
| 355 | /*! |
| 356 | Constructs the QFeedbackHapticsEffect class, and passes \a parent to |
| 357 | QObject's constructor. The default QFeedbackActuator will be used. |
| 358 | |
| 359 | \sa QFeedbackActuator::actuators() |
| 360 | */ |
| 361 | QFeedbackHapticsEffect::QFeedbackHapticsEffect(QObject *parent) : QFeedbackEffect(parent), priv(new QFeedbackHapticsEffectPrivate) |
| 362 | { |
| 363 | setActuator(0); |
| 364 | } |
| 365 | |
| 366 | |
| 367 | /*! |
| 368 | Destroys this effect and stops the feedback if it is running. |
| 369 | */ |
| 370 | QFeedbackHapticsEffect::~QFeedbackHapticsEffect() |
| 371 | { |
| 372 | stop(); |
| 373 | } |
| 374 | |
| 375 | /*! |
| 376 | \property QFeedbackHapticsEffect::duration |
| 377 | \brief the expected duration of the effect. |
| 378 | |
| 379 | This property defines the total duration of the feedback effect, in milliseconds. |
| 380 | It includes the duration of any fade-in or fade-out parts, if any, in non-periodic |
| 381 | effects, and includes all repetitions of the period in periodic-effects, if any. |
| 382 | |
| 383 | If the duration is set to a value less than attackTime() + fadeTime(), or less |
| 384 | than the period() of the effect, the waveform which will result is |
| 385 | backend-specific. |
| 386 | |
| 387 | \sa fadeTime(), attackTime(), period() |
| 388 | */ |
| 389 | int QFeedbackHapticsEffect::duration() const |
| 390 | { |
| 391 | return priv->duration; |
| 392 | } |
| 393 | void QFeedbackHapticsEffect::setDuration(int msecs) |
| 394 | { |
| 395 | if (priv->duration == msecs) |
| 396 | return; |
| 397 | priv->duration = msecs; |
| 398 | QFeedbackHapticsInterface::instance()->updateEffectProperty(this, QFeedbackHapticsInterface::Duration); |
| 399 | } |
| 400 | |
| 401 | /*! |
| 402 | \property QFeedbackHapticsEffect::intensity |
| 403 | \brief the intensity of the effect. |
| 404 | |
| 405 | This property defines the intensity of the feedback effect. |
| 406 | The value can be between 0 and 1. |
| 407 | |
| 408 | For non-periodic effects, the effect will be at this intensity for |
| 409 | (duration() - (attackTime() + fadeTime())) milliseconds. |
| 410 | For periodic effects, the effect will be at this intensity once per |
| 411 | period for (period() - (attackTime() + fadeTime())) milliseconds. |
| 412 | */ |
| 413 | qreal QFeedbackHapticsEffect::intensity() const |
| 414 | { |
| 415 | return priv->intensity; |
| 416 | } |
| 417 | void QFeedbackHapticsEffect::setIntensity(qreal intensity) |
| 418 | { |
| 419 | if (priv->intensity == intensity) |
| 420 | return; |
| 421 | priv->intensity = intensity; |
| 422 | QFeedbackHapticsInterface::instance()->updateEffectProperty(this, QFeedbackHapticsInterface::Intensity); |
| 423 | } |
| 424 | |
| 425 | /*! |
| 426 | \property QFeedbackHapticsEffect::attackTime |
| 427 | \brief the duration of the fade-in effect. |
| 428 | |
| 429 | This property defines the duration of the fade-in effect in milliseconds. |
| 430 | The effect will ramp up (or down) from attackIntensity() to intensity() in attackTime() milliseconds. |
| 431 | |
| 432 | If the attack time is set to a value such that attackTime() + fadeTime() |
| 433 | is greater than duration() for non-periodic effects, or greater than |
| 434 | period() for periodic effects, the waveform which will result is |
| 435 | backend-specific. |
| 436 | |
| 437 | \sa duration(), period() |
| 438 | */ |
| 439 | int QFeedbackHapticsEffect::attackTime() const |
| 440 | { |
| 441 | return priv->attackTime; |
| 442 | } |
| 443 | void QFeedbackHapticsEffect::setAttackTime(int msecs) |
| 444 | { |
| 445 | if (priv->attackTime == msecs) |
| 446 | return; |
| 447 | priv->attackTime = msecs; |
| 448 | QFeedbackHapticsInterface::instance()->updateEffectProperty(this, QFeedbackHapticsInterface::AttackTime); |
| 449 | } |
| 450 | |
| 451 | /*! |
| 452 | \property QFeedbackHapticsEffect::attackIntensity |
| 453 | \brief the initial intensity of the effect. |
| 454 | |
| 455 | This property defines the initial intensity of the effect, before it fades in. |
| 456 | It is usually lower than \l intensity. The effect will ramp up (or down) from |
| 457 | attackIntensity() to intensity() in attackTime() milliseconds. |
| 458 | */ |
| 459 | qreal QFeedbackHapticsEffect::attackIntensity() const |
| 460 | { |
| 461 | return priv->attackIntensity; |
| 462 | } |
| 463 | void QFeedbackHapticsEffect::setAttackIntensity(qreal intensity) |
| 464 | { |
| 465 | if (priv->attackIntensity == intensity) |
| 466 | return; |
| 467 | priv->attackIntensity = intensity; |
| 468 | QFeedbackHapticsInterface::instance()->updateEffectProperty(this, QFeedbackHapticsInterface::AttackIntensity); |
| 469 | } |
| 470 | |
| 471 | /*! |
| 472 | \property QFeedbackHapticsEffect::fadeTime |
| 473 | \brief the duration of the fade-out effect. |
| 474 | |
| 475 | This property defines the duration of the fade-out effect in milliseconds. |
| 476 | The effect will ramp down (or up) from intensity() to fadeIntensity() in fadeTime() milliseconds. |
| 477 | |
| 478 | If the fade time is set to a value such that attackTime() + fadeTime() |
| 479 | is greater than duration() for non-periodic effects, or greater than |
| 480 | period() for periodic effects, the waveform which will result is |
| 481 | backend-specific. |
| 482 | |
| 483 | \sa duration(), period() |
| 484 | */ |
| 485 | int QFeedbackHapticsEffect::fadeTime() const |
| 486 | { |
| 487 | return priv->fadeTime; |
| 488 | } |
| 489 | void QFeedbackHapticsEffect::setFadeTime(int msecs) |
| 490 | { |
| 491 | if (priv->fadeTime == msecs) |
| 492 | return; |
| 493 | priv->fadeTime = msecs; |
| 494 | QFeedbackHapticsInterface::instance()->updateEffectProperty(this, QFeedbackHapticsInterface::FadeTime); |
| 495 | } |
| 496 | |
| 497 | /*! |
| 498 | \property QFeedbackHapticsEffect::fadeIntensity |
| 499 | \brief the final intensity of the effect. |
| 500 | |
| 501 | This property defines the final intensity of the effect, after it fades out. |
| 502 | It is usually lower than \l intensity. |
| 503 | The effect will ramp down (or up) from intensity() to fadeIntensity() in fadeTime() milliseconds. |
| 504 | */ |
| 505 | qreal QFeedbackHapticsEffect::fadeIntensity() const |
| 506 | { |
| 507 | return priv->fadeIntensity; |
| 508 | } |
| 509 | void QFeedbackHapticsEffect::setFadeIntensity(qreal intensity) |
| 510 | { |
| 511 | if (priv->fadeIntensity == intensity) |
| 512 | return; |
| 513 | priv->fadeIntensity = intensity; |
| 514 | QFeedbackHapticsInterface::instance()->updateEffectProperty(this, QFeedbackHapticsInterface::FadeIntensity); |
| 515 | } |
| 516 | |
| 517 | /*! |
| 518 | \property QFeedbackHapticsEffect::actuator |
| 519 | \brief the actuator on which the effect operates. |
| 520 | |
| 521 | This property defines the actuator on which the effect operates. You can only |
| 522 | change the actuator used when the effect is stopped. Setting a null actuator |
| 523 | resets the effect to use the default actuator. |
| 524 | */ |
| 525 | QFeedbackActuator* QFeedbackHapticsEffect::actuator() const |
| 526 | { |
| 527 | return priv->actuator; |
| 528 | } |
| 529 | void QFeedbackHapticsEffect::setActuator(QFeedbackActuator *actuator) |
| 530 | { |
| 531 | if (state() != Stopped) { |
| 532 | qWarning("QFeedbackHapticsEffect::setActuator: The effect is not stopped"); |
| 533 | return; |
| 534 | } |
| 535 | |
| 536 | if (actuator) { |
| 537 | priv->actuator = actuator; |
| 538 | } else { |
| 539 | QList<QFeedbackActuator*> list = QFeedbackActuator::actuators(); |
| 540 | if (!list.isEmpty()) { |
| 541 | priv->actuator = list.first(); |
| 542 | } else { |
| 543 | priv->actuator = new QFeedbackActuator(this); |
| 544 | } |
| 545 | } |
| 546 | } |
| 547 | |
| 548 | /*! |
| 549 | \property QFeedbackHapticsEffect::period |
| 550 | \brief set the period for the effect. |
| 551 | |
| 552 | It has a default value of -1, which means that it is not a periodic effect. |
| 553 | You can only change the period when the effect is stopped. |
| 554 | The duration of the effect should be set to a value larger than the |
| 555 | period of the effect if you wish the periodicity to be discernable. |
| 556 | \note Not all actuators support periodic effects |
| 557 | |
| 558 | The period defines the total length of the periodic envelope, which will |
| 559 | be repeated up until duration() milliseconds has elapsed. For a periodic |
| 560 | effect, the intensity will start at attackIntensity(), ramp to intensity() |
| 561 | (where it stays for (period() - (attackTime() + fadeTime())) milliseconds), |
| 562 | then ramp to fadeIntensity(). This waveform will be repeated as many times |
| 563 | as required until the duration() has elapsed. |
| 564 | |
| 565 | If the period is set to a value which is less than attackTime() + fadeTime(), |
| 566 | the waveform which will result is backend-specific. |
| 567 | */ |
| 568 | int QFeedbackHapticsEffect::period() const |
| 569 | { |
| 570 | return priv->period; |
| 571 | } |
| 572 | void QFeedbackHapticsEffect::setPeriod(int msecs) |
| 573 | { |
| 574 | if (state() != Stopped) { |
| 575 | qWarning("QFeedbackHapticsEffect::setPeriod: the period can only be changed if the effect is stopped"); |
| 576 | return; |
| 577 | } |
| 578 | priv->period = msecs; |
| 579 | } |
| 580 | |
| 581 | /*! |
| 582 | \internal |
| 583 | */ |
| 584 | void QFeedbackHapticsEffect::setState(State state) |
| 585 | { |
| 586 | State oldState = this->state(); |
| 587 | if (oldState != state) { |
| 588 | QFeedbackHapticsInterface::instance()->setEffectState(this, state); |
| 589 | emit stateChanged(); |
| 590 | } |
| 591 | } |
| 592 | |
| 593 | /*! |
| 594 | \internal |
| 595 | */ |
| 596 | QFeedbackEffect::State QFeedbackHapticsEffect::state() const |
| 597 | { |
| 598 | return QFeedbackHapticsInterface::instance()->effectState(this); |
| 599 | } |
| 600 | |
| 601 | /*! |
| 602 | \internal |
| 603 | \class QFeedbackFileEffect |
| 604 | \ingroup feedback |
| 605 | \inmodule QtFeedback |
| 606 | \brief The QFeedbackFileEffect class allows to play feedback from a file. |
| 607 | |
| 608 | Several different mime types may be supported on a system, including |
| 609 | both haptic data files, and audio files. |
| 610 | |
| 611 | The files containing haptics data are usually suffixed \c .ivt. |
| 612 | The feedback is usually varying in |
| 613 | \l{QFeedbackHapticsEffect::}{intensity()}, and is for that reason |
| 614 | often referred to as a "haptic tune". They are created, for |
| 615 | instance, from music files where the feedback is based on a |
| 616 | specific feature in the audio data. For example, you could have a |
| 617 | phone vibrating along with the bass of a rock song. |
| 618 | |
| 619 | Although Qt Feedback does not let you record \c .ivt files, it lets |
| 620 | you play them back using the QFeedbackFileEffect class. Setting |
| 621 | up a QFeedbackFileEffect and starting it is done as follows: |
| 622 | |
| 623 | \code |
| 624 | QFeedbackFileEffect hapticTune; |
| 625 | hapticTune.setSource(QUrl::fromLocalFile("mySavedRumble.ivt")); |
| 626 | hapticTune.load(); |
| 627 | hapticTune.start(); |
| 628 | \endcode |
| 629 | |
| 630 | As with other \l{QFeedbackEffect}s, QFeedbackFileEffect is at any |
| 631 | given time in one of four states: \l{QFeedbackEffect::}{Loading}, |
| 632 | \l{QFeedbackEffect::}{Running}, \l{QFeedbackEffect::}{Paused}, or |
| 633 | \l{QFeedbackEffect::}{Stopped}. You request state changes with |
| 634 | start(), pause(), and stop(). |
| 635 | |
| 636 | You can load() and unload() the file at will to free resources or |
| 637 | be as fast as possible. The file must be loaded before it can be |
| 638 | started, and it cannot be unloaded while playing. After the file is |
| 639 | loaded, you can query its duration(). Some mime types may not |
| 640 | support duration information - in these cases, 0 will be returned. |
| 641 | |
| 642 | QFeedbackFileEffect reports errors through the error() signal. |
| 643 | |
| 644 | \sa QFeedbackHapticsEffect |
| 645 | */ |
| 646 | |
| 647 | |
| 648 | /*! |
| 649 | \internal |
| 650 | */ |
| 651 | void QFeedbackFileEffectPrivate::loadFinished(bool success) |
| 652 | { |
| 653 | loaded = success; |
| 654 | if( !success) |
| 655 | backendUsed = -1; |
| 656 | } |
| 657 | |
| 658 | |
| 659 | /*! |
| 660 | \internal |
| 661 | Constructs the QFeedbackFileEffect class, and passes \a parent to |
| 662 | QObject's constructor. |
| 663 | */ |
| 664 | QFeedbackFileEffect::QFeedbackFileEffect(QObject *parent) : QFeedbackEffect(parent), priv(new QFeedbackFileEffectPrivate(this)) |
| 665 | { |
| 666 | } |
| 667 | |
| 668 | /*! |
| 669 | \internal |
| 670 | Stops the feedback and unloads the file if necessary. |
| 671 | */ |
| 672 | QFeedbackFileEffect::~QFeedbackFileEffect() |
| 673 | { |
| 674 | setLoaded(false); //ensures we unload the file and frees resources |
| 675 | } |
| 676 | |
| 677 | /*! |
| 678 | \reimp |
| 679 | */ |
| 680 | int QFeedbackFileEffect::duration() const |
| 681 | { |
| 682 | return QFeedbackFileInterface::instance()->effectDuration(this); |
| 683 | } |
| 684 | |
| 685 | /*! |
| 686 | \internal |
| 687 | \property QFeedbackFileEffect::source |
| 688 | \brief the url of the file that is loaded. |
| 689 | |
| 690 | Setting that property will automatically unload the previous file (if any) and load the new one. |
| 691 | Some backends may not support all URL schemes - for example, they may only support |
| 692 | local files. |
| 693 | |
| 694 | You can only change the source of an effect when it is stopped. |
| 695 | */ |
| 696 | QUrl QFeedbackFileEffect::source() const |
| 697 | { |
| 698 | return priv->url; |
| 699 | } |
| 700 | void QFeedbackFileEffect::setSource(const QUrl &source) |
| 701 | { |
| 702 | if (state() != QFeedbackEffect::Stopped) { |
| 703 | qWarning("QFeedbackFileEffect::setSource: can't set the file while the feedback is running"); |
| 704 | return; |
| 705 | } |
| 706 | if (source != priv->url) { |
| 707 | setLoaded(false); |
| 708 | priv->url = source; |
| 709 | setLoaded(true); |
| 710 | } |
| 711 | } |
| 712 | |
| 713 | /*! |
| 714 | \internal |
| 715 | \property QFeedbackFileEffect::loaded |
| 716 | \brief reports if the file has been successfully loaded. |
| 717 | */ |
| 718 | bool QFeedbackFileEffect::isLoaded() const |
| 719 | { |
| 720 | return priv->loaded; |
| 721 | } |
| 722 | void QFeedbackFileEffect::setLoaded(bool load) |
| 723 | { |
| 724 | if (priv->loaded == load) |
| 725 | return; |
| 726 | |
| 727 | if (state() != QFeedbackEffect::Stopped) { |
| 728 | qWarning() << "QFeedbackFileEffect::setLoaded: can't load/unload a file while the effect is not stopped"; |
| 729 | return; |
| 730 | } |
| 731 | |
| 732 | QFeedbackFileInterface::instance()->setLoaded(this, load); |
| 733 | } |
| 734 | |
| 735 | |
| 736 | /*! |
| 737 | \internal |
| 738 | \fn void QFeedbackFileEffect::load() |
| 739 | |
| 740 | Makes sure that the file associated with the feedback object is loaded. |
| 741 | It will be automatically loaded when the setSource() or start() functions |
| 742 | are called. |
| 743 | */ |
| 744 | void QFeedbackFileEffect::load() |
| 745 | { |
| 746 | setLoaded(true); |
| 747 | } |
| 748 | |
| 749 | /*! |
| 750 | \internal |
| 751 | \fn void QFeedbackFileEffect::unload() |
| 752 | |
| 753 | makes sure that the file associated with the feedback object is unloaded. |
| 754 | It will be automatically unloaded when the setSource function is called with |
| 755 | another file or the object is destroyed. |
| 756 | */ |
| 757 | void QFeedbackFileEffect::unload() |
| 758 | { |
| 759 | setLoaded(false); |
| 760 | } |
| 761 | |
| 762 | |
| 763 | /*! |
| 764 | \internal |
| 765 | \fn QStringList QFeedbackFileEffect::supportedMimeTypes() |
| 766 | |
| 767 | returns the MIME types supported for playing effects from file. |
| 768 | */ |
| 769 | QStringList QFeedbackFileEffect::supportedMimeTypes() |
| 770 | { |
| 771 | return QFeedbackFileInterface::instance()->supportedMimeTypes(); |
| 772 | } |
| 773 | |
| 774 | |
| 775 | /*! |
| 776 | \reimp |
| 777 | */ |
| 778 | void QFeedbackFileEffect::setState(State newState) |
| 779 | { |
| 780 | State oldState = state(); |
| 781 | if (oldState != newState) { |
| 782 | if (newState != Stopped && state() == Stopped) |
| 783 | load(); // makes sure the file is loaded |
| 784 | QFeedbackFileInterface::instance()->setEffectState(this, newState); |
| 785 | emit stateChanged(); |
| 786 | } |
| 787 | } |
| 788 | |
| 789 | /*! |
| 790 | \reimp |
| 791 | */ |
| 792 | QFeedbackEffect::State QFeedbackFileEffect::state() const |
| 793 | { |
| 794 | return QFeedbackFileInterface::instance()->effectState(this); |
| 795 | } |
| 796 | |
| 797 | QT_END_NAMESPACE |