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();
}
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()

View File

@ -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;
};
}

View File

@ -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();
}

View File

@ -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<Application> m_app;

View File

@ -76,53 +76,10 @@
</property>
</widget>
</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>
</widget>
<resources>
<include location="../../data/snore.qrc"/>
</resources>
<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>
<connections/>
</ui>

View File

@ -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<PluginSettingsWidget *> SnoreCore::settingWidgets()
{
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();
/**
* 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.

View File

@ -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();
}

View File

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

View File

@ -23,6 +23,7 @@
#include <QAction>
#include <QApplication>
#include <QDialogButtonBox>
#include <QMenu>
#include <QSystemTrayIcon>
#include <QTimer>
@ -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");

View File

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

View File

@ -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;

View File

@ -46,12 +46,10 @@ private slots:
void slotInvoked();
void slotProcessQueue();
void setup();
private:
QList<Snore::Notification> m_queue;
QVector<NotifyWidget *> m_widgets;
QVector<NotifyWidget*> m_widgets;
QTimer *m_timer;
};

View File

@ -7,6 +7,7 @@
#include <QApplication>
#include <QComboBox>
#include <QDialogButtonBox>
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);
}
}

View File

@ -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:

View File

@ -32,6 +32,16 @@
<item>
<widget class="Snore::SettingsDialog" name="widget" native="true"/>
</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>
</widget>
</widget>