1 // For license of this file, see <project-root-folder>/LICENSE.md.
2 
3 #include "gui/notifications/singlenotificationeditor.h"
4 
5 #include "miscellaneous/application.h"
6 #include "miscellaneous/iconfactory.h"
7 
8 #include <QCompleter>
9 #include <QFileDialog>
10 #include <QFileSystemModel>
11 
SingleNotificationEditor(const Notification & notification,QWidget * parent)12 SingleNotificationEditor::SingleNotificationEditor(const Notification& notification, QWidget* parent)
13   : QGroupBox(parent), m_notificationEvent(Notification::Event::NoEvent) {
14   m_ui.setupUi(this);
15 
16 #if defined(Q_OS_OS2)
17   m_ui.m_wdgSound->setVisible(false);
18 #else
19   m_ui.m_btnBrowseSound->setIcon(qApp->icons()->fromTheme(QSL("document-open")));
20   m_ui.m_btnPlaySound->setIcon(qApp->icons()->fromTheme(QSL("media-playback-start")));
21 #endif
22 
23   loadNotification(notification);
24 
25   connect(m_ui.m_btnPlaySound, &QPushButton::clicked, this, &SingleNotificationEditor::playSound);
26   connect(m_ui.m_btnBrowseSound, &QPushButton::clicked, this, &SingleNotificationEditor::selectSoundFile);
27   connect(m_ui.m_txtSound, &QLineEdit::textChanged, this, &SingleNotificationEditor::notificationChanged);
28   connect(m_ui.m_cbBalloon, &QCheckBox::toggled, this, &SingleNotificationEditor::notificationChanged);
29   connect(m_ui.m_slidVolume, &QSlider::valueChanged, this, &SingleNotificationEditor::notificationChanged);
30 
31   QCompleter* completer = new QCompleter(qApp->builtinSounds(), this);
32   m_ui.m_txtSound->setCompleter(completer);
33 
34   setFixedHeight(sizeHint().height());
35 }
36 
notification() const37 Notification SingleNotificationEditor::notification() const {
38   return Notification(m_notificationEvent, m_ui.m_cbBalloon->isChecked(), m_ui.m_txtSound->text(), m_ui.m_slidVolume->value());
39 }
40 
selectSoundFile()41 void SingleNotificationEditor::selectSoundFile() {
42   auto fil = QFileDialog::getOpenFileName(window(), tr("Select sound file"),
43                                           qApp->homeFolder(),
44                                           tr("WAV files (*.wav);;MP3 files (*.mp3)"));
45 
46   if (!fil.isEmpty()) {
47     m_ui.m_txtSound->setText(fil);
48   }
49 }
50 
playSound()51 void SingleNotificationEditor::playSound() {
52   notification().playSound(qApp);
53 }
54 
loadNotification(const Notification & notification)55 void SingleNotificationEditor::loadNotification(const Notification& notification) {
56   m_ui.m_txtSound->setText(notification.soundPath());
57   m_ui.m_slidVolume->setValue(notification.volume());
58   m_ui.m_cbBalloon->setChecked(notification.balloonEnabled());
59   m_notificationEvent = notification.event();
60 
61   setTitle(Notification::nameForEvent(notification.event()));
62 }
63