From 503d264839bafb0087f6e4d5a8e29d1646be7088 Mon Sep 17 00:00:00 2001 From: Patrick von Reth Date: Tue, 3 Mar 2015 13:02:07 +0100 Subject: [PATCH] make it more easy to embed the settings dialog --- src/core/plugins/pluginsettingswidget.cpp | 10 ++++- src/core/plugins/pluginsettingswidget.h | 3 ++ src/core/settingsdialog.cpp | 37 +++++++--------- src/core/settingsdialog.h | 9 +--- src/core/settingsdialog.ui | 45 +------------------- src/core/snore.cpp | 9 +++- src/core/snore.h | 8 +++- src/core/snore_p.cpp | 7 ++- src/core/snore_p.h | 2 +- src/daemon/trayicon.cpp | 26 +++++++++++ src/plugins/backends/snore/notifywidget.cpp | 1 + src/plugins/backends/snore/snorenotifier.cpp | 38 +++++++---------- src/plugins/backends/snore/snorenotifier.h | 4 +- src/settings/settingswindow.cpp | 23 +++++++++- src/settings/settingswindow.h | 3 ++ src/settings/settingswindow.ui | 10 +++++ 16 files changed, 129 insertions(+), 106 deletions(-) diff --git a/src/core/plugins/pluginsettingswidget.cpp b/src/core/plugins/pluginsettingswidget.cpp index 54b8679..77d6d2d 100644 --- a/src/core/plugins/pluginsettingswidget.cpp +++ b/src/core/plugins/pluginsettingswidget.cpp @@ -70,6 +70,11 @@ void PluginSettingsWidget::saveSettings() save(); } +bool PluginSettingsWidget::isDirty() +{ + return m_dirty; +} + QVariant PluginSettingsWidget::value(const QString &key) const { return m_snorePlugin->value(key); @@ -77,7 +82,10 @@ QVariant PluginSettingsWidget::value(const QString &key) const void PluginSettingsWidget::setValue(const QString &key, const QVariant &value) { - m_snorePlugin->setValue(key, value); + if (this->value(key) != value) { + m_snorePlugin->setValue(key, value); + m_dirty = true; + } } void PluginSettingsWidget::load() diff --git a/src/core/plugins/pluginsettingswidget.h b/src/core/plugins/pluginsettingswidget.h index b2b2930..41646b6 100644 --- a/src/core/plugins/pluginsettingswidget.h +++ b/src/core/plugins/pluginsettingswidget.h @@ -42,6 +42,8 @@ public: void loadSettings(); void saveSettings(); + bool isDirty(); + protected: QVariant value(const QString &key) const; void setValue(const QString &key, const QVariant &value); @@ -53,6 +55,7 @@ private: SnorePlugin *m_snorePlugin; QFormLayout *m_layout; QCheckBox *m_enabled; + bool m_dirty = false; }; } diff --git a/src/core/settingsdialog.cpp b/src/core/settingsdialog.cpp index dd5ea6e..6b2930e 100644 --- a/src/core/settingsdialog.cpp +++ b/src/core/settingsdialog.cpp @@ -77,44 +77,39 @@ void SettingsDialog::load() void SettingsDialog::save() { snoreDebug(SNORE_DEBUG) << "saving"; + bool dirty = false; for (auto w : m_tabs) { w->saveSettings(); + dirty |= w->isDirty(); } + dirty |= SnoreCore::instance().value("PrimaryBackend", LOCAL_SETTING).toString() != ui->primaryBackendComboBox->currentText(); + dirty |= SnoreCore::instance().value("Timeout", LOCAL_SETTING).toInt() != ui->timeoutSpinBox->value(); SnoreCore::instance().setValue("PrimaryBackend", ui->primaryBackendComboBox->currentText(), LOCAL_SETTING); SnoreCore::instance().setValue("Timeout", ui->timeoutSpinBox->value(), LOCAL_SETTING); - SnoreCorePrivate::instance()->syncSettings(); -} -void Snore::SettingsDialog::on_buttonBox_clicked(QAbstractButton *button) -{ - switch (ui->buttonBox->buttonRole(button)) { - case QDialogButtonBox::ApplyRole: - save(); - break; - case QDialogButtonBox::ResetRole: - load(); - break; - default: - snoreDebug(SNORE_WARNING) << "unhandled role" << button->text(); + if (dirty) { + SnoreCorePrivate::instance()->syncSettings(); } } -void SettingsDialog::show() +void SettingsDialog::setVisible(bool b) { - load(); - QWidget::show(); + if (b) { + load(); + } else { + save(); + } + QWidget::setVisible(b); } void SettingsDialog::accept() { - hide(); save(); - emit finished(); } -void SettingsDialog::reject() + +void SettingsDialog::reset() { - hide(); - emit finished(); + load(); } diff --git a/src/core/settingsdialog.h b/src/core/settingsdialog.h index c804ef0..c702f5a 100644 --- a/src/core/settingsdialog.h +++ b/src/core/settingsdialog.h @@ -50,20 +50,15 @@ public: public slots: - void show(); + void setVisible(bool b) override; void accept(); - void reject(); - -signals: - void finished(); + void reset(); private slots: void on_pushButton_clicked(); void load(); void save(); - void on_buttonBox_clicked(QAbstractButton *button); - private: Ui::SettingsDialog *ui; QScopedPointer m_app; diff --git a/src/core/settingsdialog.ui b/src/core/settingsdialog.ui index 4c789ef..288b39d 100644 --- a/src/core/settingsdialog.ui +++ b/src/core/settingsdialog.ui @@ -76,53 +76,10 @@ - - - - Qt::Horizontal - - - QDialogButtonBox::Apply|QDialogButtonBox::Cancel|QDialogButtonBox::Ok|QDialogButtonBox::Reset - - - - - - buttonBox - accepted() - SettingsDialog - accept() - - - 248 - 254 - - - 157 - 274 - - - - - buttonBox - rejected() - SettingsDialog - reject() - - - 316 - 260 - - - 286 - 274 - - - - + diff --git a/src/core/snore.cpp b/src/core/snore.cpp index a08f62e..78cd067 100644 --- a/src/core/snore.cpp +++ b/src/core/snore.cpp @@ -38,7 +38,7 @@ SnoreCore::SnoreCore() d_ptr = new SnoreCorePrivate(); Q_D(SnoreCore); d->q_ptr = this; - d->setDefaults(); + d->init(); } SnoreCore &SnoreCore::instance() @@ -181,6 +181,13 @@ bool SnoreCore::primaryBackendSupportsRichtext() return d->m_notificationBackend->supportsRichtext(); } +void SnoreCore::setDefaultApplication(Application app) +{ + Q_D(SnoreCore); + app.addAlert(Alert("Default", Icon(":/root/snore.png"))); + d->m_defaultApp = app; +} + QList SnoreCore::settingWidgets() { Q_D(SnoreCore); diff --git a/src/core/snore.h b/src/core/snore.h index cf864f2..7ca9bb2 100644 --- a/src/core/snore.h +++ b/src/core/snore.h @@ -140,10 +140,16 @@ public: /** * - * @return whether the backend supports rhichtext encoding + * @return whether the backend supports rhichtext encoding. */ bool primaryBackendSupportsRichtext(); + /** + * Sets the default application used for internal notifications. + * @param app The default application. + */ + void setDefaultApplication(Application app); + /** * * @return A list of widgets a settings dialog. diff --git a/src/core/snore_p.cpp b/src/core/snore_p.cpp index 395a82d..5512172 100644 --- a/src/core/snore_p.cpp +++ b/src/core/snore_p.cpp @@ -31,7 +31,6 @@ using namespace Snore; SnoreCorePrivate::SnoreCorePrivate(): - m_defaultApp("SnoreNotify", Icon(":/root/snore.png")), m_settings(new QSettings("Snorenotify", "libsnore", this)) { snoreDebug(SNORE_INFO) << "Version:" << Version::version(); @@ -43,7 +42,6 @@ SnoreCorePrivate::SnoreCorePrivate(): snoreDebug(SNORE_DEBUG) << "Snore settings are located in" << m_settings->fileName(); snoreDebug(SNORE_DEBUG) << "Snore local settings are located in" << normalizeKey("Test", LOCAL_SETTING); - m_defaultApp.addAlert(Alert("Default", Icon(":/root/snore.png"))); connect(qApp, SIGNAL(aboutToQuit()), this, SLOT(slotAboutToQuit())); } @@ -132,10 +130,11 @@ bool SnoreCorePrivate::initPrimaryNotificationBackend() return false; } -void SnoreCorePrivate::setDefaults() +void SnoreCorePrivate::init() { Q_Q(SnoreCore); q->setDefaultValue("Timeout", 10, LOCAL_SETTING); + q->setDefaultApplication(Application("SnoreNotify", Icon(":/root/snore.png"))); } void SnoreCorePrivate::syncSettings() @@ -183,7 +182,7 @@ QStringList SnoreCorePrivate::knownClients(){ void SnoreCorePrivate::setLocalSttingsPrefix(const QString &prefix) { m_localSettingsPrefix = prefix; - setDefaults(); + init(); syncSettings(); } diff --git a/src/core/snore_p.h b/src/core/snore_p.h index 951563f..2613422 100644 --- a/src/core/snore_p.h +++ b/src/core/snore_p.h @@ -86,7 +86,7 @@ public: void setLocalSttingsPrefix(const QString &prefix); - void setDefaults(); + void init(); signals: void applicationRegistered(const Snore::Application &); diff --git a/src/daemon/trayicon.cpp b/src/daemon/trayicon.cpp index d639a8f..e9dce58 100644 --- a/src/daemon/trayicon.cpp +++ b/src/daemon/trayicon.cpp @@ -23,6 +23,7 @@ #include #include +#include #include #include #include @@ -43,6 +44,31 @@ TrayIcon::TrayIcon(): void TrayIcon::initConextMenu() { m_settings = new SettingsDialog(); + QDialogButtonBox *box = new QDialogButtonBox(QDialogButtonBox::Apply|QDialogButtonBox::Cancel|QDialogButtonBox::Ok|QDialogButtonBox::Reset, m_settings); + connect(box, QDialogButtonBox::clicked,[&,box](QAbstractButton *button){ + switch (box->buttonRole(button)) { + case QDialogButtonBox::AcceptRole: + m_settings->accept(); + qApp->quit(); + break; + case QDialogButtonBox::ApplyRole: + m_settings->accept(); + break; + case QDialogButtonBox::ResetRole: + m_settings->reset(); + break; + case QDialogButtonBox::RejectRole: + qApp->quit(); + break; + default: + snoreDebug(SNORE_WARNING) << "unhandled role" << button->text() << box->buttonRole(button); + } + }); + m_settings->layout()->addWidget(box); + + +// connect(m_settings, &SettingsDialog::finished, m_settings, &SettingsDialog::hide); + m_trayIcon->setVisible(true); m_trayMenu = new QMenu("SnoreNotify"); diff --git a/src/plugins/backends/snore/notifywidget.cpp b/src/plugins/backends/snore/notifywidget.cpp index a04bbec..ffc0497 100644 --- a/src/plugins/backends/snore/notifywidget.cpp +++ b/src/plugins/backends/snore/notifywidget.cpp @@ -67,6 +67,7 @@ NotifyWidget::NotifyWidget(int pos, const SnoreNotifier *parent) : NotifyWidget::~NotifyWidget() { + release(); } void NotifyWidget::display(const Notification ¬ification) diff --git a/src/plugins/backends/snore/snorenotifier.cpp b/src/plugins/backends/snore/snorenotifier.cpp index 45db25a..6e0fcf5 100644 --- a/src/plugins/backends/snore/snorenotifier.cpp +++ b/src/plugins/backends/snore/snorenotifier.cpp @@ -28,13 +28,18 @@ using namespace Snore; SnoreNotifier::SnoreNotifier(): SnoreBackend("Snore", true, true, true), - m_widgets(3) + m_widgets(3), + m_timer(new QTimer(this)) { + setDefaultValue("Position", Qt::TopRightCorner); + m_timer->setInterval(500); + connect(m_timer, &QTimer::timeout, this, &SnoreNotifier::slotProcessQueue); + } SnoreNotifier::~SnoreNotifier() { - foreach(NotifyWidget * w, m_widgets) { + for(auto w : m_widgets) { w->deleteLater(); } } @@ -121,25 +126,16 @@ void SnoreNotifier::slotProcessQueue() } } -void SnoreNotifier::setup() -{ - for (int i = 0; i < m_widgets.size(); ++i) { - NotifyWidget *w = new NotifyWidget(i, this); - m_widgets[i] = w; - connect(w, SIGNAL(dismissed()), this, SLOT(slotDismissed())); - connect(w, SIGNAL(invoked()), this, SLOT(slotInvoked())); - } - - m_timer = new QTimer(this); - m_timer->setInterval(500); - connect(m_timer, SIGNAL(timeout()), this, SLOT(slotProcessQueue())); -} - bool SnoreNotifier::initialize() { if (SnoreBackend::initialize()) { - setDefaultValue("Position", Qt::TopRightCorner); - return metaObject()->invokeMethod(this, "setup", Qt::QueuedConnection); + for (int i = 0; i < m_widgets.size(); ++i) { + NotifyWidget *w = new NotifyWidget(i, this); + m_widgets[i] = w; + connect(w, &NotifyWidget::dismissed, this, &SnoreNotifier::slotDismissed); + connect(w, &NotifyWidget::invoked, this, &SnoreNotifier::slotInvoked); + } + return true; } return false; } @@ -147,11 +143,9 @@ bool SnoreNotifier::initialize() bool SnoreNotifier::deinitialize() { if (SnoreBackend::deinitialize()) { - for (int i = 0; i < m_widgets.size(); ++i) { - m_widgets[i]->release(); - m_widgets[i]->deleteLater(); + for (auto w : m_widgets) { + w->deleteLater(); } - m_timer->deleteLater(); return true; } return false; diff --git a/src/plugins/backends/snore/snorenotifier.h b/src/plugins/backends/snore/snorenotifier.h index 808fe47..3d84ef3 100644 --- a/src/plugins/backends/snore/snorenotifier.h +++ b/src/plugins/backends/snore/snorenotifier.h @@ -46,12 +46,10 @@ private slots: void slotInvoked(); void slotProcessQueue(); - void setup(); - private: QList m_queue; - QVector m_widgets; + QVector m_widgets; QTimer *m_timer; }; diff --git a/src/settings/settingswindow.cpp b/src/settings/settingswindow.cpp index 0e0b995..663fbf2 100644 --- a/src/settings/settingswindow.cpp +++ b/src/settings/settingswindow.cpp @@ -7,6 +7,7 @@ #include #include +#include using namespace Snore; @@ -16,7 +17,6 @@ SettingsWindow::SettingsWindow(QWidget *parent) : { ui->setupUi(this); ui->widget->show(); - connect(ui->widget, &Snore::SettingsDialog::finished, qApp, &QApplication::quit); QStringList list = SnoreCorePrivate::instance()->knownClients(); list.removeAll(qApp->applicationName()); @@ -33,3 +33,24 @@ void SettingsWindow::on_comboBox_currentIndexChanged(const QString &arg1) SnoreCorePrivate::instance()->setLocalSttingsPrefix(arg1); ui->widget->show(); } + +void SettingsWindow::on_buttonBox_clicked(QAbstractButton *button) +{ + switch (ui->buttonBox->buttonRole(button)) { + case QDialogButtonBox::AcceptRole: + ui->widget->accept(); + qApp->quit(); + break; + case QDialogButtonBox::ApplyRole: + ui->widget->accept(); + break; + case QDialogButtonBox::ResetRole: + ui->widget->reset(); + break; + case QDialogButtonBox::RejectRole: + qApp->quit(); + break; + default: + snoreDebug(SNORE_WARNING) << "unhandled role" << button->text() << ui->buttonBox->buttonRole(button); + } +} diff --git a/src/settings/settingswindow.h b/src/settings/settingswindow.h index 94414bc..d780b2d 100644 --- a/src/settings/settingswindow.h +++ b/src/settings/settingswindow.h @@ -7,6 +7,8 @@ namespace Ui { class SettingsWindow; } +class QAbstractButton; + class SettingsWindow : public QMainWindow { Q_OBJECT @@ -16,6 +18,7 @@ public: ~SettingsWindow(); private slots: + void on_buttonBox_clicked(QAbstractButton *button); void on_comboBox_currentIndexChanged(const QString &arg1); private: diff --git a/src/settings/settingswindow.ui b/src/settings/settingswindow.ui index 4abb24d..e1c4987 100644 --- a/src/settings/settingswindow.ui +++ b/src/settings/settingswindow.ui @@ -32,6 +32,16 @@ + + + + Qt::Horizontal + + + QDialogButtonBox::Apply|QDialogButtonBox::Cancel|QDialogButtonBox::Ok|QDialogButtonBox::Reset + + +