#include "hapticsplayer.h"
#include <QtCore/qmetaobject.h>
#include <QtGui/QFileDialog>
#include <QDebug>
static const char ENUM_THEME_EFFECT[] = "ThemeEffect";
static const char ENUM_ANIMATION_STATE[] = "State";
HapticsPlayer::HapticsPlayer() : actuator(0)
{
ui.setupUi(this);
#if defined(Q_WS_MAEMO_5)
ui.verticalSpacer_2->changeSize(20, 20, QSizePolicy::Expanding, QSizePolicy::Fixed);
ui.verticalSpacer_3->changeSize(20, 20, QSizePolicy::Expanding, QSizePolicy::Fixed);
ui.verticalSpacer_4->changeSize(20, 30, QSizePolicy::Expanding, QSizePolicy::Fixed);
ui.verticalSpacer_5->changeSize(20, 30, QSizePolicy::Expanding, QSizePolicy::Fixed);
ui.verticalSpacer_6->changeSize(20, 20, QSizePolicy::Expanding, QSizePolicy::Fixed);
ui.verticalSpacer_7->changeSize(20, 30, QSizePolicy::Expanding, QSizePolicy::Fixed);
#endif
connect(ui.actuators, SIGNAL(currentIndexChanged(int)), SLOT(actuatorChanged()));
connect(ui.enabled, SIGNAL(toggled(bool)), SLOT(enabledChanged(bool)));
connect(ui.playPause, SIGNAL(pressed()), SLOT(playPauseClicked()));
connect(ui.stop, SIGNAL(pressed()), &effect, SLOT(stop()));
connect(ui.duration, SIGNAL(valueChanged(int)), SLOT(durationChanged(int)));
connect(ui.intensity, SIGNAL(valueChanged(int)), SLOT(intensityChanged(int)));
connect(ui.attackTime, SIGNAL(valueChanged(int)), SLOT(attackTimeChanged(int)));
connect(ui.attackIntensity, SIGNAL(valueChanged(int)), SLOT(attackIntensityChanged(int)));
connect(ui.fadeTime, SIGNAL(valueChanged(int)), SLOT(fadeTimeChanged(int)));
connect(ui.fadeIntensity, SIGNAL(valueChanged(int)), SLOT(fadeIntensityChanged(int)));
connect(ui.grpPeriod, SIGNAL(toggled(bool)), SLOT(periodToggled(bool)));
connect(ui.period, SIGNAL(valueChanged(int)), SLOT(periodChanged(int)));
connect(ui.instantPlay, SIGNAL(pressed()), SLOT(instantPlayClicked()));
connect(ui.browse, SIGNAL(pressed()), SLOT(browseClicked()));
connect(ui.filePlayPause, SIGNAL(pressed()), SLOT(filePlayPauseClicked()));
connect(ui.fileStop, SIGNAL(pressed()), &fileEffect, SLOT(stop()));
connect(&fileEffect, SIGNAL(error(QFeedbackEffect::ErrorType)), &fileEffect, SLOT(stop()));
foreach(QFeedbackActuator *dev, QFeedbackActuator::actuators()) {
ui.actuators->addItem(dev->name());
}
if (QFeedbackActuator::actuators().count() > 0)
ui.actuators->setCurrentIndex(0);
const QMetaObject &mo = QFeedbackEffect::staticMetaObject;
const QMetaEnum &me = mo.enumerator(mo.indexOfEnumerator(ENUM_THEME_EFFECT));
Q_ASSERT(me.keyCount());
for (int i = 0 ; i < me.keyCount(); ++i) {
ui.instantEffect->addItem(me.key(i));
}
durationChanged(effect.duration());
intensityChanged(ui.intensity->value());
attackTimeChanged(ui.attackTime->value());
attackIntensityChanged(ui.attackIntensity->value());
fadeTimeChanged(ui.fadeTime->value());
fadeIntensityChanged(ui.fadeIntensity->value());
ui.tabWidget->setTabEnabled(1, QFeedbackEffect::supportsThemeEffect());
ui.tabWidget->setTabEnabled(2, !QFeedbackFileEffect::supportedMimeTypes().isEmpty());
#ifdef Q_OS_SYMBIAN
connect(ui.tabWidget,SIGNAL(currentChanged(int)),this,SLOT(tabChanged(int)));
ui.tabWidget->setCurrentIndex(0);
ui.playPause->setFocus();
#endif
startTimer(50);
}
QFeedbackActuator* HapticsPlayer::currentActuator()
{
QList<QFeedbackActuator*> devs = QFeedbackActuator::actuators();
int index = ui.actuators->currentIndex();
if (index == -1 || devs.count() == 0 || index > devs.count()) {
if (!actuator) {
actuator = new QFeedbackActuator(this);
}
return actuator;
}
return devs.at(index);
}
void HapticsPlayer::actuatorChanged()
{
QFeedbackActuator* dev = currentActuator();
if (dev) {
enabledChanged(dev->isEnabled());
effect.setActuator(dev);
}
}
#ifdef Q_OS_SYMBIAN
void HapticsPlayer::tabChanged(int index)
{
switch(index) {
case 0:
ui.playPause->setFocus();
break;
case 1:
ui.instantPlay->setFocus();
break;
case 2:
ui.browse->setFocus();
break;
}
}
#endif
void HapticsPlayer::enabledChanged(bool on)
{
if (!on)
effect.stop();
QFeedbackActuator* dev = currentActuator();
if (dev) {
dev->setEnabled(on);
ui.enabled->setChecked(dev->isEnabled());
if (dev->isEnabled() && (dev->isCapabilitySupported(QFeedbackActuator::Envelope))) {
ui.envelope->setEnabled(true);
ui.envelope->show();
} else {
ui.envelope->setEnabled(true);
ui.envelope->hide();
}
if (dev->isEnabled() && (dev->isCapabilitySupported(QFeedbackActuator::Period))) {
ui.grpPeriod->setEnabled(true);
ui.grpPeriod->show();
} else {
ui.grpPeriod->setEnabled(false);
ui.grpPeriod->hide();
}
}
#ifdef Q_OS_SYMBIAN
ui.playPause->setFocus();
#endif
}
void HapticsPlayer::playPauseClicked()
{
if (effect.state() == QFeedbackEffect::Running) {
effect.pause();
} else {
effect.start();
}
}
void HapticsPlayer::durationChanged(int duration)
{
effect.setDuration(duration);
ui.attackTime->setMaximum(duration);
ui.fadeTime->setMaximum(duration);
attackTimeChanged(ui.attackTime->value());
}
void HapticsPlayer::intensityChanged(int value)
{
effect.setIntensity(qreal(value) / ui.intensity->maximum());
ui.lblIntensity->setText(QString::number(effect.intensity()));
}
void HapticsPlayer::timerEvent(QTimerEvent *e)
{
{
QFeedbackEffect::State newState = effect.state();
const QMetaObject *mo = effect.metaObject();
ui.effectState->setText(mo->enumerator(mo->indexOfEnumerator(ENUM_ANIMATION_STATE)).key(newState));
ui.stop->setEnabled(newState != QFeedbackEffect::Stopped);
if (effect.state() == QFeedbackEffect::Paused || effect.state() == QFeedbackEffect::Stopped)
ui.playPause->setText(tr("Play"));
else
ui.playPause->setText(tr("Pause"));
}
{
QFeedbackEffect::State newState = fileEffect.state();
const QMetaObject *mo = fileEffect.metaObject();
ui.fileEffectState->setText(mo->enumerator(mo->indexOfEnumerator(ENUM_ANIMATION_STATE)).key(newState));
ui.fileStop->setEnabled(newState != QFeedbackEffect::Stopped);
ui.filePlayPause->setEnabled(fileEffect.isLoaded());
ui.browse->setEnabled(newState == QFeedbackEffect::Stopped);
ui.fileStatus->setText( fileEffect.isLoaded() ? QString::fromLatin1("%1 : %2 ms").arg(tr("Loaded")).arg(fileEffect.duration()) : tr("Not Loaded") );
}
QWidget::timerEvent(e);
}
void HapticsPlayer::attackTimeChanged(int attackTime)
{
effect.setAttackTime(attackTime);
if (attackTime + ui.fadeTime->value() > ui.duration->value())
ui.fadeTime->setValue(ui.duration->value() - attackTime);
}
void HapticsPlayer::attackIntensityChanged(int attackIntensity)
{
effect.setAttackIntensity(qreal(attackIntensity) / ui.attackIntensity->maximum());
ui.lblAttackIntensity->setText(QString::number(effect.attackIntensity()));
}
void HapticsPlayer::fadeTimeChanged(int fadeTime)
{
effect.setFadeTime(fadeTime);
if (fadeTime + ui.attackTime->value() > ui.duration->value())
ui.attackTime->setValue(ui.duration->value() - fadeTime);
}
void HapticsPlayer::fadeIntensityChanged(int fadeIntensity)
{
effect.setFadeIntensity(qreal(fadeIntensity) / ui.fadeIntensity->maximum());
ui.lblFadeIntensity->setText(QString::number(effect.fadeIntensity()));
}
void HapticsPlayer::periodToggled(bool on)
{
effect.setPeriod(on ? ui.period->value() : 0);
}
void HapticsPlayer::periodChanged(int value)
{
effect.setPeriod(value);
}
void HapticsPlayer::instantPlayClicked()
{
const QMetaObject &mo = QFeedbackEffect::staticMetaObject;
const QMetaEnum &me = mo.enumerator(mo.indexOfEnumerator(ENUM_THEME_EFFECT));
QFeedbackEffect::playThemeEffect(QFeedbackEffect::ThemeEffect(me.keyToValue(ui.instantEffect->currentText().toLatin1())));
}
void HapticsPlayer::browseClicked()
{
QString filename = QFileDialog::getOpenFileName(this, tr("Feedback file"));
if (!filename.isEmpty()) {
ui.filename->setText(QDir::toNativeSeparators(filename));
fileEffect.setSource(QUrl::fromLocalFile(filename));
fileEffect.load();
}
}
void HapticsPlayer::filePlayPauseClicked()
{
if (fileEffect.state() == QFeedbackEffect::Running)
fileEffect.pause();
else
fileEffect.start();
}
#include "moc_hapticsplayer.cpp"