Tue, 07 Jun 2022 01:37:26 +0300
Continue giant refactor
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 |
8
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
6
diff
changeset
|
20 | #include <QAbstractListModel> |
3 | 21 | #include <memory> |
22 | #include "main.h" | |
200 | 23 | #include "colors.h" |
24 | ||
25 | struct SubfileReference | |
26 | { | |
27 | QString name; | |
28 | glm::mat4 transformation; | |
29 | bool inverted = false; | |
30 | }; | |
31 | ||
32 | template<typename T> | |
33 | struct Colored : T | |
34 | { | |
35 | ldraw::Color color; | |
36 | }; | |
37 | ||
38 | struct Comment | |
39 | { | |
40 | QString text; | |
41 | }; | |
3 | 42 | |
200 | 43 | struct ParseError |
8
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
6
diff
changeset
|
44 | { |
200 | 45 | QString code; |
8
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
6
diff
changeset
|
46 | }; |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
6
diff
changeset
|
47 | |
200 | 48 | struct Empty {}; |
49 | ||
50 | using ModelElement = std::variant< | |
51 | Colored<SubfileReference>, | |
52 | Colored<LineSegment>, | |
53 | Colored<Triangle>, | |
54 | Colored<Quadrilateral>, | |
55 | Colored<ConditionalEdge>, | |
56 | Comment, | |
57 | Empty, | |
58 | ParseError>; | |
59 | ||
60 | QString modelElementToString(const ModelElement& element); | |
61 | struct ModelId | |
62 | { | |
63 | std::int32_t value; | |
64 | constexpr auto operator<=>(const ModelId& other) const = default; | |
65 | }; | |
66 | ||
67 | constexpr int qHash(ModelId id) | |
68 | { | |
69 | return qHash(id.value); | |
70 | } | |
71 | ||
8
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
6
diff
changeset
|
72 | class Model : public QAbstractListModel |
3 | 73 | { |
74 | Q_OBJECT | |
200 | 75 | struct Entry { |
76 | ModelElement data; | |
77 | ModelId id; | |
78 | }; | |
79 | std::vector<Entry> body; | |
80 | std::map<ModelId, int> positions; | |
81 | ModelId runningId = {1}; | |
3 | 82 | public: |
200 | 83 | Model(QObject* parent); |
84 | virtual ~Model(); | |
85 | ModelId append(const ModelElement& value); | |
86 | const ModelElement& at(int position) const; | |
87 | ModelId idAt(int position) const; | |
88 | void assignAt(int position, const ModelElement& element); | |
89 | std::optional<int> find(ModelId id) const; | |
90 | void remove(int index); | |
8
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
6
diff
changeset
|
91 | int rowCount(const QModelIndex&) const override; |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
6
diff
changeset
|
92 | QVariant data(const QModelIndex& index, int role) const override; |
200 | 93 | const ModelElement& operator[](int index) const; |
94 | int size() const; | |
95 | auto operator[](int index) { | |
96 | struct { | |
97 | Model& model; | |
98 | int index; | |
99 | operator const ModelElement&() { | |
100 | return model.at(index); | |
101 | } | |
102 | auto& operator=(const ModelElement& newData) { | |
103 | model.assignAt(index, newData); | |
104 | return *this; | |
105 | } | |
106 | const auto* operator&() { | |
107 | return &(this->operator const ModelElement&()); | |
108 | } | |
109 | } result{*this, index}; | |
110 | return result; | |
111 | } | |
3 | 112 | }; |
113 | ||
151 | 114 | void save(const Model& model, QIODevice *device); |
200 | 115 | void updateHeaderNameField(Model& model, const QString &name); |