Sat, 05 Mar 2022 12:47:10 +0200
fix crash when loading a file
`object` is moved so we cannot access it after moving
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" |
94
164f53fb5921
added a color select dialog
Teemu Piippo <teemu@hecknology.net>
parents:
79
diff
changeset
|
30 | #include "widgets/colorselectdialog.h" |
0 | 31 | |
36
bbb901b97404
added render style storage
Teemu Piippo <teemu@hecknology.net>
parents:
26
diff
changeset
|
32 | template<typename BaseType, typename MemberType, typename DataType> |
bbb901b97404
added render style storage
Teemu Piippo <teemu@hecknology.net>
parents:
26
diff
changeset
|
33 | struct MemberData |
bbb901b97404
added render style storage
Teemu Piippo <teemu@hecknology.net>
parents:
26
diff
changeset
|
34 | { |
bbb901b97404
added render style storage
Teemu Piippo <teemu@hecknology.net>
parents:
26
diff
changeset
|
35 | std::size_t member; |
bbb901b97404
added render style storage
Teemu Piippo <teemu@hecknology.net>
parents:
26
diff
changeset
|
36 | DataType payload; |
bbb901b97404
added render style storage
Teemu Piippo <teemu@hecknology.net>
parents:
26
diff
changeset
|
37 | constexpr MemberType memberInstance(BaseType* instance) const |
bbb901b97404
added render style storage
Teemu Piippo <teemu@hecknology.net>
parents:
26
diff
changeset
|
38 | { |
bbb901b97404
added render style storage
Teemu Piippo <teemu@hecknology.net>
parents:
26
diff
changeset
|
39 | return *reinterpret_cast<MemberType*>(reinterpret_cast<char*>(instance) + this->member); |
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 | |
bbb901b97404
added render style storage
Teemu Piippo <teemu@hecknology.net>
parents:
26
diff
changeset
|
43 | static constexpr MemberData<Ui_MainWindow, QAction*, gl::RenderStyle> renderStyleButtons[] = { |
bbb901b97404
added render style storage
Teemu Piippo <teemu@hecknology.net>
parents:
26
diff
changeset
|
44 | { offsetof(Ui_MainWindow, actionRenderStyleNormal), gl::RenderStyle::Normal }, |
bbb901b97404
added render style storage
Teemu Piippo <teemu@hecknology.net>
parents:
26
diff
changeset
|
45 | { offsetof(Ui_MainWindow, actionRenderStyleBfc), gl::RenderStyle::BfcRedGreen }, |
bbb901b97404
added render style storage
Teemu Piippo <teemu@hecknology.net>
parents:
26
diff
changeset
|
46 | { offsetof(Ui_MainWindow, actionRenderStyleRandom), gl::RenderStyle::RandomColors }, |
79
5fe2dd4e161a
added a render style for pick scene
Teemu Piippo <teemu@hecknology.net>
parents:
48
diff
changeset
|
47 | { offsetof(Ui_MainWindow, actionRenderStylePickScene), gl::RenderStyle::PickScene }, |
36
bbb901b97404
added render style storage
Teemu Piippo <teemu@hecknology.net>
parents:
26
diff
changeset
|
48 | }; |
bbb901b97404
added render style storage
Teemu Piippo <teemu@hecknology.net>
parents:
26
diff
changeset
|
49 | |
41
0abada2a9802
added automated configuration collection
Teemu Piippo <teemu@hecknology.net>
parents:
40
diff
changeset
|
50 | class A : public QSettings |
0abada2a9802
added automated configuration collection
Teemu Piippo <teemu@hecknology.net>
parents:
40
diff
changeset
|
51 | { |
0abada2a9802
added automated configuration collection
Teemu Piippo <teemu@hecknology.net>
parents:
40
diff
changeset
|
52 | using QSettings::QSettings; |
0abada2a9802
added automated configuration collection
Teemu Piippo <teemu@hecknology.net>
parents:
40
diff
changeset
|
53 | }; |
0abada2a9802
added automated configuration collection
Teemu Piippo <teemu@hecknology.net>
parents:
40
diff
changeset
|
54 | |
0 | 55 | MainWindow::MainWindow(QWidget *parent) : |
3 | 56 | QMainWindow{parent}, |
5 | 57 | ui{std::make_unique<Ui_MainWindow>()}, |
7
68443f5be176
added the settings editor
Teemu Piippo <teemu@hecknology.net>
parents:
6
diff
changeset
|
58 | documents{this}, |
68443f5be176
added the settings editor
Teemu Piippo <teemu@hecknology.net>
parents:
6
diff
changeset
|
59 | settings{}, |
68443f5be176
added the settings editor
Teemu Piippo <teemu@hecknology.net>
parents:
6
diff
changeset
|
60 | libraries{this} |
0 | 61 | { |
6 | 62 | this->ui->setupUi(this); |
16 | 63 | defaultKeyboardShortcuts = uiutilities::makeKeySequenceMap(uiutilities::collectActions(this)); |
5 | 64 | connect(ui->actionNew, &QAction::triggered, this, &MainWindow::newModel); |
6 | 65 | connect(ui->actionOpen, &QAction::triggered, this, &MainWindow::openModel); |
3 | 66 | connect(ui->actionQuit, &QAction::triggered, this, &QMainWindow::close); |
7
68443f5be176
added the settings editor
Teemu Piippo <teemu@hecknology.net>
parents:
6
diff
changeset
|
67 | connect(ui->actionSettingsEditor, &QAction::triggered, this, &MainWindow::runSettingsEditor); |
128
7c834fe36b25
Moved automatic grid adjusting into a new action
Teemu Piippo <teemu@hecknology.net>
parents:
125
diff
changeset
|
68 | connect(ui->actionAdjustGridToView, &QAction::triggered, [&]() |
7c834fe36b25
Moved automatic grid adjusting into a new action
Teemu Piippo <teemu@hecknology.net>
parents:
125
diff
changeset
|
69 | { |
7c834fe36b25
Moved automatic grid adjusting into a new action
Teemu Piippo <teemu@hecknology.net>
parents:
125
diff
changeset
|
70 | if (this->currentDocument() != nullptr) |
7c834fe36b25
Moved automatic grid adjusting into a new action
Teemu Piippo <teemu@hecknology.net>
parents:
125
diff
changeset
|
71 | { |
7c834fe36b25
Moved automatic grid adjusting into a new action
Teemu Piippo <teemu@hecknology.net>
parents:
125
diff
changeset
|
72 | this->currentDocument()->adjustGridToView(); |
7c834fe36b25
Moved automatic grid adjusting into a new action
Teemu Piippo <teemu@hecknology.net>
parents:
125
diff
changeset
|
73 | } |
7c834fe36b25
Moved automatic grid adjusting into a new action
Teemu Piippo <teemu@hecknology.net>
parents:
125
diff
changeset
|
74 | }); |
141 | 75 | connect(this->ui->actionSave, &QAction::triggered, |
76 | this, &MainWindow::actionSave); | |
77 | connect(this->ui->actionSaveAs, &QAction::triggered, | |
78 | this, &MainWindow::actionSaveAs); | |
36
bbb901b97404
added render style storage
Teemu Piippo <teemu@hecknology.net>
parents:
26
diff
changeset
|
79 | for (auto data : ::renderStyleButtons) |
bbb901b97404
added render style storage
Teemu Piippo <teemu@hecknology.net>
parents:
26
diff
changeset
|
80 | { |
bbb901b97404
added render style storage
Teemu Piippo <teemu@hecknology.net>
parents:
26
diff
changeset
|
81 | QAction* action = data.memberInstance(this->ui.get()); |
bbb901b97404
added render style storage
Teemu Piippo <teemu@hecknology.net>
parents:
26
diff
changeset
|
82 | connect(action, &QAction::triggered, [this, data]() |
bbb901b97404
added render style storage
Teemu Piippo <teemu@hecknology.net>
parents:
26
diff
changeset
|
83 | { |
bbb901b97404
added render style storage
Teemu Piippo <teemu@hecknology.net>
parents:
26
diff
changeset
|
84 | this->setRenderStyle(data.payload); |
bbb901b97404
added render style storage
Teemu Piippo <teemu@hecknology.net>
parents:
26
diff
changeset
|
85 | }); |
bbb901b97404
added render style storage
Teemu Piippo <teemu@hecknology.net>
parents:
26
diff
changeset
|
86 | } |
6 | 87 | this->updateTitle(); |
40
30cb5e836736
added configurable background color
Teemu Piippo <teemu@hecknology.net>
parents:
39
diff
changeset
|
88 | this->restoreStartupSettings(); |
7
68443f5be176
added the settings editor
Teemu Piippo <teemu@hecknology.net>
parents:
6
diff
changeset
|
89 | this->restoreSettings(); |
39
caac957e9834
Main color is now configurable
Teemu Piippo <teemu@hecknology.net>
parents:
36
diff
changeset
|
90 | this->updateRenderPreferences(); |
9
8b9780700b5e
added saving of splitter state and recent files
Teemu Piippo <teemu@hecknology.net>
parents:
8
diff
changeset
|
91 | this->newModel(); |
5 | 92 | } |
93 | ||
39
caac957e9834
Main color is now configurable
Teemu Piippo <teemu@hecknology.net>
parents:
36
diff
changeset
|
94 | // MainWindow needs a destructor even if it is empty because otherwise the destructor of the |
caac957e9834
Main color is now configurable
Teemu Piippo <teemu@hecknology.net>
parents:
36
diff
changeset
|
95 | // std::unique_ptr is resolved in the header file, where it will complain about Ui_MainWindow |
caac957e9834
Main color is now configurable
Teemu Piippo <teemu@hecknology.net>
parents:
36
diff
changeset
|
96 | // being incomplete. |
5 | 97 | MainWindow::~MainWindow() |
98 | { | |
99 | } | |
100 | ||
101 | void MainWindow::newModel() | |
102 | { | |
9
8b9780700b5e
added saving of splitter state and recent files
Teemu Piippo <teemu@hecknology.net>
parents:
8
diff
changeset
|
103 | this->openModelForEditing(documents.newModel()); |
5 | 104 | } |
105 | ||
106 | void MainWindow::openModel() | |
107 | { | |
108 | const QString path = QFileDialog::getOpenFileName( | |
7
68443f5be176
added the settings editor
Teemu Piippo <teemu@hecknology.net>
parents:
6
diff
changeset
|
109 | this, |
68443f5be176
added the settings editor
Teemu Piippo <teemu@hecknology.net>
parents:
6
diff
changeset
|
110 | tr("Open model"), |
68443f5be176
added the settings editor
Teemu Piippo <teemu@hecknology.net>
parents:
6
diff
changeset
|
111 | "", |
68443f5be176
added the settings editor
Teemu Piippo <teemu@hecknology.net>
parents:
6
diff
changeset
|
112 | tr("LDraw models (*.ldr *.dat)")); |
5 | 113 | if (not path.isEmpty()) |
114 | { | |
9
8b9780700b5e
added saving of splitter state and recent files
Teemu Piippo <teemu@hecknology.net>
parents:
8
diff
changeset
|
115 | this->openModelFromPath(path); |
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 | } |
8b9780700b5e
added saving of splitter state and recent files
Teemu Piippo <teemu@hecknology.net>
parents:
8
diff
changeset
|
118 | |
8b9780700b5e
added saving of splitter state and recent files
Teemu Piippo <teemu@hecknology.net>
parents:
8
diff
changeset
|
119 | void MainWindow::openModelFromPath(const QString& path) |
8b9780700b5e
added saving of splitter state and recent files
Teemu Piippo <teemu@hecknology.net>
parents:
8
diff
changeset
|
120 | { |
8b9780700b5e
added saving of splitter state and recent files
Teemu Piippo <teemu@hecknology.net>
parents:
8
diff
changeset
|
121 | QString errorString; |
8b9780700b5e
added saving of splitter state and recent files
Teemu Piippo <teemu@hecknology.net>
parents:
8
diff
changeset
|
122 | QTextStream errorStream{&errorString}; |
148 | 123 | std::optional<ModelId> modelIdOpt = this->documents.openModel( |
124 | path, | |
125 | errorStream, | |
126 | DocumentManager::OpenType::ManuallyOpened); | |
127 | if (modelIdOpt.has_value()) | |
9
8b9780700b5e
added saving of splitter state and recent files
Teemu Piippo <teemu@hecknology.net>
parents:
8
diff
changeset
|
128 | { |
148 | 129 | const ModelId modelId = modelIdOpt.value(); |
130 | this->documents.loadDependenciesForModel(modelId, path, this->libraries, errorStream); | |
12 | 131 | if (not errorString.isEmpty()) |
132 | { | |
133 | QMessageBox::warning( | |
134 | this, | |
135 | tr("Problem loading references"), | |
136 | errorString); | |
137 | } | |
148 | 138 | this->openModelForEditing(modelId); |
9
8b9780700b5e
added saving of splitter state and recent files
Teemu Piippo <teemu@hecknology.net>
parents:
8
diff
changeset
|
139 | this->addRecentlyOpenedFile(path); |
8b9780700b5e
added saving of splitter state and recent files
Teemu Piippo <teemu@hecknology.net>
parents:
8
diff
changeset
|
140 | } |
8b9780700b5e
added saving of splitter state and recent files
Teemu Piippo <teemu@hecknology.net>
parents:
8
diff
changeset
|
141 | else |
8b9780700b5e
added saving of splitter state and recent files
Teemu Piippo <teemu@hecknology.net>
parents:
8
diff
changeset
|
142 | { |
8b9780700b5e
added saving of splitter state and recent files
Teemu Piippo <teemu@hecknology.net>
parents:
8
diff
changeset
|
143 | QMessageBox::critical( |
8b9780700b5e
added saving of splitter state and recent files
Teemu Piippo <teemu@hecknology.net>
parents:
8
diff
changeset
|
144 | this, |
8b9780700b5e
added saving of splitter state and recent files
Teemu Piippo <teemu@hecknology.net>
parents:
8
diff
changeset
|
145 | tr("Problem opening file"), |
8b9780700b5e
added saving of splitter state and recent files
Teemu Piippo <teemu@hecknology.net>
parents:
8
diff
changeset
|
146 | utility::format( |
7
68443f5be176
added the settings editor
Teemu Piippo <teemu@hecknology.net>
parents:
6
diff
changeset
|
147 | tr("Could not open %1: %2"), |
68443f5be176
added the settings editor
Teemu Piippo <teemu@hecknology.net>
parents:
6
diff
changeset
|
148 | path, |
9
8b9780700b5e
added saving of splitter state and recent files
Teemu Piippo <teemu@hecknology.net>
parents:
8
diff
changeset
|
149 | errorString)); |
5 | 150 | } |
151 | } | |
152 | ||
153 | /** | |
6 | 154 | * @brief Changes the application language to the specified language |
155 | * @param localeCode Code of the locale to translate to | |
156 | */ | |
7
68443f5be176
added the settings editor
Teemu Piippo <teemu@hecknology.net>
parents:
6
diff
changeset
|
157 | void MainWindow::changeLanguage(QString localeCode) |
6 | 158 | { |
159 | if (not localeCode.isEmpty() and localeCode != this->currentLanguage) | |
160 | { | |
161 | this->currentLanguage = localeCode; | |
7
68443f5be176
added the settings editor
Teemu Piippo <teemu@hecknology.net>
parents:
6
diff
changeset
|
162 | if (localeCode == "system") |
68443f5be176
added the settings editor
Teemu Piippo <teemu@hecknology.net>
parents:
6
diff
changeset
|
163 | { |
68443f5be176
added the settings editor
Teemu Piippo <teemu@hecknology.net>
parents:
6
diff
changeset
|
164 | localeCode = QLocale::system().name(); |
68443f5be176
added the settings editor
Teemu Piippo <teemu@hecknology.net>
parents:
6
diff
changeset
|
165 | } |
6 | 166 | QLocale::setDefault({localeCode}); |
167 | qApp->removeTranslator(&this->translator); | |
168 | const bool loadSuccessful = this->translator.load(pathToTranslation(localeCode)); | |
169 | if (loadSuccessful) | |
170 | { | |
171 | qApp->installTranslator(&this->translator); | |
172 | } | |
173 | } | |
174 | } | |
175 | ||
9
8b9780700b5e
added saving of splitter state and recent files
Teemu Piippo <teemu@hecknology.net>
parents:
8
diff
changeset
|
176 | void MainWindow::addRecentlyOpenedFile(const QString& path) |
8b9780700b5e
added saving of splitter state and recent files
Teemu Piippo <teemu@hecknology.net>
parents:
8
diff
changeset
|
177 | { |
8b9780700b5e
added saving of splitter state and recent files
Teemu Piippo <teemu@hecknology.net>
parents:
8
diff
changeset
|
178 | this->recentlyOpenedFiles.removeAll(path); |
8b9780700b5e
added saving of splitter state and recent files
Teemu Piippo <teemu@hecknology.net>
parents:
8
diff
changeset
|
179 | this->recentlyOpenedFiles.insert(0, path); |
8b9780700b5e
added saving of splitter state and recent files
Teemu Piippo <teemu@hecknology.net>
parents:
8
diff
changeset
|
180 | while (this->recentlyOpenedFiles.size() > maxRecentlyOpenedFiles) |
8b9780700b5e
added saving of splitter state and recent files
Teemu Piippo <teemu@hecknology.net>
parents:
8
diff
changeset
|
181 | { |
8b9780700b5e
added saving of splitter state and recent files
Teemu Piippo <teemu@hecknology.net>
parents:
8
diff
changeset
|
182 | this->recentlyOpenedFiles.removeLast(); |
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 | this->saveSettings(); |
8b9780700b5e
added saving of splitter state and recent files
Teemu Piippo <teemu@hecknology.net>
parents:
8
diff
changeset
|
185 | this->updateRecentlyOpenedDocumentsMenu(); |
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 | |
148 | 188 | void MainWindow::openModelForEditing(const ModelId modelId) |
9
8b9780700b5e
added saving of splitter state and recent files
Teemu Piippo <teemu@hecknology.net>
parents:
8
diff
changeset
|
189 | { |
148 | 190 | Document* document = new Document{this->documents.getModelById(modelId), &this->documents, this->colorTable}; |
39
caac957e9834
Main color is now configurable
Teemu Piippo <teemu@hecknology.net>
parents:
36
diff
changeset
|
191 | document->setRenderPreferences(this->renderPreferences); |
47 | 192 | connect(document, &Document::newStatusText, [&](const QString& newStatusText) |
193 | { | |
194 | this->statusBar()->showMessage(newStatusText); | |
195 | }); | |
148 | 196 | const QFileInfo fileInfo{*this->documents.modelPath(modelId)}; |
197 | this->ui->tabs->addTab(document, fileInfo.baseName()); | |
9
8b9780700b5e
added saving of splitter state and recent files
Teemu Piippo <teemu@hecknology.net>
parents:
8
diff
changeset
|
198 | this->ui->tabs->setCurrentWidget(document); |
8b9780700b5e
added saving of splitter state and recent files
Teemu Piippo <teemu@hecknology.net>
parents:
8
diff
changeset
|
199 | document->restoreSplitterState(this->documentSplitterState); |
8b9780700b5e
added saving of splitter state and recent files
Teemu Piippo <teemu@hecknology.net>
parents:
8
diff
changeset
|
200 | } |
8b9780700b5e
added saving of splitter state and recent files
Teemu Piippo <teemu@hecknology.net>
parents:
8
diff
changeset
|
201 | |
7
68443f5be176
added the settings editor
Teemu Piippo <teemu@hecknology.net>
parents:
6
diff
changeset
|
202 | void MainWindow::runSettingsEditor() |
6 | 203 | { |
16 | 204 | SettingsEditor settingsEditor{&this->settings, this->defaultKeyboardShortcuts, this}; |
7
68443f5be176
added the settings editor
Teemu Piippo <teemu@hecknology.net>
parents:
6
diff
changeset
|
205 | const int result = settingsEditor.exec(); |
68443f5be176
added the settings editor
Teemu Piippo <teemu@hecknology.net>
parents:
6
diff
changeset
|
206 | if (result == QDialog::Accepted) |
6 | 207 | { |
7
68443f5be176
added the settings editor
Teemu Piippo <teemu@hecknology.net>
parents:
6
diff
changeset
|
208 | this->restoreSettings(); |
6 | 209 | } |
210 | } | |
211 | ||
125
f127982d3412
Move tools under Document instead of MainWindow
Teemu Piippo <teemu@hecknology.net>
parents:
124
diff
changeset
|
212 | Document* MainWindow::currentDocument() |
f127982d3412
Move tools under Document instead of MainWindow
Teemu Piippo <teemu@hecknology.net>
parents:
124
diff
changeset
|
213 | { |
f127982d3412
Move tools under Document instead of MainWindow
Teemu Piippo <teemu@hecknology.net>
parents:
124
diff
changeset
|
214 | return qobject_cast<Document*>(this->ui->tabs->currentWidget()); |
f127982d3412
Move tools under Document instead of MainWindow
Teemu Piippo <teemu@hecknology.net>
parents:
124
diff
changeset
|
215 | } |
f127982d3412
Move tools under Document instead of MainWindow
Teemu Piippo <teemu@hecknology.net>
parents:
124
diff
changeset
|
216 | |
148 | 217 | /* |
147 | 218 | void MainWindow::closeDocument(Document *document) |
219 | { | |
220 | const int tabIndex = this->ui->tabs->indexOf(document); | |
221 | if (tabIndex != -1) | |
222 | { | |
223 | this->ui->tabs->removeTab(tabIndex); | |
224 | const QString closedDocumentName = pathToName(document->modelPath()); | |
225 | delete document; | |
226 | } | |
227 | QSet<QString> openedDocumentNames; | |
228 | for (int i = 0; i < this->ui->tabs->count(); i += 1) | |
229 | { | |
230 | Document* document = qobject_cast<Document*>(this->ui->tabs->widget(i)); | |
231 | if (document != nullptr) | |
232 | { | |
233 | openedDocumentNames.insert(pathToName(document->modelPath())); | |
234 | } | |
235 | } | |
236 | } | |
148 | 237 | */ |
147 | 238 | |
9
8b9780700b5e
added saving of splitter state and recent files
Teemu Piippo <teemu@hecknology.net>
parents:
8
diff
changeset
|
239 | void MainWindow::handleDocumentSplitterChange() |
8b9780700b5e
added saving of splitter state and recent files
Teemu Piippo <teemu@hecknology.net>
parents:
8
diff
changeset
|
240 | { |
125
f127982d3412
Move tools under Document instead of MainWindow
Teemu Piippo <teemu@hecknology.net>
parents:
124
diff
changeset
|
241 | Document* currentDocument = this->currentDocument(); |
9
8b9780700b5e
added saving of splitter state and recent files
Teemu Piippo <teemu@hecknology.net>
parents:
8
diff
changeset
|
242 | if (currentDocument != nullptr) |
8b9780700b5e
added saving of splitter state and recent files
Teemu Piippo <teemu@hecknology.net>
parents:
8
diff
changeset
|
243 | { |
8b9780700b5e
added saving of splitter state and recent files
Teemu Piippo <teemu@hecknology.net>
parents:
8
diff
changeset
|
244 | this->documentSplitterState = currentDocument->saveSplitterState(); |
8b9780700b5e
added saving of splitter state and recent files
Teemu Piippo <teemu@hecknology.net>
parents:
8
diff
changeset
|
245 | 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
|
246 | { |
8b9780700b5e
added saving of splitter state and recent files
Teemu Piippo <teemu@hecknology.net>
parents:
8
diff
changeset
|
247 | 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
|
248 | if (document != nullptr and document != currentDocument) |
8b9780700b5e
added saving of splitter state and recent files
Teemu Piippo <teemu@hecknology.net>
parents:
8
diff
changeset
|
249 | { |
8b9780700b5e
added saving of splitter state and recent files
Teemu Piippo <teemu@hecknology.net>
parents:
8
diff
changeset
|
250 | document->restoreSplitterState(this->documentSplitterState); |
8b9780700b5e
added saving of splitter state and recent files
Teemu Piippo <teemu@hecknology.net>
parents:
8
diff
changeset
|
251 | } |
8b9780700b5e
added saving of splitter state and recent files
Teemu Piippo <teemu@hecknology.net>
parents:
8
diff
changeset
|
252 | } |
41
0abada2a9802
added automated configuration collection
Teemu Piippo <teemu@hecknology.net>
parents:
40
diff
changeset
|
253 | this->settings.setMainSplitterState(this->documentSplitterState); |
9
8b9780700b5e
added saving of splitter state and recent files
Teemu Piippo <teemu@hecknology.net>
parents:
8
diff
changeset
|
254 | } |
8b9780700b5e
added saving of splitter state and recent files
Teemu Piippo <teemu@hecknology.net>
parents:
8
diff
changeset
|
255 | } |
8b9780700b5e
added saving of splitter state and recent files
Teemu Piippo <teemu@hecknology.net>
parents:
8
diff
changeset
|
256 | |
8b9780700b5e
added saving of splitter state and recent files
Teemu Piippo <teemu@hecknology.net>
parents:
8
diff
changeset
|
257 | void MainWindow::updateRecentlyOpenedDocumentsMenu() |
8b9780700b5e
added saving of splitter state and recent files
Teemu Piippo <teemu@hecknology.net>
parents:
8
diff
changeset
|
258 | { |
8b9780700b5e
added saving of splitter state and recent files
Teemu Piippo <teemu@hecknology.net>
parents:
8
diff
changeset
|
259 | this->ui->menuRecentFiles->clear(); |
8b9780700b5e
added saving of splitter state and recent files
Teemu Piippo <teemu@hecknology.net>
parents:
8
diff
changeset
|
260 | for (const QString& path : this->recentlyOpenedFiles) |
8b9780700b5e
added saving of splitter state and recent files
Teemu Piippo <teemu@hecknology.net>
parents:
8
diff
changeset
|
261 | { |
8b9780700b5e
added saving of splitter state and recent files
Teemu Piippo <teemu@hecknology.net>
parents:
8
diff
changeset
|
262 | QAction* action = new QAction{path, this}; |
8b9780700b5e
added saving of splitter state and recent files
Teemu Piippo <teemu@hecknology.net>
parents:
8
diff
changeset
|
263 | action->setData(path); |
8b9780700b5e
added saving of splitter state and recent files
Teemu Piippo <teemu@hecknology.net>
parents:
8
diff
changeset
|
264 | this->ui->menuRecentFiles->addAction(action); |
8b9780700b5e
added saving of splitter state and recent files
Teemu Piippo <teemu@hecknology.net>
parents:
8
diff
changeset
|
265 | 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
|
266 | } |
8b9780700b5e
added saving of splitter state and recent files
Teemu Piippo <teemu@hecknology.net>
parents:
8
diff
changeset
|
267 | } |
8b9780700b5e
added saving of splitter state and recent files
Teemu Piippo <teemu@hecknology.net>
parents:
8
diff
changeset
|
268 | |
8b9780700b5e
added saving of splitter state and recent files
Teemu Piippo <teemu@hecknology.net>
parents:
8
diff
changeset
|
269 | void MainWindow::openRecentFile() |
8b9780700b5e
added saving of splitter state and recent files
Teemu Piippo <teemu@hecknology.net>
parents:
8
diff
changeset
|
270 | { |
8b9780700b5e
added saving of splitter state and recent files
Teemu Piippo <teemu@hecknology.net>
parents:
8
diff
changeset
|
271 | 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
|
272 | if (action != nullptr) |
8b9780700b5e
added saving of splitter state and recent files
Teemu Piippo <teemu@hecknology.net>
parents:
8
diff
changeset
|
273 | { |
8b9780700b5e
added saving of splitter state and recent files
Teemu Piippo <teemu@hecknology.net>
parents:
8
diff
changeset
|
274 | const QString path = action->data().toString(); |
8b9780700b5e
added saving of splitter state and recent files
Teemu Piippo <teemu@hecknology.net>
parents:
8
diff
changeset
|
275 | this->openModelFromPath(path); |
8b9780700b5e
added saving of splitter state and recent files
Teemu Piippo <teemu@hecknology.net>
parents:
8
diff
changeset
|
276 | } |
8b9780700b5e
added saving of splitter state and recent files
Teemu Piippo <teemu@hecknology.net>
parents:
8
diff
changeset
|
277 | } |
8b9780700b5e
added saving of splitter state and recent files
Teemu Piippo <teemu@hecknology.net>
parents:
8
diff
changeset
|
278 | |
36
bbb901b97404
added render style storage
Teemu Piippo <teemu@hecknology.net>
parents:
26
diff
changeset
|
279 | void MainWindow::setRenderStyle(gl::RenderStyle renderStyle) |
bbb901b97404
added render style storage
Teemu Piippo <teemu@hecknology.net>
parents:
26
diff
changeset
|
280 | { |
39
caac957e9834
Main color is now configurable
Teemu Piippo <teemu@hecknology.net>
parents:
36
diff
changeset
|
281 | this->renderPreferences.style = renderStyle; |
36
bbb901b97404
added render style storage
Teemu Piippo <teemu@hecknology.net>
parents:
26
diff
changeset
|
282 | this->saveSettings(); |
39
caac957e9834
Main color is now configurable
Teemu Piippo <teemu@hecknology.net>
parents:
36
diff
changeset
|
283 | this->updateRenderPreferences(); |
36
bbb901b97404
added render style storage
Teemu Piippo <teemu@hecknology.net>
parents:
26
diff
changeset
|
284 | } |
bbb901b97404
added render style storage
Teemu Piippo <teemu@hecknology.net>
parents:
26
diff
changeset
|
285 | |
141 | 286 | /** |
287 | * @brief Handles the "Save" (Ctrl+S) action | |
288 | */ | |
289 | void MainWindow::actionSave() | |
290 | { | |
291 | if (this->currentDocument() != nullptr) | |
292 | { | |
148 | 293 | const ModelId modelId = {0}; |
294 | const QString* path = this->documents.modelPath(modelId); | |
295 | if (path == nullptr or path->isEmpty()) | |
141 | 296 | { |
297 | this->actionSaveAs(); | |
298 | } | |
299 | else | |
300 | { | |
301 | QString error; | |
302 | QTextStream errorStream{&error}; | |
148 | 303 | const bool succeeded = this->documents.saveModel(modelId, errorStream); |
141 | 304 | if (not succeeded) |
305 | { | |
306 | QMessageBox::critical(this, tr("Save error"), error); | |
307 | } | |
308 | else | |
309 | { | |
148 | 310 | this->addRecentlyOpenedFile(*path); |
141 | 311 | } |
312 | } | |
313 | } | |
314 | } | |
315 | ||
316 | /** | |
317 | * @brief Handles the "Save as…" (Ctrl+Shift+S) action | |
318 | */ | |
319 | void MainWindow::actionSaveAs() | |
320 | { | |
321 | if (this->currentDocument() != nullptr) | |
322 | { | |
148 | 323 | const ModelId modelId = {0}; |
324 | const QString* pathPtr = this->documents.modelPath(modelId); | |
325 | QString defaultPath = (pathPtr != nullptr) ? *pathPtr : ""; | |
141 | 326 | const QString newPath = QFileDialog::getSaveFileName( |
327 | this, | |
328 | tr("Save as…"), | |
148 | 329 | QFileInfo{defaultPath}.absoluteDir().path(), |
141 | 330 | tr("LDraw files (*.ldr *dat);;All files (*)") |
331 | ); | |
332 | if (not newPath.isEmpty()) | |
333 | { | |
148 | 334 | QString error; |
335 | QTextStream errorStream{&error}; | |
336 | this->documents.setModelPath(modelId, newPath, this->libraries, errorStream); | |
142
e085f36e4d9f
Update document tab label after saving
Teemu Piippo <teemu@hecknology.net>
parents:
141
diff
changeset
|
337 | this->ui->tabs->setTabText(this->ui->tabs->currentIndex(), QFileInfo{newPath}.fileName()); |
141 | 338 | this->actionSave(); |
339 | } | |
340 | } | |
341 | } | |
342 | ||
6 | 343 | void MainWindow::changeEvent(QEvent* event) |
344 | { | |
345 | if (event != nullptr) | |
346 | { | |
347 | switch (event->type()) | |
348 | { | |
349 | case QEvent::LanguageChange: | |
350 | this->ui->retranslateUi(this); | |
351 | break; | |
352 | default: | |
353 | break; | |
354 | } | |
355 | } | |
356 | QMainWindow::changeEvent(event); | |
357 | } | |
358 | ||
359 | /** | |
7
68443f5be176
added the settings editor
Teemu Piippo <teemu@hecknology.net>
parents:
6
diff
changeset
|
360 | * @brief Handles closing the main window |
68443f5be176
added the settings editor
Teemu Piippo <teemu@hecknology.net>
parents:
6
diff
changeset
|
361 | * @param event Event information |
68443f5be176
added the settings editor
Teemu Piippo <teemu@hecknology.net>
parents:
6
diff
changeset
|
362 | */ |
68443f5be176
added the settings editor
Teemu Piippo <teemu@hecknology.net>
parents:
6
diff
changeset
|
363 | void MainWindow::closeEvent(QCloseEvent* event) |
68443f5be176
added the settings editor
Teemu Piippo <teemu@hecknology.net>
parents:
6
diff
changeset
|
364 | { |
68443f5be176
added the settings editor
Teemu Piippo <teemu@hecknology.net>
parents:
6
diff
changeset
|
365 | saveSettings(); |
68443f5be176
added the settings editor
Teemu Piippo <teemu@hecknology.net>
parents:
6
diff
changeset
|
366 | event->accept(); |
68443f5be176
added the settings editor
Teemu Piippo <teemu@hecknology.net>
parents:
6
diff
changeset
|
367 | } |
68443f5be176
added the settings editor
Teemu Piippo <teemu@hecknology.net>
parents:
6
diff
changeset
|
368 | |
68443f5be176
added the settings editor
Teemu Piippo <teemu@hecknology.net>
parents:
6
diff
changeset
|
369 | /** |
5 | 370 | * @brief Updates the title of the main window so to contain the app's name |
371 | * and version as well as the open document name. | |
372 | */ | |
373 | void MainWindow::updateTitle() | |
374 | { | |
1 | 375 | QString title = ::appName; |
376 | title += " "; | |
377 | title += fullVersionString(); | |
378 | setWindowTitle(title); | |
0 | 379 | } |
6 | 380 | |
39
caac957e9834
Main color is now configurable
Teemu Piippo <teemu@hecknology.net>
parents:
36
diff
changeset
|
381 | void MainWindow::updateRenderPreferences() |
36
bbb901b97404
added render style storage
Teemu Piippo <teemu@hecknology.net>
parents:
26
diff
changeset
|
382 | { |
bbb901b97404
added render style storage
Teemu Piippo <teemu@hecknology.net>
parents:
26
diff
changeset
|
383 | 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
|
384 | { |
bbb901b97404
added render style storage
Teemu Piippo <teemu@hecknology.net>
parents:
26
diff
changeset
|
385 | Document* document = qobject_cast<Document*>(this->ui->tabs->widget(i)); |
bbb901b97404
added render style storage
Teemu Piippo <teemu@hecknology.net>
parents:
26
diff
changeset
|
386 | if (document != nullptr) |
bbb901b97404
added render style storage
Teemu Piippo <teemu@hecknology.net>
parents:
26
diff
changeset
|
387 | { |
39
caac957e9834
Main color is now configurable
Teemu Piippo <teemu@hecknology.net>
parents:
36
diff
changeset
|
388 | document->setRenderPreferences(this->renderPreferences); |
36
bbb901b97404
added render style storage
Teemu Piippo <teemu@hecknology.net>
parents:
26
diff
changeset
|
389 | } |
bbb901b97404
added render style storage
Teemu Piippo <teemu@hecknology.net>
parents:
26
diff
changeset
|
390 | } |
bbb901b97404
added render style storage
Teemu Piippo <teemu@hecknology.net>
parents:
26
diff
changeset
|
391 | for (auto data : ::renderStyleButtons) |
bbb901b97404
added render style storage
Teemu Piippo <teemu@hecknology.net>
parents:
26
diff
changeset
|
392 | { |
bbb901b97404
added render style storage
Teemu Piippo <teemu@hecknology.net>
parents:
26
diff
changeset
|
393 | QAction* action = data.memberInstance(this->ui.get()); |
39
caac957e9834
Main color is now configurable
Teemu Piippo <teemu@hecknology.net>
parents:
36
diff
changeset
|
394 | action->setChecked(this->renderPreferences.style == data.payload); |
36
bbb901b97404
added render style storage
Teemu Piippo <teemu@hecknology.net>
parents:
26
diff
changeset
|
395 | } |
bbb901b97404
added render style storage
Teemu Piippo <teemu@hecknology.net>
parents:
26
diff
changeset
|
396 | } |
bbb901b97404
added render style storage
Teemu Piippo <teemu@hecknology.net>
parents:
26
diff
changeset
|
397 | |
7
68443f5be176
added the settings editor
Teemu Piippo <teemu@hecknology.net>
parents:
6
diff
changeset
|
398 | /** |
68443f5be176
added the settings editor
Teemu Piippo <teemu@hecknology.net>
parents:
6
diff
changeset
|
399 | * @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
|
400 | */ |
68443f5be176
added the settings editor
Teemu Piippo <teemu@hecknology.net>
parents:
6
diff
changeset
|
401 | void MainWindow::saveSettings() |
68443f5be176
added the settings editor
Teemu Piippo <teemu@hecknology.net>
parents:
6
diff
changeset
|
402 | { |
41
0abada2a9802
added automated configuration collection
Teemu Piippo <teemu@hecknology.net>
parents:
40
diff
changeset
|
403 | this->settings.setMainWindowGeometry(this->saveGeometry()); |
0abada2a9802
added automated configuration collection
Teemu Piippo <teemu@hecknology.net>
parents:
40
diff
changeset
|
404 | this->settings.setRecentFiles(this->recentlyOpenedFiles); |
0abada2a9802
added automated configuration collection
Teemu Piippo <teemu@hecknology.net>
parents:
40
diff
changeset
|
405 | this->settings.setMainSplitterState(this->documentSplitterState); |
0abada2a9802
added automated configuration collection
Teemu Piippo <teemu@hecknology.net>
parents:
40
diff
changeset
|
406 | this->settings.setRenderStyle(static_cast<int>(this->renderPreferences.style)); |
7
68443f5be176
added the settings editor
Teemu Piippo <teemu@hecknology.net>
parents:
6
diff
changeset
|
407 | this->libraries.storeToSettings(&this->settings); |
68443f5be176
added the settings editor
Teemu Piippo <teemu@hecknology.net>
parents:
6
diff
changeset
|
408 | } |
68443f5be176
added the settings editor
Teemu Piippo <teemu@hecknology.net>
parents:
6
diff
changeset
|
409 | |
40
30cb5e836736
added configurable background color
Teemu Piippo <teemu@hecknology.net>
parents:
39
diff
changeset
|
410 | void MainWindow::restoreStartupSettings() |
30cb5e836736
added configurable background color
Teemu Piippo <teemu@hecknology.net>
parents:
39
diff
changeset
|
411 | { |
41
0abada2a9802
added automated configuration collection
Teemu Piippo <teemu@hecknology.net>
parents:
40
diff
changeset
|
412 | this->restoreGeometry(this->settings.mainWindowGeometry()); |
40
30cb5e836736
added configurable background color
Teemu Piippo <teemu@hecknology.net>
parents:
39
diff
changeset
|
413 | } |
30cb5e836736
added configurable background color
Teemu Piippo <teemu@hecknology.net>
parents:
39
diff
changeset
|
414 | |
7
68443f5be176
added the settings editor
Teemu Piippo <teemu@hecknology.net>
parents:
6
diff
changeset
|
415 | /** |
68443f5be176
added the settings editor
Teemu Piippo <teemu@hecknology.net>
parents:
6
diff
changeset
|
416 | * @brief Restores saved settings relating to the main window |
68443f5be176
added the settings editor
Teemu Piippo <teemu@hecknology.net>
parents:
6
diff
changeset
|
417 | */ |
68443f5be176
added the settings editor
Teemu Piippo <teemu@hecknology.net>
parents:
6
diff
changeset
|
418 | void MainWindow::restoreSettings() |
68443f5be176
added the settings editor
Teemu Piippo <teemu@hecknology.net>
parents:
6
diff
changeset
|
419 | { |
41
0abada2a9802
added automated configuration collection
Teemu Piippo <teemu@hecknology.net>
parents:
40
diff
changeset
|
420 | this->recentlyOpenedFiles = this->settings.recentFiles(); |
0abada2a9802
added automated configuration collection
Teemu Piippo <teemu@hecknology.net>
parents:
40
diff
changeset
|
421 | this->documentSplitterState = this->settings.mainSplitterState(); |
0abada2a9802
added automated configuration collection
Teemu Piippo <teemu@hecknology.net>
parents:
40
diff
changeset
|
422 | this->renderPreferences.style = static_cast<gl::RenderStyle>(this->settings.renderStyle()); |
0abada2a9802
added automated configuration collection
Teemu Piippo <teemu@hecknology.net>
parents:
40
diff
changeset
|
423 | this->renderPreferences.mainColor = this->settings.mainColor(); |
0abada2a9802
added automated configuration collection
Teemu Piippo <teemu@hecknology.net>
parents:
40
diff
changeset
|
424 | this->renderPreferences.backgroundColor = this->settings.backgroundColor(); |
44
c6114b3af3a6
added configurable line thickness
Teemu Piippo <teemu@hecknology.net>
parents:
41
diff
changeset
|
425 | this->renderPreferences.lineThickness = this->settings.lineThickness(); |
45
272c84c7c87e
added configurable line anti-aliasing
Teemu Piippo <teemu@hecknology.net>
parents:
44
diff
changeset
|
426 | this->renderPreferences.lineAntiAliasing = this->settings.lineAntiAliasing(); |
48
3c10f0e2fbe0
added selection highlighting
Teemu Piippo <teemu@hecknology.net>
parents:
47
diff
changeset
|
427 | this->renderPreferences.selectedColor = this->settings.selectedColor(); |
7
68443f5be176
added the settings editor
Teemu Piippo <teemu@hecknology.net>
parents:
6
diff
changeset
|
428 | const QString systemLocale = QLocale::system().name(); |
41
0abada2a9802
added automated configuration collection
Teemu Piippo <teemu@hecknology.net>
parents:
40
diff
changeset
|
429 | const QVariant defaultLocale = this->settings.locale(); |
0abada2a9802
added automated configuration collection
Teemu Piippo <teemu@hecknology.net>
parents:
40
diff
changeset
|
430 | this->changeLanguage(defaultLocale.toString()); |
7
68443f5be176
added the settings editor
Teemu Piippo <teemu@hecknology.net>
parents:
6
diff
changeset
|
431 | this->libraries.restoreFromSettings(&this->settings); |
9
8b9780700b5e
added saving of splitter state and recent files
Teemu Piippo <teemu@hecknology.net>
parents:
8
diff
changeset
|
432 | this->updateRecentlyOpenedDocumentsMenu(); |
26 | 433 | this->loadColors(); |
39
caac957e9834
Main color is now configurable
Teemu Piippo <teemu@hecknology.net>
parents:
36
diff
changeset
|
434 | this->updateRenderPreferences(); |
7
68443f5be176
added the settings editor
Teemu Piippo <teemu@hecknology.net>
parents:
6
diff
changeset
|
435 | } |
68443f5be176
added the settings editor
Teemu Piippo <teemu@hecknology.net>
parents:
6
diff
changeset
|
436 | |
6 | 437 | QString MainWindow::pathToTranslation(const QString& localeCode) |
438 | { | |
439 | QDir dir {":/locale"}; | |
440 | return dir.filePath(localeCode + ".qm"); | |
441 | } | |
26 | 442 | |
443 | void MainWindow::loadColors() | |
444 | { | |
445 | QTextStream errors; | |
446 | this->colorTable = this->libraries.loadColorTable(errors); | |
447 | } | |
97 | 448 | |
125
f127982d3412
Move tools under Document instead of MainWindow
Teemu Piippo <teemu@hecknology.net>
parents:
124
diff
changeset
|
449 | void MainWindow::keyReleaseEvent(QKeyEvent* event) |
97 | 450 | { |
125
f127982d3412
Move tools under Document instead of MainWindow
Teemu Piippo <teemu@hecknology.net>
parents:
124
diff
changeset
|
451 | Document* document = this->currentDocument(); |
f127982d3412
Move tools under Document instead of MainWindow
Teemu Piippo <teemu@hecknology.net>
parents:
124
diff
changeset
|
452 | if (document != nullptr) |
97 | 453 | { |
125
f127982d3412
Move tools under Document instead of MainWindow
Teemu Piippo <teemu@hecknology.net>
parents:
124
diff
changeset
|
454 | document->handleKeyPress(event); |
97 | 455 | } |
456 | } |