From 2cb4f4196457cbeac1bedb1930b423041adfa995 Mon Sep 17 00:00:00 2001 From: Patrick von Reth Date: Fri, 10 Apr 2015 10:06:16 +0200 Subject: [PATCH] added a volume option to the sound backend. stop sound when notification timed out --- src/plugins/secondary_backends/sound/sound.cpp | 12 ++++++++++-- src/plugins/secondary_backends/sound/sound.h | 1 - .../secondary_backends/sound/soundsettings.cpp | 17 +++++++++++------ .../secondary_backends/sound/soundsettings.h | 4 +++- 4 files changed, 24 insertions(+), 10 deletions(-) diff --git a/src/plugins/secondary_backends/sound/sound.cpp b/src/plugins/secondary_backends/sound/sound.cpp index a6a867b..34d8f87 100644 --- a/src/plugins/secondary_backends/sound/sound.cpp +++ b/src/plugins/secondary_backends/sound/sound.cpp @@ -19,6 +19,7 @@ #include "soundsettings.h" #include +#include using namespace Snore; @@ -26,7 +27,8 @@ Sound::Sound(): SnoreSecondaryBackend("Sound", false), m_player(new QMediaPlayer(this)) { - m_player->setVolume(50); + setDefaultValue("Volume",50); + m_player->setVolume(value("Volume").toInt()); // connect(m_player,QMediaPlayer::positionChanged,[](qint64 pos){ // snoreDebug(SNORE_DEBUG) << "Player: " << pos; // }); @@ -60,6 +62,12 @@ void Sound::slotNotify(Snore::Notification notification) m_player->setMedia(QUrl::fromLocalFile(sound)); snoreDebug(SNORE_DEBUG) << "SoundFile:" << m_player->media().canonicalUrl(); m_player->play(); + QTimer *timeout = new QTimer(this); + timeout->setSingleShot(true); + timeout->setInterval(notification.timeout() * 1000); + connect(timeout, &QTimer::timeout, [&m_player,timeout]{ + m_player->stop(); + timeout->deleteLater(); + }); } } - diff --git a/src/plugins/secondary_backends/sound/sound.h b/src/plugins/secondary_backends/sound/sound.h index 707ec5a..de71a07 100644 --- a/src/plugins/secondary_backends/sound/sound.h +++ b/src/plugins/secondary_backends/sound/sound.h @@ -33,7 +33,6 @@ public: public slots: void slotNotify(Snore::Notification notification) override; - private: class QMediaPlayer *m_player; }; diff --git a/src/plugins/secondary_backends/sound/soundsettings.cpp b/src/plugins/secondary_backends/sound/soundsettings.cpp index df61dd8..60ac1a7 100644 --- a/src/plugins/secondary_backends/sound/soundsettings.cpp +++ b/src/plugins/secondary_backends/sound/soundsettings.cpp @@ -20,23 +20,26 @@ #include #include +#include #include using namespace Snore; SoundSettings::SoundSettings(SnorePlugin *snorePlugin, QWidget *parent) : PluginSettingsWidget(snorePlugin, parent), - m_lineEdit(new QLineEdit) + m_lineEditFileName(new QLineEdit), + m_spinBoxVolume(new QSpinBox) { - addRow(tr("Audio file:"), m_lineEdit); + addRow(tr("Volume:"), m_spinBoxVolume); + addRow(tr("Audio file:"), m_lineEditFileName); QPushButton *button = new QPushButton(tr("Select a audio file")); connect(button, &QPushButton::clicked, [this]() { QFileDialog dialog; dialog.setNameFilter(tr("All Audio files").append("(*.mp3 *.wav *.ogg)")); dialog.setFileMode(QFileDialog::ExistingFile); - dialog.setDirectory(m_lineEdit->text()); + dialog.setDirectory(m_lineEditFileName->text()); if (dialog.exec()) { - m_lineEdit->setText(dialog.selectedFiles().first()); + m_lineEditFileName->setText(dialog.selectedFiles().first()); } }); addRow("", button); @@ -48,11 +51,13 @@ SoundSettings::~SoundSettings() void SoundSettings::load() { - m_lineEdit->setText(value("Sound").toString()); + m_lineEditFileName->setText(value("Sound").toString()); + m_spinBoxVolume->setValue(value("Volume").toInt()); } void SoundSettings::save() { - setValue("Sound", m_lineEdit->text()); + setValue("Sound", m_lineEditFileName->text()); + setValue("Volume", m_spinBoxVolume->value()); } diff --git a/src/plugins/secondary_backends/sound/soundsettings.h b/src/plugins/secondary_backends/sound/soundsettings.h index b8fd60f..4f4dec6 100644 --- a/src/plugins/secondary_backends/sound/soundsettings.h +++ b/src/plugins/secondary_backends/sound/soundsettings.h @@ -5,6 +5,7 @@ #include "plugins/pluginsettingswidget.h" class QLineEdit; +class QSpinBox; class SoundSettings : public Snore::PluginSettingsWidget { Q_OBJECT @@ -17,7 +18,8 @@ public: void save() override; private: - QLineEdit *m_lineEdit; + QLineEdit *m_lineEditFileName; + QSpinBox *m_spinBoxVolume; };