make it more easy to embed the settings dialog

This commit is contained in:
Patrick von Reth 2015-03-03 13:02:07 +01:00
parent f83bbe53a8
commit 503d264839
16 changed files with 129 additions and 106 deletions

View File

@ -70,6 +70,11 @@ void PluginSettingsWidget::saveSettings()
save(); save();
} }
bool PluginSettingsWidget::isDirty()
{
return m_dirty;
}
QVariant PluginSettingsWidget::value(const QString &key) const QVariant PluginSettingsWidget::value(const QString &key) const
{ {
return m_snorePlugin->value(key); 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) void PluginSettingsWidget::setValue(const QString &key, const QVariant &value)
{ {
if (this->value(key) != value) {
m_snorePlugin->setValue(key, value); m_snorePlugin->setValue(key, value);
m_dirty = true;
}
} }
void PluginSettingsWidget::load() void PluginSettingsWidget::load()

View File

@ -42,6 +42,8 @@ public:
void loadSettings(); void loadSettings();
void saveSettings(); void saveSettings();
bool isDirty();
protected: protected:
QVariant value(const QString &key) const; QVariant value(const QString &key) const;
void setValue(const QString &key, const QVariant &value); void setValue(const QString &key, const QVariant &value);
@ -53,6 +55,7 @@ private:
SnorePlugin *m_snorePlugin; SnorePlugin *m_snorePlugin;
QFormLayout *m_layout; QFormLayout *m_layout;
QCheckBox *m_enabled; QCheckBox *m_enabled;
bool m_dirty = false;
}; };
} }

View File

