Debian Codegraph @

examples/hapticsplayer/hapticsplayer.cpp

raw symbols SCIP
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 examples of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:BSD$
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 ** BSD License Usage
18 ** Alternatively, you may use this file under the terms of the BSD license
19 ** as follows:
20 **
21 ** "Redistribution and use in source and binary forms, with or without
22 ** modification, are permitted provided that the following conditions are
23 ** met:
24 ** * Redistributions of source code must retain the above copyright
25 ** notice, this list of conditions and the following disclaimer.
26 ** * Redistributions in binary form must reproduce the above copyright
27 ** notice, this list of conditions and the following disclaimer in
28 ** the documentation and/or other materials provided with the
29 ** distribution.
30 ** * Neither the name of The Qt Company Ltd nor the names of its
31 ** contributors may be used to endorse or promote products derived
32 ** from this software without specific prior written permission.
33 **
34 **
35 ** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
36 ** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
37 ** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
38 ** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
39 ** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
40 ** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
41 ** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
42 ** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
43 ** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
44 ** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
45 ** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
46 **
47 ** $QT_END_LICENSE$
48 **
49 ****************************************************************************/
50
51 #include "hapticsplayer.h"
52
53 #include <QtCore/qmetaobject.h>
54
55 #include <QtGui/QFileDialog>
56 #include <QDebug>
57
58 static const char ENUM_THEME_EFFECT[] = "ThemeEffect";
59 static const char ENUM_ANIMATION_STATE[] = "State";
60
61 HapticsPlayer::HapticsPlayer() : actuator(0)
62 {
63 ui.setupUi(this);
64
65 #if defined(Q_WS_MAEMO_5)
66 // maemo5 style problem: title of groupboxes is rendered badly if there isn't enough space
67 // that is, the sizehint of the title area is Preferred rather than Minimum.
68 // to fix that, we manually tweak some spacers.
69 ui.verticalSpacer_2->changeSize(20, 20, QSizePolicy::Expanding, QSizePolicy::Fixed);
70 ui.verticalSpacer_3->changeSize(20, 20, QSizePolicy::Expanding, QSizePolicy::Fixed);
71 ui.verticalSpacer_4->changeSize(20, 30, QSizePolicy::Expanding, QSizePolicy::Fixed);
72 ui.verticalSpacer_5->changeSize(20, 30, QSizePolicy::Expanding, QSizePolicy::Fixed);
73 ui.verticalSpacer_6->changeSize(20, 20, QSizePolicy::Expanding, QSizePolicy::Fixed);
74 ui.verticalSpacer_7->changeSize(20, 30, QSizePolicy::Expanding, QSizePolicy::Fixed);
75 #endif
76
77 connect(ui.actuators, SIGNAL(currentIndexChanged(int)), SLOT(actuatorChanged()));
78 connect(ui.enabled, SIGNAL(toggled(bool)), SLOT(enabledChanged(bool)));
79 connect(ui.playPause, SIGNAL(pressed()), SLOT(playPauseClicked()));
80 connect(ui.stop, SIGNAL(pressed()), &effect, SLOT(stop()));
81 connect(ui.duration, SIGNAL(valueChanged(int)), SLOT(durationChanged(int)));
82 connect(ui.intensity, SIGNAL(valueChanged(int)), SLOT(intensityChanged(int)));
83
84 //for the envelope
85 connect(ui.attackTime, SIGNAL(valueChanged(int)), SLOT(attackTimeChanged(int)));
86 connect(ui.attackIntensity, SIGNAL(valueChanged(int)), SLOT(attackIntensityChanged(int)));
87 connect(ui.fadeTime, SIGNAL(valueChanged(int)), SLOT(fadeTimeChanged(int)));
88 connect(ui.fadeIntensity, SIGNAL(valueChanged(int)), SLOT(fadeIntensityChanged(int)));
89
90 //for the period
91 connect(ui.grpPeriod, SIGNAL(toggled(bool)), SLOT(periodToggled(bool)));
92 connect(ui.period, SIGNAL(valueChanged(int)), SLOT(periodChanged(int)));
93
94 connect(ui.instantPlay, SIGNAL(pressed()), SLOT(instantPlayClicked()));
95
96 //file API
97 connect(ui.browse, SIGNAL(pressed()), SLOT(browseClicked()));
98 connect(ui.filePlayPause, SIGNAL(pressed()), SLOT(filePlayPauseClicked()));
99 connect(ui.fileStop, SIGNAL(pressed()), &fileEffect, SLOT(stop()));
100 // if pausing a playing effect is not supported then stop playback
101 connect(&fileEffect, SIGNAL(error(QFeedbackEffect::ErrorType)), &fileEffect, SLOT(stop()));
102
103 foreach (QFeedbackActuator *dev, QFeedbackActuator::actuators()) {
104 ui.actuators->addItem(dev->name());
105 }
106 if (QFeedbackActuator::actuators().count() > 0)
107 ui.actuators->setCurrentIndex(0);
108
109 //adding the instant effects
110 const QMetaObject &mo = QFeedbackEffect::staticMetaObject;
111 const QMetaEnum &me = mo.enumerator(mo.indexOfEnumerator(ENUM_THEME_EFFECT));
112 Q_ASSERT(me.keyCount());
113 for (int i = 0 ; i < me.keyCount(); ++i) {
114 ui.instantEffect->addItem(me.key(i));
115 }
116
117 //initialization
118 durationChanged(effect.duration());
119 intensityChanged(ui.intensity->value());
120 attackTimeChanged(ui.attackTime->value());
121 attackIntensityChanged(ui.attackIntensity->value());
122 fadeTimeChanged(ui.fadeTime->value());
123 fadeIntensityChanged(ui.fadeIntensity->value());
124
125 ui.tabWidget->setTabEnabled(1, QFeedbackEffect::supportsThemeEffect());
126 ui.tabWidget->setTabEnabled(2, !QFeedbackFileEffect::supportedMimeTypes().isEmpty());
127 //that is a hackish way of updating the info concerning the effects
128 startTimer(50);
129 }
130
131 QFeedbackActuator* HapticsPlayer::currentActuator()
132 {
133 QList<QFeedbackActuator*> devs = QFeedbackActuator::actuators();
134 int index = ui.actuators->currentIndex();
135 if (index == -1 || devs.count() == 0 || index > devs.count()) {
136 if (!actuator) {
137 actuator = new QFeedbackActuator(this);
138 }
139 return actuator;
140 }
141 return devs.at(index);
142 }
143
144 void HapticsPlayer::actuatorChanged()
145 {
146 QFeedbackActuator* dev = currentActuator();
147 if (dev) {
148 enabledChanged(dev->isEnabled());
149 effect.setActuator(dev);
150 }
151 }
152
153 void HapticsPlayer::enabledChanged(bool on)
154 {
155 if (!on)
156 effect.stop();
157 QFeedbackActuator* dev = currentActuator();
158 if (dev) {
159 dev->setEnabled(on);
160 ui.enabled->setChecked(dev->isEnabled());
161
162 if (dev->isEnabled() && (dev->isCapabilitySupported(QFeedbackActuator::Envelope))) {
163 ui.envelope->setEnabled(true);
164 ui.envelope->show();
165 } else {
166 ui.envelope->setEnabled(true);
167 ui.envelope->hide();
168 }
169 if (dev->isEnabled() && (dev->isCapabilitySupported(QFeedbackActuator::Period))) {
170 ui.grpPeriod->setEnabled(true);
171 ui.grpPeriod->show();
172 } else {
173 ui.grpPeriod->setEnabled(false);
174 ui.grpPeriod->hide();
175 }
176 }
177 }
178
179 void HapticsPlayer::playPauseClicked()
180 {
181 if (effect.state() == QFeedbackEffect::Running) {
182 effect.pause();
183 } else {
184 effect.start();
185 }
186 }
187
188 void HapticsPlayer::durationChanged(int duration)
189 {
190 effect.setDuration(duration);
191 ui.attackTime->setMaximum(duration);
192 ui.fadeTime->setMaximum(duration);
193 attackTimeChanged(ui.attackTime->value());
194 }
195
196 void HapticsPlayer::intensityChanged(int value)
197 {
198 effect.setIntensity(qreal(value) / ui.intensity->maximum());
199 ui.lblIntensity->setText(QString::number(effect.intensity()));
200 }
201
202 void HapticsPlayer::timerEvent(QTimerEvent *e)
203 {
204 //update the display for effect
205 {
206 QFeedbackEffect::State newState = effect.state();
207 const QMetaObject *mo = effect.metaObject();
208 ui.effectState->setText(mo->enumerator(mo->indexOfEnumerator(ENUM_ANIMATION_STATE)).key(newState));
209 ui.stop->setEnabled(newState != QFeedbackEffect::Stopped);
210 if (effect.state() == QFeedbackEffect::Paused || effect.state() == QFeedbackEffect::Stopped)
211 ui.playPause->setText(tr("Play"));
212 else
213 ui.playPause->setText(tr("Pause"));
214 }
215
216 //update the display for effect
217 {
218 QFeedbackEffect::State newState = fileEffect.state();
219 const QMetaObject *mo = fileEffect.metaObject();
220 ui.fileEffectState->setText(mo->enumerator(mo->indexOfEnumerator(ENUM_ANIMATION_STATE)).key(newState));
221 ui.fileStop->setEnabled(newState != QFeedbackEffect::Stopped);
222 ui.filePlayPause->setEnabled(fileEffect.isLoaded());
223 ui.browse->setEnabled(newState == QFeedbackEffect::Stopped);
224 ui.fileStatus->setText( fileEffect.isLoaded() ? QString::fromLatin1("%1 : %2 ms").arg(tr("Loaded")).arg(fileEffect.duration()) : tr("Not Loaded") );
225 }
226 QWidget::timerEvent(e);
227 }
228
229 void HapticsPlayer::attackTimeChanged(int attackTime)
230 {
231 effect.setAttackTime(attackTime);
232 //let's check the boundaries
233 if (attackTime + ui.fadeTime->value() > ui.duration->value())
234 ui.fadeTime->setValue(ui.duration->value() - attackTime);
235 }
236
237 void HapticsPlayer::attackIntensityChanged(int attackIntensity)
238 {
239 effect.setAttackIntensity(qreal(attackIntensity) / ui.attackIntensity->maximum());
240 ui.lblAttackIntensity->setText(QString::number(effect.attackIntensity()));
241 }
242
243 void HapticsPlayer::fadeTimeChanged(int fadeTime)
244 {
245 effect.setFadeTime(fadeTime);
246 //let's check the boundaries
247 if (fadeTime + ui.attackTime->value() > ui.duration->value())
248 ui.attackTime->setValue(ui.duration->value() - fadeTime);
249 }
250
251 void HapticsPlayer::fadeIntensityChanged(int fadeIntensity)
252 {
253 effect.setFadeIntensity(qreal(fadeIntensity) / ui.fadeIntensity->maximum());
254 ui.lblFadeIntensity->setText(QString::number(effect.fadeIntensity()));
255 }
256
257 void HapticsPlayer::periodToggled(bool on)
258 {
259 effect.setPeriod(on ? ui.period->value() : 0);
260 }
261
262 void HapticsPlayer::periodChanged(int value)
263 {
264 effect.setPeriod(value);
265 }
266
267 void HapticsPlayer::instantPlayClicked()
268 {
269 const QMetaObject &mo = QFeedbackEffect::staticMetaObject;
270 const QMetaEnum &me = mo.enumerator(mo.indexOfEnumerator(ENUM_THEME_EFFECT));
271 QFeedbackEffect::playThemeEffect(QFeedbackEffect::ThemeEffect(me.keyToValue(ui.instantEffect->currentText().toLatin1())));
272 }
273
274 void HapticsPlayer::browseClicked()
275 {
276 QString filename = QFileDialog::getOpenFileName(this, tr("Feedback file"));
277 if (!filename.isEmpty()) {
278 ui.filename->setText(QDir::toNativeSeparators(filename));
279 fileEffect.setSource(QUrl::fromLocalFile(filename));
280 fileEffect.load();
281 }
282 }
283
284 void HapticsPlayer::filePlayPauseClicked()
285 {
286 if (fileEffect.state() == QFeedbackEffect::Running)
287 fileEffect.pause();
288 else
289 fileEffect.start();
290 }
291
292 #include "moc_hapticsplayer.cpp"