From 2432f9781d268fc61f7fd05300e78e2560517e7a Mon Sep 17 00:00:00 2001 From: =?utf8?q?Michal=20Mal=C3=BD?= Date: Mon, 29 Jul 2013 15:12:26 +0200 Subject: [PATCH] Enable the option to remove effect. --- ffbdevice.cpp | 23 +++++++++++++++++++---- ffbdevice.h | 3 ++- mainwindow.cpp | 14 ++++++++++++++ mainwindow.h | 2 ++ 4 files changed, 37 insertions(+), 5 deletions(-) diff --git a/ffbdevice.cpp b/ffbdevice.cpp index 8314a3a..4dfe712 100644 --- a/ffbdevice.cpp +++ b/ffbdevice.cpp @@ -180,11 +180,28 @@ bool FFBDevice::queryDeviceCapabilities() return true; } -bool FFBDevice::removeEffect(const int idx) +bool FFBDevice::removeAndEraseEffect(const int idx) { if (m_effects[idx]->status() == FFBEffect::FFBEffectStatus::NOT_LOADED) return true; + if (removeEffect(idx)) { + m_effects[idx] = FFBEffectFactory::createEffect(FFBEffectTypes::NONE); + if (m_effects[idx]->type() != FFBEffectTypes::NONE) { + qCritical("Unable to empty the effect slot."); + return false; + } + } else { + qCritical("Unable to stop the effect."); + return false; + } + + m_effects[idx]->setStatus(FFBEffect::FFBEffectStatus::NOT_LOADED); + return true; +} + +bool FFBDevice::removeEffect(const int idx) +{ if (!stopEffect(idx)) return false; @@ -192,15 +209,13 @@ bool FFBDevice::removeEffect(const int idx) int ret = ioctl(c_fd, EVIOCRMFF, internalIdx); if (ret < 0) return false; - - m_effects[idx]->setStatus(FFBEffect::FFBEffectStatus::NOT_LOADED); return true; } bool FFBDevice::startEffect(const int idx, FFBEffectTypes type, std::shared_ptr params) { if (idx < 0 || idx > c_maxEffectCount) { - qDebug() << "Effect index out of bounds"; + qCritical() << "Effect index out of bounds"; return false; } diff --git a/ffbdevice.h b/ffbdevice.h index 80f2744..f63e5ce 100644 --- a/ffbdevice.h +++ b/ffbdevice.h @@ -30,7 +30,7 @@ public: inline const QString& id() const { return c_id; } inline int maxEffectCount() const { return c_maxEffectCount; } bool queryDeviceCapabilities(); - bool removeEffect(const int idx); + bool removeAndEraseEffect(const int idx); bool startEffect(const int idx, FFBEffectTypes type, std::shared_ptr params); bool stopEffect(const int idx); QString waveformName(const PeriodicWaveforms waveform) const; @@ -38,6 +38,7 @@ public: private: bool isEffectUpdateable(const std::shared_ptr effect, const std::shared_ptr params, const FFBEffectTypes type); + bool removeEffect(const int idx); int uploadEffect(struct ff_effect* effect); std::vector m_availableEffects; std::vector m_availablePeriodicWaveforms; diff --git a/mainwindow.cpp b/mainwindow.cpp index 7a3ee0f..7c246d0 100644 --- a/mainwindow.cpp +++ b/mainwindow.cpp @@ -3,6 +3,7 @@ #include #include +const QString MainWindow::res_deviceErrorCap("Device error"); const QString MainWindow::res_effectNotLoaded("Not loaded"); const QString MainWindow::res_effectPlaying("Playing"); const QString MainWindow::res_effectStopped("Stopped"); @@ -26,6 +27,7 @@ MainWindow::MainWindow(std::shared_ptr prober, const QString& titl connect(ui->cbox_effectSlots, SIGNAL(activated(const int)), this, SLOT(onEffectSlotSelected(const int))); connect(ui->cbox_effectTypes, SIGNAL(activated(const int)), this, SLOT(onEffectTypeSelected(const int))); connect(ui->qpb_refreshDevices, SIGNAL(clicked()), this, SLOT(onRefreshDevicesClicked())); + connect(ui->qpb_remove, SIGNAL(clicked()), this, SLOT(onRemoveEffectClicked())); connect(ui->qpb_start, SIGNAL(clicked()), this, SLOT(onStartEffectClicked())); connect(ui->qpb_stop, SIGNAL(clicked()), this, SLOT(onStopEffectClicked())); } @@ -119,6 +121,16 @@ void MainWindow::onRefreshDevicesClicked() fillDeviceList(); } +void MainWindow::onRemoveEffectClicked() +{ + if (m_activeDevice == nullptr) + return; + if (!m_activeDevice->removeAndEraseEffect(ui->cbox_effectSlots->currentIndex())) + QMessageBox::warning(this, res_deviceErrorCap, "Unable to remove the effect."); + else + setEffectStatusText(FFBEffect::FFBEffectStatus::NOT_LOADED); +} + void MainWindow::onStartEffectClicked() { if (m_activeDevice == nullptr) @@ -134,6 +146,8 @@ void MainWindow::onStartEffectClicked() bool ret = m_activeDevice->startEffect(effectSlot, type, params); if (ret) setEffectStatusText(m_activeDevice->effectStatusByIdx(effectSlot)); + else + QMessageBox::warning(this, res_deviceErrorCap, "Unable to start the effect."); } void MainWindow::onStopEffectClicked() diff --git a/mainwindow.h b/mainwindow.h index f134b9f..95d33f4 100644 --- a/mainwindow.h +++ b/mainwindow.h @@ -38,6 +38,7 @@ private: std::shared_ptr m_prober; Ui::MainWindow* ui; + static const QString res_deviceErrorCap; static const QString res_effectNotLoaded; static const QString res_effectPlaying; static const QString res_effectStopped; @@ -48,6 +49,7 @@ private slots: void onEffectSlotSelected(const int idx); void onEffectTypeSelected(const int idx); void onRefreshDevicesClicked(); + void onRemoveEffectClicked(); void onStartEffectClicked(); void onStopEffectClicked(); }; -- 2.43.5