@ -77,44 +77,39 @@ void SettingsDialog::load()
void SettingsDialog::save() void SettingsDialog::save()
{ {
snoreDebug(SNORE_DEBUG) << "saving"; snoreDebug(SNORE_DEBUG) << "saving";
bool dirty = false;
for (auto w : m_tabs) { for (auto w : m_tabs) {
w->saveSettings(); 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("PrimaryBackend", ui->primaryBackendComboBox->currentText(), LOCAL_SETTING);
SnoreCore::instance().setValue("Timeout", ui->timeoutSpinBox->value(), LOCAL_SETTING); SnoreCore::instance().setValue("Timeout", ui->timeoutSpinBox->value(), LOCAL_SETTING);
if (dirty) {
SnoreCorePrivate::instance()->syncSettings(); SnoreCorePrivate::instance()->syncSettings();
} }
}
void Snore::SettingsDialog::on_buttonBox_clicked(QAbstractButton *button) void SettingsDialog::setVisible(bool b)
{ {
switch (ui->buttonBox->buttonRole(button)) { if (b) {
case QDialogButtonBox::ApplyRole: load();
} else {
save(); save();
break;
case QDialogButtonBox::ResetRole:
load();
break;
default:
snoreDebug(SNORE_WARNING) << "unhandled role" << button->text();
} }
} QWidget::setVisible(b);
void SettingsDialog::show()
{
load();
QWidget::show();
} }
void SettingsDialog::accept() void SettingsDialog::accept()
{ {
hide();
save(); save();
emit finished();
} }
void SettingsDialog::reject()
void SettingsDialog::reset()
{ {
hide(); load();
emit finished();
} }

View File

@ -50,20 +50,15 @@ public:
public slots: public slots:
void show(); void setVisible(bool b) override;
void accept(); void accept();
void reject(); void reset();
signals:
void finished();
private slots: private slots:
void on_pushButton_clicked(); void on_pushButton_clicked();
void load(); void load();
void save(); void save();
void on_buttonBox_clicked(QAbstractButton *button);
private: private:
Ui::SettingsDialog *ui; Ui::SettingsDialog *ui;
QScopedPointer<Application> m_app; QScopedPointer<Application> m_app;

View File

@ -76,53 +76,10 @@
</property> </property>
</widget> </widget>
</item> </item>
<item>
<widget class="QDialogButtonBox" name="buttonBox">
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
<property name="standardButtons">
<set>QDialogButtonBox::Apply|QDialogButtonBox::Cancel|QDialogButtonBox::Ok|QDialogButtonBox::Reset</set>
</property>
</widget>
</item>
</layout> </layout>
</widget> </widget>
<resources> <resources>
<include location="../../data/snore.qrc"/> <include location="../../data/snore.qrc"/>
</resources> </resources>
<connections> <connections/>
<connection>
<sender>buttonBox</sender>
<signal>accepted()</signal>
<receiver>SettingsDialog</receiver>
<slot>accept()</slot>
<hints>
<hint type="sourcelabel">
<x>248</x>
<y>254</y>
</hint>
<hint type="destinationlabel">
<x>157</x>
<y>274</y>
</hint>
</hints>
</connection>
<connection>
<sender>buttonBox</sender>
<signal>rejected()</signal>
<receiver>SettingsDialog</receiver>
<slot>reject()</slot>
<hints>
<hint type="sourcelabel">
<x>316</x>
<y>260</y>
</hint>
<hint type="destinationlabel">
<x>286</x>
<y>274</y>
</hint>
</hints>
</connection>
</connections>
</ui> </ui>

View File

@ -38,7 +38,7 @@ SnoreCore::SnoreCore()
d_ptr = new SnoreCorePrivate(); d_ptr = new SnoreCorePrivate();
Q_D(SnoreCore); Q_D(SnoreCore);
d->q_ptr = this; d->q_ptr = this;
d->setDefaults(); d->init();
} }
SnoreCore &SnoreCore::instance() SnoreCore &SnoreCore::instance()
@ -181,6 +181,13 @@ bool SnoreCore::primaryBackendSupportsRichtext()
return d->m_notificationBackend->supportsRichtext(); 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<PluginSettingsWidget *> SnoreCore::settingWidgets() QList<PluginSettingsWidget *> SnoreCore::settingWidgets()
{ {
Q_D(SnoreCore); Q_D(SnoreCore);

View File

@ -140,10 +140,16 @@ public:
/** /**
* *
* @return whether the backend supports rhichtext encoding * @return whether the backend supports rhichtext encoding.
*/ */
bool primaryBackendSupportsRichtext(); 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. * @return A list of widgets a settings dialog.

View File

@ -31,7 +31,6 @@
using namespace Snore; using namespace Snore;
SnoreCorePrivate::SnoreCorePrivate(): SnoreCorePrivate::SnoreCorePrivate():
m_defaultApp("SnoreNotify", Icon(":/root/snore.png")),
m_settings(new QSettings("Snorenotify", "libsnore", this)) m_settings(new QSettings("Snorenotify", "libsnore", this))
{ {
snoreDebug(SNORE_INFO) << "Version:" << Version::version(); 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 settings are located in" << m_settings->fileName();
snoreDebug(SNORE_DEBUG) << "Snore local settings are located in" << normalizeKey("Test", LOCAL_SETTING); 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())); connect(qApp, SIGNAL(aboutToQuit()), this, SLOT(slotAboutToQuit()));
} }
@ -132,10 +130,11 @@ bool SnoreCorePrivate::initPrimaryNotificationBackend()
return false; return false;
} }
void SnoreCorePrivate::setDefaults() void SnoreCorePrivate::init()
{ {
Q_Q(SnoreCore); Q_Q(SnoreCore);
q->setDefaultValue("Timeout", 10, LOCAL_SETTING); q->setDefaultValue("Timeout", 10, LOCAL_SETTING);
q->setDefaultApplication(Application("SnoreNotify", Icon(":/root/snore.png")));
} }
void SnoreCorePrivate::syncSettings() void SnoreCorePrivate::syncSettings()
@ -183,7 +182,7 @@ QStringList SnoreCorePrivate::knownClients(){
void SnoreCorePrivate::setLocalSttingsPrefix(const QString &prefix) void SnoreCorePrivate::setLocalSttingsPrefix(const QString &prefix)
{ {
m_localSettingsPrefix = prefix; m_localSettingsPrefix = prefix;
setDefaults(); init();
syncSettings(); syncSettings();
} }

View File

@ -86,7 +86,7 @@ public:
void setLocalSttingsPrefix(const QString &prefix); void setLocalSttingsPrefix(const QString &prefix);
void setDefaults(); void init();
signals: signals:
void applicationRegistered(const Snore::Application &); void applicationRegistered(const Snore::Application &);

View File

@ -23,6 +23,7 @@
#include <QAction> #include <QAction>
#include <QApplication> #include <QApplication>
#include <QDialogButtonBox>
#include <QMenu> #include <QMenu>
#include <QSystemTrayIcon> #include <QSystemTrayIcon>
#include <QTimer> #include <QTimer>
@ -43,6 +44,31 @@ TrayIcon::TrayIcon():
void TrayIcon::initConextMenu() void TrayIcon::initConextMenu()
{ {
m_settings = new SettingsDialog(); 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_trayIcon->setVisible(true);
m_trayMenu = new QMenu("SnoreNotify"); m_trayMenu = new QMenu("SnoreNotify");

View File

@ -67,6 +67,7 @@ NotifyWidget::NotifyWidget(int pos, const SnoreNotifier *parent) :
NotifyWidget::~NotifyWidget() NotifyWidget::~NotifyWidget()
{ {
release();
} }
void NotifyWidget::display(const Notification &notification) void NotifyWidget::display(const Notification &notification)

View File

@ -28,13 +28,18 @@ using namespace Snore;
SnoreNotifier::SnoreNotifier(): SnoreNotifier::SnoreNotifier():
SnoreBackend("Snore", true, true, true), 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() SnoreNotifier::~SnoreNotifier()
{ {
foreach(NotifyWidget * w, m_widgets) { for(auto w : m_widgets) {
w->deleteLater(); 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() bool SnoreNotifier::initialize()
{ {
if (SnoreBackend::initialize()) { if (SnoreBackend::initialize()) {
setDefaultValue("Position", Qt::TopRightCorner); for (int i = 0; i < m_widgets.size(); ++i) {
return metaObject()->invokeMethod(this, "setup", Qt::QueuedConnection); 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; return false;
} }
@ -147,11 +143,9 @@ bool SnoreNotifier::initialize()
bool SnoreNotifier::deinitialize() bool SnoreNotifier::deinitialize()
{ {
if (SnoreBackend::deinitialize()) { if (SnoreBackend::deinitialize()) {
for (int i = 0; i < m_widgets.size(); ++i) { for (auto w : m_widgets) {
m_widgets[i]->release(); w->deleteLater();
m_widgets[i]->deleteLater();
} }
m_timer->deleteLater();
return true; return true;
} }
return false; return false;

View File

@ -46,8 +46,6 @@ private slots:
void slotInvoked(); void slotInvoked();
void slotProcessQueue(); void slotProcessQueue();
void setup();
private: private:
QList<Snore::Notification> m_queue; QList<Snore::Notification> m_queue;

View File

@ -7,6 +7,7 @@
#include <QApplication> #include <QApplication>
#include <QComboBox> #include <QComboBox>
#include <QDialogButtonBox>
using namespace Snore; using namespace Snore;
@ -16,7 +17,6 @@ SettingsWindow::SettingsWindow(QWidget *parent) :
{ {
ui->setupUi(this); ui->setupUi(this);
ui->widget->show(); ui->widget->show();
connect(ui->widget, &Snore::SettingsDialog::finished, qApp, &QApplication::quit);
QStringList list = SnoreCorePrivate::instance()->knownClients(); QStringList list = SnoreCorePrivate::instance()->knownClients();
list.removeAll(qApp->applicationName()); list.removeAll(qApp->applicationName());
@ -33,3 +33,24 @@ void SettingsWindow::on_comboBox_currentIndexChanged(const QString &arg1)
SnoreCorePrivate::instance()->setLocalSttingsPrefix(arg1); SnoreCorePrivate::instance()->setLocalSttingsPrefix(arg1);
ui->widget->show(); 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);
}
}

View File

@ -7,6 +7,8 @@ namespace Ui {
class SettingsWindow; class SettingsWindow;
} }
class QAbstractButton;
class SettingsWindow : public QMainWindow class SettingsWindow : public QMainWindow
{ {
Q_OBJECT Q_OBJECT
@ -16,6 +18,7 @@ public:
~SettingsWindow(); ~SettingsWindow();
private slots: private slots:
void on_buttonBox_clicked(QAbstractButton *button);
void on_comboBox_currentIndexChanged(const QString &arg1); void on_comboBox_currentIndexChanged(const QString &arg1);
private: private:

View File

@ -32,6 +32,16 @@
<item> <item>
<widget class="Snore::SettingsDialog" name="widget" native="true"/> <widget class="Snore::SettingsDialog" name="widget" native="true"/>
</item> </item>
<item>
<widget class="QDialogButtonBox" name="buttonBox">
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
<property name="standardButtons">
<set>QDialogButtonBox::Apply|QDialogButtonBox::Cancel|QDialogButtonBox::Ok|QDialogButtonBox::Reset</set>
</property>
</widget>
</item>
</layout> </layout>
</widget> </widget>
</widget> </widget>