src/mainwindow.cpp

changeset 36
bbb901b97404
parent 26
3a9e761e4faa
child 39
caac957e9834
--- a/src/mainwindow.cpp	Thu Jan 30 19:20:11 2020 +0200
+++ b/src/mainwindow.cpp	Fri Jan 31 00:25:35 2020 +0200
@@ -28,6 +28,23 @@
 #include "document.h"
 #include "uiutilities.h"
 
+template<typename BaseType, typename MemberType, typename DataType>
+struct MemberData
+{
+	std::size_t member;
+	DataType payload;
+	constexpr MemberType memberInstance(BaseType* instance) const
+	{
+		return *reinterpret_cast<MemberType*>(reinterpret_cast<char*>(instance) + this->member);
+	}
+};
+
+static constexpr MemberData<Ui_MainWindow, QAction*, gl::RenderStyle> renderStyleButtons[] = {
+	{ offsetof(Ui_MainWindow, actionRenderStyleNormal), gl::RenderStyle::Normal },
+	{ offsetof(Ui_MainWindow, actionRenderStyleBfc), gl::RenderStyle::BfcRedGreen },
+	{ offsetof(Ui_MainWindow, actionRenderStyleRandom), gl::RenderStyle::RandomColors },
+};
+
 MainWindow::MainWindow(QWidget *parent) :
 	QMainWindow{parent},
 	ui{std::make_unique<Ui_MainWindow>()},
@@ -41,8 +58,17 @@
 	connect(ui->actionOpen, &QAction::triggered, this, &MainWindow::openModel);
 	connect(ui->actionQuit, &QAction::triggered, this, &QMainWindow::close);
 	connect(ui->actionSettingsEditor, &QAction::triggered, this, &MainWindow::runSettingsEditor);
+	for (auto data : ::renderStyleButtons)
+	{
+		QAction* action = data.memberInstance(this->ui.get());
+		connect(action, &QAction::triggered, [this, data]()
+		{
+			this->setRenderStyle(data.payload);
+		});
+	}
 	this->updateTitle();
 	this->restoreSettings();
+	this->updateRenderStyles();
 	this->newModel();
 }
 
@@ -136,6 +162,7 @@
 void MainWindow::openModelForEditing(const QString& modelName)
 {
 	Document* document = new Document{this->documents.findModelByName(modelName), &this->documents, this->colorTable};
+	document->setRenderStyle(this->renderStyle);
 	this->ui->tabs->addTab(document, modelName);
 	this->ui->tabs->setCurrentWidget(document);
 	document->restoreSplitterState(this->documentSplitterState);
@@ -192,6 +219,13 @@
 	}
 }
 
+void MainWindow::setRenderStyle(gl::RenderStyle renderStyle)
+{
+	this->renderStyle = renderStyle;
+	this->saveSettings();
+	this->updateRenderStyles();
+}
+
 void MainWindow::changeEvent(QEvent* event)
 {
 	if (event != nullptr)
@@ -230,6 +264,23 @@
 	setWindowTitle(title);
 }
 
+void MainWindow::updateRenderStyles()
+{
+	for (int i = 0; i < this->ui->tabs->count(); i += 1)
+	{
+		Document* document = qobject_cast<Document*>(this->ui->tabs->widget(i));
+		if (document != nullptr)
+		{
+			document->setRenderStyle(renderStyle);
+		}
+	}
+	for (auto data : ::renderStyleButtons)
+	{
+		QAction* action = data.memberInstance(this->ui.get());
+		action->setChecked(this->renderStyle == data.payload);
+	}
+}
+
 /**
  * @brief Stores the settings of the main window, storing geometry, etc
  */
@@ -238,6 +289,7 @@
 	this->settings.setValue("MainWindow/Geometry", this->saveGeometry());
 	this->settings.setValue("MainWindow/RecentlyOpened", this->recentlyOpenedFiles);
 	this->settings.setValue("MainWindow/DocumentSplitterState", this->documentSplitterState);
+	this->settings.setValue("MainWindow/RenderStyle", static_cast<int>(this->renderStyle));
 	this->libraries.storeToSettings(&this->settings);
 }
 
@@ -249,6 +301,7 @@
 	this->restoreGeometry(this->settings.value("MainWindow/Geometry").toByteArray());
 	this->recentlyOpenedFiles = this->settings.value("MainWindow/RecentlyOpened").toStringList();
 	this->documentSplitterState = this->settings.value("MainWindow/DocumentSplitterState").toByteArray();
+	this->renderStyle = static_cast<gl::RenderStyle>(this->settings.value("MainWindow/RenderStyle").toInt());
 	const QString systemLocale = QLocale::system().name();
 	const QVariant defaultLocale = this->settings.value("locale", systemLocale);
 	changeLanguage(defaultLocale.toString());

mercurial