Sat, 01 Feb 2020 15:33:57 +0200
wired renderstyle up
24 | 1 | /* |
2 | * LDForge: LDraw parts authoring CAD | |
3 | * Copyright (C) 2013 - 2020 Teemu Piippo | |
4 | * | |
5 | * This program is free software: you can redistribute it and/or modify | |
6 | * it under the terms of the GNU General Public License as published by | |
7 | * the Free Software Foundation, either version 3 of the License, or | |
8 | * (at your option) any later version. | |
9 | * | |
10 | * This program is distributed in the hope that it will be useful, | |
11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
13 | * GNU General Public License for more details. | |
14 | * | |
15 | * You should have received a copy of the GNU General Public License | |
16 | * along with this program. If not, see <http://www.gnu.org/licenses/>. | |
17 | */ | |
18 | ||
7
68443f5be176
added the settings editor
Teemu Piippo <teemu@hecknology.net>
parents:
6
diff
changeset
|
19 | #include <QLabel> |
68443f5be176
added the settings editor
Teemu Piippo <teemu@hecknology.net>
parents:
6
diff
changeset
|
20 | #include <QVBoxLayout> |
68443f5be176
added the settings editor
Teemu Piippo <teemu@hecknology.net>
parents:
6
diff
changeset
|
21 | #include <QCloseEvent> |
68443f5be176
added the settings editor
Teemu Piippo <teemu@hecknology.net>
parents:
6
diff
changeset
|
22 | #include <QFileDialog> |
68443f5be176
added the settings editor
Teemu Piippo <teemu@hecknology.net>
parents:
6
diff
changeset
|
23 | #include <QMessageBox> |
0 | 24 | #include "mainwindow.h" |
25 | #include "ui_mainwindow.h" | |
7
68443f5be176
added the settings editor
Teemu Piippo <teemu@hecknology.net>
parents:
6
diff
changeset
|
26 | #include "settingseditor/settingseditor.h" |
1 | 27 | #include "version.h" |
8
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
28 | #include "document.h" |
16 | 29 | #include "uiutilities.h" |
0 | 30 | |
36
bbb901b97404
added render style storage
Teemu Piippo <teemu@hecknology.net>
parents:
26
diff
changeset
|
31 | template<typename BaseType, typename MemberType, typename DataType> |
bbb901b97404
added render style storage
Teemu Piippo <teemu@hecknology.net>
parents:
26
diff
changeset
|
32 | struct MemberData |
bbb901b97404
added render style storage
Teemu Piippo <teemu@hecknology.net>
parents:
26
diff
changeset
|
33 | { |
bbb901b97404
added render style storage
Teemu Piippo <teemu@hecknology.net>
parents:
26
diff
changeset
|
34 | std::size_t member; |
bbb901b97404
added render style storage
Teemu Piippo <teemu@hecknology.net>
parents:
26
diff
changeset
|
35 | DataType payload; |
bbb901b97404
added render style storage
Teemu Piippo <teemu@hecknology.net>
parents:
26
diff
changeset
|
36 | constexpr MemberType memberInstance(BaseType* instance) const |
bbb901b97404
added render style storage
Teemu Piippo <teemu@hecknology.net>
parents:
26
diff
changeset
|
37 | { |
bbb901b97404
added render style storage
Teemu Piippo <teemu@hecknology.net>
parents:
26
diff
changeset
|
38 | return *reinterpret_cast<MemberType*>(reinterpret_cast<char*>(instance) + this->member); |
bbb901b97404
added render style storage
Teemu Piippo <teemu@hecknology.net>
parents:
26
diff
changeset
|
39 | } |
bbb901b97404
added render style storage
Teemu Piippo <teemu@hecknology.net>
parents:
26
diff
changeset
|
40 | }; |
bbb901b97404
added render style storage
Teemu Piippo <teemu@hecknology.net>
parents:
26
diff
changeset
|
41 | |
bbb901b97404
added render style storage
Teemu Piippo <teemu@hecknology.net>
parents:
26
diff
changeset
|
42 | static constexpr MemberData<Ui_MainWindow, QAction*, gl::RenderStyle> renderStyleButtons[] = { |
bbb901b97404
added render style storage
Teemu Piippo <teemu@hecknology.net>
parents:
26
diff
changeset
|
43 | { offsetof(Ui_MainWindow, actionRenderStyleNormal), gl::RenderStyle::Normal }, |
bbb901b97404
added render style storage
Teemu Piippo <teemu@hecknology.net>
parents:
26
diff
changeset
|
44 | { offsetof(Ui_MainWindow, actionRenderStyleBfc), gl::RenderStyle::BfcRedGreen }, |
bbb901b97404
added render style storage
Teemu Piippo <teemu@hecknology.net>
parents:
26
diff
changeset
|
45 | { offsetof(Ui_MainWindow, actionRenderStyleRandom), gl::RenderStyle::RandomColors }, |
bbb901b97404
added render style storage
Teemu Piippo <teemu@hecknology.net>
parents:
26
diff
changeset
|
46 | }; |
bbb901b97404
added render style storage
Teemu Piippo <teemu@hecknology.net>
parents:
26
diff
changeset
|
47 | |
0 | 48 | MainWindow::MainWindow(QWidget *parent) : |
3 | 49 | QMainWindow{parent}, |
5 | 50 | ui{std::make_unique<Ui_MainWindow>()}, |
7
68443f5be176
added the settings editor
Teemu Piippo <teemu@hecknology.net>
parents:
6
diff
changeset
|
51 | documents{this}, |
68443f5be176
added the settings editor
Teemu Piippo <teemu@hecknology.net>
parents:
6
diff
changeset
|
52 | settings{}, |
68443f5be176
added the settings editor
Teemu Piippo <teemu@hecknology.net>
parents:
6
diff
changeset
|
53 | libraries{this} |
0 | 54 | { |
6 | 55 | this->ui->setupUi(this); |
16 | 56 | defaultKeyboardShortcuts = uiutilities::makeKeySequenceMap(uiutilities::collectActions(this)); |
5 | 57 | connect(ui->actionNew, &QAction::triggered, this, &MainWindow::newModel); |
6 | 58 | connect(ui->actionOpen, &QAction::triggered, this, &MainWindow::openModel); |
3 | 59 | connect(ui->actionQuit, &QAction::triggered, this, &QMainWindow::close); |
7
68443f5be176
added the settings editor
Teemu Piippo <teemu@hecknology.net>
parents:
6
diff
changeset
|
60 | connect(ui->actionSettingsEditor, &QAction::triggered, this, &MainWindow::runSettingsEditor); |
36
bbb901b97404
added render style storage
Teemu Piippo <teemu@hecknology.net>
parents:
26
diff
changeset
|
61 | for (auto data : ::renderStyleButtons) |
bbb901b97404
added render style storage
Teemu Piippo <teemu@hecknology.net>
parents:
26
diff
changeset
|
62 | { |
bbb901b97404
added render style storage
Teemu Piippo <teemu@hecknology.net>
parents:
26
diff
changeset
|
63 | QAction* action = data.memberInstance(this->ui.get()); |
bbb901b97404
added render style storage
Teemu Piippo <teemu@hecknology.net>
parents:
26
diff
changeset
|
64 | connect(action, &QAction::triggered, [this, data]() |
bbb901b97404
added render style storage
Teemu Piippo <teemu@hecknology.net>
parents:
26
diff
changeset
|
65 | { |
bbb901b97404
added render style storage
Teemu Piippo <teemu@hecknology.net>
parents:
26
diff
changeset
|
66 | this->setRenderStyle(data.payload); |
bbb901b97404
added render style storage
Teemu Piippo <teemu@hecknology.net>
parents:
26
diff
changeset
|
67 | }); |
bbb901b97404
added render style storage
Teemu Piippo <teemu@hecknology.net>
parents:
26
diff
changeset
|
68 | } |
6 | 69 | this->updateTitle(); |
7
68443f5be176
added the settings editor
Teemu Piippo <teemu@hecknology.net>
parents:
6
diff
changeset
|
70 | this->restoreSettings(); |
36
bbb901b97404
added render style storage
Teemu Piippo <teemu@hecknology.net>
parents:
26
diff
changeset
|
71 | this->updateRenderStyles(); |
9
8b9780700b5e
added saving of splitter state and recent files
Teemu Piippo <teemu@hecknology.net>
parents:
8
diff
changeset
|
72 | this->newModel(); |
5 | 73 | } |
74 | ||
75 | MainWindow::~MainWindow() | |
76 | { | |
77 | } | |
78 | ||
79 | void MainWindow::newModel() | |
80 | { | |
9
8b9780700b5e
added saving of splitter state and recent files
Teemu Piippo <teemu@hecknology.net>
parents:
8
diff
changeset
|
81 | this->openModelForEditing(documents.newModel()); |
5 | 82 | } |
83 | ||
84 | void MainWindow::openModel() | |
85 | { | |
86 | const QString path = QFileDialog::getOpenFileName( | |
7
68443f5be176
added the settings editor
Teemu Piippo <teemu@hecknology.net>
parents:
6
diff
changeset
|
87 | this, |
68443f5be176
added the settings editor
Teemu Piippo <teemu@hecknology.net>
parents:
6
diff
changeset
|
88 | tr("Open model"), |
68443f5be176
added the settings editor
Teemu Piippo <teemu@hecknology.net>
parents:
6
diff
changeset
|
89 | "", |
68443f5be176
added the settings editor
Teemu Piippo <teemu@hecknology.net>
parents:
6
diff
changeset
|
90 | tr("LDraw models (*.ldr *.dat)")); |
5 | 91 | if (not path.isEmpty()) |
92 | { | |
9
8b9780700b5e
added saving of splitter state and recent files
Teemu Piippo <teemu@hecknology.net>
parents:
8
diff
changeset
|
93 | this->openModelFromPath(path); |
8b9780700b5e
added saving of splitter state and recent files
Teemu Piippo <teemu@hecknology.net>
parents:
8
diff
changeset
|
94 | } |
8b9780700b5e
added saving of splitter state and recent files
Teemu Piippo <teemu@hecknology.net>
parents:
8
diff
changeset
|
95 | } |
8b9780700b5e
added saving of splitter state and recent files
Teemu Piippo <teemu@hecknology.net>
parents:
8
diff
changeset
|
96 | |
8b9780700b5e
added saving of splitter state and recent files
Teemu Piippo <teemu@hecknology.net>
parents:
8
diff
changeset
|
97 | void MainWindow::openModelFromPath(const QString& path) |
8b9780700b5e
added saving of splitter state and recent files
Teemu Piippo <teemu@hecknology.net>
parents:
8
diff
changeset
|
98 | { |
8b9780700b5e
added saving of splitter state and recent files
Teemu Piippo <teemu@hecknology.net>
parents:
8
diff
changeset
|
99 | QString errorString; |
8b9780700b5e
added saving of splitter state and recent files
Teemu Piippo <teemu@hecknology.net>
parents:
8
diff
changeset
|
100 | QTextStream errorStream{&errorString}; |
8b9780700b5e
added saving of splitter state and recent files
Teemu Piippo <teemu@hecknology.net>
parents:
8
diff
changeset
|
101 | QString modelName = this->documents.openModel(path, errorStream); |
8b9780700b5e
added saving of splitter state and recent files
Teemu Piippo <teemu@hecknology.net>
parents:
8
diff
changeset
|
102 | if (not modelName.isEmpty()) |
8b9780700b5e
added saving of splitter state and recent files
Teemu Piippo <teemu@hecknology.net>
parents:
8
diff
changeset
|
103 | { |
23
3387a84ddaba
fixed a pile of nonsense that caused subfiles to go haywire
Teemu Piippo <teemu@hecknology.net>
parents:
21
diff
changeset
|
104 | this->documents.loadDependenciesForModel(modelName, path, this->libraries, errorStream); |
12 | 105 | if (not errorString.isEmpty()) |
106 | { | |
107 | QMessageBox::warning( | |
108 | this, | |
109 | tr("Problem loading references"), | |
110 | errorString); | |
111 | } | |
9
8b9780700b5e
added saving of splitter state and recent files
Teemu Piippo <teemu@hecknology.net>
parents:
8
diff
changeset
|
112 | this->openModelForEditing(modelName); |
8b9780700b5e
added saving of splitter state and recent files
Teemu Piippo <teemu@hecknology.net>
parents:
8
diff
changeset
|
113 | this->addRecentlyOpenedFile(path); |
8b9780700b5e
added saving of splitter state and recent files
Teemu Piippo <teemu@hecknology.net>
parents:
8
diff
changeset
|
114 | } |
8b9780700b5e
added saving of splitter state and recent files
Teemu Piippo <teemu@hecknology.net>
parents:
8
diff
changeset
|
115 | else |
8b9780700b5e
added saving of splitter state and recent files
Teemu Piippo <teemu@hecknology.net>
parents:
8
diff
changeset
|
116 | { |
8b9780700b5e
added saving of splitter state and recent files
Teemu Piippo <teemu@hecknology.net>
parents:
8
diff
changeset
|
117 | QMessageBox::critical( |
8b9780700b5e
added saving of splitter state and recent files
Teemu Piippo <teemu@hecknology.net>
parents:
8
diff
changeset
|
118 | this, |
8b9780700b5e
added saving of splitter state and recent files
Teemu Piippo <teemu@hecknology.net>
parents:
8
diff
changeset
|
119 | tr("Problem opening file"), |
8b9780700b5e
added saving of splitter state and recent files
Teemu Piippo <teemu@hecknology.net>
parents:
8
diff
changeset
|
120 | utility::format( |
7
68443f5be176
added the settings editor
Teemu Piippo <teemu@hecknology.net>
parents:
6
diff
changeset
|
121 | tr("Could not open %1: %2"), |
68443f5be176
added the settings editor
Teemu Piippo <teemu@hecknology.net>
parents:
6
diff
changeset
|
122 | path, |
9
8b9780700b5e
added saving of splitter state and recent files
Teemu Piippo <teemu@hecknology.net>
parents:
8
diff
changeset
|
123 | errorString)); |
5 | 124 | } |
125 | } | |
126 | ||
127 | /** | |
6 | 128 | * @brief Changes the application language to the specified language |
129 | * @param localeCode Code of the locale to translate to | |
130 | */ | |
7
68443f5be176
added the settings editor
Teemu Piippo <teemu@hecknology.net>
parents:
6
diff
changeset
|
131 | void MainWindow::changeLanguage(QString localeCode) |
6 | 132 | { |
133 | if (not localeCode.isEmpty() and localeCode != this->currentLanguage) | |
134 | { | |
135 | this->currentLanguage = localeCode; | |
7
68443f5be176
added the settings editor
Teemu Piippo <teemu@hecknology.net>
parents:
6
diff
changeset
|
136 | if (localeCode == "system") |
68443f5be176
added the settings editor
Teemu Piippo <teemu@hecknology.net>
parents:
6
diff
changeset
|
137 | { |
68443f5be176
added the settings editor
Teemu Piippo <teemu@hecknology.net>
parents:
6
diff
changeset
|
138 | localeCode = QLocale::system().name(); |
68443f5be176
added the settings editor
Teemu Piippo <teemu@hecknology.net>
parents:
6
diff
changeset
|
139 | } |
6 | 140 | QLocale::setDefault({localeCode}); |
141 | qApp->removeTranslator(&this->translator); | |
142 | const bool loadSuccessful = this->translator.load(pathToTranslation(localeCode)); | |
143 | if (loadSuccessful) | |
144 | { | |
145 | qApp->installTranslator(&this->translator); | |
146 | } | |
147 | } | |
148 | } | |
149 | ||
9
8b9780700b5e
added saving of splitter state and recent files
Teemu Piippo <teemu@hecknology.net>
parents:
8
diff
changeset
|
150 | void MainWindow::addRecentlyOpenedFile(const QString& path) |
8b9780700b5e
added saving of splitter state and recent files
Teemu Piippo <teemu@hecknology.net>
parents:
8
diff
changeset
|
151 | { |
8b9780700b5e
added saving of splitter state and recent files
Teemu Piippo <teemu@hecknology.net>
parents:
8
diff
changeset
|
152 | this->recentlyOpenedFiles.removeAll(path); |
8b9780700b5e
added saving of splitter state and recent files
Teemu Piippo <teemu@hecknology.net>
parents:
8
diff
changeset
|
153 | this->recentlyOpenedFiles.insert(0, path); |
8b9780700b5e
added saving of splitter state and recent files
Teemu Piippo <teemu@hecknology.net>
parents:
8
diff
changeset
|
154 | while (this->recentlyOpenedFiles.size() > maxRecentlyOpenedFiles) |
8b9780700b5e
added saving of splitter state and recent files
Teemu Piippo <teemu@hecknology.net>
parents:
8
diff
changeset
|
155 | { |
8b9780700b5e
added saving of splitter state and recent files
Teemu Piippo <teemu@hecknology.net>
parents:
8
diff
changeset
|
156 | this->recentlyOpenedFiles.removeLast(); |
8b9780700b5e
added saving of splitter state and recent files
Teemu Piippo <teemu@hecknology.net>
parents:
8
diff
changeset
|
157 | } |
8b9780700b5e
added saving of splitter state and recent files
Teemu Piippo <teemu@hecknology.net>
parents:
8
diff
changeset
|
158 | this->saveSettings(); |
8b9780700b5e
added saving of splitter state and recent files
Teemu Piippo <teemu@hecknology.net>
parents:
8
diff
changeset
|
159 | this->updateRecentlyOpenedDocumentsMenu(); |
8b9780700b5e
added saving of splitter state and recent files
Teemu Piippo <teemu@hecknology.net>
parents:
8
diff
changeset
|
160 | } |
8b9780700b5e
added saving of splitter state and recent files
Teemu Piippo <teemu@hecknology.net>
parents:
8
diff
changeset
|
161 | |
8b9780700b5e
added saving of splitter state and recent files
Teemu Piippo <teemu@hecknology.net>
parents:
8
diff
changeset
|
162 | void MainWindow::openModelForEditing(const QString& modelName) |
8b9780700b5e
added saving of splitter state and recent files
Teemu Piippo <teemu@hecknology.net>
parents:
8
diff
changeset
|
163 | { |
26 | 164 | Document* document = new Document{this->documents.findModelByName(modelName), &this->documents, this->colorTable}; |
36
bbb901b97404
added render style storage
Teemu Piippo <teemu@hecknology.net>
parents:
26
diff
changeset
|
165 | document->setRenderStyle(this->renderStyle); |
9
8b9780700b5e
added saving of splitter state and recent files
Teemu Piippo <teemu@hecknology.net>
parents:
8
diff
changeset
|
166 | this->ui->tabs->addTab(document, modelName); |
8b9780700b5e
added saving of splitter state and recent files
Teemu Piippo <teemu@hecknology.net>
parents:
8
diff
changeset
|
167 | this->ui->tabs->setCurrentWidget(document); |
8b9780700b5e
added saving of splitter state and recent files
Teemu Piippo <teemu@hecknology.net>
parents:
8
diff
changeset
|
168 | document->restoreSplitterState(this->documentSplitterState); |
8b9780700b5e
added saving of splitter state and recent files
Teemu Piippo <teemu@hecknology.net>
parents:
8
diff
changeset
|
169 | connect(document, &Document::splitterChanged, this, &MainWindow::handleDocumentSplitterChange); |
8b9780700b5e
added saving of splitter state and recent files
Teemu Piippo <teemu@hecknology.net>
parents:
8
diff
changeset
|
170 | } |
8b9780700b5e
added saving of splitter state and recent files
Teemu Piippo <teemu@hecknology.net>
parents:
8
diff
changeset
|
171 | |
7
68443f5be176
added the settings editor
Teemu Piippo <teemu@hecknology.net>
parents:
6
diff
changeset
|
172 | void MainWindow::runSettingsEditor() |
6 | 173 | { |
16 | 174 | SettingsEditor settingsEditor{&this->settings, this->defaultKeyboardShortcuts, this}; |
7
68443f5be176
added the settings editor
Teemu Piippo <teemu@hecknology.net>
parents:
6
diff
changeset
|
175 | const int result = settingsEditor.exec(); |
68443f5be176
added the settings editor
Teemu Piippo <teemu@hecknology.net>
parents:
6
diff
changeset
|
176 | if (result == QDialog::Accepted) |
6 | 177 | { |
7
68443f5be176
added the settings editor
Teemu Piippo <teemu@hecknology.net>
parents:
6
diff
changeset
|
178 | this->restoreSettings(); |
6 | 179 | } |
180 | } | |
181 | ||
9
8b9780700b5e
added saving of splitter state and recent files
Teemu Piippo <teemu@hecknology.net>
parents:
8
diff
changeset
|
182 | void MainWindow::handleDocumentSplitterChange() |
8b9780700b5e
added saving of splitter state and recent files
Teemu Piippo <teemu@hecknology.net>
parents:
8
diff
changeset
|
183 | { |
8b9780700b5e
added saving of splitter state and recent files
Teemu Piippo <teemu@hecknology.net>
parents:
8
diff
changeset
|
184 | Document* currentDocument = qobject_cast<Document*>(this->ui->tabs->currentWidget()); |
8b9780700b5e
added saving of splitter state and recent files
Teemu Piippo <teemu@hecknology.net>
parents:
8
diff
changeset
|
185 | if (currentDocument != nullptr) |
8b9780700b5e
added saving of splitter state and recent files
Teemu Piippo <teemu@hecknology.net>
parents:
8
diff
changeset
|
186 | { |
8b9780700b5e
added saving of splitter state and recent files
Teemu Piippo <teemu@hecknology.net>
parents:
8
diff
changeset
|
187 | this->documentSplitterState = currentDocument->saveSplitterState(); |
8b9780700b5e
added saving of splitter state and recent files
Teemu Piippo <teemu@hecknology.net>
parents:
8
diff
changeset
|
188 | for (int i = 0; i < this->ui->tabs->count(); i += 1) |
8b9780700b5e
added saving of splitter state and recent files
Teemu Piippo <teemu@hecknology.net>
parents:
8
diff
changeset
|
189 | { |
8b9780700b5e
added saving of splitter state and recent files
Teemu Piippo <teemu@hecknology.net>
parents:
8
diff
changeset
|
190 | Document* document = qobject_cast<Document*>(this->ui->tabs->widget(i)); |
8b9780700b5e
added saving of splitter state and recent files
Teemu Piippo <teemu@hecknology.net>
parents:
8
diff
changeset
|
191 | if (document != nullptr and document != currentDocument) |
8b9780700b5e
added saving of splitter state and recent files
Teemu Piippo <teemu@hecknology.net>
parents:
8
diff
changeset
|
192 | { |
8b9780700b5e
added saving of splitter state and recent files
Teemu Piippo <teemu@hecknology.net>
parents:
8
diff
changeset
|
193 | document->restoreSplitterState(this->documentSplitterState); |
8b9780700b5e
added saving of splitter state and recent files
Teemu Piippo <teemu@hecknology.net>
parents:
8
diff
changeset
|
194 | } |
8b9780700b5e
added saving of splitter state and recent files
Teemu Piippo <teemu@hecknology.net>
parents:
8
diff
changeset
|
195 | } |
8b9780700b5e
added saving of splitter state and recent files
Teemu Piippo <teemu@hecknology.net>
parents:
8
diff
changeset
|
196 | this->settings.setValue("MainWindow/DocumentSplitterState", this->documentSplitterState); |
8b9780700b5e
added saving of splitter state and recent files
Teemu Piippo <teemu@hecknology.net>
parents:
8
diff
changeset
|
197 | } |
8b9780700b5e
added saving of splitter state and recent files
Teemu Piippo <teemu@hecknology.net>
parents:
8
diff
changeset
|
198 | } |
8b9780700b5e
added saving of splitter state and recent files
Teemu Piippo <teemu@hecknology.net>
parents:
8
diff
changeset
|
199 | |
8b9780700b5e
added saving of splitter state and recent files
Teemu Piippo <teemu@hecknology.net>
parents:
8
diff
changeset
|
200 | void MainWindow::updateRecentlyOpenedDocumentsMenu() |
8b9780700b5e
added saving of splitter state and recent files
Teemu Piippo <teemu@hecknology.net>
parents:
8
diff
changeset
|
201 | { |
8b9780700b5e
added saving of splitter state and recent files
Teemu Piippo <teemu@hecknology.net>
parents:
8
diff
changeset
|
202 | this->ui->menuRecentFiles->clear(); |
8b9780700b5e
added saving of splitter state and recent files
Teemu Piippo <teemu@hecknology.net>
parents:
8
diff
changeset
|
203 | for (const QString& path : this->recentlyOpenedFiles) |
8b9780700b5e
added saving of splitter state and recent files
Teemu Piippo <teemu@hecknology.net>
parents:
8
diff
changeset
|
204 | { |
8b9780700b5e
added saving of splitter state and recent files
Teemu Piippo <teemu@hecknology.net>
parents:
8
diff
changeset
|
205 | QAction* action = new QAction{path, this}; |
8b9780700b5e
added saving of splitter state and recent files
Teemu Piippo <teemu@hecknology.net>
parents:
8
diff
changeset
|
206 | action->setData(path); |
8b9780700b5e
added saving of splitter state and recent files
Teemu Piippo <teemu@hecknology.net>
parents:
8
diff
changeset
|
207 | this->ui->menuRecentFiles->addAction(action); |
8b9780700b5e
added saving of splitter state and recent files
Teemu Piippo <teemu@hecknology.net>
parents:
8
diff
changeset
|
208 | connect(action, &QAction::triggered, this, &MainWindow::openRecentFile); |
8b9780700b5e
added saving of splitter state and recent files
Teemu Piippo <teemu@hecknology.net>
parents:
8
diff
changeset
|
209 | } |
8b9780700b5e
added saving of splitter state and recent files
Teemu Piippo <teemu@hecknology.net>
parents:
8
diff
changeset
|
210 | } |
8b9780700b5e
added saving of splitter state and recent files
Teemu Piippo <teemu@hecknology.net>
parents:
8
diff
changeset
|
211 | |
8b9780700b5e
added saving of splitter state and recent files
Teemu Piippo <teemu@hecknology.net>
parents:
8
diff
changeset
|
212 | void MainWindow::openRecentFile() |
8b9780700b5e
added saving of splitter state and recent files
Teemu Piippo <teemu@hecknology.net>
parents:
8
diff
changeset
|
213 | { |
8b9780700b5e
added saving of splitter state and recent files
Teemu Piippo <teemu@hecknology.net>
parents:
8
diff
changeset
|
214 | QAction* action = qobject_cast<QAction*>(this->sender()); |
8b9780700b5e
added saving of splitter state and recent files
Teemu Piippo <teemu@hecknology.net>
parents:
8
diff
changeset
|
215 | if (action != nullptr) |
8b9780700b5e
added saving of splitter state and recent files
Teemu Piippo <teemu@hecknology.net>
parents:
8
diff
changeset
|
216 | { |
8b9780700b5e
added saving of splitter state and recent files
Teemu Piippo <teemu@hecknology.net>
parents:
8
diff
changeset
|
217 | const QString path = action->data().toString(); |
8b9780700b5e
added saving of splitter state and recent files
Teemu Piippo <teemu@hecknology.net>
parents:
8
diff
changeset
|
218 | this->openModelFromPath(path); |
8b9780700b5e
added saving of splitter state and recent files
Teemu Piippo <teemu@hecknology.net>
parents:
8
diff
changeset
|
219 | } |
8b9780700b5e
added saving of splitter state and recent files
Teemu Piippo <teemu@hecknology.net>
parents:
8
diff
changeset
|
220 | } |
8b9780700b5e
added saving of splitter state and recent files
Teemu Piippo <teemu@hecknology.net>
parents:
8
diff
changeset
|
221 | |
36
bbb901b97404
added render style storage
Teemu Piippo <teemu@hecknology.net>
parents:
26
diff
changeset
|
222 | void MainWindow::setRenderStyle(gl::RenderStyle renderStyle) |
bbb901b97404
added render style storage
Teemu Piippo <teemu@hecknology.net>
parents:
26
diff
changeset
|
223 | { |
bbb901b97404
added render style storage
Teemu Piippo <teemu@hecknology.net>
parents:
26
diff
changeset
|
224 | this->renderStyle = renderStyle; |
bbb901b97404
added render style storage
Teemu Piippo <teemu@hecknology.net>
parents:
26
diff
changeset
|
225 | this->saveSettings(); |
bbb901b97404
added render style storage
Teemu Piippo <teemu@hecknology.net>
parents:
26
diff
changeset
|
226 | this->updateRenderStyles(); |
bbb901b97404
added render style storage
Teemu Piippo <teemu@hecknology.net>
parents:
26
diff
changeset
|
227 | } |
bbb901b97404
added render style storage
Teemu Piippo <teemu@hecknology.net>
parents:
26
diff
changeset
|
228 | |
6 | 229 | void MainWindow::changeEvent(QEvent* event) |
230 | { | |
231 | if (event != nullptr) | |
232 | { | |
233 | switch (event->type()) | |
234 | { | |
235 | case QEvent::LanguageChange: | |
236 | this->ui->retranslateUi(this); | |
237 | break; | |
238 | default: | |
239 | break; | |
240 | } | |
241 | } | |
242 | QMainWindow::changeEvent(event); | |
243 | } | |
244 | ||
245 | /** | |
7
68443f5be176
added the settings editor
Teemu Piippo <teemu@hecknology.net>
parents:
6
diff
changeset
|
246 | * @brief Handles closing the main window |
68443f5be176
added the settings editor
Teemu Piippo <teemu@hecknology.net>
parents:
6
diff
changeset
|
247 | * @param event Event information |
68443f5be176
added the settings editor
Teemu Piippo <teemu@hecknology.net>
parents:
6
diff
changeset
|
248 | */ |
68443f5be176
added the settings editor
Teemu Piippo <teemu@hecknology.net>
parents:
6
diff
changeset
|
249 | void MainWindow::closeEvent(QCloseEvent* event) |
68443f5be176
added the settings editor
Teemu Piippo <teemu@hecknology.net>
parents:
6
diff
changeset
|
250 | { |
68443f5be176
added the settings editor
Teemu Piippo <teemu@hecknology.net>
parents:
6
diff
changeset
|
251 | saveSettings(); |
68443f5be176
added the settings editor
Teemu Piippo <teemu@hecknology.net>
parents:
6
diff
changeset
|
252 | event->accept(); |
68443f5be176
added the settings editor
Teemu Piippo <teemu@hecknology.net>
parents:
6
diff
changeset
|
253 | } |
68443f5be176
added the settings editor
Teemu Piippo <teemu@hecknology.net>
parents:
6
diff
changeset
|
254 | |
68443f5be176
added the settings editor
Teemu Piippo <teemu@hecknology.net>
parents:
6
diff
changeset
|
255 | /** |
5 | 256 | * @brief Updates the title of the main window so to contain the app's name |
257 | * and version as well as the open document name. | |
258 | */ | |
259 | void MainWindow::updateTitle() | |
260 | { | |
1 | 261 | QString title = ::appName; |
262 | title += " "; | |
263 | title += fullVersionString(); | |
264 | setWindowTitle(title); | |
0 | 265 | } |
6 | 266 | |
36
bbb901b97404
added render style storage
Teemu Piippo <teemu@hecknology.net>
parents:
26
diff
changeset
|
267 | void MainWindow::updateRenderStyles() |
bbb901b97404
added render style storage
Teemu Piippo <teemu@hecknology.net>
parents:
26
diff
changeset
|
268 | { |
bbb901b97404
added render style storage
Teemu Piippo <teemu@hecknology.net>
parents:
26
diff
changeset
|
269 | for (int i = 0; i < this->ui->tabs->count(); i += 1) |
bbb901b97404
added render style storage
Teemu Piippo <teemu@hecknology.net>
parents:
26
diff
changeset
|
270 | { |
bbb901b97404
added render style storage
Teemu Piippo <teemu@hecknology.net>
parents:
26
diff
changeset
|
271 | Document* document = qobject_cast<Document*>(this->ui->tabs->widget(i)); |
bbb901b97404
added render style storage
Teemu Piippo <teemu@hecknology.net>
parents:
26
diff
changeset
|
272 | if (document != nullptr) |
bbb901b97404
added render style storage
Teemu Piippo <teemu@hecknology.net>
parents:
26
diff
changeset
|
273 | { |
bbb901b97404
added render style storage
Teemu Piippo <teemu@hecknology.net>
parents:
26
diff
changeset
|
274 | document->setRenderStyle(renderStyle); |
bbb901b97404
added render style storage
Teemu Piippo <teemu@hecknology.net>
parents:
26
diff
changeset
|
275 | } |
bbb901b97404
added render style storage
Teemu Piippo <teemu@hecknology.net>
parents:
26
diff
changeset
|
276 | } |
bbb901b97404
added render style storage
Teemu Piippo <teemu@hecknology.net>
parents:
26
diff
changeset
|
277 | for (auto data : ::renderStyleButtons) |
bbb901b97404
added render style storage
Teemu Piippo <teemu@hecknology.net>
parents:
26
diff
changeset
|
278 | { |
bbb901b97404
added render style storage
Teemu Piippo <teemu@hecknology.net>
parents:
26
diff
changeset
|
279 | QAction* action = data.memberInstance(this->ui.get()); |
bbb901b97404
added render style storage
Teemu Piippo <teemu@hecknology.net>
parents:
26
diff
changeset
|
280 | action->setChecked(this->renderStyle == data.payload); |
bbb901b97404
added render style storage
Teemu Piippo <teemu@hecknology.net>
parents:
26
diff
changeset
|
281 | } |
bbb901b97404
added render style storage
Teemu Piippo <teemu@hecknology.net>
parents:
26
diff
changeset
|
282 | } |
bbb901b97404
added render style storage
Teemu Piippo <teemu@hecknology.net>
parents:
26
diff
changeset
|
283 | |
7
68443f5be176
added the settings editor
Teemu Piippo <teemu@hecknology.net>
parents:
6
diff
changeset
|
284 | /** |
68443f5be176
added the settings editor
Teemu Piippo <teemu@hecknology.net>
parents:
6
diff
changeset
|
285 | * @brief Stores the settings of the main window, storing geometry, etc |
68443f5be176
added the settings editor
Teemu Piippo <teemu@hecknology.net>
parents:
6
diff
changeset
|
286 | */ |
68443f5be176
added the settings editor
Teemu Piippo <teemu@hecknology.net>
parents:
6
diff
changeset
|
287 | void MainWindow::saveSettings() |
68443f5be176
added the settings editor
Teemu Piippo <teemu@hecknology.net>
parents:
6
diff
changeset
|
288 | { |
9
8b9780700b5e
added saving of splitter state and recent files
Teemu Piippo <teemu@hecknology.net>
parents:
8
diff
changeset
|
289 | this->settings.setValue("MainWindow/Geometry", this->saveGeometry()); |
8b9780700b5e
added saving of splitter state and recent files
Teemu Piippo <teemu@hecknology.net>
parents:
8
diff
changeset
|
290 | this->settings.setValue("MainWindow/RecentlyOpened", this->recentlyOpenedFiles); |
8b9780700b5e
added saving of splitter state and recent files
Teemu Piippo <teemu@hecknology.net>
parents:
8
diff
changeset
|
291 | this->settings.setValue("MainWindow/DocumentSplitterState", this->documentSplitterState); |
36
bbb901b97404
added render style storage
Teemu Piippo <teemu@hecknology.net>
parents:
26
diff
changeset
|
292 | this->settings.setValue("MainWindow/RenderStyle", static_cast<int>(this->renderStyle)); |
7
68443f5be176
added the settings editor
Teemu Piippo <teemu@hecknology.net>
parents:
6
diff
changeset
|
293 | this->libraries.storeToSettings(&this->settings); |
68443f5be176
added the settings editor
Teemu Piippo <teemu@hecknology.net>
parents:
6
diff
changeset
|
294 | } |
68443f5be176
added the settings editor
Teemu Piippo <teemu@hecknology.net>
parents:
6
diff
changeset
|
295 | |
68443f5be176
added the settings editor
Teemu Piippo <teemu@hecknology.net>
parents:
6
diff
changeset
|
296 | /** |
68443f5be176
added the settings editor
Teemu Piippo <teemu@hecknology.net>
parents:
6
diff
changeset
|
297 | * @brief Restores saved settings relating to the main window |
68443f5be176
added the settings editor
Teemu Piippo <teemu@hecknology.net>
parents:
6
diff
changeset
|
298 | */ |
68443f5be176
added the settings editor
Teemu Piippo <teemu@hecknology.net>
parents:
6
diff
changeset
|
299 | void MainWindow::restoreSettings() |
68443f5be176
added the settings editor
Teemu Piippo <teemu@hecknology.net>
parents:
6
diff
changeset
|
300 | { |
9
8b9780700b5e
added saving of splitter state and recent files
Teemu Piippo <teemu@hecknology.net>
parents:
8
diff
changeset
|
301 | this->restoreGeometry(this->settings.value("MainWindow/Geometry").toByteArray()); |
8b9780700b5e
added saving of splitter state and recent files
Teemu Piippo <teemu@hecknology.net>
parents:
8
diff
changeset
|
302 | this->recentlyOpenedFiles = this->settings.value("MainWindow/RecentlyOpened").toStringList(); |
8b9780700b5e
added saving of splitter state and recent files
Teemu Piippo <teemu@hecknology.net>
parents:
8
diff
changeset
|
303 | this->documentSplitterState = this->settings.value("MainWindow/DocumentSplitterState").toByteArray(); |
36
bbb901b97404
added render style storage
Teemu Piippo <teemu@hecknology.net>
parents:
26
diff
changeset
|
304 | this->renderStyle = static_cast<gl::RenderStyle>(this->settings.value("MainWindow/RenderStyle").toInt()); |
7
68443f5be176
added the settings editor
Teemu Piippo <teemu@hecknology.net>
parents:
6
diff
changeset
|
305 | const QString systemLocale = QLocale::system().name(); |
68443f5be176
added the settings editor
Teemu Piippo <teemu@hecknology.net>
parents:
6
diff
changeset
|
306 | const QVariant defaultLocale = this->settings.value("locale", systemLocale); |
68443f5be176
added the settings editor
Teemu Piippo <teemu@hecknology.net>
parents:
6
diff
changeset
|
307 | changeLanguage(defaultLocale.toString()); |
68443f5be176
added the settings editor
Teemu Piippo <teemu@hecknology.net>
parents:
6
diff
changeset
|
308 | this->libraries.restoreFromSettings(&this->settings); |
9
8b9780700b5e
added saving of splitter state and recent files
Teemu Piippo <teemu@hecknology.net>
parents:
8
diff
changeset
|
309 | this->updateRecentlyOpenedDocumentsMenu(); |
26 | 310 | this->loadColors(); |
7
68443f5be176
added the settings editor
Teemu Piippo <teemu@hecknology.net>
parents:
6
diff
changeset
|
311 | } |
68443f5be176
added the settings editor
Teemu Piippo <teemu@hecknology.net>
parents:
6
diff
changeset
|
312 | |
6 | 313 | QString MainWindow::pathToTranslation(const QString& localeCode) |
314 | { | |
315 | QDir dir {":/locale"}; | |
316 | return dir.filePath(localeCode + ".qm"); | |
317 | } | |
26 | 318 | |
319 | void MainWindow::loadColors() | |
320 | { | |
321 | QTextStream errors; | |
322 | this->colorTable = this->libraries.loadColorTable(errors); | |
323 | } |