src/documentmanager.h

Tue, 01 Mar 2022 17:00:19 +0200

author
Teemu Piippo <teemu@hecknology.net>
date
Tue, 01 Mar 2022 17:00:19 +0200
changeset 149
008989bc7d6e
parent 148
e1ced2523cad
child 150
b6cbba6e29a1
permissions
-rw-r--r--

work on edit history

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
3
55a55a9ec2c2 Added lots of code
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
19 #pragma once
12
fe67489523b5 added dependency loading
Teemu Piippo <teemu@hecknology.net>
parents: 8
diff changeset
20 #include "libraries.h"
3
55a55a9ec2c2 Added lots of code
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
21 #include "model.h"
55a55a9ec2c2 Added lots of code
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
22
5
Teemu Piippo <teemu@hecknology.net>
parents: 3
diff changeset
23 class DocumentManager : public QObject
3
55a55a9ec2c2 Added lots of code
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
24 {
5
Teemu Piippo <teemu@hecknology.net>
parents: 3
diff changeset
25 Q_OBJECT
3
55a55a9ec2c2 Added lots of code
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
26 public:
147
Teemu Piippo <teemu@hecknology.net>
parents: 24
diff changeset
27 enum OpenType
Teemu Piippo <teemu@hecknology.net>
parents: 24
diff changeset
28 {
148
e1ced2523cad reworking
Teemu Piippo <teemu@hecknology.net>
parents: 147
diff changeset
29 /**
e1ced2523cad reworking
Teemu Piippo <teemu@hecknology.net>
parents: 147
diff changeset
30 * Document was opened manually by the user
e1ced2523cad reworking
Teemu Piippo <teemu@hecknology.net>
parents: 147
diff changeset
31 */
147
Teemu Piippo <teemu@hecknology.net>
parents: 24
diff changeset
32 ManuallyOpened,
148
e1ced2523cad reworking
Teemu Piippo <teemu@hecknology.net>
parents: 147
diff changeset
33 /**
e1ced2523cad reworking
Teemu Piippo <teemu@hecknology.net>
parents: 147
diff changeset
34 * Document was opened automatically in order to resolve subfile references
e1ced2523cad reworking
Teemu Piippo <teemu@hecknology.net>
parents: 147
diff changeset
35 */
147
Teemu Piippo <teemu@hecknology.net>
parents: 24
diff changeset
36 AutomaticallyOpened,
Teemu Piippo <teemu@hecknology.net>
parents: 24
diff changeset
37 };
21
Teemu Piippo <teemu@hecknology.net>
parents: 12
diff changeset
38 DocumentManager(QObject* parent = nullptr);
148
e1ced2523cad reworking
Teemu Piippo <teemu@hecknology.net>
parents: 147
diff changeset
39 ModelId newModel();
e1ced2523cad reworking
Teemu Piippo <teemu@hecknology.net>
parents: 147
diff changeset
40 Model* findDependencyByName(const ModelId modelId, const QString& name);
e1ced2523cad reworking
Teemu Piippo <teemu@hecknology.net>
parents: 147
diff changeset
41 Model* getModelById(ModelId modelId);
e1ced2523cad reworking
Teemu Piippo <teemu@hecknology.net>
parents: 147
diff changeset
42 std::optional<ModelId> openModel(const QString& path, QTextStream& errorStream, const OpenType openType);
8
44679e468ba9 major update with many things
Teemu Piippo <teemu@hecknology.net>
parents: 5
diff changeset
43 QString makeNewModelName();
148
e1ced2523cad reworking
Teemu Piippo <teemu@hecknology.net>
parents: 147
diff changeset
44 void loadDependenciesForAllModels(const LibraryManager &libraries, QTextStream &errorStream);
e1ced2523cad reworking
Teemu Piippo <teemu@hecknology.net>
parents: 147
diff changeset
45 void loadDependenciesForModel(const ModelId modelId,
23
3387a84ddaba fixed a pile of nonsense that caused subfiles to go haywire
Teemu Piippo <teemu@hecknology.net>
parents: 21
diff changeset
46 const QString& path,
12
fe67489523b5 added dependency loading
Teemu Piippo <teemu@hecknology.net>
parents: 8
diff changeset
47 const LibraryManager& libraries,
fe67489523b5 added dependency loading
Teemu Piippo <teemu@hecknology.net>
parents: 8
diff changeset
48 QTextStream& errorStream);
148
e1ced2523cad reworking
Teemu Piippo <teemu@hecknology.net>
parents: 147
diff changeset
49 void closeDocument(const ModelId modelId);
e1ced2523cad reworking
Teemu Piippo <teemu@hecknology.net>
parents: 147
diff changeset
50 const QString* modelPath(ModelId modelId) const;
e1ced2523cad reworking
Teemu Piippo <teemu@hecknology.net>
parents: 147
diff changeset
51 void setModelPath(
e1ced2523cad reworking
Teemu Piippo <teemu@hecknology.net>
parents: 147
diff changeset
52 const ModelId modelId,
e1ced2523cad reworking
Teemu Piippo <teemu@hecknology.net>
parents: 147
diff changeset
53 const QString& newPath,
e1ced2523cad reworking
Teemu Piippo <teemu@hecknology.net>
parents: 147
diff changeset
54 const LibraryManager &libraries,
e1ced2523cad reworking
Teemu Piippo <teemu@hecknology.net>
parents: 147
diff changeset
55 QTextStream &errorStream);
e1ced2523cad reworking
Teemu Piippo <teemu@hecknology.net>
parents: 147
diff changeset
56 bool saveModel(const ModelId modelId, QTextStream& errors);
e1ced2523cad reworking
Teemu Piippo <teemu@hecknology.net>
parents: 147
diff changeset
57 std::optional<ModelId> findIdForModel(const Model* model) const;
3
55a55a9ec2c2 Added lots of code
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
58 private:
147
Teemu Piippo <teemu@hecknology.net>
parents: 24
diff changeset
59 struct ModelInfo
Teemu Piippo <teemu@hecknology.net>
parents: 24
diff changeset
60 {
Teemu Piippo <teemu@hecknology.net>
parents: 24
diff changeset
61 std::unique_ptr<Model> model;
148
e1ced2523cad reworking
Teemu Piippo <teemu@hecknology.net>
parents: 147
diff changeset
62 QString path;
147
Teemu Piippo <teemu@hecknology.net>
parents: 24
diff changeset
63 OpenType opentype;
148
e1ced2523cad reworking
Teemu Piippo <teemu@hecknology.net>
parents: 147
diff changeset
64 std::map<QString, ModelId> dependencies = {};
147
Teemu Piippo <teemu@hecknology.net>
parents: 24
diff changeset
65 };
148
e1ced2523cad reworking
Teemu Piippo <teemu@hecknology.net>
parents: 147
diff changeset
66 struct LoadDepedenciesBag;
e1ced2523cad reworking
Teemu Piippo <teemu@hecknology.net>
parents: 147
diff changeset
67 int modelIdCounter = 0;
8
44679e468ba9 major update with many things
Teemu Piippo <teemu@hecknology.net>
parents: 5
diff changeset
68 int untitledNameCounter = 0;
148
e1ced2523cad reworking
Teemu Piippo <teemu@hecknology.net>
parents: 147
diff changeset
69 std::map<ModelId, ModelInfo> openModels;
e1ced2523cad reworking
Teemu Piippo <teemu@hecknology.net>
parents: 147
diff changeset
70 void loadDependenciesForModel(const ModelId modelId, const QString& path, LoadDepedenciesBag& bag);
e1ced2523cad reworking
Teemu Piippo <teemu@hecknology.net>
parents: 147
diff changeset
71 void collectReferences(QSet<QString> &referenced, const QString& name, const Model* model);
e1ced2523cad reworking
Teemu Piippo <teemu@hecknology.net>
parents: 147
diff changeset
72 void updateDependencies(ModelInfo* model);
e1ced2523cad reworking
Teemu Piippo <teemu@hecknology.net>
parents: 147
diff changeset
73 void prune();
e1ced2523cad reworking
Teemu Piippo <teemu@hecknology.net>
parents: 147
diff changeset
74 bool isReferencedByAnything(const ModelId modelId) const;
3
55a55a9ec2c2 Added lots of code
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
75 };
147
Teemu Piippo <teemu@hecknology.net>
parents: 24
diff changeset
76
Teemu Piippo <teemu@hecknology.net>
parents: 24
diff changeset
77 QString pathToName(const QFileInfo& path);

mercurial