Wed, 22 Sep 2021 00:25:31 +0300
Merge commit
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" | |
23 | #include "header.h" | |
14 | 24 | #include "linetypes/object.h" |
21 | 25 | #include "gl/common.h" |
3 | 26 | |
8
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
6
diff
changeset
|
27 | enum class HeaderProperty |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
6
diff
changeset
|
28 | { |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
6
diff
changeset
|
29 | Name |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
6
diff
changeset
|
30 | }; |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
6
diff
changeset
|
31 | |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
6
diff
changeset
|
32 | class Model : public QAbstractListModel |
3 | 33 | { |
34 | Q_OBJECT | |
35 | public: | |
36 | class EditContext; | |
21 | 37 | Model(QObject* parent = nullptr); |
5 | 38 | Model(const Model&) = delete; |
3 | 39 | int size() const; |
133
e39326ee48dc
Begin work on edit history
Teemu Piippo <teemu@hecknology.net>
parents:
117
diff
changeset
|
40 | ldraw::id_t at(int index) const; |
3 | 41 | EditContext edit(); |
8
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
6
diff
changeset
|
42 | int rowCount(const QModelIndex&) const override; |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
6
diff
changeset
|
43 | QVariant data(const QModelIndex& index, int role) const override; |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
6
diff
changeset
|
44 | QVariant getHeaderProperty(const HeaderProperty property); |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
6
diff
changeset
|
45 | const QString& getName() const; |
35
98906a94732f
renamed the linetypes namespace to ldraw namespace and added more structures to it
Teemu Piippo <teemu@hecknology.net>
parents:
26
diff
changeset
|
46 | QVariant getObjectProperty(const int index, const ldraw::Property property) const; |
21 | 47 | std::vector<gl::Polygon> getPolygons(class DocumentManager* documents) const; |
73
97df974b5ed5
ldraw::Id is now templated for extra type safety
Teemu Piippo <teemu@hecknology.net>
parents:
51
diff
changeset
|
48 | QModelIndex lookup(ldraw::id_t id) const; |
97df974b5ed5
ldraw::Id is now templated for extra type safety
Teemu Piippo <teemu@hecknology.net>
parents:
51
diff
changeset
|
49 | ldraw::id_t resolve(const QModelIndex& index) const; |
97df974b5ed5
ldraw::Id is now templated for extra type safety
Teemu Piippo <teemu@hecknology.net>
parents:
51
diff
changeset
|
50 | template<typename R> |
97df974b5ed5
ldraw::Id is now templated for extra type safety
Teemu Piippo <teemu@hecknology.net>
parents:
51
diff
changeset
|
51 | ldraw::Id<R> checkType(ldraw::id_t id) const; |
97df974b5ed5
ldraw::Id is now templated for extra type safety
Teemu Piippo <teemu@hecknology.net>
parents:
51
diff
changeset
|
52 | template<typename R> |
116 | 53 | const R* get(ldraw::Id<R> id) const; |
54 | template<typename R> | |
55 | struct Get2Result | |
56 | { | |
57 | QModelIndex index; | |
58 | const R* object; | |
59 | }; | |
60 | template<typename R> | |
61 | Get2Result<R> get2(ldraw::Id<R> id) const; | |
117 | 62 | template<typename R, typename Fn> |
63 | void apply(Fn f); | |
64 | template<typename R, typename Fn> | |
65 | void apply(Fn f) const; | |
112 | 66 | Q_SIGNALS: |
73
97df974b5ed5
ldraw::Id is now templated for extra type safety
Teemu Piippo <teemu@hecknology.net>
parents:
51
diff
changeset
|
67 | void objectAdded(ldraw::id_t id, int position); |
3 | 68 | private: |
35
98906a94732f
renamed the linetypes namespace to ldraw namespace and added more structures to it
Teemu Piippo <teemu@hecknology.net>
parents:
26
diff
changeset
|
69 | using ModelObjectPointer = std::unique_ptr<ldraw::Object>; |
3 | 70 | template<typename T, typename... Args> |
73
97df974b5ed5
ldraw::Id is now templated for extra type safety
Teemu Piippo <teemu@hecknology.net>
parents:
51
diff
changeset
|
71 | ldraw::Id<T> append(Args&&... args); |
8
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
6
diff
changeset
|
72 | void append(ModelObjectPointer&& object); |
3 | 73 | template<typename T, typename... Args> |
76
7c4a63a02632
finished splitQuadrilateral theoretically (untested)
Teemu Piippo <teemu@hecknology.net>
parents:
73
diff
changeset
|
74 | ldraw::Id<T> insert(std::size_t position, Args&&... args); |
7c4a63a02632
finished splitQuadrilateral theoretically (untested)
Teemu Piippo <teemu@hecknology.net>
parents:
73
diff
changeset
|
75 | void remove(int position); |
51 | 76 | ldraw::Object* objectAt(const QModelIndex& index); |
77 | const ldraw::Object* objectAt(const QModelIndex& index) const; | |
73
97df974b5ed5
ldraw::Id is now templated for extra type safety
Teemu Piippo <teemu@hecknology.net>
parents:
51
diff
changeset
|
78 | template<typename T> |
97df974b5ed5
ldraw::Id is now templated for extra type safety
Teemu Piippo <teemu@hecknology.net>
parents:
51
diff
changeset
|
79 | T* objectAt(ldraw::Id<T> id) |
97df974b5ed5
ldraw::Id is now templated for extra type safety
Teemu Piippo <teemu@hecknology.net>
parents:
51
diff
changeset
|
80 | { |
97df974b5ed5
ldraw::Id is now templated for extra type safety
Teemu Piippo <teemu@hecknology.net>
parents:
51
diff
changeset
|
81 | return static_cast<T*>(this->objectAt(this->lookup(id))); |
97df974b5ed5
ldraw::Id is now templated for extra type safety
Teemu Piippo <teemu@hecknology.net>
parents:
51
diff
changeset
|
82 | } |
97df974b5ed5
ldraw::Id is now templated for extra type safety
Teemu Piippo <teemu@hecknology.net>
parents:
51
diff
changeset
|
83 | template<typename T> |
97df974b5ed5
ldraw::Id is now templated for extra type safety
Teemu Piippo <teemu@hecknology.net>
parents:
51
diff
changeset
|
84 | const T* objectAt(ldraw::Id<T> id) const |
97df974b5ed5
ldraw::Id is now templated for extra type safety
Teemu Piippo <teemu@hecknology.net>
parents:
51
diff
changeset
|
85 | { |
97df974b5ed5
ldraw::Id is now templated for extra type safety
Teemu Piippo <teemu@hecknology.net>
parents:
51
diff
changeset
|
86 | return static_cast<const T*>(this->objectAt(this->lookup(id))); |
97df974b5ed5
ldraw::Id is now templated for extra type safety
Teemu Piippo <teemu@hecknology.net>
parents:
51
diff
changeset
|
87 | } |
51 | 88 | void getObjectPolygons( |
89 | const int index, | |
90 | std::vector<gl::Polygon>& polygons_out, | |
91 | ldraw::GetPolygonsContext* context) const; | |
133
e39326ee48dc
Begin work on edit history
Teemu Piippo <teemu@hecknology.net>
parents:
117
diff
changeset
|
92 | void editFinished(); |
e39326ee48dc
Begin work on edit history
Teemu Piippo <teemu@hecknology.net>
parents:
117
diff
changeset
|
93 | void objectModified(ldraw::id_t id); |
3 | 94 | bool modified = false; |
95 | QString path; | |
96 | LDHeader header; | |
97 | std::vector<ModelObjectPointer> body; | |
73
97df974b5ed5
ldraw::Id is now templated for extra type safety
Teemu Piippo <teemu@hecknology.net>
parents:
51
diff
changeset
|
98 | std::map<ldraw::id_t, ldraw::Object*> objectsById; |
21 | 99 | mutable std::vector<gl::Polygon> cachedPolygons; |
100 | mutable bool needRecache = true; | |
133
e39326ee48dc
Begin work on edit history
Teemu Piippo <teemu@hecknology.net>
parents:
117
diff
changeset
|
101 | int editCounter = 0; |
3 | 102 | }; |
103 | ||
73
97df974b5ed5
ldraw::Id is now templated for extra type safety
Teemu Piippo <teemu@hecknology.net>
parents:
51
diff
changeset
|
104 | /** |
117 | 105 | * \brief Call @c f for all objects of type \c R |
106 | */ | |
107 | template<typename R, typename Fn> | |
108 | void Model::apply(Fn f) const | |
109 | { | |
110 | for (const ModelObjectPointer& object : this->body) | |
111 | { | |
112 | const R* subobject = dynamic_cast<const R*>(object.get()); | |
113 | if (subobject != nullptr) | |
114 | { | |
115 | f(subobject); | |
116 | } | |
117 | } | |
118 | } | |
119 | ||
120 | /** | |
121 | * \brief Call @c f for all objects of type \c R | |
122 | */ | |
123 | template<typename R, typename Fn> | |
124 | void Model::apply(Fn f) | |
125 | { | |
126 | for (ModelObjectPointer& object : this->body) | |
127 | { | |
128 | R* subobject = dynamic_cast<R*>(object.get()); | |
129 | if (subobject != nullptr) | |
130 | { | |
131 | f(subobject); | |
132 | } | |
133 | } | |
134 | } | |
135 | ||
136 | /** | |
73
97df974b5ed5
ldraw::Id is now templated for extra type safety
Teemu Piippo <teemu@hecknology.net>
parents:
51
diff
changeset
|
137 | * \brief Checks type of object behind id |
97df974b5ed5
ldraw::Id is now templated for extra type safety
Teemu Piippo <teemu@hecknology.net>
parents:
51
diff
changeset
|
138 | * Checks whether the specified id refers to an object of the specified type. |
97df974b5ed5
ldraw::Id is now templated for extra type safety
Teemu Piippo <teemu@hecknology.net>
parents:
51
diff
changeset
|
139 | * \returns id casted to subclass if appropriate, null id otherwise |
97df974b5ed5
ldraw::Id is now templated for extra type safety
Teemu Piippo <teemu@hecknology.net>
parents:
51
diff
changeset
|
140 | */ |
97df974b5ed5
ldraw::Id is now templated for extra type safety
Teemu Piippo <teemu@hecknology.net>
parents:
51
diff
changeset
|
141 | template<typename R> |
97df974b5ed5
ldraw::Id is now templated for extra type safety
Teemu Piippo <teemu@hecknology.net>
parents:
51
diff
changeset
|
142 | ldraw::Id<R> Model::checkType(ldraw::id_t id) const |
97df974b5ed5
ldraw::Id is now templated for extra type safety
Teemu Piippo <teemu@hecknology.net>
parents:
51
diff
changeset
|
143 | { |
97df974b5ed5
ldraw::Id is now templated for extra type safety
Teemu Piippo <teemu@hecknology.net>
parents:
51
diff
changeset
|
144 | if (dynamic_cast<const R*>(this->objectAt(this->lookup(id))) != nullptr) |
97df974b5ed5
ldraw::Id is now templated for extra type safety
Teemu Piippo <teemu@hecknology.net>
parents:
51
diff
changeset
|
145 | { |
97df974b5ed5
ldraw::Id is now templated for extra type safety
Teemu Piippo <teemu@hecknology.net>
parents:
51
diff
changeset
|
146 | return ldraw::Id<R>{id.value}; |
97df974b5ed5
ldraw::Id is now templated for extra type safety
Teemu Piippo <teemu@hecknology.net>
parents:
51
diff
changeset
|
147 | } |
97df974b5ed5
ldraw::Id is now templated for extra type safety
Teemu Piippo <teemu@hecknology.net>
parents:
51
diff
changeset
|
148 | else |
97df974b5ed5
ldraw::Id is now templated for extra type safety
Teemu Piippo <teemu@hecknology.net>
parents:
51
diff
changeset
|
149 | { |
97df974b5ed5
ldraw::Id is now templated for extra type safety
Teemu Piippo <teemu@hecknology.net>
parents:
51
diff
changeset
|
150 | return ldraw::NULL_ID; |
97df974b5ed5
ldraw::Id is now templated for extra type safety
Teemu Piippo <teemu@hecknology.net>
parents:
51
diff
changeset
|
151 | } |
97df974b5ed5
ldraw::Id is now templated for extra type safety
Teemu Piippo <teemu@hecknology.net>
parents:
51
diff
changeset
|
152 | } |
97df974b5ed5
ldraw::Id is now templated for extra type safety
Teemu Piippo <teemu@hecknology.net>
parents:
51
diff
changeset
|
153 | |
3 | 154 | template<typename T, typename... Args> |
73
97df974b5ed5
ldraw::Id is now templated for extra type safety
Teemu Piippo <teemu@hecknology.net>
parents:
51
diff
changeset
|
155 | ldraw::Id<T> Model::append(Args&&... args) |
3 | 156 | { |
111
1f42c03fafca
Draw tool actually adds objects now
Teemu Piippo <teemu@hecknology.net>
parents:
81
diff
changeset
|
157 | const int position = static_cast<int>(this->body.size()); |
112 | 158 | Q_EMIT beginInsertRows({}, position, position); |
3 | 159 | this->body.push_back(std::make_unique<T>(args...)); |
35
98906a94732f
renamed the linetypes namespace to ldraw namespace and added more structures to it
Teemu Piippo <teemu@hecknology.net>
parents:
26
diff
changeset
|
160 | ldraw::Object* pointer = this->body.back().get(); |
6 | 161 | this->objectsById[pointer->id] = pointer; |
112 | 162 | Q_EMIT objectAdded(pointer->id, static_cast<int>(this->body.size() - 1)); |
163 | Q_EMIT endInsertRows(); | |
111
1f42c03fafca
Draw tool actually adds objects now
Teemu Piippo <teemu@hecknology.net>
parents:
81
diff
changeset
|
164 | this->needRecache = true; |
76
7c4a63a02632
finished splitQuadrilateral theoretically (untested)
Teemu Piippo <teemu@hecknology.net>
parents:
73
diff
changeset
|
165 | return ldraw::Id<T>{pointer->id.value}; |
3 | 166 | } |
167 | ||
168 | template<typename T, typename... Args> | |
111
1f42c03fafca
Draw tool actually adds objects now
Teemu Piippo <teemu@hecknology.net>
parents:
81
diff
changeset
|
169 | ldraw::Id<T> Model::insert(const std::size_t position, Args&&... args) |
3 | 170 | { |
112 | 171 | Q_EMIT beginInsertRows({}, position, position); |
76
7c4a63a02632
finished splitQuadrilateral theoretically (untested)
Teemu Piippo <teemu@hecknology.net>
parents:
73
diff
changeset
|
172 | this->body.insert(std::begin(this->body) + position, std::make_unique<T>(args...)); |
35
98906a94732f
renamed the linetypes namespace to ldraw namespace and added more structures to it
Teemu Piippo <teemu@hecknology.net>
parents:
26
diff
changeset
|
173 | ldraw::Object* pointer = this->body[position].get(); |
6 | 174 | this->objectsById[pointer->id] = pointer; |
112 | 175 | Q_EMIT objectAdded(pointer->id, static_cast<int>(position)); |
176 | Q_EMIT endInsertRows(); | |
111
1f42c03fafca
Draw tool actually adds objects now
Teemu Piippo <teemu@hecknology.net>
parents:
81
diff
changeset
|
177 | this->needRecache = true; |
76
7c4a63a02632
finished splitQuadrilateral theoretically (untested)
Teemu Piippo <teemu@hecknology.net>
parents:
73
diff
changeset
|
178 | return ldraw::Id<T>{pointer->id.value}; |
3 | 179 | } |
73
97df974b5ed5
ldraw::Id is now templated for extra type safety
Teemu Piippo <teemu@hecknology.net>
parents:
51
diff
changeset
|
180 | |
97df974b5ed5
ldraw::Id is now templated for extra type safety
Teemu Piippo <teemu@hecknology.net>
parents:
51
diff
changeset
|
181 | template<typename R> |
116 | 182 | const R* Model::get(ldraw::Id<R> id) const |
73
97df974b5ed5
ldraw::Id is now templated for extra type safety
Teemu Piippo <teemu@hecknology.net>
parents:
51
diff
changeset
|
183 | { |
116 | 184 | return this->get2(id).object; |
185 | } | |
186 | ||
187 | template<typename R> | |
188 | Model::Get2Result<R> Model::get2(const ldraw::Id<R> id) const | |
189 | { | |
190 | Get2Result<R> result; | |
191 | result.index = this->lookup(id); | |
192 | if (result.index.isValid()) | |
76
7c4a63a02632
finished splitQuadrilateral theoretically (untested)
Teemu Piippo <teemu@hecknology.net>
parents:
73
diff
changeset
|
193 | { |
116 | 194 | result.object = static_cast<const R*>(this->objectAt(result.index)); |
81
62373840e33a
object editor widgets start to form up
Teemu Piippo <teemu@hecknology.net>
parents:
76
diff
changeset
|
195 | } |
62373840e33a
object editor widgets start to form up
Teemu Piippo <teemu@hecknology.net>
parents:
76
diff
changeset
|
196 | else |
62373840e33a
object editor widgets start to form up
Teemu Piippo <teemu@hecknology.net>
parents:
76
diff
changeset
|
197 | { |
116 | 198 | result.object = nullptr; |
81
62373840e33a
object editor widgets start to form up
Teemu Piippo <teemu@hecknology.net>
parents:
76
diff
changeset
|
199 | } |
116 | 200 | return result; |
73
97df974b5ed5
ldraw::Id is now templated for extra type safety
Teemu Piippo <teemu@hecknology.net>
parents:
51
diff
changeset
|
201 | } |