Wed, 15 Jun 2022 12:33:16 +0300
remove old todo
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> |
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:
200
diff
changeset
|
22 | #include "basics.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 | ||
210
232e7634cc8a
more refactoring, dosn't build yet
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
206
diff
changeset
|
60 | using PolygonElement = Colored<std::variant< |
232e7634cc8a
more refactoring, dosn't build yet
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
206
diff
changeset
|
61 | LineSegment, |
232e7634cc8a
more refactoring, dosn't build yet
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
206
diff
changeset
|
62 | Triangle, |
232e7634cc8a
more refactoring, dosn't build yet
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
206
diff
changeset
|
63 | Quadrilateral, |
232e7634cc8a
more refactoring, dosn't build yet
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
206
diff
changeset
|
64 | ConditionalEdge>>; |
232e7634cc8a
more refactoring, dosn't build yet
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
206
diff
changeset
|
65 | |
232e7634cc8a
more refactoring, dosn't build yet
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
206
diff
changeset
|
66 | template<typename T> |
211 | 67 | struct remove_color {}; |
210
232e7634cc8a
more refactoring, dosn't build yet
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
206
diff
changeset
|
68 | |
232e7634cc8a
more refactoring, dosn't build yet
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
206
diff
changeset
|
69 | template<typename T> |
232e7634cc8a
more refactoring, dosn't build yet
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
206
diff
changeset
|
70 | struct remove_color<Colored<T>> { using type = T; }; |
232e7634cc8a
more refactoring, dosn't build yet
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
206
diff
changeset
|
71 | |
232e7634cc8a
more refactoring, dosn't build yet
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
206
diff
changeset
|
72 | template<typename T> |
232e7634cc8a
more refactoring, dosn't build yet
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
206
diff
changeset
|
73 | struct remove_color<Colored<T>&> { using type = T&; }; |
232e7634cc8a
more refactoring, dosn't build yet
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
206
diff
changeset
|
74 | |
232e7634cc8a
more refactoring, dosn't build yet
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
206
diff
changeset
|
75 | template<typename T> |
211 | 76 | struct remove_color<const Colored<T>&> { using type = const T&; }; |
77 | ||
78 | template<typename T> | |
210
232e7634cc8a
more refactoring, dosn't build yet
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
206
diff
changeset
|
79 | struct remove_color<Colored<T>&&> { using type = T&&; }; |
232e7634cc8a
more refactoring, dosn't build yet
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
206
diff
changeset
|
80 | |
232e7634cc8a
more refactoring, dosn't build yet
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
206
diff
changeset
|
81 | template<typename T> |
232e7634cc8a
more refactoring, dosn't build yet
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
206
diff
changeset
|
82 | using remove_color_t = typename remove_color<T>::type; |
232e7634cc8a
more refactoring, dosn't build yet
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
206
diff
changeset
|
83 | |
211 | 84 | static_assert(std::is_same_v<remove_color_t<Colored<Triangle>>, Triangle>); |
85 | static_assert(std::is_same_v<remove_color_t<Colored<Triangle>&>, Triangle&>); | |
86 | static_assert(std::is_same_v<remove_color_t<const Colored<Triangle>&>, const Triangle&>); | |
87 | ||
210
232e7634cc8a
more refactoring, dosn't build yet
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
206
diff
changeset
|
88 | template<typename T> |
232e7634cc8a
more refactoring, dosn't build yet
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
206
diff
changeset
|
89 | constexpr remove_color_t<T&&> extract_colored(T&& x) |
232e7634cc8a
more refactoring, dosn't build yet
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
206
diff
changeset
|
90 | { |
232e7634cc8a
more refactoring, dosn't build yet
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
206
diff
changeset
|
91 | return static_cast<remove_color_t<T&&>>(x); |
232e7634cc8a
more refactoring, dosn't build yet
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
206
diff
changeset
|
92 | } |
232e7634cc8a
more refactoring, dosn't build yet
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
206
diff
changeset
|
93 | |
211 | 94 | template<typename Ret, typename Fn1, typename Fn2, typename Fn3, typename Fn4, typename T> |
95 | constexpr auto visitPolygon(Fn1&& f1, Fn2&& f2, Fn3&& f3, Fn4&& f4, T&& element) | |
210
232e7634cc8a
more refactoring, dosn't build yet
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
206
diff
changeset
|
96 | { |
211 | 97 | if (std::holds_alternative<LineSegment>(element)) { |
98 | return f1(std::get<LineSegment>(element)); | |
99 | } | |
100 | else if (std::holds_alternative<Triangle>(element)) { | |
101 | return f2(std::get<Triangle>(element)); | |
102 | } | |
103 | else if (std::holds_alternative<Quadrilateral>(element)) { | |
104 | return f3(std::get<Quadrilateral>(element)); | |
105 | } | |
106 | else { | |
107 | return f4(std::get<ConditionalEdge>(element)); | |
108 | } | |
210
232e7634cc8a
more refactoring, dosn't build yet
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
206
diff
changeset
|
109 | } |
232e7634cc8a
more refactoring, dosn't build yet
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
206
diff
changeset
|
110 | |
232e7634cc8a
more refactoring, dosn't build yet
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
206
diff
changeset
|
111 | template<typename T, typename Fn> |
211 | 112 | constexpr void visitPoints(Fn&& func, T&& element) |
210
232e7634cc8a
more refactoring, dosn't build yet
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
206
diff
changeset
|
113 | { |
232e7634cc8a
more refactoring, dosn't build yet
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
206
diff
changeset
|
114 | visitPolygon<void>( |
218
63125c36de73
Replace config collector with a simpler system
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
212
diff
changeset
|
115 | [&func](transfer_cvref_t<T&&, LineSegment> edge) |
210
232e7634cc8a
more refactoring, dosn't build yet
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
206
diff
changeset
|
116 | { |
232e7634cc8a
more refactoring, dosn't build yet
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
206
diff
changeset
|
117 | func(edge.p1); |
232e7634cc8a
more refactoring, dosn't build yet
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
206
diff
changeset
|
118 | func(edge.p2); |
232e7634cc8a
more refactoring, dosn't build yet
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
206
diff
changeset
|
119 | }, |
218
63125c36de73
Replace config collector with a simpler system
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
212
diff
changeset
|
120 | [&func](transfer_cvref_t<T&&, Triangle>& tri) |
210
232e7634cc8a
more refactoring, dosn't build yet
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
206
diff
changeset
|
121 | { |
232e7634cc8a
more refactoring, dosn't build yet
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
206
diff
changeset
|
122 | func(tri.p1); |
232e7634cc8a
more refactoring, dosn't build yet
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
206
diff
changeset
|
123 | func(tri.p2); |
232e7634cc8a
more refactoring, dosn't build yet
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
206
diff
changeset
|
124 | func(tri.p3); |
232e7634cc8a
more refactoring, dosn't build yet
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
206
diff
changeset
|
125 | }, |
218
63125c36de73
Replace config collector with a simpler system
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
212
diff
changeset
|
126 | [&func](transfer_cvref_t<T&&, Quadrilateral>& quad) |
210
232e7634cc8a
more refactoring, dosn't build yet
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
206
diff
changeset
|
127 | { |
232e7634cc8a
more refactoring, dosn't build yet
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
206
diff
changeset
|
128 | func(quad.p1); |
232e7634cc8a
more refactoring, dosn't build yet
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
206
diff
changeset
|
129 | func(quad.p2); |
232e7634cc8a
more refactoring, dosn't build yet
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
206
diff
changeset
|
130 | func(quad.p3); |
232e7634cc8a
more refactoring, dosn't build yet
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
206
diff
changeset
|
131 | func(quad.p4); |
232e7634cc8a
more refactoring, dosn't build yet
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
206
diff
changeset
|
132 | }, |
218
63125c36de73
Replace config collector with a simpler system
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
212
diff
changeset
|
133 | [&func](transfer_cvref_t<T&&, ConditionalEdge>& cedge) |
210
232e7634cc8a
more refactoring, dosn't build yet
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
206
diff
changeset
|
134 | { |
232e7634cc8a
more refactoring, dosn't build yet
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
206
diff
changeset
|
135 | func(cedge.p1); |
232e7634cc8a
more refactoring, dosn't build yet
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
206
diff
changeset
|
136 | func(cedge.p2); |
232e7634cc8a
more refactoring, dosn't build yet
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
206
diff
changeset
|
137 | func(cedge.c1); |
232e7634cc8a
more refactoring, dosn't build yet
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
206
diff
changeset
|
138 | func(cedge.c2); |
232e7634cc8a
more refactoring, dosn't build yet
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
206
diff
changeset
|
139 | }, |
232e7634cc8a
more refactoring, dosn't build yet
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
206
diff
changeset
|
140 | element); |
232e7634cc8a
more refactoring, dosn't build yet
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
206
diff
changeset
|
141 | } |
232e7634cc8a
more refactoring, dosn't build yet
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
206
diff
changeset
|
142 | |
200 | 143 | QString modelElementToString(const ModelElement& element); |
144 | struct ModelId | |
145 | { | |
146 | std::int32_t value; | |
147 | constexpr auto operator<=>(const ModelId& other) const = default; | |
148 | }; | |
149 | ||
150 | constexpr int qHash(ModelId id) | |
151 | { | |
152 | return qHash(id.value); | |
153 | } | |
154 | ||
8
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
6
diff
changeset
|
155 | class Model : public QAbstractListModel |
3 | 156 | { |
157 | Q_OBJECT | |
200 | 158 | struct Entry { |
159 | ModelElement data; | |
160 | ModelId id; | |
161 | }; | |
162 | std::vector<Entry> body; | |
163 | std::map<ModelId, int> positions; | |
164 | ModelId runningId = {1}; | |
3 | 165 | public: |
200 | 166 | Model(QObject* parent); |
167 | virtual ~Model(); | |
168 | ModelId append(const ModelElement& value); | |
169 | const ModelElement& at(int position) const; | |
170 | ModelId idAt(int position) const; | |
171 | void assignAt(int position, const ModelElement& element); | |
172 | std::optional<int> find(ModelId id) const; | |
173 | void remove(int index); | |
8
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
6
diff
changeset
|
174 | int rowCount(const QModelIndex&) const override; |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
6
diff
changeset
|
175 | QVariant data(const QModelIndex& index, int role) const override; |
200 | 176 | const ModelElement& operator[](int index) const; |
177 | int size() const; | |
178 | auto operator[](int index) { | |
179 | struct { | |
180 | Model& model; | |
181 | int index; | |
182 | operator const ModelElement&() { | |
183 | return model.at(index); | |
184 | } | |
185 | auto& operator=(const ModelElement& newData) { | |
186 | model.assignAt(index, newData); | |
187 | return *this; | |
188 | } | |
189 | const auto* operator&() { | |
190 | return &(this->operator const ModelElement&()); | |
191 | } | |
192 | } result{*this, index}; | |
193 | return result; | |
194 | } | |
3 | 195 | }; |
196 | ||
151 | 197 | void save(const Model& model, QIODevice *device); |
200 | 198 | void updateHeaderNameField(Model& model, const QString &name); |
212
27259810da6d
Rewrite dependency loading
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
211
diff
changeset
|
199 | |
27259810da6d
Rewrite dependency loading
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
211
diff
changeset
|
200 | template<typename T> |
27259810da6d
Rewrite dependency loading
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
211
diff
changeset
|
201 | void iterate(const Model& model, std::function<void(const T&)> fn) |
27259810da6d
Rewrite dependency loading
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
211
diff
changeset
|
202 | { |
27259810da6d
Rewrite dependency loading
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
211
diff
changeset
|
203 | for (int i = 0; i < model.size(); ++i) { |
27259810da6d
Rewrite dependency loading
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
211
diff
changeset
|
204 | if (std::holds_alternative<T>(model[i])) { |
27259810da6d
Rewrite dependency loading
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
211
diff
changeset
|
205 | fn(std::get<T>(model[i])); |
27259810da6d
Rewrite dependency loading
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
211
diff
changeset
|
206 | } |
27259810da6d
Rewrite dependency loading
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
211
diff
changeset
|
207 | } |
27259810da6d
Rewrite dependency loading
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
211
diff
changeset
|
208 | } |