Sat, 08 Apr 2023 12:24:04 +0300
Save settings as soon as they are changed, Cancel and Reset buttons revert changes
src/settingseditor/settingseditor.cpp | file | annotate | diff | comparison | revisions | |
src/settingseditor/settingseditor.ui | file | annotate | diff | comparison | revisions |
--- 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<QAbstractButton*>()) { + connect(widget, &QAbstractButton::clicked, this, &SettingsEditor::saveSettings); + } + for (auto* widget : this->findChildren<QSpinBox*>()) { + connect(widget, qOverload<int>(&QSpinBox::valueChanged), this, &SettingsEditor::saveSettings); + } + for (auto* widget : this->findChildren<ColorButton*>()) { + connect(widget, &ColorButton::colorChanged, this, &SettingsEditor::saveSettings); + } + for (auto* widget : this->findChildren<QComboBox*>()) { + connect(widget, qOverload<int>(&QComboBox::currentIndexChanged), this, &SettingsEditor::saveSettings); + } } SettingsEditor::~SettingsEditor()
--- 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 @@ <rect> <x>0</x> <y>0</y> - <width>690</width> + <width>694</width> <height>558</height> </rect> </property> @@ -255,7 +255,7 @@ <enum>Qt::Horizontal</enum> </property> <property name="standardButtons"> - <set>QDialogButtonBox::Apply</set> + <set>QDialogButtonBox::Cancel|QDialogButtonBox::Ok|QDialogButtonBox::Reset</set> </property> </widget> </item>