Thu, 09 Jun 2022 13:32:55 +0300
more refactoring, dosn't build yet
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 | ||
208
930928b760a2
Add model icons back into the list view
Teemu Piippo <teemu@hecknology.net>
parents:
204
diff
changeset
|
19 | #include <QPixmap> |
3 | 20 | #include "model.h" |
21 | ||
208
930928b760a2
Add model icons back into the list view
Teemu Piippo <teemu@hecknology.net>
parents:
204
diff
changeset
|
22 | static const char* iconPathForElement(const ModelElement& element) |
930928b760a2
Add model icons back into the list view
Teemu Piippo <teemu@hecknology.net>
parents:
204
diff
changeset
|
23 | { |
930928b760a2
Add model icons back into the list view
Teemu Piippo <teemu@hecknology.net>
parents:
204
diff
changeset
|
24 | return std::visit(overloaded{ |
930928b760a2
Add model icons back into the list view
Teemu Piippo <teemu@hecknology.net>
parents:
204
diff
changeset
|
25 | [](const Colored<SubfileReference>&) { |
930928b760a2
Add model icons back into the list view
Teemu Piippo <teemu@hecknology.net>
parents:
204
diff
changeset
|
26 | return ":/icons/linetype-subfile.png"; |
930928b760a2
Add model icons back into the list view
Teemu Piippo <teemu@hecknology.net>
parents:
204
diff
changeset
|
27 | }, |
930928b760a2
Add model icons back into the list view
Teemu Piippo <teemu@hecknology.net>
parents:
204
diff
changeset
|
28 | [](const Colored<LineSegment>&) { |
930928b760a2
Add model icons back into the list view
Teemu Piippo <teemu@hecknology.net>
parents:
204
diff
changeset
|
29 | return ":/icons/linetype-edgeline.png"; |
930928b760a2
Add model icons back into the list view
Teemu Piippo <teemu@hecknology.net>
parents:
204
diff
changeset
|
30 | }, |
930928b760a2
Add model icons back into the list view
Teemu Piippo <teemu@hecknology.net>
parents:
204
diff
changeset
|
31 | [](const Colored<Triangle>&) { |
930928b760a2
Add model icons back into the list view
Teemu Piippo <teemu@hecknology.net>
parents:
204
diff
changeset
|
32 | return ":/icons/linetype-triangle.png"; |
930928b760a2
Add model icons back into the list view
Teemu Piippo <teemu@hecknology.net>
parents:
204
diff
changeset
|
33 | }, |
930928b760a2
Add model icons back into the list view
Teemu Piippo <teemu@hecknology.net>
parents:
204
diff
changeset
|
34 | [](const Colored<Quadrilateral>&) { |
930928b760a2
Add model icons back into the list view
Teemu Piippo <teemu@hecknology.net>
parents:
204
diff
changeset
|
35 | return ":/icons/linetype-quadrilateral.png"; |
930928b760a2
Add model icons back into the list view
Teemu Piippo <teemu@hecknology.net>
parents:
204
diff
changeset
|
36 | }, |
930928b760a2
Add model icons back into the list view
Teemu Piippo <teemu@hecknology.net>
parents:
204
diff
changeset
|
37 | [](const Colored<ConditionalEdge>&) { |
930928b760a2
Add model icons back into the list view
Teemu Piippo <teemu@hecknology.net>
parents:
204
diff
changeset
|
38 | return ":/icons/linetype-conditionaledge.png"; |
930928b760a2
Add model icons back into the list view
Teemu Piippo <teemu@hecknology.net>
parents:
204
diff
changeset
|
39 | }, |
930928b760a2
Add model icons back into the list view
Teemu Piippo <teemu@hecknology.net>
parents:
204
diff
changeset
|
40 | [](const Comment&) { |
930928b760a2
Add model icons back into the list view
Teemu Piippo <teemu@hecknology.net>
parents:
204
diff
changeset
|
41 | return ":/icons/chatbubble-ellipses-outline.png"; |
930928b760a2
Add model icons back into the list view
Teemu Piippo <teemu@hecknology.net>
parents:
204
diff
changeset
|
42 | }, |
930928b760a2
Add model icons back into the list view
Teemu Piippo <teemu@hecknology.net>
parents:
204
diff
changeset
|
43 | [](const Empty&) { |
930928b760a2
Add model icons back into the list view
Teemu Piippo <teemu@hecknology.net>
parents:
204
diff
changeset
|
44 | return ""; |
930928b760a2
Add model icons back into the list view
Teemu Piippo <teemu@hecknology.net>
parents:
204
diff
changeset
|
45 | }, |
930928b760a2
Add model icons back into the list view
Teemu Piippo <teemu@hecknology.net>
parents:
204
diff
changeset
|
46 | [](const ParseError&) { |
930928b760a2
Add model icons back into the list view
Teemu Piippo <teemu@hecknology.net>
parents:
204
diff
changeset
|
47 | return ":/icons/linetype-errorline.png"; |
930928b760a2
Add model icons back into the list view
Teemu Piippo <teemu@hecknology.net>
parents:
204
diff
changeset
|
48 | }, |
930928b760a2
Add model icons back into the list view
Teemu Piippo <teemu@hecknology.net>
parents:
204
diff
changeset
|
49 | }, element); |
930928b760a2
Add model icons back into the list view
Teemu Piippo <teemu@hecknology.net>
parents:
204
diff
changeset
|
50 | } |
930928b760a2
Add model icons back into the list view
Teemu Piippo <teemu@hecknology.net>
parents:
204
diff
changeset
|
51 | |
930928b760a2
Add model icons back into the list view
Teemu Piippo <teemu@hecknology.net>
parents:
204
diff
changeset
|
52 | static QPixmap iconForElement(const ModelElement& element) |
930928b760a2
Add model icons back into the list view
Teemu Piippo <teemu@hecknology.net>
parents:
204
diff
changeset
|
53 | { |
930928b760a2
Add model icons back into the list view
Teemu Piippo <teemu@hecknology.net>
parents:
204
diff
changeset
|
54 | // We avoid processing the same image over and over again by storing it |
930928b760a2
Add model icons back into the list view
Teemu Piippo <teemu@hecknology.net>
parents:
204
diff
changeset
|
55 | // in a static constant. However, we need one per each possible type |
930928b760a2
Add model icons back into the list view
Teemu Piippo <teemu@hecknology.net>
parents:
204
diff
changeset
|
56 | // of ModelElement, so we put the pixmap constant inside a templated lambda, |
930928b760a2
Add model icons back into the list view
Teemu Piippo <teemu@hecknology.net>
parents:
204
diff
changeset
|
57 | // which gets instiated once for each type of ModelElement. |
930928b760a2
Add model icons back into the list view
Teemu Piippo <teemu@hecknology.net>
parents:
204
diff
changeset
|
58 | return std::visit([](auto&& element){ |
930928b760a2
Add model icons back into the list view
Teemu Piippo <teemu@hecknology.net>
parents:
204
diff
changeset
|
59 | static const QPixmap pixmap = QPixmap::fromImage( |
930928b760a2
Add model icons back into the list view
Teemu Piippo <teemu@hecknology.net>
parents:
204
diff
changeset
|
60 | QImage{iconPathForElement(element)} |
930928b760a2
Add model icons back into the list view
Teemu Piippo <teemu@hecknology.net>
parents:
204
diff
changeset
|
61 | .scaledToHeight(24, Qt::SmoothTransformation) |
930928b760a2
Add model icons back into the list view
Teemu Piippo <teemu@hecknology.net>
parents:
204
diff
changeset
|
62 | ); |
930928b760a2
Add model icons back into the list view
Teemu Piippo <teemu@hecknology.net>
parents:
204
diff
changeset
|
63 | return pixmap; |
930928b760a2
Add model icons back into the list view
Teemu Piippo <teemu@hecknology.net>
parents:
204
diff
changeset
|
64 | }, element); |
930928b760a2
Add model icons back into the list view
Teemu Piippo <teemu@hecknology.net>
parents:
204
diff
changeset
|
65 | } |
930928b760a2
Add model icons back into the list view
Teemu Piippo <teemu@hecknology.net>
parents:
204
diff
changeset
|
66 | |
200 | 67 | QString modelElementToString(const ModelElement &element) |
68 | { | |
69 | return std::visit(overloaded{ | |
70 | [](const Colored<SubfileReference>& ref) { | |
71 | return QStringLiteral("1 %1 %2 %3") | |
72 | .arg(ref.color.index) | |
73 | .arg(transformToString(ref.transformation)) | |
74 | .arg(ref.name); | |
75 | }, | |
76 | [](const Colored<LineSegment>& seg) { | |
77 | return QStringLiteral("2 %1 %2 %3") | |
78 | .arg(seg.color.index) | |
79 | .arg(vertexToString(seg.p1)) | |
80 | .arg(vertexToString(seg.p2)); | |
81 | }, | |
82 | [](const Colored<Triangle>& triangle) { | |
83 | return QStringLiteral("3 %1 %2 %3 %4") | |
84 | .arg(triangle.color.index) | |
85 | .arg(vertexToString(triangle.p1)) | |
86 | .arg(vertexToString(triangle.p2)) | |
87 | .arg(vertexToString(triangle.p3)); | |
88 | }, | |
89 | [](const Colored<Quadrilateral>& quad) { | |
90 | return QStringLiteral("4 %1 %2 %3 %4 %5") | |
91 | .arg(quad.color.index) | |
92 | .arg(vertexToString(quad.p1)) | |
93 | .arg(vertexToString(quad.p2)) | |
94 | .arg(vertexToString(quad.p3)) | |
95 | .arg(vertexToString(quad.p4)); | |
96 | }, | |
97 | [](const Colored<ConditionalEdge>& cedge) { | |
98 | return QStringLiteral("5 %1 %2 %3 %4 %5") | |
99 | .arg(cedge.color.index) | |
100 | .arg(vertexToString(cedge.p1)) | |
101 | .arg(vertexToString(cedge.p2)) | |
102 | .arg(vertexToString(cedge.c1)) | |
103 | .arg(vertexToString(cedge.c2)); | |
104 | }, | |
105 | [](const Comment& comment) { | |
106 | return "0 " + comment.text; | |
107 | }, | |
108 | [](const Empty&) { | |
109 | return QStringLiteral(""); | |
110 | }, | |
111 | [](const ParseError& parseError) { | |
112 | return parseError.code; | |
113 | }, | |
114 | }, element); | |
115 | } | |
116 | ||
148 | 117 | Model::Model(QObject *parent) : |
118 | QAbstractListModel{parent} | |
86
4bec0525ef1b
PolygonObjectEditor can now modify the object properly
Teemu Piippo <teemu@hecknology.net>
parents:
76
diff
changeset
|
119 | { |
4bec0525ef1b
PolygonObjectEditor can now modify the object properly
Teemu Piippo <teemu@hecknology.net>
parents:
76
diff
changeset
|
120 | } |
3 | 121 | |
200 | 122 | Model::~Model() |
123 | { | |
124 | } | |
125 | ||
126 | ModelId Model::append(const ModelElement &value) | |
3 | 127 | { |
200 | 128 | const int position = static_cast<int>(this->body.size()); |
129 | const ModelId id = this->runningId; | |
130 | this->runningId.value += 1; | |
131 | Q_EMIT this->beginInsertRows({}, position, position); | |
132 | this->body.push_back({value, id}); | |
133 | this->positions[id] = position; | |
134 | Q_EMIT this->endInsertRows(); | |
135 | return id; | |
136 | } | |
137 | ||
138 | const ModelElement &Model::at(int position) const | |
139 | { | |
140 | return this->body[position].data; | |
3 | 141 | } |
142 | ||
200 | 143 | ModelId Model::idAt(int position) const |
144 | { | |
145 | return this->body[position].id; | |
146 | } | |
147 | ||
148 | void Model::assignAt(int position, const ModelElement &element) | |
133
e39326ee48dc
Begin work on edit history
Teemu Piippo <teemu@hecknology.net>
parents:
112
diff
changeset
|
149 | { |
200 | 150 | this->body[position].data = element; |
151 | const QModelIndex index = this->index(position); | |
152 | Q_EMIT this->dataChanged(index, index); | |
153 | } | |
154 | ||
155 | std::optional<int> Model::find(ModelId id) const | |
156 | { | |
157 | return pointerToOptional(findInMap(this->positions, id)); | |
158 | } | |
159 | ||
204
52e10e8d88cc
Concentrate model editing into one coroutine inside main()
Teemu Piippo <teemu@hecknology.net>
parents:
200
diff
changeset
|
160 | template<typename K, typename V> |
52e10e8d88cc
Concentrate model editing into one coroutine inside main()
Teemu Piippo <teemu@hecknology.net>
parents:
200
diff
changeset
|
161 | void removeFromMap(std::map<K, V>& map, const K& key) |
52e10e8d88cc
Concentrate model editing into one coroutine inside main()
Teemu Piippo <teemu@hecknology.net>
parents:
200
diff
changeset
|
162 | { |
52e10e8d88cc
Concentrate model editing into one coroutine inside main()
Teemu Piippo <teemu@hecknology.net>
parents:
200
diff
changeset
|
163 | const auto it = map.find(key); |
52e10e8d88cc
Concentrate model editing into one coroutine inside main()
Teemu Piippo <teemu@hecknology.net>
parents:
200
diff
changeset
|
164 | if (it != map.end()) { |
52e10e8d88cc
Concentrate model editing into one coroutine inside main()
Teemu Piippo <teemu@hecknology.net>
parents:
200
diff
changeset
|
165 | map.erase(it); |
52e10e8d88cc
Concentrate model editing into one coroutine inside main()
Teemu Piippo <teemu@hecknology.net>
parents:
200
diff
changeset
|
166 | } |
52e10e8d88cc
Concentrate model editing into one coroutine inside main()
Teemu Piippo <teemu@hecknology.net>
parents:
200
diff
changeset
|
167 | } |
52e10e8d88cc
Concentrate model editing into one coroutine inside main()
Teemu Piippo <teemu@hecknology.net>
parents:
200
diff
changeset
|
168 | |
200 | 169 | void Model::remove(int index) |
170 | { | |
171 | if (index >= 0 and index < this->size()) { | |
172 | Q_EMIT this->beginRemoveRows({}, index, index); | |
204
52e10e8d88cc
Concentrate model editing into one coroutine inside main()
Teemu Piippo <teemu@hecknology.net>
parents:
200
diff
changeset
|
173 | removeFromMap(this->positions, this->body[index].id); |
200 | 174 | this->body.erase(this->body.begin() + index); |
204
52e10e8d88cc
Concentrate model editing into one coroutine inside main()
Teemu Piippo <teemu@hecknology.net>
parents:
200
diff
changeset
|
175 | for (int i = index; i < this->size(); ++i) { |
52e10e8d88cc
Concentrate model editing into one coroutine inside main()
Teemu Piippo <teemu@hecknology.net>
parents:
200
diff
changeset
|
176 | this->positions[this->body[i].id] = i; |
52e10e8d88cc
Concentrate model editing into one coroutine inside main()
Teemu Piippo <teemu@hecknology.net>
parents:
200
diff
changeset
|
177 | } |
200 | 178 | Q_EMIT this->endRemoveRows(); |
133
e39326ee48dc
Begin work on edit history
Teemu Piippo <teemu@hecknology.net>
parents:
112
diff
changeset
|
179 | } |
e39326ee48dc
Begin work on edit history
Teemu Piippo <teemu@hecknology.net>
parents:
112
diff
changeset
|
180 | } |
e39326ee48dc
Begin work on edit history
Teemu Piippo <teemu@hecknology.net>
parents:
112
diff
changeset
|
181 | |
200 | 182 | int Model::rowCount(const QModelIndex &) const |
8
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
3
diff
changeset
|
183 | { |
137
fb9990772357
Add documentation to model.cpp
Teemu Piippo <teemu@hecknology.net>
parents:
133
diff
changeset
|
184 | return this->size(); |
8
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
3
diff
changeset
|
185 | } |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
3
diff
changeset
|
186 | |
200 | 187 | QVariant Model::data(const QModelIndex &index, int role) const |
8
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
3
diff
changeset
|
188 | { |
200 | 189 | const int i = index.row(); |
208
930928b760a2
Add model icons back into the list view
Teemu Piippo <teemu@hecknology.net>
parents:
204
diff
changeset
|
190 | const ModelElement& element = this->body[i].data; |
8
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
3
diff
changeset
|
191 | switch(role) |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
3
diff
changeset
|
192 | { |
158
5bd755eaa5a8
Add icons from ionicons
Teemu Piippo <teemu@hecknology.net>
parents:
153
diff
changeset
|
193 | case Qt::DecorationRole: |
208
930928b760a2
Add model icons back into the list view
Teemu Piippo <teemu@hecknology.net>
parents:
204
diff
changeset
|
194 | return iconForElement(element); |
8
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
3
diff
changeset
|
195 | case Qt::DisplayRole: |
208
930928b760a2
Add model icons back into the list view
Teemu Piippo <teemu@hecknology.net>
parents:
204
diff
changeset
|
196 | return modelElementToString(element); |
200 | 197 | /* |
8
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
3
diff
changeset
|
198 | case Qt::ForegroundRole: |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
3
diff
changeset
|
199 | return object->textRepresentationForeground(); |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
3
diff
changeset
|
200 | case Qt::BackgroundRole: |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
3
diff
changeset
|
201 | return object->textRepresentationBackground(); |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
3
diff
changeset
|
202 | case Qt::FontRole: |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
3
diff
changeset
|
203 | return object->textRepresentationFont(); |
200 | 204 | */ |
8
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
3
diff
changeset
|
205 | default: |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
3
diff
changeset
|
206 | return {}; |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
3
diff
changeset
|
207 | } |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
3
diff
changeset
|
208 | } |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
3
diff
changeset
|
209 | |
200 | 210 | const ModelElement &Model::operator[](int index) const |
211 | { | |
212 | return this->body[index].data; | |
213 | } | |
214 | ||
215 | int Model::size() const | |
51 | 216 | { |
200 | 217 | return this->body.size(); |
218 | } | |
219 | ||
220 | void save(const Model &model, QIODevice *device) | |
221 | { | |
222 | QTextStream out{device}; | |
223 | for (int i = 0; i < model.size(); ++i) { | |
224 | out << modelElementToString(model[i]) << "\r\n"; | |
173
8a3047468994
Fix performance issues in Model::find
Teemu Piippo <teemu@hecknology.net>
parents:
159
diff
changeset
|
225 | } |
51 | 226 | } |
227 | ||
137
fb9990772357
Add documentation to model.cpp
Teemu Piippo <teemu@hecknology.net>
parents:
133
diff
changeset
|
228 | /** |
141 | 229 | * @brief Sets the path to the model |
230 | * @param path New path to use | |
231 | */ | |
200 | 232 | void updateHeaderNameField(Model& model, const QString &name) |
141 | 233 | { |
234 | // Update the "Name: 1234.dat" comment | |
200 | 235 | if (model.size() >= 2) { |
236 | if (const Comment* nameObject = std::get_if<Comment>(&model[1])) { | |
237 | if (nameObject->text.startsWith("Name: ")) { | |
238 | model[1] = Comment{"Name: " + name}; | |
141 | 239 | } |
240 | } | |
241 | } | |
242 | } |