Sat, 08 Apr 2023 12:55:11 +0300
Fix BFC formatting not working due to being evaluated after comment format
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 | ||
3 | 19 | #pragma once |
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:
235
diff
changeset
|
20 | #include "src/libraries.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:
235
diff
changeset
|
21 | #include "src/model.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:
235
diff
changeset
|
22 | #include "src/polygoncache.h" |
3 | 23 | |
212
27259810da6d
Rewrite dependency loading
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
193
diff
changeset
|
24 | enum OpenType |
3 | 25 | { |
212
27259810da6d
Rewrite dependency loading
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
193
diff
changeset
|
26 | //! \brief Document was opened manually by the user |
27259810da6d
Rewrite dependency loading
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
193
diff
changeset
|
27 | ManuallyOpened, |
27259810da6d
Rewrite dependency loading
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
193
diff
changeset
|
28 | //! \brief Document was opened automatically in order to resolve subfile references |
27259810da6d
Rewrite dependency loading
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
193
diff
changeset
|
29 | AutomaticallyOpened, |
27259810da6d
Rewrite dependency loading
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
193
diff
changeset
|
30 | }; |
27259810da6d
Rewrite dependency loading
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
193
diff
changeset
|
31 | |
214
8e1fe64ce4e3
begin refactor of gl side
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
213
diff
changeset
|
32 | class DocumentManager : public QObject |
212
27259810da6d
Rewrite dependency loading
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
193
diff
changeset
|
33 | { |
214
8e1fe64ce4e3
begin refactor of gl side
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
213
diff
changeset
|
34 | Q_OBJECT |
3 | 35 | public: |
212
27259810da6d
Rewrite dependency loading
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
193
diff
changeset
|
36 | struct ModelInfo |
147 | 37 | { |
333
07e65a4c6611
Experiment to delete the Model class and rely solely on text documents
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
264
diff
changeset
|
38 | std::unique_ptr<QTextDocument> model; |
212
27259810da6d
Rewrite dependency loading
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
193
diff
changeset
|
39 | ModelId id; |
27259810da6d
Rewrite dependency loading
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
193
diff
changeset
|
40 | QString path; |
27259810da6d
Rewrite dependency loading
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
193
diff
changeset
|
41 | OpenType opentype; |
27259810da6d
Rewrite dependency loading
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
193
diff
changeset
|
42 | std::map<QString, ModelId> dependencies = {}; |
214
8e1fe64ce4e3
begin refactor of gl side
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
213
diff
changeset
|
43 | PolygonCache polygonCache = {}; |
8e1fe64ce4e3
begin refactor of gl side
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
213
diff
changeset
|
44 | QObject* payload; |
147 | 45 | }; |
212
27259810da6d
Rewrite dependency loading
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
193
diff
changeset
|
46 | using MissingDependencies = std::map<QString, QString>; |
214
8e1fe64ce4e3
begin refactor of gl side
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
213
diff
changeset
|
47 | DocumentManager(QObject* parent = nullptr); |
8e1fe64ce4e3
begin refactor of gl side
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
213
diff
changeset
|
48 | auto begin() const { return this->openModels.begin(); } |
8e1fe64ce4e3
begin refactor of gl side
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
213
diff
changeset
|
49 | auto end() const { return this->openModels.end(); } |
148 | 50 | ModelId newModel(); |
338 | 51 | QTextDocument* findDependencyByName(const ModelId modelId, const QString& name); |
52 | QTextDocument* getModelById(ModelId modelId); | |
148 | 53 | std::optional<ModelId> openModel(const QString& path, QTextStream& errorStream, const OpenType openType); |
230
a1f3f7d9078b
rename LibraryManager -> LibrariesModel
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
214
diff
changeset
|
54 | std::map<QString, QString> loadDependenciesForAllModels(const LibrariesModel &libraries); |
148 | 55 | void closeDocument(const ModelId modelId); |
56 | const QString* modelPath(ModelId modelId) const; | |
57 | void setModelPath( | |
58 | const ModelId modelId, | |
59 | const QString& newPath, | |
230
a1f3f7d9078b
rename LibraryManager -> LibrariesModel
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
214
diff
changeset
|
60 | const LibrariesModel &libraries, |
148 | 61 | QTextStream &errorStream); |
62 | bool saveModel(const ModelId modelId, QTextStream& errors); | |
338 | 63 | std::optional<ModelId> findIdForModel(const QTextDocument* model) const; |
150
b6cbba6e29a1
extract polygon cache out of Model
Teemu Piippo <teemu@hecknology.net>
parents:
148
diff
changeset
|
64 | PolygonCache* getPolygonCacheForModel(ModelId modelId); |
214
8e1fe64ce4e3
begin refactor of gl side
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
213
diff
changeset
|
65 | const ModelInfo* find(ModelId modelId) const; |
8e1fe64ce4e3
begin refactor of gl side
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
213
diff
changeset
|
66 | void setModelPayload(ModelId modelId, QObject* object); |
8e1fe64ce4e3
begin refactor of gl side
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
213
diff
changeset
|
67 | template<typename T> |
8e1fe64ce4e3
begin refactor of gl side
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
213
diff
changeset
|
68 | T* findPayload(ModelId modelId) const |
8e1fe64ce4e3
begin refactor of gl side
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
213
diff
changeset
|
69 | { |
8e1fe64ce4e3
begin refactor of gl side
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
213
diff
changeset
|
70 | const ModelInfo* info = this->find(modelId); |
8e1fe64ce4e3
begin refactor of gl side
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
213
diff
changeset
|
71 | return info ? qobject_cast<T*>(info->payload) : nullptr; |
8e1fe64ce4e3
begin refactor of gl side
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
213
diff
changeset
|
72 | } |
235
7ef03c2b46ab
Add a basic message log
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
230
diff
changeset
|
73 | Q_SIGNALS: |
7ef03c2b46ab
Add a basic message log
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
230
diff
changeset
|
74 | void message(const Message& message); |
3 | 75 | private: |
148 | 76 | int modelIdCounter = 0; |
77 | std::map<ModelId, ModelInfo> openModels; | |
338 | 78 | void collectReferences(QSet<QString> &referenced, const QString& name, const QTextDocument* model); |
148 | 79 | void updateDependencies(ModelInfo* model); |
80 | void prune(); | |
150
b6cbba6e29a1
extract polygon cache out of Model
Teemu Piippo <teemu@hecknology.net>
parents:
148
diff
changeset
|
81 | void makePolygonCacheForModel(const ModelId modelId); |
3 | 82 | }; |
147 | 83 | |
212
27259810da6d
Rewrite dependency loading
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
193
diff
changeset
|
84 | QString errorStringFromMissingDependencies(const DocumentManager::MissingDependencies& missing); |
147 | 85 | QString pathToName(const QFileInfo& path); |