Sun, 09 Apr 2023 15:59:08 +0300
Extracted the state of the program into a MainState structure, and extracted local functions of main() into static functions.
I was planning to make the core logic and state of the program into a Main class, which would be a QObject that would
have lots of signals and slots, but it looks like this works even without it
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> |
335 | 23 | #include <QPlainTextDocumentLayout> |
213
ee5758ddb6d2
Rewrite prune to use graphs rather than O(n²) searches
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
212
diff
changeset
|
24 | #include <deque> |
264
76a025db4948
Convert all includes to be relative to project root directory. Files that cannot be found in this manner use angle brackets.
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
262
diff
changeset
|
25 | #include "src/documentmanager.h" |
76a025db4948
Convert all includes to be relative to project root directory. Files that cannot be found in this manner use angle brackets.
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
262
diff
changeset
|
26 | #include "src/parser.h" |
3 | 27 | |
214
8e1fe64ce4e3
begin refactor of gl side
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
213
diff
changeset
|
28 | DocumentManager::DocumentManager(QObject *parent) : |
8e1fe64ce4e3
begin refactor of gl side
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
213
diff
changeset
|
29 | QObject{parent} |
5 | 30 | { |
31 | } | |
32 | ||
335 | 33 | static std::unique_ptr<QTextDocument> newTextDocument() |
34 | { | |
35 | std::unique_ptr<QTextDocument> newModel = std::make_unique<QTextDocument>(nullptr); | |
36 | newModel->setDocumentLayout(new QPlainTextDocumentLayout{newModel.get()}); | |
37 | return newModel; | |
38 | } | |
39 | ||
8
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
5
diff
changeset
|
40 | /** |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
5
diff
changeset
|
41 | * @brief Creates a new model. |
148 | 42 | * @returns the ID of the new model |
8
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
5
diff
changeset
|
43 | */ |
148 | 44 | ModelId DocumentManager::newModel() |
8
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
5
diff
changeset
|
45 | { |
148 | 46 | const ModelId modelId{++this->modelIdCounter}; |
214
8e1fe64ce4e3
begin refactor of gl side
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
213
diff
changeset
|
47 | this->openModels.emplace(std::make_pair(modelId, ModelInfo{ |
335 | 48 | .model = newTextDocument(), |
214
8e1fe64ce4e3
begin refactor of gl side
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
213
diff
changeset
|
49 | .id = modelId, |
8e1fe64ce4e3
begin refactor of gl side
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
213
diff
changeset
|
50 | .opentype = OpenType::ManuallyOpened, |
8e1fe64ce4e3
begin refactor of gl side
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
213
diff
changeset
|
51 | })); |
150
b6cbba6e29a1
extract polygon cache out of Model
Teemu Piippo <teemu@hecknology.net>
parents:
148
diff
changeset
|
52 | this->makePolygonCacheForModel(modelId); |
235
7ef03c2b46ab
Add a basic message log
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
230
diff
changeset
|
53 | Q_EMIT this->message(logInfo(tr("New model %1 created").arg(modelId.value))); |
148 | 54 | return modelId; |
8
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 | |
338 | 57 | QTextDocument* 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); |
212
27259810da6d
Rewrite dependency loading
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
206
diff
changeset
|
60 | if (modelsIterator != std::end(this->openModels)) { |
148 | 61 | const auto& dependencies = modelsIterator->second.dependencies; |
62 | const auto dependenciesIterator = dependencies.find(name); | |
212
27259810da6d
Rewrite dependency loading
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
206
diff
changeset
|
63 | if (dependenciesIterator != dependencies.end()) { |
148 | 64 | ModelInfo& modelInfo = this->openModels[dependenciesIterator->second]; |
65 | return modelInfo.model.get(); | |
66 | } | |
212
27259810da6d
Rewrite dependency loading
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
206
diff
changeset
|
67 | else { |
148 | 68 | return nullptr; |
69 | } | |
70 | } | |
212
27259810da6d
Rewrite dependency loading
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
206
diff
changeset
|
71 | else { |
8
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
5
diff
changeset
|
72 | return nullptr; |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
5
diff
changeset
|
73 | } |
148 | 74 | } |
75 | ||
76 | /** | |
77 | * @brief Gets a model pointer by id or nullptr if not found | |
78 | * @param modelId id of model to find | |
79 | * @returns model pointer or null | |
80 | */ | |
338 | 81 | QTextDocument *DocumentManager::getModelById(ModelId modelId) |
148 | 82 | { |
83 | const auto iterator = this->openModels.find(modelId); | |
84 | if (iterator != this->openModels.end()) | |
85 | { | |
86 | return iterator->second.model.get(); | |
87 | } | |
8
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
5
diff
changeset
|
88 | else |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
5
diff
changeset
|
89 | { |
148 | 90 | return nullptr; |
8
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
5
diff
changeset
|
91 | } |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
5
diff
changeset
|
92 | } |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
5
diff
changeset
|
93 | |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
5
diff
changeset
|
94 | QString pathToName(const QFileInfo& path) |
3 | 95 | { |
8
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
5
diff
changeset
|
96 | static const char* paths[] = { |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
5
diff
changeset
|
97 | "s", |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
5
diff
changeset
|
98 | "48" |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
5
diff
changeset
|
99 | "8" |
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 | const QString baseName = path.fileName(); |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
5
diff
changeset
|
102 | const QString dirName = QFileInfo{path.dir().path()}.fileName(); |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
5
diff
changeset
|
103 | QString result; |
17 | 104 | 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
|
105 | { |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
5
diff
changeset
|
106 | result = dirName + "\\" + baseName; |
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 | else |
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 = 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 | return result; |
3 | 113 | } |
8
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
5
diff
changeset
|
114 | |
147 | 115 | /** |
116 | * @brief Tries to open the model at the specified path | |
117 | * @param path Path to the model to open | |
118 | * @param errorStream Where to write any errors | |
119 | * @param openType rationale behind opening this file | |
148 | 120 | * @returns model id, or no value on error |
147 | 121 | */ |
148 | 122 | std::optional<ModelId> DocumentManager::openModel( |
123 | const QString& path, | |
124 | QTextStream& errorStream, | |
125 | const OpenType openType | |
126 | ) { | |
8
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
5
diff
changeset
|
127 | QFile file{path}; |
259
c27612f0eac0
- Made it build under Qt6
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
235
diff
changeset
|
128 | const QString name = pathToName(QFileInfo{path}); |
8
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
5
diff
changeset
|
129 | file.open(QFile::ReadOnly | QFile::Text); |
335 | 130 | std::unique_ptr<QTextDocument> newModel = newTextDocument(); |
333
07e65a4c6611
Experiment to delete the Model class and rely solely on text documents
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
329
diff
changeset
|
131 | newModel->setPlainText(file.readAll()); |
148 | 132 | std::optional<ModelId> result; |
8
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
5
diff
changeset
|
133 | if (file.error() == QFile::NoError) |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
5
diff
changeset
|
134 | { |
148 | 135 | const ModelId modelId{++this->modelIdCounter}; |
214
8e1fe64ce4e3
begin refactor of gl side
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
213
diff
changeset
|
136 | this->openModels.emplace(std::make_pair(modelId, ModelInfo{ |
212
27259810da6d
Rewrite dependency loading
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
206
diff
changeset
|
137 | .model = std::move(newModel), |
27259810da6d
Rewrite dependency loading
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
206
diff
changeset
|
138 | .id = modelId, |
27259810da6d
Rewrite dependency loading
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
206
diff
changeset
|
139 | .path = path, |
27259810da6d
Rewrite dependency loading
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
206
diff
changeset
|
140 | .opentype = openType, |
27259810da6d
Rewrite dependency loading
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
206
diff
changeset
|
141 | .polygonCache = {}, |
214
8e1fe64ce4e3
begin refactor of gl side
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
213
diff
changeset
|
142 | })); |
150
b6cbba6e29a1
extract polygon cache out of Model
Teemu Piippo <teemu@hecknology.net>
parents:
148
diff
changeset
|
143 | this->makePolygonCacheForModel(modelId); |
148 | 144 | result = modelId; |
235
7ef03c2b46ab
Add a basic message log
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
230
diff
changeset
|
145 | Q_EMIT this->message(logInfo(tr("Opened %1 as model %2").arg(quoted(path)).arg(modelId.value))); |
8
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
5
diff
changeset
|
146 | } |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
5
diff
changeset
|
147 | else |
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 | errorStream << file.errorString(); |
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 | return result; |
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 | |
148 | 154 | void DocumentManager::closeDocument(const ModelId modelId) |
155 | { | |
156 | ModelInfo* modelInfo = findInMap(this->openModels, modelId); | |
157 | if (modelInfo != nullptr) | |
158 | { | |
159 | modelInfo->opentype = OpenType::AutomaticallyOpened; | |
160 | this->prune(); | |
161 | } | |
162 | } | |
163 | ||
164 | const QString *DocumentManager::modelPath(ModelId modelId) const | |
165 | { | |
166 | const auto iterator = this->openModels.find(modelId); | |
167 | if (iterator != this->openModels.end()) | |
168 | { | |
169 | return &iterator->second.path; | |
170 | } | |
171 | else | |
172 | { | |
173 | return nullptr; | |
174 | } | |
175 | } | |
176 | ||
212
27259810da6d
Rewrite dependency loading
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
206
diff
changeset
|
177 | //! \brief Changes the path of the specified model. This can cause dependencies |
27259810da6d
Rewrite dependency loading
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
206
diff
changeset
|
178 | //! to be resolved differently. As such, dependencies need to be resolved for |
27259810da6d
Rewrite dependency loading
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
206
diff
changeset
|
179 | //! all files after this operation. |
148 | 180 | void DocumentManager::setModelPath( |
181 | const ModelId modelId, | |
182 | const QString &newPath, | |
230
a1f3f7d9078b
rename LibraryManager -> LibrariesModel
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
217
diff
changeset
|
183 | const LibrariesModel &libraries, |
148 | 184 | QTextStream &errorStream) |
185 | { | |
212
27259810da6d
Rewrite dependency loading
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
206
diff
changeset
|
186 | ModelInfo* info = findInMap(this->openModels, modelId); |
27259810da6d
Rewrite dependency loading
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
206
diff
changeset
|
187 | if (info != nullptr and info->opentype == OpenType::ManuallyOpened) { |
27259810da6d
Rewrite dependency loading
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
206
diff
changeset
|
188 | info->path = newPath; |
27259810da6d
Rewrite dependency loading
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
206
diff
changeset
|
189 | const MissingDependencies missing = this->loadDependenciesForAllModels(libraries); |
27259810da6d
Rewrite dependency loading
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
206
diff
changeset
|
190 | if (not missing.empty()) { |
27259810da6d
Rewrite dependency loading
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
206
diff
changeset
|
191 | errorStream << errorStringFromMissingDependencies(missing); |
27259810da6d
Rewrite dependency loading
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
206
diff
changeset
|
192 | } |
12 | 193 | } |
194 | } | |
195 | ||
148 | 196 | bool DocumentManager::saveModel(const ModelId modelId, QTextStream &errors) |
147 | 197 | { |
212
27259810da6d
Rewrite dependency loading
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
206
diff
changeset
|
198 | ModelInfo* info = findInMap(this->openModels, modelId); |
27259810da6d
Rewrite dependency loading
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
206
diff
changeset
|
199 | if (info != nullptr) |
147 | 200 | { |
212
27259810da6d
Rewrite dependency loading
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
206
diff
changeset
|
201 | QSaveFile file{info->path}; |
148 | 202 | file.setDirectWriteFallback(true); |
212
27259810da6d
Rewrite dependency loading
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
206
diff
changeset
|
203 | if (file.open(QSaveFile::WriteOnly)) { |
334
083b0d8e220a
Convert text model to CR+LF when saving
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
333
diff
changeset
|
204 | file.write(info->model->toPlainText().replace("\n", "\r\n").toUtf8()); |
148 | 205 | const bool commitSucceeded = file.commit(); |
212
27259810da6d
Rewrite dependency loading
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
206
diff
changeset
|
206 | if (not commitSucceeded) { |
27259810da6d
Rewrite dependency loading
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
206
diff
changeset
|
207 | errors << QObject::tr("Could not save: %1").arg(file.errorString()); |
148 | 208 | return false; |
209 | } | |
212
27259810da6d
Rewrite dependency loading
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
206
diff
changeset
|
210 | else { |
148 | 211 | return true; |
212 | } | |
213 | } | |
212
27259810da6d
Rewrite dependency loading
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
206
diff
changeset
|
214 | else { |
27259810da6d
Rewrite dependency loading
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
206
diff
changeset
|
215 | errors << QObject::tr("Could not open %1 for writing: %2") |
206
654661eab7f3
More refactor, merged main.h, basics.h and utility.h into one header file basics.h and removed plenty of unused code
Teemu Piippo <teemu@hecknology.net>
parents:
201
diff
changeset
|
216 | .arg(file.fileName(), file.errorString()); |
148 | 217 | return false; |
147 | 218 | } |
219 | } | |
212
27259810da6d
Rewrite dependency loading
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
206
diff
changeset
|
220 | else { |
27259810da6d
Rewrite dependency loading
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
206
diff
changeset
|
221 | errors << QObject::tr("Bad model ID %1").arg(modelId.value); |
148 | 222 | return false; |
223 | } | |
147 | 224 | } |
225 | ||
148 | 226 | /** |
227 | * @brief Searches the open models for the specified model and returns its id if found | |
228 | * @param model model to look for | |
229 | * @return id or no value if not found | |
230 | */ | |
338 | 231 | std::optional<ModelId> DocumentManager::findIdForModel(const QTextDocument *model) const |
147 | 232 | { |
148 | 233 | std::optional<ModelId> result; |
234 | for (auto it = this->openModels.begin(); it != this->openModels.end(); ++it) | |
147 | 235 | { |
148 | 236 | if (it->second.model.get() == model) |
147 | 237 | { |
148 | 238 | result = it->first; |
239 | break; | |
240 | } | |
241 | } | |
242 | return result; | |
243 | } | |
244 | ||
150
b6cbba6e29a1
extract polygon cache out of Model
Teemu Piippo <teemu@hecknology.net>
parents:
148
diff
changeset
|
245 | PolygonCache *DocumentManager::getPolygonCacheForModel(ModelId modelId) |
b6cbba6e29a1
extract polygon cache out of Model
Teemu Piippo <teemu@hecknology.net>
parents:
148
diff
changeset
|
246 | { |
212
27259810da6d
Rewrite dependency loading
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
206
diff
changeset
|
247 | ModelInfo* info = findInMap(this->openModels, modelId); |
27259810da6d
Rewrite dependency loading
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
206
diff
changeset
|
248 | if (info != nullptr) { |
27259810da6d
Rewrite dependency loading
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
206
diff
changeset
|
249 | return &info->polygonCache; |
150
b6cbba6e29a1
extract polygon cache out of Model
Teemu Piippo <teemu@hecknology.net>
parents:
148
diff
changeset
|
250 | } |
212
27259810da6d
Rewrite dependency loading
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
206
diff
changeset
|
251 | else { |
150
b6cbba6e29a1
extract polygon cache out of Model
Teemu Piippo <teemu@hecknology.net>
parents:
148
diff
changeset
|
252 | return nullptr; |
b6cbba6e29a1
extract polygon cache out of Model
Teemu Piippo <teemu@hecknology.net>
parents:
148
diff
changeset
|
253 | } |
b6cbba6e29a1
extract polygon cache out of Model
Teemu Piippo <teemu@hecknology.net>
parents:
148
diff
changeset
|
254 | } |
b6cbba6e29a1
extract polygon cache out of Model
Teemu Piippo <teemu@hecknology.net>
parents:
148
diff
changeset
|
255 | |
214
8e1fe64ce4e3
begin refactor of gl side
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
213
diff
changeset
|
256 | const DocumentManager::ModelInfo *DocumentManager::find(ModelId modelId) const |
212
27259810da6d
Rewrite dependency loading
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
206
diff
changeset
|
257 | { |
27259810da6d
Rewrite dependency loading
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
206
diff
changeset
|
258 | return findInMap(this->openModels, modelId); |
27259810da6d
Rewrite dependency loading
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
206
diff
changeset
|
259 | } |
27259810da6d
Rewrite dependency loading
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
206
diff
changeset
|
260 | |
214
8e1fe64ce4e3
begin refactor of gl side
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
213
diff
changeset
|
261 | void DocumentManager::setModelPayload(ModelId modelId, QObject *object) |
8e1fe64ce4e3
begin refactor of gl side
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
213
diff
changeset
|
262 | { |
8e1fe64ce4e3
begin refactor of gl side
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
213
diff
changeset
|
263 | ModelInfo* info = findInMap(this->openModels, modelId); |
8e1fe64ce4e3
begin refactor of gl side
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
213
diff
changeset
|
264 | if (info != nullptr) { |
8e1fe64ce4e3
begin refactor of gl side
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
213
diff
changeset
|
265 | info->payload = object; |
8e1fe64ce4e3
begin refactor of gl side
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
213
diff
changeset
|
266 | object->setParent(this); |
8e1fe64ce4e3
begin refactor of gl side
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
213
diff
changeset
|
267 | } |
8e1fe64ce4e3
begin refactor of gl side
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
213
diff
changeset
|
268 | } |
8e1fe64ce4e3
begin refactor of gl side
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
213
diff
changeset
|
269 | |
212
27259810da6d
Rewrite dependency loading
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
206
diff
changeset
|
270 | QString errorStringFromMissingDependencies(const DocumentManager::MissingDependencies& missing) |
27259810da6d
Rewrite dependency loading
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
206
diff
changeset
|
271 | { |
27259810da6d
Rewrite dependency loading
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
206
diff
changeset
|
272 | QString missingString; |
27259810da6d
Rewrite dependency loading
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
206
diff
changeset
|
273 | forValueInMap(missing, [&missingString](const QString& path){ |
27259810da6d
Rewrite dependency loading
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
206
diff
changeset
|
274 | missingString = joined(missingString, QStringLiteral(", "), path); |
27259810da6d
Rewrite dependency loading
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
206
diff
changeset
|
275 | }); |
27259810da6d
Rewrite dependency loading
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
206
diff
changeset
|
276 | return QObject::tr("The following files could not be opened: %1") |
27259810da6d
Rewrite dependency loading
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
206
diff
changeset
|
277 | .arg(missingString); |
27259810da6d
Rewrite dependency loading
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
206
diff
changeset
|
278 | } |
27259810da6d
Rewrite dependency loading
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
206
diff
changeset
|
279 | |
213
ee5758ddb6d2
Rewrite prune to use graphs rather than O(n²) searches
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
212
diff
changeset
|
280 | template<typename T, typename K> |
ee5758ddb6d2
Rewrite prune to use graphs rather than O(n²) searches
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
212
diff
changeset
|
281 | void removeFromSet(std::set<T>& set, K&& valueToRemove) |
148 | 282 | { |
213
ee5758ddb6d2
Rewrite prune to use graphs rather than O(n²) searches
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
212
diff
changeset
|
283 | const auto it = std::lower_bound(set.begin(), set.end(), valueToRemove); |
ee5758ddb6d2
Rewrite prune to use graphs rather than O(n²) searches
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
212
diff
changeset
|
284 | if (it != set.end() and *it == valueToRemove) { |
ee5758ddb6d2
Rewrite prune to use graphs rather than O(n²) searches
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
212
diff
changeset
|
285 | set.erase(it); |
148 | 286 | } |
287 | } | |
288 | ||
213
ee5758ddb6d2
Rewrite prune to use graphs rather than O(n²) searches
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
212
diff
changeset
|
289 | //! @brief Cleans up and erases models that are no longer required. |
ee5758ddb6d2
Rewrite prune to use graphs rather than O(n²) searches
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
212
diff
changeset
|
290 | void DocumentManager::prune() |
148 | 291 | { |
213
ee5758ddb6d2
Rewrite prune to use graphs rather than O(n²) searches
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
212
diff
changeset
|
292 | Graph<ModelId> dependencyGraph; |
ee5758ddb6d2
Rewrite prune to use graphs rather than O(n²) searches
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
212
diff
changeset
|
293 | forValueInMap(this->openModels, [&dependencyGraph](const ModelInfo& info) { |
ee5758ddb6d2
Rewrite prune to use graphs rather than O(n²) searches
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
212
diff
changeset
|
294 | forValueInMap(info.dependencies, [&dependencyGraph, &info](ModelId dep){ |
ee5758ddb6d2
Rewrite prune to use graphs rather than O(n²) searches
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
212
diff
changeset
|
295 | dependencyGraph.push_back({.from = info.id, .to = dep}); |
ee5758ddb6d2
Rewrite prune to use graphs rather than O(n²) searches
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
212
diff
changeset
|
296 | }); |
ee5758ddb6d2
Rewrite prune to use graphs rather than O(n²) searches
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
212
diff
changeset
|
297 | }); |
ee5758ddb6d2
Rewrite prune to use graphs rather than O(n²) searches
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
212
diff
changeset
|
298 | std::set<ModelId> autoOpened; |
ee5758ddb6d2
Rewrite prune to use graphs rather than O(n²) searches
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
212
diff
changeset
|
299 | forValueInMap(this->openModels, [&autoOpened](const ModelInfo& info) { |
ee5758ddb6d2
Rewrite prune to use graphs rather than O(n²) searches
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
212
diff
changeset
|
300 | if (info.opentype == OpenType::AutomaticallyOpened) { |
ee5758ddb6d2
Rewrite prune to use graphs rather than O(n²) searches
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
212
diff
changeset
|
301 | autoOpened.insert(info.id); |
ee5758ddb6d2
Rewrite prune to use graphs rather than O(n²) searches
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
212
diff
changeset
|
302 | } |
ee5758ddb6d2
Rewrite prune to use graphs rather than O(n²) searches
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
212
diff
changeset
|
303 | }); |
ee5758ddb6d2
Rewrite prune to use graphs rather than O(n²) searches
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
212
diff
changeset
|
304 | bool repeat = true; |
ee5758ddb6d2
Rewrite prune to use graphs rather than O(n²) searches
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
212
diff
changeset
|
305 | while (repeat) { |
ee5758ddb6d2
Rewrite prune to use graphs rather than O(n²) searches
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
212
diff
changeset
|
306 | repeat = false; |
ee5758ddb6d2
Rewrite prune to use graphs rather than O(n²) searches
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
212
diff
changeset
|
307 | std::set<ModelId> prunable = autoOpened; |
ee5758ddb6d2
Rewrite prune to use graphs rather than O(n²) searches
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
212
diff
changeset
|
308 | for (const auto& pair : dependencyGraph) { |
ee5758ddb6d2
Rewrite prune to use graphs rather than O(n²) searches
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
212
diff
changeset
|
309 | removeFromSet(prunable, pair.to); |
ee5758ddb6d2
Rewrite prune to use graphs rather than O(n²) searches
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
212
diff
changeset
|
310 | } |
ee5758ddb6d2
Rewrite prune to use graphs rather than O(n²) searches
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
212
diff
changeset
|
311 | for (ModelId idToPrune : prunable) { |
ee5758ddb6d2
Rewrite prune to use graphs rather than O(n²) searches
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
212
diff
changeset
|
312 | auto it = this->openModels.find(idToPrune); |
ee5758ddb6d2
Rewrite prune to use graphs rather than O(n²) searches
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
212
diff
changeset
|
313 | if (it != this->openModels.end()) { |
235
7ef03c2b46ab
Add a basic message log
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
230
diff
changeset
|
314 | Q_EMIT this->message(logInfo(tr("Model %1 (%2) pruned").arg(idToPrune.value).arg(it->second.path))); |
213
ee5758ddb6d2
Rewrite prune to use graphs rather than O(n²) searches
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
212
diff
changeset
|
315 | this->openModels.erase(it); |
147 | 316 | } |
213
ee5758ddb6d2
Rewrite prune to use graphs rather than O(n²) searches
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
212
diff
changeset
|
317 | removeFromSet(autoOpened, idToPrune); |
ee5758ddb6d2
Rewrite prune to use graphs rather than O(n²) searches
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
212
diff
changeset
|
318 | std::erase_if(dependencyGraph, [&idToPrune](const GraphEdge<ModelId>& edge) { |
ee5758ddb6d2
Rewrite prune to use graphs rather than O(n²) searches
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
212
diff
changeset
|
319 | return edge.from == idToPrune; |
ee5758ddb6d2
Rewrite prune to use graphs rather than O(n²) searches
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
212
diff
changeset
|
320 | }); |
ee5758ddb6d2
Rewrite prune to use graphs rather than O(n²) searches
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
212
diff
changeset
|
321 | repeat = true; |
148 | 322 | } |
147 | 323 | } |
324 | } | |
325 | ||
150
b6cbba6e29a1
extract polygon cache out of Model
Teemu Piippo <teemu@hecknology.net>
parents:
148
diff
changeset
|
326 | void DocumentManager::makePolygonCacheForModel(const ModelId modelId) |
b6cbba6e29a1
extract polygon cache out of Model
Teemu Piippo <teemu@hecknology.net>
parents:
148
diff
changeset
|
327 | { |
338 | 328 | QTextDocument* model = this->getModelById(modelId); |
150
b6cbba6e29a1
extract polygon cache out of Model
Teemu Piippo <teemu@hecknology.net>
parents:
148
diff
changeset
|
329 | if (model != nullptr) |
b6cbba6e29a1
extract polygon cache out of Model
Teemu Piippo <teemu@hecknology.net>
parents:
148
diff
changeset
|
330 | { |
212
27259810da6d
Rewrite dependency loading
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
206
diff
changeset
|
331 | const auto modelModified = [this, model]{ |
27259810da6d
Rewrite dependency loading
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
206
diff
changeset
|
332 | const std::optional<ModelId> modelId = this->findIdForModel(model); |
27259810da6d
Rewrite dependency loading
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
206
diff
changeset
|
333 | if (modelId.has_value()) { |
27259810da6d
Rewrite dependency loading
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
206
diff
changeset
|
334 | ModelInfo* info = findInMap(this->openModels, *modelId); |
27259810da6d
Rewrite dependency loading
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
206
diff
changeset
|
335 | if (info != nullptr) { |
27259810da6d
Rewrite dependency loading
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
206
diff
changeset
|
336 | info->polygonCache.needRecache = true; |
27259810da6d
Rewrite dependency loading
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
206
diff
changeset
|
337 | } |
27259810da6d
Rewrite dependency loading
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
206
diff
changeset
|
338 | } |
27259810da6d
Rewrite dependency loading
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
206
diff
changeset
|
339 | }; |
333
07e65a4c6611
Experiment to delete the Model class and rely solely on text documents
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
329
diff
changeset
|
340 | QObject::connect(model, &QTextDocument::contentsChanged, modelModified); |
193 | 341 | } |
342 | } | |
343 | ||
212
27259810da6d
Rewrite dependency loading
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
206
diff
changeset
|
344 | static QString findFile( |
27259810da6d
Rewrite dependency loading
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
206
diff
changeset
|
345 | QString referenceName, |
27259810da6d
Rewrite dependency loading
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
206
diff
changeset
|
346 | const QString& modelPath, |
230
a1f3f7d9078b
rename LibraryManager -> LibrariesModel
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
217
diff
changeset
|
347 | const LibrariesModel& libraries) |
23
3387a84ddaba
fixed a pile of nonsense that caused subfiles to go haywire
Teemu Piippo <teemu@hecknology.net>
parents:
17
diff
changeset
|
348 | { |
3387a84ddaba
fixed a pile of nonsense that caused subfiles to go haywire
Teemu Piippo <teemu@hecknology.net>
parents:
17
diff
changeset
|
349 | // 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
|
350 | referenceName.replace("\\", "/"); |
212
27259810da6d
Rewrite dependency loading
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
206
diff
changeset
|
351 | const QDir dir = QFileInfo{modelPath}.dir(); |
23
3387a84ddaba
fixed a pile of nonsense that caused subfiles to go haywire
Teemu Piippo <teemu@hecknology.net>
parents:
17
diff
changeset
|
352 | 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
|
353 | 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
|
354 | { |
3387a84ddaba
fixed a pile of nonsense that caused subfiles to go haywire
Teemu Piippo <teemu@hecknology.net>
parents:
17
diff
changeset
|
355 | // 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
|
356 | 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
|
357 | } |
3387a84ddaba
fixed a pile of nonsense that caused subfiles to go haywire
Teemu Piippo <teemu@hecknology.net>
parents:
17
diff
changeset
|
358 | return referencedFilePath; |
3387a84ddaba
fixed a pile of nonsense that caused subfiles to go haywire
Teemu Piippo <teemu@hecknology.net>
parents:
17
diff
changeset
|
359 | } |
3387a84ddaba
fixed a pile of nonsense that caused subfiles to go haywire
Teemu Piippo <teemu@hecknology.net>
parents:
17
diff
changeset
|
360 | |
338 | 361 | static std::set<QString> referenceNames(const QTextDocument* model) |
212
27259810da6d
Rewrite dependency loading
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
206
diff
changeset
|
362 | { |
27259810da6d
Rewrite dependency loading
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
206
diff
changeset
|
363 | std::set<QString> result; |
333
07e65a4c6611
Experiment to delete the Model class and rely solely on text documents
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
329
diff
changeset
|
364 | for (const QString& line : model->toPlainText().split("\n")) { |
07e65a4c6611
Experiment to delete the Model class and rely solely on text documents
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
329
diff
changeset
|
365 | const opt<ParsedLine> parsed = parse(line); |
07e65a4c6611
Experiment to delete the Model class and rely solely on text documents
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
329
diff
changeset
|
366 | if (parsed.has_value() and std::holds_alternative<LineType1>(*parsed)) { |
07e65a4c6611
Experiment to delete the Model class and rely solely on text documents
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
329
diff
changeset
|
367 | result.insert(std::get<LineType1>(*parsed).value.name); |
07e65a4c6611
Experiment to delete the Model class and rely solely on text documents
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
329
diff
changeset
|
368 | } |
07e65a4c6611
Experiment to delete the Model class and rely solely on text documents
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
329
diff
changeset
|
369 | } |
212
27259810da6d
Rewrite dependency loading
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
206
diff
changeset
|
370 | return result; |
27259810da6d
Rewrite dependency loading
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
206
diff
changeset
|
371 | } |
27259810da6d
Rewrite dependency loading
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
206
diff
changeset
|
372 | |
27259810da6d
Rewrite dependency loading
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
206
diff
changeset
|
373 | struct Dependency |
27259810da6d
Rewrite dependency loading
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
206
diff
changeset
|
374 | { |
27259810da6d
Rewrite dependency loading
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
206
diff
changeset
|
375 | QString name; |
27259810da6d
Rewrite dependency loading
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
206
diff
changeset
|
376 | QString path; |
27259810da6d
Rewrite dependency loading
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
206
diff
changeset
|
377 | bool operator<(const Dependency& other) const |
27259810da6d
Rewrite dependency loading
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
206
diff
changeset
|
378 | { |
27259810da6d
Rewrite dependency loading
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
206
diff
changeset
|
379 | if (this->name != other.name) { |
27259810da6d
Rewrite dependency loading
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
206
diff
changeset
|
380 | return this->name < other.name; |
27259810da6d
Rewrite dependency loading
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
206
diff
changeset
|
381 | } |
27259810da6d
Rewrite dependency loading
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
206
diff
changeset
|
382 | else { |
27259810da6d
Rewrite dependency loading
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
206
diff
changeset
|
383 | return this->path < other.path; |
27259810da6d
Rewrite dependency loading
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
206
diff
changeset
|
384 | } |
27259810da6d
Rewrite dependency loading
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
206
diff
changeset
|
385 | } |
27259810da6d
Rewrite dependency loading
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
206
diff
changeset
|
386 | }; |
27259810da6d
Rewrite dependency loading
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
206
diff
changeset
|
387 | |
27259810da6d
Rewrite dependency loading
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
206
diff
changeset
|
388 | static std::set<Dependency> resolveReferencePaths( |
27259810da6d
Rewrite dependency loading
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
206
diff
changeset
|
389 | const DocumentManager::ModelInfo* modelInfo, |
230
a1f3f7d9078b
rename LibraryManager -> LibrariesModel
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
217
diff
changeset
|
390 | const LibrariesModel* libraries) |
200 | 391 | { |
212
27259810da6d
Rewrite dependency loading
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
206
diff
changeset
|
392 | std::set<Dependency> result; |
27259810da6d
Rewrite dependency loading
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
206
diff
changeset
|
393 | const std::set<QString> refNames = referenceNames(modelInfo->model.get()); |
27259810da6d
Rewrite dependency loading
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
206
diff
changeset
|
394 | if (modelInfo != nullptr) { |
27259810da6d
Rewrite dependency loading
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
206
diff
changeset
|
395 | for (const QString& name : refNames) { |
27259810da6d
Rewrite dependency loading
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
206
diff
changeset
|
396 | const QString path = findFile(name, modelInfo->path, *libraries); |
27259810da6d
Rewrite dependency loading
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
206
diff
changeset
|
397 | if (not path.isEmpty()) { |
27259810da6d
Rewrite dependency loading
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
206
diff
changeset
|
398 | result.insert(Dependency{.name = name, .path = path}); |
27259810da6d
Rewrite dependency loading
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
206
diff
changeset
|
399 | } |
27259810da6d
Rewrite dependency loading
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
206
diff
changeset
|
400 | } |
27259810da6d
Rewrite dependency loading
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
206
diff
changeset
|
401 | } |
27259810da6d
Rewrite dependency loading
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
206
diff
changeset
|
402 | return result; |
27259810da6d
Rewrite dependency loading
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
206
diff
changeset
|
403 | } |
27259810da6d
Rewrite dependency loading
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
206
diff
changeset
|
404 | |
27259810da6d
Rewrite dependency loading
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
206
diff
changeset
|
405 | static void loadDependenciesForModel( |
27259810da6d
Rewrite dependency loading
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
206
diff
changeset
|
406 | DocumentManager::ModelInfo* info, |
27259810da6d
Rewrite dependency loading
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
206
diff
changeset
|
407 | DocumentManager* documents, |
230
a1f3f7d9078b
rename LibraryManager -> LibrariesModel
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
217
diff
changeset
|
408 | const LibrariesModel* libraries, |
212
27259810da6d
Rewrite dependency loading
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
206
diff
changeset
|
409 | std::map<QString, QString>& missing) |
27259810da6d
Rewrite dependency loading
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
206
diff
changeset
|
410 | { |
27259810da6d
Rewrite dependency loading
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
206
diff
changeset
|
411 | bool repeat = true; |
329
6d75fa09cc0c
fix some issues like subfile references not showing up properly
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
328
diff
changeset
|
412 | const auto olddeps = info->dependencies; |
212
27259810da6d
Rewrite dependency loading
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
206
diff
changeset
|
413 | info->dependencies.clear(); |
27259810da6d
Rewrite dependency loading
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
206
diff
changeset
|
414 | while (repeat) { |
27259810da6d
Rewrite dependency loading
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
206
diff
changeset
|
415 | repeat = false; |
27259810da6d
Rewrite dependency loading
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
206
diff
changeset
|
416 | const std::set<Dependency> dependencies = resolveReferencePaths(info, libraries); |
27259810da6d
Rewrite dependency loading
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
206
diff
changeset
|
417 | for (const Dependency& dep : dependencies) { |
329
6d75fa09cc0c
fix some issues like subfile references not showing up properly
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
328
diff
changeset
|
418 | const ModelId* const idp = findInMap(olddeps, dep.name); |
6d75fa09cc0c
fix some issues like subfile references not showing up properly
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
328
diff
changeset
|
419 | if (idp != nullptr) { |
6d75fa09cc0c
fix some issues like subfile references not showing up properly
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
328
diff
changeset
|
420 | info->dependencies[dep.name] = *idp; |
6d75fa09cc0c
fix some issues like subfile references not showing up properly
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
328
diff
changeset
|
421 | } |
6d75fa09cc0c
fix some issues like subfile references not showing up properly
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
328
diff
changeset
|
422 | else if (not info->dependencies.contains(dep.name) and not missing.contains(dep.path)) { |
212
27259810da6d
Rewrite dependency loading
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
206
diff
changeset
|
423 | QString loadErrorString; |
27259810da6d
Rewrite dependency loading
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
206
diff
changeset
|
424 | QTextStream localErrorStream{&loadErrorString}; |
27259810da6d
Rewrite dependency loading
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
206
diff
changeset
|
425 | const std::optional<ModelId> modelIdOpt = documents->openModel( |
27259810da6d
Rewrite dependency loading
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
206
diff
changeset
|
426 | dep.path, |
27259810da6d
Rewrite dependency loading
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
206
diff
changeset
|
427 | localErrorStream, |
27259810da6d
Rewrite dependency loading
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
206
diff
changeset
|
428 | OpenType::AutomaticallyOpened); |
27259810da6d
Rewrite dependency loading
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
206
diff
changeset
|
429 | if (not modelIdOpt.has_value()) { |
27259810da6d
Rewrite dependency loading
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
206
diff
changeset
|
430 | const QString& errorMessage = QObject::tr("could not load '%1': %2") |
329
6d75fa09cc0c
fix some issues like subfile references not showing up properly
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
328
diff
changeset
|
431 | .arg(dep.path, loadErrorString); |
212
27259810da6d
Rewrite dependency loading
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
206
diff
changeset
|
432 | missing[dep.path] = errorMessage; |
27259810da6d
Rewrite dependency loading
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
206
diff
changeset
|
433 | } |
27259810da6d
Rewrite dependency loading
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
206
diff
changeset
|
434 | else { |
27259810da6d
Rewrite dependency loading
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
206
diff
changeset
|
435 | info->dependencies[dep.name] = modelIdOpt.value(); |
27259810da6d
Rewrite dependency loading
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
206
diff
changeset
|
436 | repeat = true; |
27259810da6d
Rewrite dependency loading
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
206
diff
changeset
|
437 | } |
27259810da6d
Rewrite dependency loading
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
206
diff
changeset
|
438 | } |
200 | 439 | } |
440 | } | |
441 | } | |
442 | ||
230
a1f3f7d9078b
rename LibraryManager -> LibrariesModel
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
217
diff
changeset
|
443 | std::map<QString, QString> DocumentManager::loadDependenciesForAllModels(const LibrariesModel& libraries) |
12 | 444 | { |
212
27259810da6d
Rewrite dependency loading
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
206
diff
changeset
|
445 | std::map<QString, QString> missing; |
27259810da6d
Rewrite dependency loading
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
206
diff
changeset
|
446 | for (auto& modelInfoPair : this->openModels) |
12 | 447 | { |
212
27259810da6d
Rewrite dependency loading
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
206
diff
changeset
|
448 | loadDependenciesForModel(&modelInfoPair.second, this, &libraries, missing); |
148 | 449 | } |
212
27259810da6d
Rewrite dependency loading
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
206
diff
changeset
|
450 | this->prune(); |
27259810da6d
Rewrite dependency loading
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
206
diff
changeset
|
451 | return missing; |
12 | 452 | } |