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