src/settingseditor/settingseditor.cpp

Wed, 20 Jul 2022 21:48:46 +0300

author
Teemu Piippo <teemu.s.piippo@gmail.com>
date
Wed, 20 Jul 2022 21:48:46 +0300
changeset 338
719b909a7d2b
parent 335
c5830bce1c23
child 341
71c8cea3c205
permissions
-rw-r--r--

Delete unused code
Remove Model legacy type alias, QTextDocument is used now instead

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

SettingsEditor::SettingsEditor(
	const uiutilities::KeySequenceMap& defaultKeyboardShortcuts,
	QWidget* parent
) :
	QMdiSubWindow{parent},
	ui{*new Ui_SettingsEditor},
	libraries{this},
	librariesEditor{this},
	defaultKeyboardShortcuts{defaultKeyboardShortcuts}
{
	QWidget* widget = new QWidget{this};
	this->ui.setupUi(widget);
	this->setWidget(widget);
	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->ui.toolButtonStyle->addItem(tr("Icons only"), Qt::ToolButtonIconOnly);
	this->ui.toolButtonStyle->addItem(tr("Text only"), Qt::ToolButtonTextOnly);
	this->ui.toolButtonStyle->addItem(tr("Text beside icon"), Qt::ToolButtonTextBesideIcon);
	this->ui.toolButtonStyle->addItem(tr("Text under icon"), Qt::ToolButtonTextUnderIcon);
	this->ui.toolButtonStyle->addItem(tr("Style default"), Qt::ToolButtonFollowStyle);
	this->loadSettings();
	this->setWindowTitle(tr("Settings"));
	this->setWindowIcon(QIcon{":/icons/settings-outline.png"});
	this->librariesEditor.setModel(&libraries);
	QVBoxLayout* layout = new QVBoxLayout{this};
	layout->addWidget(&librariesEditor);
	this->ui.tabLdrawLibraries->setLayout(layout);
	connect(this->ui.buttonBox, &QDialogButtonBox::clicked,
		[&](QAbstractButton* button) {
			const auto role = this->ui.buttonBox->buttonRole(button);
			if (role == QDialogButtonBox::ApplyRole) {
				this->saveSettings();
			}
		});
}

SettingsEditor::~SettingsEditor()
{
	delete &this->ui;
}

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());
	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();
}

void SettingsEditor::loadSettings()
{
	this->libraries.restoreFromSettings();
	this->ui.mainColorButton->setColor(setting<Setting::MainColor>());
	this->ui.backgroundColorButton->setColor(setting<Setting::BackgroundColor>());
	this->ui.selectedColorButton->setColor(setting<Setting::SelectedColor>());
	this->ui.lineThickness->setValue(double_cast(setting<Setting::LineThickness>()));
	this->ui.lineAntiAliasing->setChecked(setting<Setting::LineAntiAliasing>());
	this->ui.logOpenGLDebugMessages->setChecked(setting<Setting::LogOpenGLDebugMessages>());
	auto* const viewModeButton = this->ui.viewModeButtonGroup->button(setting<Setting::ViewMode>());
	if (viewModeButton != nullptr) {
		viewModeButton->setChecked(true);
	}
	int toolButtonStyleIndex = this->ui.toolButtonStyle->findData(
		setting<Setting::ToolButtonStyle>());
	if (toolButtonStyleIndex != -1) {
		this->ui.toolButtonStyle->setCurrentIndex(toolButtonStyleIndex);
	}
}

mercurial