diff --git a/src/gui/preferences/kbibtexpreferencesdialog.cpp b/src/gui/preferences/kbibtexpreferencesdialog.cpp index f470e9bd..0c9bf0a3 100644 --- a/src/gui/preferences/kbibtexpreferencesdialog.cpp +++ b/src/gui/preferences/kbibtexpreferencesdialog.cpp @@ -1,211 +1,217 @@ /*************************************************************************** * Copyright (C) 2004-2019 by Thomas Fischer * * * * This program is free software; you can redistribute it and/or modify * * it under the terms of the GNU General Public License as published by * * the Free Software Foundation; either version 2 of the License, or * * (at your option) any later version. * * * * This program is distributed in the hope that it will be useful, * * but WITHOUT ANY WARRANTY; without even the implied warranty of * * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * * GNU General Public License for more details. * * * * You should have received a copy of the GNU General Public License * * along with this program; if not, see . * ***************************************************************************/ #include "kbibtexpreferencesdialog.h" #include #include #include #include #include #include #include #include #include #include #include "settingsgeneralwidget.h" #include "settingsglobalkeywordswidget.h" #include "settingsfileexporterpdfpswidget.h" #include "settingsfileexporterwidget.h" #include "settingscolorlabelwidget.h" #include "settingsuserinterfacewidget.h" #include "settingsidsuggestionswidget.h" #include "logging_gui.h" class KBibTeXPreferencesDialog::KBibTeXPreferencesDialogPrivate { private: KBibTeXPreferencesDialog *p; QSet settingWidgets; public: bool notifyOfChanges; + QSet eventIdsToNotify; KBibTeXPreferencesDialogPrivate(KBibTeXPreferencesDialog *parent) : p(parent) { notifyOfChanges = false; } void addPages() { SettingsAbstractWidget *settingsWidget = new SettingsGeneralWidget(p); settingWidgets.insert(settingsWidget); KPageWidgetItem *pageGlobal = p->addPage(settingsWidget, settingsWidget->label()); pageGlobal->setIcon(settingsWidget->icon()); connect(settingsWidget, &SettingsAbstractWidget::changed, p, &KBibTeXPreferencesDialog::gotChanged); settingsWidget = new SettingsGlobalKeywordsWidget(p); settingWidgets.insert(settingsWidget); KPageWidgetItem *page = p->addSubPage(pageGlobal, settingsWidget, settingsWidget->label()); page->setIcon(settingsWidget->icon()); connect(settingsWidget, &SettingsAbstractWidget::changed, p, &KBibTeXPreferencesDialog::gotChanged); settingsWidget = new SettingsColorLabelWidget(p); settingWidgets.insert(settingsWidget); page = p->addSubPage(pageGlobal, settingsWidget, settingsWidget->label()); page->setIcon(settingsWidget->icon()); connect(settingsWidget, &SettingsAbstractWidget::changed, p, &KBibTeXPreferencesDialog::gotChanged); settingsWidget = new SettingsIdSuggestionsWidget(p); settingWidgets.insert(settingsWidget); page = p->addSubPage(pageGlobal, settingsWidget, settingsWidget->label()); page->setIcon(settingsWidget->icon()); connect(settingsWidget, &SettingsAbstractWidget::changed, p, &KBibTeXPreferencesDialog::gotChanged); settingsWidget = new SettingsUserInterfaceWidget(p); settingWidgets.insert(settingsWidget); page = p->addPage(settingsWidget, settingsWidget->label()); page->setIcon(settingsWidget->icon()); connect(settingsWidget, &SettingsAbstractWidget::changed, p, &KBibTeXPreferencesDialog::gotChanged); settingsWidget = new SettingsFileExporterWidget(p); settingWidgets.insert(settingsWidget); KPageWidgetItem *pageSaving = p->addPage(settingsWidget, settingsWidget->label()); pageSaving->setIcon(settingsWidget->icon()); connect(settingsWidget, &SettingsAbstractWidget::changed, p, &KBibTeXPreferencesDialog::gotChanged); settingsWidget = new SettingsFileExporterPDFPSWidget(p); settingWidgets.insert(settingsWidget); page = p->addSubPage(pageSaving, settingsWidget, settingsWidget->label()); page->setIcon(settingsWidget->icon()); connect(settingsWidget, &SettingsAbstractWidget::changed, p, &KBibTeXPreferencesDialog::gotChanged); } void loadState() { for (SettingsAbstractWidget *settingsWidget : const_cast &>(settingWidgets)) { settingsWidget->loadState(); } } void saveState() { for (SettingsAbstractWidget *settingsWidget : const_cast &>(settingWidgets)) { - settingsWidget->saveState(); + const bool settingsGotChanged = settingsWidget->saveState(); + if (settingsGotChanged) + eventIdsToNotify.insert(settingsWidget->eventId()); } } void restoreDefaults() { notifyOfChanges = true; switch (KMessageBox::warningYesNoCancel(p, i18n("This will reset the settings to factory defaults. Should this affect only the current page or all settings?"), i18n("Reset to Defaults"), KGuiItem(i18n("All settings"), QStringLiteral("edit-undo")), KGuiItem(i18n("Only current page"), QStringLiteral("document-revert")))) { case KMessageBox::Yes: { for (SettingsAbstractWidget *settingsWidget : const_cast &>(settingWidgets)) { settingsWidget->resetToDefaults(); } break; } case KMessageBox::No: { SettingsAbstractWidget *widget = qobject_cast(p->currentPage()->widget()); if (widget != nullptr) widget->resetToDefaults(); break; } case KMessageBox::Cancel: break; /// nothing to do here default: qCWarning(LOG_KBIBTEX_GUI) << "There should be no use for a default case here!"; } } void apply() { p->buttonBox()->button(QDialogButtonBox::Apply)->setEnabled(false); p->buttonBox()->button(QDialogButtonBox::Reset)->setEnabled(false); saveState(); notifyOfChanges = true; } void reset() { p->buttonBox()->button(QDialogButtonBox::Apply)->setEnabled(false); p->buttonBox()->button(QDialogButtonBox::Reset)->setEnabled(false); loadState(); } void ok() { notifyOfChanges = true; if (p->buttonBox()->button(QDialogButtonBox::Apply)->isEnabled()) { /// Apply settings only if changes have made /// (state stored by Apply button being enabled or disabled) apply(); } p->accept(); } }; KBibTeXPreferencesDialog::KBibTeXPreferencesDialog(QWidget *parent, Qt::WindowFlags flags) : KPageDialog(parent, flags), d(new KBibTeXPreferencesDialogPrivate(this)) { setFaceType(KPageDialog::Tree); setWindowTitle(i18n("Preferences")); QDialogButtonBox *buttonBox = new QDialogButtonBox(QDialogButtonBox::RestoreDefaults | QDialogButtonBox::Reset | QDialogButtonBox::Ok | QDialogButtonBox::Apply | QDialogButtonBox::Cancel, Qt::Horizontal, this); buttonBox->button(QDialogButtonBox::Apply)->setEnabled(false); buttonBox->button(QDialogButtonBox::Reset)->setEnabled(false); connect(buttonBox, &QDialogButtonBox::clicked, this, &KBibTeXPreferencesDialog::buttonClicked); setButtonBox(buttonBox); setModal(true); d->addPages(); } KBibTeXPreferencesDialog::~KBibTeXPreferencesDialog() { delete d; } void KBibTeXPreferencesDialog::hideEvent(QHideEvent *) { // TODO missing documentation: what triggers 'hideEvent' and why is 'notifyOfChanges' necessary? - if (d->notifyOfChanges) - NotificationHub::publishEvent(NotificationHub::EventConfigurationChanged); + if (d->notifyOfChanges) { + for (const int eventId : const_cast &>(d->eventIdsToNotify)) + NotificationHub::publishEvent(eventId); + d->notifyOfChanges = false; + } } void KBibTeXPreferencesDialog::buttonClicked(QAbstractButton *button) { switch (buttonBox()->standardButton(button)) { case QDialogButtonBox::Apply: d->apply(); break; case QDialogButtonBox::Ok: d->ok(); break; case QDialogButtonBox::RestoreDefaults: d->restoreDefaults(); break; case QDialogButtonBox::Reset: d->reset(); break; default: qCWarning(LOG_KBIBTEX_GUI) << "There should be no use for a default case here!"; } } void KBibTeXPreferencesDialog::gotChanged() { buttonBox()->button(QDialogButtonBox::Apply)->setEnabled(true); buttonBox()->button(QDialogButtonBox::Reset)->setEnabled(true); } diff --git a/src/gui/preferences/settingsabstractwidget.cpp b/src/gui/preferences/settingsabstractwidget.cpp index 901f38c5..f1812757 100644 --- a/src/gui/preferences/settingsabstractwidget.cpp +++ b/src/gui/preferences/settingsabstractwidget.cpp @@ -1,24 +1,31 @@ /*************************************************************************** * Copyright (C) 2004-2019 by Thomas Fischer * * * * This program is free software; you can redistribute it and/or modify * * it under the terms of the GNU General Public License as published by * * the Free Software Foundation; either version 2 of the License, or * * (at your option) any later version. * * * * This program is distributed in the hope that it will be useful, * * but WITHOUT ANY WARRANTY; without even the implied warranty of * * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * * GNU General Public License for more details. * * * * You should have received a copy of the GNU General Public License * * along with this program; if not, see . * ***************************************************************************/ #include "settingsabstractwidget.h" +#include + SettingsAbstractWidget::SettingsAbstractWidget(QWidget *parent) : QWidget(parent) { /// nothing } + +int SettingsAbstractWidget::eventId() const +{ + return NotificationHub::EventConfigurationChanged; +} diff --git a/src/gui/preferences/settingsabstractwidget.h b/src/gui/preferences/settingsabstractwidget.h index 83549759..f43e3994 100644 --- a/src/gui/preferences/settingsabstractwidget.h +++ b/src/gui/preferences/settingsabstractwidget.h @@ -1,55 +1,56 @@ /*************************************************************************** * Copyright (C) 2004-2019 by Thomas Fischer * * * * This program is free software; you can redistribute it and/or modify * * it under the terms of the GNU General Public License as published by * * the Free Software Foundation; either version 2 of the License, or * * (at your option) any later version. * * * * This program is distributed in the hope that it will be useful, * * but WITHOUT ANY WARRANTY; without even the implied warranty of * * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * * GNU General Public License for more details. * * * * You should have received a copy of the GNU General Public License * * along with this program; if not, see . * ***************************************************************************/ #ifndef KBIBTEX_GUI_SETTINGSABSTRACTWIDGET_H #define KBIBTEX_GUI_SETTINGSABSTRACTWIDGET_H #include #include #include #include "kbibtexgui_export.h" /** * @author Thomas Fischer */ class KBIBTEXGUI_EXPORT SettingsAbstractWidget : public QWidget { Q_OBJECT public: explicit SettingsAbstractWidget(QWidget *parent); // virtual ~SettingsAbstractWidget() { /* nothing */ }; + virtual int eventId() const; virtual QString label() const = 0; virtual QIcon icon() const = 0; signals: void changed(); public slots: virtual void loadState() = 0; /** * Save the state of this settings widget into the configuration settings, * usually using the Preferences class. * @return true if saved settings differed from previous values, false otherwise or under error conditions */ virtual bool saveState() = 0; virtual void resetToDefaults() = 0; }; #endif // KBIBTEX_GUI_SETTINGSABSTRACTWIDGET_H