1/****************************************************************************
2**
3** Copyright (C) 2016 The Qt Company Ltd.
4** Contact: http://www.qt.io/licensing/
5**
6** This file is part of the QtFeedback module of the Qt Toolkit.
7**
8** $QT_BEGIN_LICENSE:LGPL3$
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 http://www.qt.io/terms-conditions. For further
15** information use the contact form at http://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.LGPLv3 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.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 later as published by the Free
28** Software Foundation and appearing in the file LICENSE.GPL included in
29** the packaging of this file. Please review the following information to
30** ensure the GNU General Public License version 2.0 requirements will be
31** met: http://www.gnu.org/licenses/gpl-2.0.html.
32**
33** $QT_END_LICENSE$
34**
35****************************************************************************/
36
37#include "qdeclarativefeedbackeffect_p.h"
38
39/*!
40 \qmltype FeedbackEffect
41 \instantiates QDeclarativeFeedbackEffect
42 \brief The FeedbackEffect element is the base class for all feedback effects.
43 \ingroup qml-feedback-api
44
45 You can't create one of these elements directly, but several other elements
46 inherit the methods and properties of these elements.
47
48 There are several predefined enumerations and constants provided in this class:
49
50 1. Duration
51 This enum describes the possible predefined duration types. Generally a specific
52 value in milliseconds can be supplied instead of one of these values.
53 \list
54 \li Feedback.Infinite - Infinite effect duration
55 \endlist
56
57 2. State
58 This enum describes the state of the effect. An effect will be in one of these states.
59 \list
60 \li Feedback.Stopped - The effect is not running. This is the initial state.
61 \li Feedback.Paused - The effect is paused.
62 \li Feedback.Running - The effect is running.
63 \li Feedback.Loading - The effect is loading.
64 \endlist
65
66 3. ErrorType
67 This enum describes the possible errors happening on the effect.
68 \list
69 \li Feedback.UnknownError - An unknown error occurred.
70 \li Feedback.DeviceBusy - The feedback could not start because the device is busy,
71 the device could be busy if a higher-priority client is using the haptics/actuator device.
72 \endlist
73
74
75 \sa FileEffect, ThemeEffect, HapticsEffect, {QFeedbackEffect}
76*/
77
78QDeclarativeFeedbackEffect::QDeclarativeFeedbackEffect(QObject *parent)
79 : QObject(parent), m_running(false), m_paused(false), m_error(UnknownError)
80{
81}
82
83void QDeclarativeFeedbackEffect::setFeedbackEffect(QFeedbackEffect* effect)
84{
85 m_effect = effect;
86 QObject::connect(sender: m_effect, SIGNAL(stateChanged()), receiver: this, SLOT(updateState()));
87 QObject::connect(sender: m_effect, SIGNAL(error(QFeedbackEffect::ErrorType)), receiver: this, SLOT(_error(QFeedbackEffect::ErrorType)));
88}
89QFeedbackEffect* QDeclarativeFeedbackEffect::feedbackEffect()
90{
91 return m_effect;
92}
93
94/*!
95 \qmlproperty bool FeedbackEffect::running
96
97 This property is true if this feedback effect is running.
98 */
99bool QDeclarativeFeedbackEffect::isRunning() const
100{
101 return m_running;
102}
103void QDeclarativeFeedbackEffect::setRunning(bool running)
104{
105 QDeclarativeFeedbackEffect::State currentState = static_cast<QDeclarativeFeedbackEffect::State>(m_effect->state());
106 if (currentState != QDeclarativeFeedbackEffect::Running && running) {
107 m_running = true;
108 m_effect->start();
109 emit runningChanged();
110 } else if (currentState != QDeclarativeFeedbackEffect::Stopped && !running) {
111 m_running = false;
112 m_effect->stop();
113 emit runningChanged();
114 }
115}
116
117/*!
118 \qmlproperty bool FeedbackEffect::paused
119
120 This property is true if this feedback effect is paused.
121 */
122bool QDeclarativeFeedbackEffect::isPaused() const
123{
124 return m_paused;
125}
126void QDeclarativeFeedbackEffect::setPaused(bool paused)
127{
128 QDeclarativeFeedbackEffect::State currentState = static_cast<QDeclarativeFeedbackEffect::State>(m_effect->state());
129 if (currentState == QDeclarativeFeedbackEffect::Paused && !paused) {
130 m_paused = true;
131 m_effect->start();
132 emit pausedChanged();
133 } else if (currentState == QDeclarativeFeedbackEffect::Running && paused) {
134 paused = false;
135 m_effect->pause();
136 emit pausedChanged();
137 }
138}
139
140
141/*!
142 \qmlproperty int FeedbackEffect::duration
143
144 The duration of the effect, in milliseconds. This is 0 for effects of unknown
145 duration, or Feedback.Infinite for effects that don't stop.
146 \sa Feedback
147 */
148int QDeclarativeFeedbackEffect::duration() const
149{
150 return m_effect->duration();
151}
152void QDeclarativeFeedbackEffect::setDuration(int newDuration)
153{
154 Q_UNUSED(newDuration)
155 //default do nothing
156}
157
158/*!
159 \qmlproperty FeedbackEffect::State FeedbackEffect::state
160
161 This is the current state of the effect. It is one of:
162 \list
163 \li Feedback.Stopped - the effect is not playing.
164 \li Feedback.Loading - the effect is being loaded.
165 \li Feedback.Running - the effect is playing.
166 \li Feedback.Paused - the effect was being played, but is now paused.
167 \endlist
168 \sa Feedback
169 */
170QDeclarativeFeedbackEffect::State QDeclarativeFeedbackEffect::state() const
171{
172 return static_cast<QDeclarativeFeedbackEffect::State>(m_effect->state());
173}
174
175void QDeclarativeFeedbackEffect::setState(QDeclarativeFeedbackEffect::State newState)
176{
177 Q_UNUSED(newState)
178 //default do nothing
179}
180/*!
181 \qmlproperty Feedback::ErrorType FeedbackEffect::error
182
183 This property holds the error status of the FeedbackEffect.
184 The error is one of the following values:
185 \list
186 \li Feedback.UnknownError - An unknown error occurred.
187 \li Feedback.DeviceBusy - The device resource is already being used.
188 \endlist
189
190 \sa Feedback, QFeedbackEffect::ErrorType
191 */
192QDeclarativeFeedbackEffect::ErrorType QDeclarativeFeedbackEffect::error() const
193{
194 return m_error;
195}
196
197/*!
198 \qmlmethod Feedback::updateState()
199
200 updates the state of the effect.
201*/
202void QDeclarativeFeedbackEffect::updateState() {
203 bool running = m_effect->state() == QFeedbackEffect::Running;
204 bool paused = m_effect->state() == QFeedbackEffect::Paused;
205 if (running != m_running) {
206 m_running = running;
207 emit runningChanged();
208 }
209 if (paused != m_paused) {
210 m_paused = paused;
211 emit pausedChanged();
212 }
213 emit stateChanged();
214}
215
216/*!
217 \qmlmethod Feedback::start()
218
219 makes sure that the effect associated with the feedback object is started.
220 \sa QFeedbackEffect::start()
221*/
222void QDeclarativeFeedbackEffect::start() {
223 m_effect->start();
224}
225
226/*!
227 \qmlmethod Feedback::stop()
228
229 makes sure that the effect associated with the feedback object is stoped.
230 \sa QFeedbackEffect::stop()
231*/
232void QDeclarativeFeedbackEffect::stop() {
233 m_effect->stop();
234}
235
236/*!
237 \qmlmethod Feedback::pause()
238
239 makes sure that the effect associated with the feedback object is paused.
240 \sa QFeedbackEffect::pause()
241*/
242void QDeclarativeFeedbackEffect::pause() {
243 m_effect->pause();
244}
245
246void QDeclarativeFeedbackEffect::_error(QFeedbackEffect::ErrorType err)
247{
248 if (static_cast<ErrorType>(err) != m_error) {
249 m_error = static_cast<ErrorType>(err);
250 emit errorChanged();
251 }
252}
253

source code of qtfeedback/src/imports/feedback/qdeclarativefeedbackeffect.cpp