src/settingseditor/settingseditor.cpp

changeset 7
68443f5be176
child 16
aeb5f203b3eb
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/settingseditor/settingseditor.cpp	Sat Oct 05 23:47:03 2019 +0300
@@ -0,0 +1,73 @@
+#include <QSettings>
+#include "settingseditor.h"
+#include "ui_settingseditor.h"
+
+SettingsEditor::SettingsEditor(QSettings* settings, QWidget* parent) :
+	QDialog{parent},
+	ui{*new Ui_SettingsEditor},
+	settings{settings},
+	libraries{settings, this},
+	librariesEditor{settings, this}
+{
+	this->ui.setupUi(this);
+	this->loadLocales();
+	this->setDefaults();
+	QVBoxLayout* layout = new QVBoxLayout{this};
+	layout->addWidget(&librariesEditor);
+	this->ui.tabLdrawLibraries->setLayout(layout);
+	connect(
+		this,
+		&SettingsEditor::accepted,
+		this,
+		&SettingsEditor::handleAccepted);
+}
+
+SettingsEditor::~SettingsEditor()
+{
+	delete &this->ui;
+}
+
+void SettingsEditor::handleAccepted()
+{
+	this->settings->setValue("locale", this->ui.language->currentData().toString());
+	this->librariesEditor.saveSettings(this->settings);
+}
+
+void SettingsEditor::loadLocales()
+{
+	this->ui.language->clear();
+	QDir dir{":/locale"};
+	// Collect translation files in built-in resources
+	QVector<QString> localeCodes = {"en"}; // English is the default locale
+	for (const QFileInfo& file : dir.entryInfoList(QDir::Files))
+	{
+		localeCodes.append(file.baseName());
+	}
+	std::sort(localeCodes.begin(), localeCodes.end());
+	this->ui.language->addItem(tr("System language"), "system");
+	for (const QString& localeCode : localeCodes)
+	{
+		const QLocale locale{localeCode};
+		const QString languageName = QLocale::languageToString(locale.language());
+		const QIcon flag{":/flags/" + localeCode + ".png"};
+		this->ui.language->addItem(languageName, localeCode);
+		this->ui.language->setItemIcon(this->ui.language->count() - 1, flag);
+	}
+}
+
+void SettingsEditor::setDefaults()
+{
+	this->setCurrentLanguage(this->settings->value("locale", QLocale::system().name()).toString());
+}
+
+void SettingsEditor::setCurrentLanguage(const QString& localeCode)
+{
+	for (int i = 0; i < this->ui.language->count(); i += 1)
+	{
+		if (this->ui.language->itemData(i) == localeCode)
+		{
+			this->ui.language->setCurrentIndex(i);
+			break;
+		}
+	}
+}

mercurial