src/settingseditor/settingseditor.cpp

changeset 341
71c8cea3c205
parent 335
c5830bce1c23
child 347
5c655cc006de
--- 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()

mercurial