src/settingseditor/settingseditor.cpp

Thu, 09 Jun 2022 13:32:55 +0300

author
Teemu Piippo <teemu.s.piippo@gmail.com>
date
Thu, 09 Jun 2022 13:32:55 +0300
changeset 210
232e7634cc8a
parent 202
b05af0bab735
child 218
63125c36de73
permissions
-rw-r--r--

more refactoring, dosn't build yet

#include <QSettings>
#include <QMdiArea>
#include "gl/common.h"
#include "keyboardshortcutseditor.h"
#include "settingseditor.h"
#include "ui_settingseditor.h"

SettingsEditor::SettingsEditor(
	Configuration* settings,
	const uiutilities::KeySequenceMap& defaultKeyboardShortcuts,
	QWidget* parent
) :
	QDialog{parent},
	ui{*new Ui_SettingsEditor},
	settings{settings},
	libraries{settings, this},
	librariesEditor{settings, this},
	defaultKeyboardShortcuts{defaultKeyboardShortcuts}
{
	this->ui.setupUi(this);
	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});
	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->setLocale(this->ui.language->currentData().toString());
	this->settings->setMainColor(this->ui.mainColorButton->selectedColor());
	this->settings->setBackgroundColor(this->ui.backgroundColorButton->selectedColor());
	this->settings->setSelectedColor(this->ui.selectedColorButton->selectedColor());
	this->settings->setLineThickness(static_cast<GLfloat>(this->ui.lineThickness->value()));
	this->settings->setLineAntiAliasing(this->ui.lineAntiAliasing->isChecked());
	const int viewMode = this->ui.viewModeButtonGroup->checkedId();
	if (viewMode != -1) {
		this->settings->setViewMode(viewMode);
	}
	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->locale());
	this->ui.mainColorButton->setSelectedColor(this->settings->mainColor());
	this->ui.backgroundColorButton->setSelectedColor(this->settings->backgroundColor());
	this->ui.selectedColorButton->setSelectedColor(this->settings->selectedColor());
	this->ui.lineThickness->setValue(static_cast<double>(this->settings->lineThickness()));
	this->ui.lineAntiAliasing->setChecked(this->settings->lineAntiAliasing());
	auto* const viewModeButton = this->ui.viewModeButtonGroup->button(this->settings->viewMode());
	if (viewModeButton != nullptr) {
		viewModeButton->setChecked(true);
	}
}

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