src/settingseditor/settingseditor.cpp

changeset 357
66c75604f5b8
parent 356
65b4741b302d
--- a/src/settingseditor/settingseditor.cpp	Sat Apr 08 21:48:49 2023 +0300
+++ b/src/settingseditor/settingseditor.cpp	Sat Apr 08 22:09:29 2023 +0300
@@ -1,5 +1,6 @@
 #include <QSettings>
 #include <QMdiArea>
+#include <QFileDialog>
 #include <ui_settingseditor.h>
 #include "src/settings.h"
 #include "src/gl/common.h"
@@ -87,6 +88,45 @@
 			}
 		}
 	);
+	connect(
+		this->ui.exportSettingsButton,
+		&QAbstractButton::clicked,
+		[&]
+		{
+			const QString defaultName = QStringLiteral(CMAKE_PROJECT_NAME ".conf");
+			QString filename = QFileDialog::getSaveFileName(this, "", defaultName, "*.conf");
+			if (not filename.isEmpty())
+			{
+				QSettings settings;
+				QSettings exportSettings{filename, QSettings::IniFormat};
+				for (const QString& key : settings.allKeys())
+				{
+					exportSettings.setValue(key, settings.value(key));
+				}
+				exportSettings.sync();
+			}
+		}
+	);
+	connect(
+		this->ui.importSettingsButton,
+		&QAbstractButton::clicked,
+		[&]
+		{
+			QString filename = QFileDialog::getOpenFileName(this, "", "", "*.ini");
+			if (not filename.isEmpty())
+			{
+				QSettings settings;
+				const QSettings importSettings{filename};
+				settings.clear();
+				for (const QString& key : importSettings.allKeys())
+				{
+					settings.setValue(key, importSettings.value(key));
+				}
+				settings.sync();
+				this->loadSettings();
+			}
+		}
+	);
 	for (auto* widget : this->findChildren<QAbstractButton*>()) {
 		connect(widget, &QAbstractButton::clicked, this, &SettingsEditor::saveSettings);
 	}
@@ -108,30 +148,34 @@
 
 void SettingsEditor::saveSettings()
 {
-	setSetting<Setting::MainColor>(this->ui.mainColorButton->color());
-	setSetting<Setting::BackgroundColor>(this->ui.backgroundColorButton->color());
-	setSetting<Setting::SelectedColor>(this->ui.selectedColorButton->color());
-	setSetting<Setting::LineThickness>(static_cast<GLfloat>(this->ui.lineThickness->value()));
-	setSetting<Setting::LineAntiAliasing>(this->ui.lineAntiAliasing->isChecked());
-	setSetting<Setting::LogOpenGLDebugMessages>(this->ui.logOpenGLDebugMessages->isChecked());
-	setSetting<Setting::CodeEditorUseSystemFont>(this->ui.codeEditorSystemFont->isChecked());
-	setSetting<Setting::CodeEditorFontFamily>(this->ui.codeEditorFontFamily->currentText());
-	setSetting<Setting::CodeEditorFontSize>(this->ui.codeEditorFontSize->value());
-	const int viewMode = this->ui.viewModeButtonGroup->checkedId();
-	if (viewMode != -1) {
-		setSetting<Setting::ViewMode>(static_cast<QMdiArea::ViewMode>(viewMode));
+	if (not disableSettingsAutoSave)
+	{
+		setSetting<Setting::MainColor>(this->ui.mainColorButton->color());
+		setSetting<Setting::BackgroundColor>(this->ui.backgroundColorButton->color());
+		setSetting<Setting::SelectedColor>(this->ui.selectedColorButton->color());
+		setSetting<Setting::LineThickness>(static_cast<GLfloat>(this->ui.lineThickness->value()));
+		setSetting<Setting::LineAntiAliasing>(this->ui.lineAntiAliasing->isChecked());
+		setSetting<Setting::LogOpenGLDebugMessages>(this->ui.logOpenGLDebugMessages->isChecked());
+		setSetting<Setting::CodeEditorUseSystemFont>(this->ui.codeEditorSystemFont->isChecked());
+		setSetting<Setting::CodeEditorFontFamily>(this->ui.codeEditorFontFamily->currentText());
+		setSetting<Setting::CodeEditorFontSize>(this->ui.codeEditorFontSize->value());
+		const int viewMode = this->ui.viewModeButtonGroup->checkedId();
+		if (viewMode != -1) {
+			setSetting<Setting::ViewMode>(static_cast<QMdiArea::ViewMode>(viewMode));
+		}
+		const QVariant toolButtonStyle = this->ui.toolButtonStyle->currentData();
+		if (toolButtonStyle.isValid()) {
+			setSetting<Setting::ToolButtonStyle>(
+				static_cast<Qt::ToolButtonStyle>(toolButtonStyle.toInt()));
+		}
+		this->librariesEditor.saveSettings();
+		Q_EMIT this->settingsChanged();
 	}
-	const QVariant toolButtonStyle = this->ui.toolButtonStyle->currentData();
-	if (toolButtonStyle.isValid()) {
-		setSetting<Setting::ToolButtonStyle>(
-			static_cast<Qt::ToolButtonStyle>(toolButtonStyle.toInt()));
-	}
-	this->librariesEditor.saveSettings();
-	Q_EMIT this->settingsChanged();
 }
 
 void SettingsEditor::loadSettings()
 {
+	this->disableSettingsAutoSave = true;
 	this->libraries.restoreFromSettings();
 	this->ui.mainColorButton->setColor(setting<Setting::MainColor>());
 	this->ui.backgroundColorButton->setColor(setting<Setting::BackgroundColor>());
@@ -151,4 +195,5 @@
 	if (toolButtonStyleIndex != -1) {
 		this->ui.toolButtonStyle->setCurrentIndex(toolButtonStyleIndex);
 	}
+	this->disableSettingsAutoSave = false;
 }

mercurial