src/mainwindow.cpp

changeset 36
bbb901b97404
parent 26
3a9e761e4faa
child 39
caac957e9834
equal deleted inserted replaced
35:98906a94732f 36:bbb901b97404
26 #include "settingseditor/settingseditor.h" 26 #include "settingseditor/settingseditor.h"
27 #include "version.h" 27 #include "version.h"
28 #include "document.h" 28 #include "document.h"
29 #include "uiutilities.h" 29 #include "uiutilities.h"
30 30
31 template<typename BaseType, typename MemberType, typename DataType>
32 struct MemberData
33 {
34 std::size_t member;
35 DataType payload;
36 constexpr MemberType memberInstance(BaseType* instance) const
37 {
38 return *reinterpret_cast<MemberType*>(reinterpret_cast<char*>(instance) + this->member);
39 }
40 };
41
42 static constexpr MemberData<Ui_MainWindow, QAction*, gl::RenderStyle> renderStyleButtons[] = {
43 { offsetof(Ui_MainWindow, actionRenderStyleNormal), gl::RenderStyle::Normal },
44 { offsetof(Ui_MainWindow, actionRenderStyleBfc), gl::RenderStyle::BfcRedGreen },
45 { offsetof(Ui_MainWindow, actionRenderStyleRandom), gl::RenderStyle::RandomColors },
46 };
47
31 MainWindow::MainWindow(QWidget *parent) : 48 MainWindow::MainWindow(QWidget *parent) :
32 QMainWindow{parent}, 49 QMainWindow{parent},
33 ui{std::make_unique<Ui_MainWindow>()}, 50 ui{std::make_unique<Ui_MainWindow>()},
34 documents{this}, 51 documents{this},
35 settings{}, 52 settings{},
39 defaultKeyboardShortcuts = uiutilities::makeKeySequenceMap(uiutilities::collectActions(this)); 56 defaultKeyboardShortcuts = uiutilities::makeKeySequenceMap(uiutilities::collectActions(this));
40 connect(ui->actionNew, &QAction::triggered, this, &MainWindow::newModel); 57 connect(ui->actionNew, &QAction::triggered, this, &MainWindow::newModel);
41 connect(ui->actionOpen, &QAction::triggered, this, &MainWindow::openModel); 58 connect(ui->actionOpen, &QAction::triggered, this, &MainWindow::openModel);
42 connect(ui->actionQuit, &QAction::triggered, this, &QMainWindow::close); 59 connect(ui->actionQuit, &QAction::triggered, this, &QMainWindow::close);
43 connect(ui->actionSettingsEditor, &QAction::triggered, this, &MainWindow::runSettingsEditor); 60 connect(ui->actionSettingsEditor, &QAction::triggered, this, &MainWindow::runSettingsEditor);
61 for (auto data : ::renderStyleButtons)
62 {
63 QAction* action = data.memberInstance(this->ui.get());
64 connect(action, &QAction::triggered, [this, data]()
65 {
66 this->setRenderStyle(data.payload);
67 });
68 }
44 this->updateTitle(); 69 this->updateTitle();
45 this->restoreSettings(); 70 this->restoreSettings();
71 this->updateRenderStyles();
46 this->newModel(); 72 this->newModel();
47 } 73 }
48 74
49 MainWindow::~MainWindow() 75 MainWindow::~MainWindow()
50 { 76 {
134 } 160 }
135 161
136 void MainWindow::openModelForEditing(const QString& modelName) 162 void MainWindow::openModelForEditing(const QString& modelName)
137 { 163 {
138 Document* document = new Document{this->documents.findModelByName(modelName), &this->documents, this->colorTable}; 164 Document* document = new Document{this->documents.findModelByName(modelName), &this->documents, this->colorTable};
165 document->setRenderStyle(this->renderStyle);
139 this->ui->tabs->addTab(document, modelName); 166 this->ui->tabs->addTab(document, modelName);
140 this->ui->tabs->setCurrentWidget(document); 167 this->ui->tabs->setCurrentWidget(document);
141 document->restoreSplitterState(this->documentSplitterState); 168 document->restoreSplitterState(this->documentSplitterState);
142 connect(document, &Document::splitterChanged, this, &MainWindow::handleDocumentSplitterChange); 169 connect(document, &Document::splitterChanged, this, &MainWindow::handleDocumentSplitterChange);
143 } 170 }
190 const QString path = action->data().toString(); 217 const QString path = action->data().toString();
191 this->openModelFromPath(path); 218 this->openModelFromPath(path);
192 } 219 }
193 } 220 }
194 221
222 void MainWindow::setRenderStyle(gl::RenderStyle renderStyle)
223 {
224 this->renderStyle = renderStyle;
225 this->saveSettings();
226 this->updateRenderStyles();
227 }
228
195 void MainWindow::changeEvent(QEvent* event) 229 void MainWindow::changeEvent(QEvent* event)
196 { 230 {
197 if (event != nullptr) 231 if (event != nullptr)
198 { 232 {
199 switch (event->type()) 233 switch (event->type())
228 title += " "; 262 title += " ";
229 title += fullVersionString(); 263 title += fullVersionString();
230 setWindowTitle(title); 264 setWindowTitle(title);
231 } 265 }
232 266
267 void MainWindow::updateRenderStyles()
268 {
269 for (int i = 0; i < this->ui->tabs->count(); i += 1)
270 {
271 Document* document = qobject_cast<Document*>(this->ui->tabs->widget(i));
272 if (document != nullptr)
273 {
274 document->setRenderStyle(renderStyle);
275 }
276 }
277 for (auto data : ::renderStyleButtons)
278 {
279 QAction* action = data.memberInstance(this->ui.get());
280 action->setChecked(this->renderStyle == data.payload);
281 }
282 }
283
233 /** 284 /**
234 * @brief Stores the settings of the main window, storing geometry, etc 285 * @brief Stores the settings of the main window, storing geometry, etc
235 */ 286 */
236 void MainWindow::saveSettings() 287 void MainWindow::saveSettings()
237 { 288 {
238 this->settings.setValue("MainWindow/Geometry", this->saveGeometry()); 289 this->settings.setValue("MainWindow/Geometry", this->saveGeometry());
239 this->settings.setValue("MainWindow/RecentlyOpened", this->recentlyOpenedFiles); 290 this->settings.setValue("MainWindow/RecentlyOpened", this->recentlyOpenedFiles);
240 this->settings.setValue("MainWindow/DocumentSplitterState", this->documentSplitterState); 291 this->settings.setValue("MainWindow/DocumentSplitterState", this->documentSplitterState);
292 this->settings.setValue("MainWindow/RenderStyle", static_cast<int>(this->renderStyle));
241 this->libraries.storeToSettings(&this->settings); 293 this->libraries.storeToSettings(&this->settings);
242 } 294 }
243 295
244 /** 296 /**
245 * @brief Restores saved settings relating to the main window 297 * @brief Restores saved settings relating to the main window
247 void MainWindow::restoreSettings() 299 void MainWindow::restoreSettings()
248 { 300 {
249 this->restoreGeometry(this->settings.value("MainWindow/Geometry").toByteArray()); 301 this->restoreGeometry(this->settings.value("MainWindow/Geometry").toByteArray());
250 this->recentlyOpenedFiles = this->settings.value("MainWindow/RecentlyOpened").toStringList(); 302 this->recentlyOpenedFiles = this->settings.value("MainWindow/RecentlyOpened").toStringList();
251 this->documentSplitterState = this->settings.value("MainWindow/DocumentSplitterState").toByteArray(); 303 this->documentSplitterState = this->settings.value("MainWindow/DocumentSplitterState").toByteArray();
304 this->renderStyle = static_cast<gl::RenderStyle>(this->settings.value("MainWindow/RenderStyle").toInt());
252 const QString systemLocale = QLocale::system().name(); 305 const QString systemLocale = QLocale::system().name();
253 const QVariant defaultLocale = this->settings.value("locale", systemLocale); 306 const QVariant defaultLocale = this->settings.value("locale", systemLocale);
254 changeLanguage(defaultLocale.toString()); 307 changeLanguage(defaultLocale.toString());
255 this->libraries.restoreFromSettings(&this->settings); 308 this->libraries.restoreFromSettings(&this->settings);
256 this->updateRecentlyOpenedDocumentsMenu(); 309 this->updateRecentlyOpenedDocumentsMenu();

mercurial