Add settings import and export

Sat, 08 Apr 2023 22:09:29 +0300

author
Teemu Piippo <teemu.s.piippo@gmail.com>
date
Sat, 08 Apr 2023 22:09:29 +0300
changeset 357
66c75604f5b8
parent 356
65b4741b302d
child 358
ef90ed0a5720

Add settings import and export

src/settingseditor/settingseditor.cpp file | annotate | diff | comparison | revisions
src/settingseditor/settingseditor.h file | annotate | diff | comparison | revisions
src/settingseditor/settingseditor.ui file | annotate | diff | comparison | revisions
--- 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;
 }
--- a/src/settingseditor/settingseditor.h	Sat Apr 08 21:48:49 2023 +0300
+++ b/src/settingseditor/settingseditor.h	Sat Apr 08 22:09:29 2023 +0300
@@ -17,6 +17,7 @@
 Q_SIGNALS:
 	void settingsChanged();
 private:
+	bool disableSettingsAutoSave = false;
 	class Ui_SettingsEditor& ui;
 	LibrariesModel libraries;
 	LibrariesEditor librariesEditor;
--- a/src/settingseditor/settingseditor.ui	Sat Apr 08 21:48:49 2023 +0300
+++ b/src/settingseditor/settingseditor.ui	Sat Apr 08 22:09:29 2023 +0300
@@ -6,8 +6,8 @@
    <rect>
     <x>0</x>
     <y>0</y>
-    <width>718</width>
-    <height>629</height>
+    <width>972</width>
+    <height>817</height>
    </rect>
   </property>
   <property name="windowTitle">
@@ -30,8 +30,8 @@
        <rect>
         <x>0</x>
         <y>0</y>
-        <width>680</width>
-        <height>560</height>
+        <width>948</width>
+        <height>746</height>
        </rect>
       </property>
       <layout class="QVBoxLayout" name="verticalLayout">
@@ -159,6 +159,37 @@
                </layout>
               </widget>
              </item>
+             <item>
+              <widget class="QGroupBox" name="groupBox_4">
+               <property name="title">
+                <string>Export settings</string>
+               </property>
+               <layout class="QVBoxLayout" name="verticalLayout_7">
+                <item>
+                 <widget class="QPushButton" name="exportSettingsButton">
+                  <property name="text">
+                   <string>Export settings to file…</string>
+                  </property>
+                  <property name="icon">
+                   <iconset resource="../../resources.qrc">
+                    <normaloff>:/icons/save-as-outline.png</normaloff>:/icons/save-as-outline.png</iconset>
+                  </property>
+                 </widget>
+                </item>
+                <item>
+                 <widget class="QPushButton" name="importSettingsButton">
+                  <property name="text">
+                   <string>Import settings from file…</string>
+                  </property>
+                  <property name="icon">
+                   <iconset resource="../../resources.qrc">
+                    <normaloff>:/icons/folder-open-outline.png</normaloff>:/icons/folder-open-outline.png</iconset>
+                  </property>
+                 </widget>
+                </item>
+               </layout>
+              </widget>
+             </item>
             </layout>
            </item>
            <item>
@@ -316,7 +347,10 @@
    <header>widgets/coloredit.h</header>
   </customwidget>
  </customwidgets>
- <resources/>
+ <resources>
+  <include location="../../resources.qrc"/>
+  <include location="../../resources.qrc"/>
+ </resources>
  <connections/>
  <buttongroups>
   <buttongroup name="viewModeButtonGroup"/>

mercurial