enhance notification sounds with volume control

This commit is contained in:
Martin Rotter 2021-09-07 09:30:24 +02:00
parent 8b7b57a2fd
commit 5c5a5b004c
6 changed files with 68 additions and 16 deletions

View File

@ -39,7 +39,6 @@ void NotificationsEditor::loadNotifications(const QList<Notification>& notificat
connect(notif_editor, &SingleNotificationEditor::notificationChanged, this, &NotificationsEditor::someNotificationChanged);
m_layout->addWidget(notif_editor);
}
}

View File

@ -26,6 +26,7 @@ SingleNotificationEditor::SingleNotificationEditor(const Notification& notificat
connect(m_ui.m_btnBrowseSound, &QPushButton::clicked, this, &SingleNotificationEditor::selectSoundFile);
connect(m_ui.m_txtSound, &QLineEdit::textChanged, this, &SingleNotificationEditor::notificationChanged);
connect(m_ui.m_cbBalloon, &QCheckBox::toggled, this, &SingleNotificationEditor::notificationChanged);
connect(m_ui.m_slidVolume, &QSlider::valueChanged, this, &SingleNotificationEditor::notificationChanged);
QCompleter* completer = new QCompleter(qApp->builtinSounds(), this);
m_ui.m_txtSound->setCompleter(completer);
@ -34,13 +35,13 @@ SingleNotificationEditor::SingleNotificationEditor(const Notification& notificat
}
Notification SingleNotificationEditor::notification() const {
return Notification(m_notificationEvent, m_ui.m_cbBalloon->isChecked(), m_ui.m_txtSound->text());
return Notification(m_notificationEvent, m_ui.m_cbBalloon->isChecked(), m_ui.m_txtSound->text(), m_ui.m_slidVolume->value());
}
void SingleNotificationEditor::selectSoundFile() {
auto fil = QFileDialog::getOpenFileName(window(), tr("Select sound file"),
qApp->homeFolder(),
tr("WAV files (*.wav)"));
tr("WAV files (*.wav);;MP3 files (*.mp3)"));
if (!fil.isEmpty()) {
m_ui.m_txtSound->setText(fil);
@ -48,13 +49,14 @@ void SingleNotificationEditor::selectSoundFile() {
}
void SingleNotificationEditor::playSound() {
Notification({}, {}, m_ui.m_txtSound->text()).playSound(qApp);
notification().playSound(qApp);
}
void SingleNotificationEditor::loadNotification(const Notification& notification) {
m_ui.m_txtSound->setText(notification.soundPath());
m_ui.m_slidVolume->setValue(notification.volume());
m_ui.m_cbBalloon->setChecked(notification.balloonEnabled());
setTitle(Notification::nameForEvent(notification.event()));
m_notificationEvent = notification.event();
setTitle(Notification::nameForEvent(notification.event()));
}

View File

@ -80,6 +80,23 @@
</item>
</layout>
</item>
<item row="1" column="1">
<widget class="QSlider" name="m_slidVolume">
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
</widget>
</item>
<item row="1" column="0">
<widget class="QLabel" name="label_2">
<property name="text">
<string>Volume</string>
</property>
<property name="buddy">
<cstring>m_slidVolume</cstring>
</property>
</widget>
</item>
</layout>
</widget>
</item>

View File

@ -7,17 +7,17 @@
#include <QDir>
#if !defined(Q_OS_OS2)
#include <QSound>
#include <QMediaPlayer>
#endif
Notification::Notification(Notification::Event event, bool balloon, const QString& sound_path)
: m_event(event), m_balloonEnabled(balloon), m_soundPath(sound_path) {}
Notification::Notification(Notification::Event event, bool balloon, const QString& sound_path, int volume)
: m_event(event), m_balloonEnabled(balloon), m_soundPath(sound_path), m_volume(volume) {}
Notification::Event Notification::event() const {
return m_event;
}
void Notification::setEvent(const Event& event) {
void Notification::setEvent(Event event) {
m_event = event;
}
@ -32,7 +32,26 @@ void Notification::setSoundPath(const QString& sound_path) {
void Notification::playSound(Application* app) const {
if (!m_soundPath.isEmpty()) {
#if !defined(Q_OS_OS2)
QSound::play(QDir::toNativeSeparators(app->replaceDataUserDataFolderPlaceholder(m_soundPath)));
QMediaPlayer* play = new QMediaPlayer(app);
QObject::connect(play, &QMediaPlayer::stateChanged, play, [play](QMediaPlayer::State state) {
if (state == QMediaPlayer::State::StoppedState) {
play->deleteLater();
}
});
if (m_soundPath.startsWith(QSL(":"))) {
play->setMedia(QMediaContent(QUrl(QSL("qrc") + m_soundPath)));
}
else {
play->setMedia(QMediaContent(
QUrl::fromLocalFile(
QDir::toNativeSeparators(app->replaceDataUserDataFolderPlaceholder(m_soundPath)))));
}
play->setVolume(m_volume);
play->play();
#endif
}
}
@ -63,7 +82,7 @@ QString Notification::nameForEvent(Notification::Event event) {
return QObject::tr("Login failed");
case Notification::Event::NewAppVersionAvailable:
return QObject::tr("New %1 version is available").arg(APP_NAME);
return QObject::tr("New %1 version is available").arg(QSL(APP_NAME));
case Notification::Event::GeneralEvent:
return QObject::tr("Miscellaneous events");
@ -73,6 +92,14 @@ QString Notification::nameForEvent(Notification::Event event) {
}
}
int Notification::volume() const {
return m_volume;
}
void Notification::setVolume(int volume) {
m_volume = volume;
}
bool Notification::balloonEnabled() const {
return m_balloonEnabled;
}

View File

@ -33,12 +33,16 @@ class Notification {
LoginFailure = 6
};
explicit Notification(Event event = Event::NoEvent, bool balloon = {}, const QString& sound_path = {});
explicit Notification(Event event = Event::NoEvent, bool balloon = {}, const QString& sound_path = {},
int volume = {});
bool balloonEnabled() const;
Event event() const;
void setEvent(const Event& event);
void setEvent(Event event);
int volume() const;
void setVolume(int volume);
// Returns full path to audio file which should be played when notification
// is launched.
@ -55,6 +59,7 @@ class Notification {
Event m_event;
bool m_balloonEnabled;
QString m_soundPath;
int m_volume;
};
#endif // NOTIFICATION_H

View File

@ -48,8 +48,9 @@ void NotificationFactory::load(Settings* settings) {
auto data = settings->value(GROUP(Notifications), key).toStringList();
auto enabled = data.at(0).toInt() != 0;
auto sound = data.at(1);
auto volume = data.size() > 2 ? data.at(2).toInt() : 100;
m_notifications.append(Notification(event, enabled, sound));
m_notifications.append(Notification(event, enabled, sound, volume));
}
}
@ -60,7 +61,8 @@ void NotificationFactory::save(const QList<Notification>& new_notifications, Set
for (const auto& n : qAsConst(m_notifications)) {
settings->setValue(GROUP(Notifications), QString::number(int(n.event())), QStringList {
n.balloonEnabled() ? QSL("1") : QSL("0"),
n.soundPath()
n.soundPath(),
QString::number(n.volume())
});
}
}