# HG changeset patch # User Teemu Piippo # Date 1680945844 -10800 # Node ID 71c8cea3c2052300eeeb66f70c014ee19bc685e8 # Parent e17e07661f4c399b02f07576ec6aee646fd452e0 Save settings as soon as they are changed, Cancel and Reset buttons revert changes diff -r e17e07661f4c -r 71c8cea3c205 src/settingseditor/settingseditor.cpp --- a/src/settingseditor/settingseditor.cpp Sat Jul 23 01:38:43 2022 +0300 +++ b/src/settingseditor/settingseditor.cpp Sat Apr 08 12:24:04 2023 +0300 @@ -6,6 +6,25 @@ #include "src/settingseditor/keyboardshortcutseditor.h" #include "src/settingseditor/settingseditor.h" +static QVariantMap storeSettings() +{ + QVariantMap result; + QSettings settingsObject; + for (const QString& key : settingsObject.allKeys()) { + result[key] = settingsObject.value(key); + } + return result; +} + +static void restoreSettings(const QVariantMap& storedValues) +{ + QSettings settingsObject; + settingsObject.clear(); + for (const QString& key : storedValues.keys()) { + settingsObject.setValue(key, storedValues[key]); + } +} + SettingsEditor::SettingsEditor( const uiutilities::KeySequenceMap& defaultKeyboardShortcuts, QWidget* parent @@ -16,9 +35,9 @@ librariesEditor{this}, defaultKeyboardShortcuts{defaultKeyboardShortcuts} { - QWidget* widget = new QWidget{this}; - this->ui.setupUi(widget); - this->setWidget(widget); + QWidget* centralWidget = new QWidget{this}; + this->ui.setupUi(centralWidget); + this->setWidget(centralWidget); this->ui.keyboardShortcutsView->setModel(new KeyboardShortcutsEditor{parent, this}); this->ui.viewModeButtonGroup->setId(this->ui.viewModeTabs, int{QMdiArea::TabbedView}); this->ui.viewModeButtonGroup->setId(this->ui.viewModeSubWindows, int{QMdiArea::SubWindowView}); @@ -34,13 +53,43 @@ QVBoxLayout* layout = new QVBoxLayout{this}; layout->addWidget(&librariesEditor); this->ui.tabLdrawLibraries->setLayout(layout); - connect(this->ui.buttonBox, &QDialogButtonBox::clicked, - [&](QAbstractButton* button) { + QSettings settingsObject; + connect( + this->ui.buttonBox, &QDialogButtonBox::clicked, + [this, previousSettings = storeSettings()](QAbstractButton* button) + { const auto role = this->ui.buttonBox->buttonRole(button); - if (role == QDialogButtonBox::ApplyRole) { - this->saveSettings(); + switch (role) + { + case QDialogButtonBox::AcceptRole: + this->close(); + break; + case QDialogButtonBox::RejectRole: + restoreSettings(previousSettings); + Q_EMIT this->settingsChanged(); + this->close(); + break; + case QDialogButtonBox::ResetRole: + restoreSettings(previousSettings); + Q_EMIT this->settingsChanged(); + break; + default: + break; } - }); + } + ); + for (auto* widget : this->findChildren()) { + connect(widget, &QAbstractButton::clicked, this, &SettingsEditor::saveSettings); + } + for (auto* widget : this->findChildren()) { + connect(widget, qOverload(&QSpinBox::valueChanged), this, &SettingsEditor::saveSettings); + } + for (auto* widget : this->findChildren()) { + connect(widget, &ColorButton::colorChanged, this, &SettingsEditor::saveSettings); + } + for (auto* widget : this->findChildren()) { + connect(widget, qOverload(&QComboBox::currentIndexChanged), this, &SettingsEditor::saveSettings); + } } SettingsEditor::~SettingsEditor() diff -r e17e07661f4c -r 71c8cea3c205 src/settingseditor/settingseditor.ui --- a/src/settingseditor/settingseditor.ui Sat Jul 23 01:38:43 2022 +0300 +++ b/src/settingseditor/settingseditor.ui Sat Apr 08 12:24:04 2023 +0300 @@ -30,7 +30,7 @@ 0 0 - 690 + 694 558 @@ -255,7 +255,7 @@ Qt::Horizontal - QDialogButtonBox::Apply + QDialogButtonBox::Cancel|QDialogButtonBox::Ok|QDialogButtonBox::Reset