Tue, 07 Jun 2022 21:35:29 +0300
Move editing modes tool bar, tool options widget stack and model list view into the main window
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 | ||
8
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
5
diff
changeset
|
19 | #include <QFile> |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
5
diff
changeset
|
20 | #include <QDir> |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
5
diff
changeset
|
21 | #include <QFileInfo> |
148 | 22 | #include <QSaveFile> |
3 | 23 | #include "documentmanager.h" |
8
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
5
diff
changeset
|
24 | #include "parser.h" |
3 | 25 | |
8
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
5
diff
changeset
|
26 | /** |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
5
diff
changeset
|
27 | * @brief Constructs a new document manager |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
5
diff
changeset
|
28 | * @param parent Parent object |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
5
diff
changeset
|
29 | */ |
5 | 30 | DocumentManager::DocumentManager(QObject* parent) : |
31 | QObject{parent} | |
32 | { | |
33 | } | |
34 | ||
8
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
5
diff
changeset
|
35 | /** |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
5
diff
changeset
|
36 | * @brief Creates a new model. |
148 | 37 | * @returns the ID of the new model |
8
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
5
diff
changeset
|
38 | */ |
148 | 39 | ModelId DocumentManager::newModel() |
8
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
5
diff
changeset
|
40 | { |
148 | 41 | const ModelId modelId{++this->modelIdCounter}; |
8
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
5
diff
changeset
|
42 | const QString name = makeNewModelName(); |
148 | 43 | this->openModels[modelId] = ModelInfo{ |
200 | 44 | .model = std::make_unique<Model>(this), |
150
b6cbba6e29a1
extract polygon cache out of Model
Teemu Piippo <teemu@hecknology.net>
parents:
148
diff
changeset
|
45 | .id = modelId, |
148 | 46 | .opentype = OpenType::ManuallyOpened, |
47 | }; | |
150
b6cbba6e29a1
extract polygon cache out of Model
Teemu Piippo <teemu@hecknology.net>
parents:
148
diff
changeset
|
48 | this->makePolygonCacheForModel(modelId); |
148 | 49 | return modelId; |
8
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
5
diff
changeset
|
50 | } |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
5
diff
changeset
|
51 | |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
5
diff
changeset
|
52 | /** |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
5
diff
changeset
|
53 | * @brief Looks for a model by name |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
5
diff
changeset
|
54 | * @param name Name of the model |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
5
diff
changeset
|
55 | * @returns model or null |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
5
diff
changeset
|
56 | */ |
148 | 57 | Model* DocumentManager::findDependencyByName(const ModelId modelId, const QString& name) |
8
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
5
diff
changeset
|
58 | { |
148 | 59 | const auto modelsIterator = this->openModels.find(modelId); |
60 | if (modelsIterator != std::end(this->openModels)) | |
61 | { | |
62 | const auto& dependencies = modelsIterator->second.dependencies; | |
63 | const auto dependenciesIterator = dependencies.find(name); | |
64 | if (dependenciesIterator != dependencies.end()) | |
65 | { | |
66 | ModelInfo& modelInfo = this->openModels[dependenciesIterator->second]; | |
67 | return modelInfo.model.get(); | |
68 | } | |
69 | else | |
70 | { | |
71 | return nullptr; | |
72 | } | |
73 | } | |
74 | else | |
8
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
5
diff
changeset
|
75 | { |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
5
diff
changeset
|
76 | return nullptr; |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
5
diff
changeset
|
77 | } |
148 | 78 | } |
79 | ||
80 | /** | |
81 | * @brief Gets a model pointer by id or nullptr if not found | |
82 | * @param modelId id of model to find | |
83 | * @returns model pointer or null | |
84 | */ | |
85 | Model *DocumentManager::getModelById(ModelId modelId) | |
86 | { | |
87 | const auto iterator = this->openModels.find(modelId); | |
88 | if (iterator != this->openModels.end()) | |
89 | { | |
90 | return iterator->second.model.get(); | |
91 | } | |
8
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
5
diff
changeset
|
92 | else |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
5
diff
changeset
|
93 | { |
148 | 94 | return nullptr; |
8
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
5
diff
changeset
|
95 | } |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
5
diff
changeset
|
96 | } |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
5
diff
changeset
|
97 | |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
5
diff
changeset
|
98 | QString pathToName(const QFileInfo& path) |
3 | 99 | { |
8
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
5
diff
changeset
|
100 | static const char* paths[] = { |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
5
diff
changeset
|
101 | "s", |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
5
diff
changeset
|
102 | "48" |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
5
diff
changeset
|
103 | "8" |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
5
diff
changeset
|
104 | }; |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
5
diff
changeset
|
105 | const QString baseName = path.fileName(); |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
5
diff
changeset
|
106 | const QString dirName = QFileInfo{path.dir().path()}.fileName(); |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
5
diff
changeset
|
107 | QString result; |
17 | 108 | if (std::find(std::begin(paths), std::end(paths), dirName) != std::end(paths)) |
8
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
5
diff
changeset
|
109 | { |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
5
diff
changeset
|
110 | result = dirName + "\\" + baseName; |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
5
diff
changeset
|
111 | } |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
5
diff
changeset
|
112 | else |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
5
diff
changeset
|
113 | { |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
5
diff
changeset
|
114 | result = baseName; |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
5
diff
changeset
|
115 | } |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
5
diff
changeset
|
116 | return result; |
3 | 117 | } |
8
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
5
diff
changeset
|
118 | |
147 | 119 | /** |
120 | * @brief Tries to open the model at the specified path | |
121 | * @param path Path to the model to open | |
122 | * @param errorStream Where to write any errors | |
123 | * @param openType rationale behind opening this file | |
148 | 124 | * @returns model id, or no value on error |
147 | 125 | */ |
148 | 126 | std::optional<ModelId> DocumentManager::openModel( |
127 | const QString& path, | |
128 | QTextStream& errorStream, | |
129 | const OpenType openType | |
130 | ) { | |
8
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
5
diff
changeset
|
131 | QFile file{path}; |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
5
diff
changeset
|
132 | const QString name = pathToName(path); |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
5
diff
changeset
|
133 | file.open(QFile::ReadOnly | QFile::Text); |
148 | 134 | std::unique_ptr<Model> newModel = std::make_unique<Model>(this); |
8
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
5
diff
changeset
|
135 | QTextStream textStream{&file}; |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
5
diff
changeset
|
136 | Parser parser{file}; |
152 | 137 | parser.parseBody(*newModel); |
148 | 138 | std::optional<ModelId> result; |
8
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
5
diff
changeset
|
139 | if (file.error() == QFile::NoError) |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
5
diff
changeset
|
140 | { |
148 | 141 | const ModelId modelId{++this->modelIdCounter}; |
150
b6cbba6e29a1
extract polygon cache out of Model
Teemu Piippo <teemu@hecknology.net>
parents:
148
diff
changeset
|
142 | this->openModels[modelId] = {std::move(newModel), modelId, path, openType}; |
b6cbba6e29a1
extract polygon cache out of Model
Teemu Piippo <teemu@hecknology.net>
parents:
148
diff
changeset
|
143 | this->makePolygonCacheForModel(modelId); |
148 | 144 | result = modelId; |
8
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
5
diff
changeset
|
145 | } |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
5
diff
changeset
|
146 | else |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
5
diff
changeset
|
147 | { |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
5
diff
changeset
|
148 | errorStream << file.errorString(); |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
5
diff
changeset
|
149 | } |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
5
diff
changeset
|
150 | return result; |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
5
diff
changeset
|
151 | } |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
5
diff
changeset
|
152 | |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
5
diff
changeset
|
153 | QString DocumentManager::makeNewModelName() |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
5
diff
changeset
|
154 | { |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
5
diff
changeset
|
155 | untitledNameCounter += 1; |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
5
diff
changeset
|
156 | return "untitled-" + QString::number(untitledNameCounter); |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
5
diff
changeset
|
157 | } |
12 | 158 | |
148 | 159 | void DocumentManager::loadDependenciesForAllModels(const LibraryManager& libraries, QTextStream& errorStream) |
160 | { | |
161 | for (const auto& modelInfoPair : this->openModels) | |
162 | { | |
163 | this->loadDependenciesForModel(modelInfoPair.first, modelInfoPair.second.path, libraries, errorStream); | |
164 | } | |
165 | } | |
166 | ||
167 | struct DocumentManager::LoadDepedenciesBag | |
168 | { | |
169 | const LibraryManager& libraries; | |
170 | QStringList missing; | |
171 | QSet<ModelId> processed; | |
172 | QTextStream& errorStream; | |
173 | }; | |
174 | ||
12 | 175 | void DocumentManager::loadDependenciesForModel( |
148 | 176 | const ModelId modelId, |
23
3387a84ddaba
fixed a pile of nonsense that caused subfiles to go haywire
Teemu Piippo <teemu@hecknology.net>
parents:
17
diff
changeset
|
177 | const QString& path, |
12 | 178 | const LibraryManager& libraries, |
179 | QTextStream& errorStream) | |
180 | { | |
148 | 181 | LoadDepedenciesBag bag { |
182 | .libraries = libraries, | |
183 | .missing = {}, | |
184 | .processed = {}, | |
185 | .errorStream = errorStream, | |
186 | }; | |
187 | this->loadDependenciesForModel(modelId, path, bag); | |
188 | if (not bag.missing.empty()) | |
12 | 189 | { |
148 | 190 | bag.missing.sort(Qt::CaseInsensitive); |
201
5d201ee4a9c3
Continue giant refactor
Teemu Piippo <teemu@hecknology.net>
parents:
200
diff
changeset
|
191 | errorStream << format( |
5d201ee4a9c3
Continue giant refactor
Teemu Piippo <teemu@hecknology.net>
parents:
200
diff
changeset
|
192 | tr("The following files could not be opened: %1"), |
148 | 193 | bag.missing.join(", ")); |
194 | } | |
195 | } | |
196 | ||
197 | void DocumentManager::closeDocument(const ModelId modelId) | |
198 | { | |
199 | ModelInfo* modelInfo = findInMap(this->openModels, modelId); | |
200 | if (modelInfo != nullptr) | |
201 | { | |
202 | modelInfo->opentype = OpenType::AutomaticallyOpened; | |
203 | this->prune(); | |
204 | } | |
205 | } | |
206 | ||
207 | const QString *DocumentManager::modelPath(ModelId modelId) const | |
208 | { | |
209 | const auto iterator = this->openModels.find(modelId); | |
210 | if (iterator != this->openModels.end()) | |
211 | { | |
212 | return &iterator->second.path; | |
213 | } | |
214 | else | |
215 | { | |
216 | return nullptr; | |
217 | } | |
218 | } | |
219 | ||
220 | /** | |
221 | * @brief Changes the path of the specified model. Since the name of the file may change, | |
222 | * changing the path can cause dependencies to be resolved differently. As such, dependencies | |
223 | * need to be resolved for all files after this operation. | |
224 | * @param modelId Model to change the path of | |
225 | * @param newPath New path | |
226 | * @param libraries Library manager for the purpose of dependency resolving | |
227 | * @param errorStream Where to write any errors regarding dependency resolving | |
228 | */ | |
229 | void DocumentManager::setModelPath( | |
230 | const ModelId modelId, | |
231 | const QString &newPath, | |
232 | const LibraryManager &libraries, | |
233 | QTextStream &errorStream) | |
234 | { | |
235 | auto modelInfoPair = this->openModels.find(modelId); | |
236 | if (true | |
237 | and modelInfoPair != this->openModels.end() | |
238 | and modelInfoPair->second.opentype == OpenType::ManuallyOpened | |
239 | ) { | |
240 | modelInfoPair->second.path = newPath; | |
241 | this->loadDependenciesForAllModels(libraries, errorStream); | |
12 | 242 | } |
243 | } | |
244 | ||
148 | 245 | bool DocumentManager::saveModel(const ModelId modelId, QTextStream &errors) |
147 | 246 | { |
148 | 247 | const QString* const path = this->modelPath(modelId); |
248 | if (path != nullptr) | |
147 | 249 | { |
148 | 250 | QSaveFile file{*path}; |
251 | file.setDirectWriteFallback(true); | |
252 | if (file.open(QSaveFile::WriteOnly)) | |
147 | 253 | { |
148 | 254 | // if path is not nullptr, getModelById will always return a value as well |
151 | 255 | ::save(*this->getModelById(modelId), &file); |
148 | 256 | const bool commitSucceeded = file.commit(); |
257 | if (not commitSucceeded) | |
258 | { | |
259 | errors << tr("Could not save: %1").arg(file.errorString()); | |
260 | return false; | |
261 | } | |
262 | else | |
263 | { | |
264 | return true; | |
265 | } | |
266 | } | |
267 | else | |
268 | { | |
269 | errors << tr("Could not open %1 for writing: %2") | |
270 | .arg(file.fileName()) | |
271 | .arg(file.errorString()); | |
272 | return false; | |
147 | 273 | } |
274 | } | |
148 | 275 | else |
276 | { | |
277 | errors << tr("Bad model ID %1").arg(modelId.value); | |
278 | return false; | |
279 | } | |
147 | 280 | } |
281 | ||
148 | 282 | /** |
283 | * @brief Searches the open models for the specified model and returns its id if found | |
284 | * @param model model to look for | |
285 | * @return id or no value if not found | |
286 | */ | |
287 | std::optional<ModelId> DocumentManager::findIdForModel(const Model *model) const | |
147 | 288 | { |
148 | 289 | std::optional<ModelId> result; |
290 | for (auto it = this->openModels.begin(); it != this->openModels.end(); ++it) | |
147 | 291 | { |
148 | 292 | if (it->second.model.get() == model) |
147 | 293 | { |
148 | 294 | result = it->first; |
295 | break; | |
296 | } | |
297 | } | |
298 | return result; | |
299 | } | |
300 | ||
150
b6cbba6e29a1
extract polygon cache out of Model
Teemu Piippo <teemu@hecknology.net>
parents:
148
diff
changeset
|
301 | PolygonCache *DocumentManager::getPolygonCacheForModel(ModelId modelId) |
b6cbba6e29a1
extract polygon cache out of Model
Teemu Piippo <teemu@hecknology.net>
parents:
148
diff
changeset
|
302 | { |
b6cbba6e29a1
extract polygon cache out of Model
Teemu Piippo <teemu@hecknology.net>
parents:
148
diff
changeset
|
303 | auto it = this->polygonCaches.find(modelId); |
b6cbba6e29a1
extract polygon cache out of Model
Teemu Piippo <teemu@hecknology.net>
parents:
148
diff
changeset
|
304 | if (it != this->polygonCaches.end()) |
b6cbba6e29a1
extract polygon cache out of Model
Teemu Piippo <teemu@hecknology.net>
parents:
148
diff
changeset
|
305 | { |
b6cbba6e29a1
extract polygon cache out of Model
Teemu Piippo <teemu@hecknology.net>
parents:
148
diff
changeset
|
306 | return &it->second; |
b6cbba6e29a1
extract polygon cache out of Model
Teemu Piippo <teemu@hecknology.net>
parents:
148
diff
changeset
|
307 | } |
b6cbba6e29a1
extract polygon cache out of Model
Teemu Piippo <teemu@hecknology.net>
parents:
148
diff
changeset
|
308 | else |
b6cbba6e29a1
extract polygon cache out of Model
Teemu Piippo <teemu@hecknology.net>
parents:
148
diff
changeset
|
309 | { |
b6cbba6e29a1
extract polygon cache out of Model
Teemu Piippo <teemu@hecknology.net>
parents:
148
diff
changeset
|
310 | return nullptr; |
b6cbba6e29a1
extract polygon cache out of Model
Teemu Piippo <teemu@hecknology.net>
parents:
148
diff
changeset
|
311 | } |
b6cbba6e29a1
extract polygon cache out of Model
Teemu Piippo <teemu@hecknology.net>
parents:
148
diff
changeset
|
312 | } |
b6cbba6e29a1
extract polygon cache out of Model
Teemu Piippo <teemu@hecknology.net>
parents:
148
diff
changeset
|
313 | |
148 | 314 | /** |
315 | * @brief Cleans up and erases models that are no longer required. | |
316 | */ | |
317 | void DocumentManager::prune() | |
318 | { | |
319 | for (auto it = this->openModels.begin(); it != this->openModels.end(); ++it) | |
320 | { | |
321 | // Find models that are not edited by the user and are not needed by any other model | |
322 | if (true | |
323 | and it->second.opentype == OpenType::AutomaticallyOpened | |
324 | and not this->isReferencedByAnything(it->first) | |
325 | ) { | |
150
b6cbba6e29a1
extract polygon cache out of Model
Teemu Piippo <teemu@hecknology.net>
parents:
148
diff
changeset
|
326 | // Remove its polygon cache |
b6cbba6e29a1
extract polygon cache out of Model
Teemu Piippo <teemu@hecknology.net>
parents:
148
diff
changeset
|
327 | const auto polygonCache = this->polygonCaches.find(it->first); |
b6cbba6e29a1
extract polygon cache out of Model
Teemu Piippo <teemu@hecknology.net>
parents:
148
diff
changeset
|
328 | if (polygonCache != this->polygonCaches.end()) |
b6cbba6e29a1
extract polygon cache out of Model
Teemu Piippo <teemu@hecknology.net>
parents:
148
diff
changeset
|
329 | { |
b6cbba6e29a1
extract polygon cache out of Model
Teemu Piippo <teemu@hecknology.net>
parents:
148
diff
changeset
|
330 | this->polygonCaches.erase(polygonCache); |
b6cbba6e29a1
extract polygon cache out of Model
Teemu Piippo <teemu@hecknology.net>
parents:
148
diff
changeset
|
331 | } |
148 | 332 | // Remove the model |
333 | this->openModels.erase(it); | |
334 | // We need to start over now. It is possible that other models that previously | |
335 | // were referenced by the model we just erased have become prunable. | |
336 | // Moreover, our iterator is invalid now and we cannot continue in this for loop. | |
337 | this->prune(); | |
338 | break; | |
339 | } | |
340 | } | |
341 | } | |
342 | ||
343 | /** | |
344 | * @brief Finds out whether the specified model id is referenced by any other model | |
345 | * @param modelId | |
346 | * @returns bool | |
347 | */ | |
348 | bool DocumentManager::isReferencedByAnything(const ModelId modelId) const | |
349 | { | |
350 | for (auto& haystackModelPair : this->openModels) | |
351 | { | |
352 | if (haystackModelPair.first != modelId) | |
353 | { | |
354 | for (auto& dependencyPair : haystackModelPair.second.dependencies) | |
147 | 355 | { |
148 | 356 | if (dependencyPair.second == modelId) |
357 | { | |
358 | return true; | |
359 | } | |
147 | 360 | } |
148 | 361 | } |
147 | 362 | } |
148 | 363 | return false; |
147 | 364 | } |
365 | ||
150
b6cbba6e29a1
extract polygon cache out of Model
Teemu Piippo <teemu@hecknology.net>
parents:
148
diff
changeset
|
366 | void DocumentManager::makePolygonCacheForModel(const ModelId modelId) |
b6cbba6e29a1
extract polygon cache out of Model
Teemu Piippo <teemu@hecknology.net>
parents:
148
diff
changeset
|
367 | { |
b6cbba6e29a1
extract polygon cache out of Model
Teemu Piippo <teemu@hecknology.net>
parents:
148
diff
changeset
|
368 | Model* model = this->getModelById(modelId); |
b6cbba6e29a1
extract polygon cache out of Model
Teemu Piippo <teemu@hecknology.net>
parents:
148
diff
changeset
|
369 | if (model != nullptr) |
b6cbba6e29a1
extract polygon cache out of Model
Teemu Piippo <teemu@hecknology.net>
parents:
148
diff
changeset
|
370 | { |
193 | 371 | this->polygonCaches[modelId] = {}; |
372 | connect(model, &Model::dataChanged, this, &DocumentManager::modelModified); | |
373 | connect(model, &Model::rowsInserted, this, &DocumentManager::modelModified); | |
374 | connect(model, &Model::rowsRemoved, this, &DocumentManager::modelModified); | |
375 | } | |
376 | } | |
377 | ||
378 | void DocumentManager::modelModified() | |
379 | { | |
195 | 380 | Model* const model = qobject_cast<Model*>(this->sender()); |
194 | 381 | const std::optional<ModelId> modelId = this->findIdForModel(model); |
382 | if (modelId.has_value()) { | |
383 | this->polygonCaches[*modelId].needRecache = true; | |
150
b6cbba6e29a1
extract polygon cache out of Model
Teemu Piippo <teemu@hecknology.net>
parents:
148
diff
changeset
|
384 | } |
b6cbba6e29a1
extract polygon cache out of Model
Teemu Piippo <teemu@hecknology.net>
parents:
148
diff
changeset
|
385 | } |
b6cbba6e29a1
extract polygon cache out of Model
Teemu Piippo <teemu@hecknology.net>
parents:
148
diff
changeset
|
386 | |
23
3387a84ddaba
fixed a pile of nonsense that caused subfiles to go haywire
Teemu Piippo <teemu@hecknology.net>
parents:
17
diff
changeset
|
387 | static QString findFile(QString referenceName, const QString& path, const LibraryManager& libraries) |
3387a84ddaba
fixed a pile of nonsense that caused subfiles to go haywire
Teemu Piippo <teemu@hecknology.net>
parents:
17
diff
changeset
|
388 | { |
3387a84ddaba
fixed a pile of nonsense that caused subfiles to go haywire
Teemu Piippo <teemu@hecknology.net>
parents:
17
diff
changeset
|
389 | // Try to find the file in the same place as the model itself |
3387a84ddaba
fixed a pile of nonsense that caused subfiles to go haywire
Teemu Piippo <teemu@hecknology.net>
parents:
17
diff
changeset
|
390 | referenceName.replace("\\", "/"); |
3387a84ddaba
fixed a pile of nonsense that caused subfiles to go haywire
Teemu Piippo <teemu@hecknology.net>
parents:
17
diff
changeset
|
391 | const QDir dir = QFileInfo{path}.dir(); |
3387a84ddaba
fixed a pile of nonsense that caused subfiles to go haywire
Teemu Piippo <teemu@hecknology.net>
parents:
17
diff
changeset
|
392 | QString referencedFilePath = dir.filePath(referenceName); |
3387a84ddaba
fixed a pile of nonsense that caused subfiles to go haywire
Teemu Piippo <teemu@hecknology.net>
parents:
17
diff
changeset
|
393 | if (not QFileInfo{referencedFilePath}.exists()) |
3387a84ddaba
fixed a pile of nonsense that caused subfiles to go haywire
Teemu Piippo <teemu@hecknology.net>
parents:
17
diff
changeset
|
394 | { |
3387a84ddaba
fixed a pile of nonsense that caused subfiles to go haywire
Teemu Piippo <teemu@hecknology.net>
parents:
17
diff
changeset
|
395 | // Look for it in the libraries |
3387a84ddaba
fixed a pile of nonsense that caused subfiles to go haywire
Teemu Piippo <teemu@hecknology.net>
parents:
17
diff
changeset
|
396 | referencedFilePath = libraries.findFile(referenceName); |
3387a84ddaba
fixed a pile of nonsense that caused subfiles to go haywire
Teemu Piippo <teemu@hecknology.net>
parents:
17
diff
changeset
|
397 | } |
3387a84ddaba
fixed a pile of nonsense that caused subfiles to go haywire
Teemu Piippo <teemu@hecknology.net>
parents:
17
diff
changeset
|
398 | return referencedFilePath; |
3387a84ddaba
fixed a pile of nonsense that caused subfiles to go haywire
Teemu Piippo <teemu@hecknology.net>
parents:
17
diff
changeset
|
399 | } |
3387a84ddaba
fixed a pile of nonsense that caused subfiles to go haywire
Teemu Piippo <teemu@hecknology.net>
parents:
17
diff
changeset
|
400 | |
200 | 401 | template<typename T> |
402 | void iterate(const Model& model, std::function<void(const T&)> fn) | |
403 | { | |
404 | for (int i = 0; i < model.size(); ++i) { | |
405 | if (std::holds_alternative<T>(model[i])) { | |
406 | fn(std::get<T>(model[i])); | |
407 | } | |
408 | } | |
409 | } | |
410 | ||
12 | 411 | void DocumentManager::loadDependenciesForModel( |
148 | 412 | const ModelId modelId, |
23
3387a84ddaba
fixed a pile of nonsense that caused subfiles to go haywire
Teemu Piippo <teemu@hecknology.net>
parents:
17
diff
changeset
|
413 | const QString &path, |
148 | 414 | LoadDepedenciesBag& bag) |
12 | 415 | { |
148 | 416 | QSet<QString> failedToOpen; |
12 | 417 | struct LoadingError |
418 | { | |
419 | QString message; | |
420 | }; | |
148 | 421 | bag.processed.insert(modelId); |
422 | if (not this->openModels.contains(modelId)) | |
12 | 423 | { |
148 | 424 | bag.errorStream << tr("bad model ID %1").arg(modelId.value); |
425 | return; | |
426 | } | |
427 | ModelInfo& modelInfo = this->openModels[modelId]; | |
428 | modelInfo.dependencies.clear(); | |
200 | 429 | iterate<Colored<SubfileReference>>(*modelInfo.model, [&](const SubfileReference& ref) { |
430 | const QString referenceName = ref.name; | |
12 | 431 | if (not referenceName.isEmpty() |
148 | 432 | and modelInfo.dependencies.count(referenceName) == 0 |
433 | and not failedToOpen.contains(referenceName)) | |
12 | 434 | { |
435 | try | |
436 | { | |
148 | 437 | const QString referencedFilePath = ::findFile(referenceName, path, bag.libraries); |
23
3387a84ddaba
fixed a pile of nonsense that caused subfiles to go haywire
Teemu Piippo <teemu@hecknology.net>
parents:
17
diff
changeset
|
438 | if (referencedFilePath.isEmpty()) |
12 | 439 | { |
148 | 440 | throw LoadingError{tr("could not find '%1'").arg(referenceName)}; |
12 | 441 | } |
148 | 442 | QString loadErrorString; |
443 | QTextStream localErrorStream{&loadErrorString}; | |
444 | const std::optional<ModelId> modelIdOpt = this->openModel( | |
147 | 445 | referencedFilePath, |
446 | localErrorStream, | |
447 | OpenType::AutomaticallyOpened); | |
148 | 448 | if (not modelIdOpt.has_value()) |
12 | 449 | { |
148 | 450 | const QString& errorMessage = tr("could not load '%1': %2") |
451 | .arg(referencedFilePath) | |
452 | .arg(loadErrorString); | |
453 | throw LoadingError{errorMessage}; | |
12 | 454 | } |
148 | 455 | modelInfo.dependencies[referenceName] = modelIdOpt.value(); |
456 | if (not bag.processed.contains(modelIdOpt.value())) | |
12 | 457 | { |
148 | 458 | this->loadDependenciesForModel(modelIdOpt.value(), referencedFilePath, bag); |
12 | 459 | } |
460 | } | |
461 | catch(const LoadingError& error) | |
462 | { | |
148 | 463 | bag.errorStream << error.message << "\n"; |
464 | failedToOpen.insert(referenceName); | |
465 | bag.missing.append(referenceName); | |
12 | 466 | } |
467 | } | |
200 | 468 | }); |
12 | 469 | } |