diff -r 98906a94732f -r bbb901b97404 src/mainwindow.cpp --- 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 +struct MemberData +{ + std::size_t member; + DataType payload; + constexpr MemberType memberInstance(BaseType* instance) const + { + return *reinterpret_cast(reinterpret_cast(instance) + this->member); + } +}; + +static constexpr MemberData 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()}, @@ -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(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(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(this->settings.value("MainWindow/RenderStyle").toInt()); const QString systemLocale = QLocale::system().name(); const QVariant defaultLocale = this->settings.value("locale", systemLocale); changeLanguage(defaultLocale.toString());