src/documentmanager.cpp

Sat, 08 Apr 2023 16:12:12 +0300

author
Teemu Piippo <teemu.s.piippo@gmail.com>
date
Sat, 08 Apr 2023 16:12:12 +0300
changeset 353
c229d38f04c6
parent 338
719b909a7d2b
child 374
75efc3ba5a56
permissions
-rw-r--r--

Use explicit captures in main

24
1a0faaaceb84 added license
Teemu Piippo <teemu@hecknology.net>
parents: 23
diff changeset
1 /*
1a0faaaceb84 added license
Teemu Piippo <teemu@hecknology.net>
parents: 23
diff changeset
2 * LDForge: LDraw parts authoring CAD
1a0faaaceb84 added license
Teemu Piippo <teemu@hecknology.net>
parents: 23
diff changeset
3 * Copyright (C) 2013 - 2020 Teemu Piippo
1a0faaaceb84 added license
Teemu Piippo <teemu@hecknology.net>
parents: 23
diff changeset
4 *
1a0faaaceb84 added license
Teemu Piippo <teemu@hecknology.net>
parents: 23
diff changeset
5 * This program is free software: you can redistribute it and/or modify
1a0faaaceb84 added license
Teemu Piippo <teemu@hecknology.net>
parents: 23
diff changeset
6 * it under the terms of the GNU General Public License as published by
1a0faaaceb84 added license
Teemu Piippo <teemu@hecknology.net>
parents: 23
diff changeset
7 * the Free Software Foundation, either version 3 of the License, or
1a0faaaceb84 added license
Teemu Piippo <teemu@hecknology.net>
parents: 23
diff changeset
8 * (at your option) any later version.
1a0faaaceb84 added license
Teemu Piippo <teemu@hecknology.net>
parents: 23
diff changeset
9 *
1a0faaaceb84 added license
Teemu Piippo <teemu@hecknology.net>
parents: 23
diff changeset
10 * This program is distributed in the hope that it will be useful,
1a0faaaceb84 added license
Teemu Piippo <teemu@hecknology.net>
parents: 23
diff changeset
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
1a0faaaceb84 added license
Teemu Piippo <teemu@hecknology.net>
parents: 23
diff changeset
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1a0faaaceb84 added license
Teemu Piippo <teemu@hecknology.net>
parents: 23
diff changeset
13 * GNU General Public License for more details.
1a0faaaceb84 added license
Teemu Piippo <teemu@hecknology.net>
parents: 23
diff changeset
14 *
1a0faaaceb84 added license
Teemu Piippo <teemu@hecknology.net>
parents: 23
diff changeset
15 * You should have received a copy of the GNU General Public License
1a0faaaceb84 added license
Teemu Piippo <teemu@hecknology.net>
parents: 23
diff changeset
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
1a0faaaceb84 added license
Teemu Piippo <teemu@hecknology.net>
parents: 23
diff changeset
17 */
1a0faaaceb84 added license
Teemu Piippo <teemu@hecknology.net>
parents: 23
diff changeset
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
e1ced2523cad reworking
Teemu Piippo <teemu@hecknology.net>
parents: 147
diff changeset
22 #include <QSaveFile>
335
c5830bce1c23 Fix crashing
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 334
diff changeset
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
55a55a9ec2c2 Added lots of code
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
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
Teemu Piippo <teemu@hecknology.net>
parents: 3
diff changeset
30 {
Teemu Piippo <teemu@hecknology.net>
parents: 3
diff changeset
31 }
Teemu Piippo <teemu@hecknology.net>
parents: 3
diff changeset
32
335
c5830bce1c23 Fix crashing
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 334
diff changeset
33 static std::unique_ptr<QTextDocument> newTextDocument()
c5830bce1c23 Fix crashing
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 334
diff changeset
34 {
c5830bce1c23 Fix crashing
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 334
diff changeset
35 std::unique_ptr<QTextDocument> newModel = std::make_unique<QTextDocument>(nullptr);
c5830bce1c23 Fix crashing
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 334
diff changeset
36 newModel->setDocumentLayout(new QPlainTextDocumentLayout{newModel.get()});
c5830bce1c23 Fix crashing
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 334
diff changeset
37 return newModel;
c5830bce1c23 Fix crashing
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 334
diff changeset
38 }
c5830bce1c23 Fix crashing
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 334
diff changeset
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
e1ced2523cad reworking
Teemu Piippo <teemu@hecknology.net>
parents: 147
diff changeset
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
e1ced2523cad reworking
Teemu Piippo <teemu@hecknology.net>
parents: 147
diff changeset
44 ModelId DocumentManager::newModel()
8
44679e468ba9 major update with many things
Teemu Piippo <teemu@hecknology.net>
parents: 5
diff changeset
45 {
148
e1ced2523cad reworking
Teemu Piippo <teemu@hecknology.net>
parents: 147
diff changeset
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
c5830bce1c23 Fix crashing
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 334
diff changeset
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
e1ced2523cad reworking
Teemu Piippo <teemu@hecknology.net>
parents: 147
diff changeset
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
719b909a7d2b Delete unused code
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 335
diff changeset
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
e1ced2523cad reworking
Teemu Piippo <teemu@hecknology.net>
parents: 147
diff changeset
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
e1ced2523cad reworking
Teemu Piippo <teemu@hecknology.net>
parents: 147
diff changeset
61 const auto& dependencies = modelsIterator->second.dependencies;
e1ced2523cad reworking
Teemu Piippo <teemu@hecknology.net>
parents: 147
diff changeset
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
e1ced2523cad reworking
Teemu Piippo <teemu@hecknology.net>
parents: 147
diff changeset
64 ModelInfo& modelInfo = this->openModels[dependenciesIterator->second];
e1ced2523cad reworking
Teemu Piippo <teemu@hecknology.net>
parents: 147
diff changeset
65 return modelInfo.model.get();
e1ced2523cad reworking
Teemu Piippo <teemu@hecknology.net>
parents: 147
diff changeset
66 }
212
27259810da6d Rewrite dependency loading
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 206
diff changeset
67 else {
148
e1ced2523cad reworking
Teemu Piippo <teemu@hecknology.net>
parents: 147
diff changeset
68 return nullptr;
e1ced2523cad reworking
Teemu Piippo <teemu@hecknology.net>
parents: 147
diff changeset
69 }
e1ced2523cad reworking
Teemu Piippo <teemu@hecknology.net>
parents: 147
diff changeset
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
e1ced2523cad reworking
Teemu Piippo <teemu@hecknology.net>
parents: 147
diff changeset
74 }
e1ced2523cad reworking
Teemu Piippo <teemu@hecknology.net>
parents: 147
diff changeset
75
e1ced2523cad reworking
Teemu Piippo <teemu@hecknology.net>
parents: 147
diff changeset
76 /**
e1ced2523cad reworking
Teemu Piippo <teemu@hecknology.net>
parents: 147
diff changeset
77 * @brief Gets a model pointer by id or nullptr if not found
e1ced2523cad reworking
Teemu Piippo <teemu@hecknology.net>
parents: 147
diff changeset
78 * @param modelId id of model to find
e1ced2523cad reworking
Teemu Piippo <teemu@hecknology.net>
parents: 147
diff changeset
79 * @returns model pointer or null
e1ced2523cad reworking
Teemu Piippo <teemu@hecknology.net>
parents: 147
diff changeset
80 */
338
719b909a7d2b Delete unused code
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 335
diff changeset
81 QTextDocument *DocumentManager::getModelById(ModelId modelId)
148
e1ced2523cad reworking
Teemu Piippo <teemu@hecknology.net>
parents: 147
diff changeset
82 {
e1ced2523cad reworking
Teemu Piippo <teemu@hecknology.net>
parents: 147
diff changeset
83 const auto iterator = this->openModels.find(modelId);
e1ced2523cad reworking
Teemu Piippo <teemu@hecknology.net>
parents: 147
diff changeset
84 if (iterator != this->openModels.end())
e1ced2523cad reworking
Teemu Piippo <teemu@hecknology.net>
parents: 147
diff changeset
85 {
e1ced2523cad reworking
Teemu Piippo <teemu@hecknology.net>
parents: 147
diff changeset
86 return iterator->second.model.get();
e1ced2523cad reworking
Teemu Piippo <teemu@hecknology.net>
parents: 147
diff changeset
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
e1ced2523cad reworking
Teemu Piippo <teemu@hecknology.net>
parents: 147
diff changeset
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
55a55a9ec2c2 Added lots of code
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
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
a5111f4e6412 added teapot rendering
Teemu Piippo <teemu@hecknology.net>
parents: 14
diff changeset
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
55a55a9ec2c2 Added lots of code
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
113 }
8
44679e468ba9 major update with many things
Teemu Piippo <teemu@hecknology.net>
parents: 5
diff changeset
114
147
Teemu Piippo <teemu@hecknology.net>
parents: 140
diff changeset
115 /**
Teemu Piippo <teemu@hecknology.net>
parents: 140
diff changeset
116 * @brief Tries to open the model at the specified path
Teemu Piippo <teemu@hecknology.net>
parents: 140
diff changeset
117 * @param path Path to the model to open
Teemu Piippo <teemu@hecknology.net>
parents: 140
diff changeset
118 * @param errorStream Where to write any errors
Teemu Piippo <teemu@hecknology.net>
parents: 140
diff changeset
119 * @param openType rationale behind opening this file
148
e1ced2523cad reworking
Teemu Piippo <teemu@hecknology.net>
parents: 147
diff changeset
120 * @returns model id, or no value on error
147
Teemu Piippo <teemu@hecknology.net>
parents: 140
diff changeset
121 */
148
e1ced2523cad reworking
Teemu Piippo <teemu@hecknology.net>
parents: 147
diff changeset
122 std::optional<ModelId> DocumentManager::openModel(
e1ced2523cad reworking
Teemu Piippo <teemu@hecknology.net>
parents: 147
diff changeset
123 const QString& path,
e1ced2523cad reworking
Teemu Piippo <teemu@hecknology.net>
parents: 147
diff changeset
124 QTextStream& errorStream,
e1ced2523cad reworking
Teemu Piippo <teemu@hecknology.net>
parents: 147
diff changeset
125 const OpenType openType
e1ced2523cad reworking
Teemu Piippo <teemu@hecknology.net>
parents: 147
diff changeset
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
c5830bce1c23 Fix crashing
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 334
diff changeset
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
e1ced2523cad reworking
Teemu Piippo <teemu@hecknology.net>
parents: 147
diff changeset
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
e1ced2523cad reworking
Teemu Piippo <teemu@hecknology.net>
parents: 147
diff changeset
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
e1ced2523cad reworking
Teemu Piippo <teemu@hecknology.net>
parents: 147
diff changeset
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
e1ced2523cad reworking
Teemu Piippo <teemu@hecknology.net>
parents: 147
diff changeset
154 void DocumentManager::closeDocument(const ModelId modelId)
e1ced2523cad reworking
Teemu Piippo <teemu@hecknology.net>
parents: 147
diff changeset
155 {
e1ced2523cad reworking
Teemu Piippo <teemu@hecknology.net>
parents: 147
diff changeset
156 ModelInfo* modelInfo = findInMap(this->openModels, modelId);
e1ced2523cad reworking
Teemu Piippo <teemu@hecknology.net>
parents: 147
diff changeset
157 if (modelInfo != nullptr)
e1ced2523cad reworking
Teemu Piippo <teemu@hecknology.net>
parents: 147
diff changeset
158 {
e1ced2523cad reworking
Teemu Piippo <teemu@hecknology.net>
parents: 147
diff changeset
159 modelInfo->opentype = OpenType::AutomaticallyOpened;
e1ced2523cad reworking
Teemu Piippo <teemu@hecknology.net>
parents: 147
diff changeset
160 this->prune();
e1ced2523cad reworking
Teemu Piippo <teemu@hecknology.net>
parents: 147
diff changeset
161 }
e1ced2523cad reworking
Teemu Piippo <teemu@hecknology.net>
parents: 147
diff changeset
162 }
e1ced2523cad reworking
Teemu Piippo <teemu@hecknology.net>
parents: 147
diff changeset
163
e1ced2523cad reworking
Teemu Piippo <teemu@hecknology.net>
parents: 147
diff changeset
164 const QString *DocumentManager::modelPath(ModelId modelId) const
e1ced2523cad reworking
Teemu Piippo <teemu@hecknology.net>
parents: 147
diff changeset
165 {
e1ced2523cad reworking
Teemu Piippo <teemu@hecknology.net>
parents: 147
diff changeset
166 const auto iterator = this->openModels.find(modelId);
e1ced2523cad reworking
Teemu Piippo <teemu@hecknology.net>
parents: 147
diff changeset
167 if (iterator != this->openModels.end())
e1ced2523cad reworking
Teemu Piippo <teemu@hecknology.net>
parents: 147
diff changeset
168 {
e1ced2523cad reworking
Teemu Piippo <teemu@hecknology.net>
parents: 147
diff changeset
169 return &iterator->second.path;
e1ced2523cad reworking
Teemu Piippo <teemu@hecknology.net>
parents: 147
diff changeset
170 }
e1ced2523cad reworking
Teemu Piippo <teemu@hecknology.net>
parents: 147
diff changeset
171 else
e1ced2523cad reworking
Teemu Piippo <teemu@hecknology.net>
parents: 147
diff changeset
172 {
e1ced2523cad reworking
Teemu Piippo <teemu@hecknology.net>
parents: 147
diff changeset
173 return nullptr;
e1ced2523cad reworking
Teemu Piippo <teemu@hecknology.net>
parents: 147
diff changeset
174 }
e1ced2523cad reworking
Teemu Piippo <teemu@hecknology.net>
parents: 147
diff changeset
175 }
e1ced2523cad reworking
Teemu Piippo <teemu@hecknology.net>
parents: 147
diff changeset
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
e1ced2523cad reworking
Teemu Piippo <teemu@hecknology.net>
parents: 147
diff changeset
180 void DocumentManager::setModelPath(
e1ced2523cad reworking
Teemu Piippo <teemu@hecknology.net>
parents: 147
diff changeset
181 const ModelId modelId,
e1ced2523cad reworking
Teemu Piippo <teemu@hecknology.net>
parents: 147
diff changeset
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
e1ced2523cad reworking
Teemu Piippo <teemu@hecknology.net>
parents: 147
diff changeset
184 QTextStream &errorStream)
e1ced2523cad reworking
Teemu Piippo <teemu@hecknology.net>
parents: 147
diff changeset
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
fe67489523b5 added dependency loading
Teemu Piippo <teemu@hecknology.net>
parents: 8
diff changeset
193 }
fe67489523b5 added dependency loading
Teemu Piippo <teemu@hecknology.net>
parents: 8
diff changeset
194 }
fe67489523b5 added dependency loading
Teemu Piippo <teemu@hecknology.net>
parents: 8
diff changeset
195
148
e1ced2523cad reworking
Teemu Piippo <teemu@hecknology.net>
parents: 147
diff changeset
196 bool DocumentManager::saveModel(const ModelId modelId, QTextStream &errors)
147
Teemu Piippo <teemu@hecknology.net>
parents: 140
diff changeset
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
Teemu Piippo <teemu@hecknology.net>
parents: 140
diff changeset
200 {
212
27259810da6d Rewrite dependency loading
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 206
diff changeset
201 QSaveFile file{info->path};
148
e1ced2523cad reworking
Teemu Piippo <teemu@hecknology.net>
parents: 147
diff changeset
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
e1ced2523cad reworking
Teemu Piippo <teemu@hecknology.net>
parents: 147
diff changeset
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
e1ced2523cad reworking
Teemu Piippo <teemu@hecknology.net>
parents: 147
diff changeset
208 return false;
e1ced2523cad reworking
Teemu Piippo <teemu@hecknology.net>
parents: 147
diff changeset
209 }
212
27259810da6d Rewrite dependency loading
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 206
diff changeset
210 else {
148
e1ced2523cad reworking
Teemu Piippo <teemu@hecknology.net>
parents: 147
diff changeset
211 return true;
e1ced2523cad reworking
Teemu Piippo <teemu@hecknology.net>
parents: 147
diff changeset
212 }
e1ced2523cad reworking
Teemu Piippo <teemu@hecknology.net>
parents: 147
diff changeset
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
e1ced2523cad reworking
Teemu Piippo <teemu@hecknology.net>
parents: 147
diff changeset
217 return false;
147
Teemu Piippo <teemu@hecknology.net>
parents: 140
diff changeset
218 }
Teemu Piippo <teemu@hecknology.net>
parents: 140
diff changeset
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
e1ced2523cad reworking
Teemu Piippo <teemu@hecknology.net>
parents: 147
diff changeset
222 return false;
e1ced2523cad reworking
Teemu Piippo <teemu@hecknology.net>
parents: 147
diff changeset
223 }
147
Teemu Piippo <teemu@hecknology.net>
parents: 140
diff changeset
224 }
Teemu Piippo <teemu@hecknology.net>
parents: 140
diff changeset
225
148
e1ced2523cad reworking
Teemu Piippo <teemu@hecknology.net>
parents: 147
diff changeset
226 /**
e1ced2523cad reworking
Teemu Piippo <teemu@hecknology.net>
parents: 147
diff changeset
227 * @brief Searches the open models for the specified model and returns its id if found
e1ced2523cad reworking
Teemu Piippo <teemu@hecknology.net>
parents: 147
diff changeset
228 * @param model model to look for
e1ced2523cad reworking
Teemu Piippo <teemu@hecknology.net>
parents: 147
diff changeset
229 * @return id or no value if not found
e1ced2523cad reworking
Teemu Piippo <teemu@hecknology.net>
parents: 147
diff changeset
230 */
338
719b909a7d2b Delete unused code
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 335
diff changeset
231 std::optional<ModelId> DocumentManager::findIdForModel(const QTextDocument *model) const
147
Teemu Piippo <teemu@hecknology.net>
parents: 140
diff changeset
232 {
148
e1ced2523cad reworking
Teemu Piippo <teemu@hecknology.net>
parents: 147
diff changeset
233 std::optional<ModelId> result;
e1ced2523cad reworking
Teemu Piippo <teemu@hecknology.net>
parents: 147
diff changeset
234 for (auto it = this->openModels.begin(); it != this->openModels.end(); ++it)
147
Teemu Piippo <teemu@hecknology.net>
parents: 140
diff changeset
235 {
148
e1ced2523cad reworking
Teemu Piippo <teemu@hecknology.net>
parents: 147
diff changeset
236 if (it->second.model.get() == model)
147
Teemu Piippo <teemu@hecknology.net>
parents: 140
diff changeset
237 {
148
e1ced2523cad reworking
Teemu Piippo <teemu@hecknology.net>
parents: 147
diff changeset
238 result = it->first;
e1ced2523cad reworking
Teemu Piippo <teemu@hecknology.net>
parents: 147
diff changeset
239 break;
e1ced2523cad reworking
Teemu Piippo <teemu@hecknology.net>
parents: 147
diff changeset
240 }
e1ced2523cad reworking
Teemu Piippo <teemu@hecknology.net>
parents: 147
diff changeset
241 }
e1ced2523cad reworking
Teemu Piippo <teemu@hecknology.net>
parents: 147
diff changeset
242 return result;
e1ced2523cad reworking
Teemu Piippo <teemu@hecknology.net>
parents: 147
diff changeset
243 }
e1ced2523cad reworking
Teemu Piippo <teemu@hecknology.net>
parents: 147
diff changeset
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
e1ced2523cad reworking
Teemu Piippo <teemu@hecknology.net>
parents: 147
diff changeset
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
e1ced2523cad reworking
Teemu Piippo <teemu@hecknology.net>
parents: 147
diff changeset
286 }
e1ced2523cad reworking
Teemu Piippo <teemu@hecknology.net>
parents: 147
diff changeset
287 }
e1ced2523cad reworking
Teemu Piippo <teemu@hecknology.net>
parents: 147
diff changeset
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
e1ced2523cad reworking
Teemu Piippo <teemu@hecknology.net>
parents: 147
diff changeset
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
Teemu Piippo <teemu@hecknology.net>
parents: 140
diff changeset
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
e1ced2523cad reworking
Teemu Piippo <teemu@hecknology.net>
parents: 147
diff changeset
322 }
147
Teemu Piippo <teemu@hecknology.net>
parents: 140
diff changeset
323 }
Teemu Piippo <teemu@hecknology.net>
parents: 140
diff changeset
324 }
Teemu Piippo <teemu@hecknology.net>
parents: 140
diff changeset
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
719b909a7d2b Delete unused code
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 335
diff changeset
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
b4beff48bb7a Simplify PolygonCache
Teemu Piippo <teemu@hecknology.net>
parents: 176
diff changeset
341 }
b4beff48bb7a Simplify PolygonCache
Teemu Piippo <teemu@hecknology.net>
parents: 176
diff changeset
342 }
b4beff48bb7a Simplify PolygonCache
Teemu Piippo <teemu@hecknology.net>
parents: 176
diff changeset
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
719b909a7d2b Delete unused code
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 335
diff changeset
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
ca23936b455b Giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 195
diff changeset
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
ca23936b455b Giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 195
diff changeset
439 }
ca23936b455b Giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 195
diff changeset
440 }
ca23936b455b Giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 195
diff changeset
441 }
ca23936b455b Giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 195
diff changeset
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
fe67489523b5 added dependency loading
Teemu Piippo <teemu@hecknology.net>
parents: 8
diff changeset
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
fe67489523b5 added dependency loading
Teemu Piippo <teemu@hecknology.net>
parents: 8
diff changeset
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
e1ced2523cad reworking
Teemu Piippo <teemu@hecknology.net>
parents: 147
diff changeset
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
fe67489523b5 added dependency loading
Teemu Piippo <teemu@hecknology.net>
parents: 8
diff changeset
452 }

mercurial