Wed, 08 Jun 2022 22:29:44 +0300
More refactor, merged main.h, basics.h and utility.h into one header file basics.h and removed plenty of unused code
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 | #include "model.h" |
20 | ||
200 | 21 | QString modelElementToString(const ModelElement &element) |
22 | { | |
23 | return std::visit(overloaded{ | |
24 | [](const Colored<SubfileReference>& ref) { | |
25 | return QStringLiteral("1 %1 %2 %3") | |
26 | .arg(ref.color.index) | |
27 | .arg(transformToString(ref.transformation)) | |
28 | .arg(ref.name); | |
29 | }, | |
30 | [](const Colored<LineSegment>& seg) { | |
31 | return QStringLiteral("2 %1 %2 %3") | |
32 | .arg(seg.color.index) | |
33 | .arg(vertexToString(seg.p1)) | |
34 | .arg(vertexToString(seg.p2)); | |
35 | }, | |
36 | [](const Colored<Triangle>& triangle) { | |
37 | return QStringLiteral("3 %1 %2 %3 %4") | |
38 | .arg(triangle.color.index) | |
39 | .arg(vertexToString(triangle.p1)) | |
40 | .arg(vertexToString(triangle.p2)) | |
41 | .arg(vertexToString(triangle.p3)); | |
42 | }, | |
43 | [](const Colored<Quadrilateral>& quad) { | |
44 | return QStringLiteral("4 %1 %2 %3 %4 %5") | |
45 | .arg(quad.color.index) | |
46 | .arg(vertexToString(quad.p1)) | |
47 | .arg(vertexToString(quad.p2)) | |
48 | .arg(vertexToString(quad.p3)) | |
49 | .arg(vertexToString(quad.p4)); | |
50 | }, | |
51 | [](const Colored<ConditionalEdge>& cedge) { | |
52 | return QStringLiteral("5 %1 %2 %3 %4 %5") | |
53 | .arg(cedge.color.index) | |
54 | .arg(vertexToString(cedge.p1)) | |
55 | .arg(vertexToString(cedge.p2)) | |
56 | .arg(vertexToString(cedge.c1)) | |
57 | .arg(vertexToString(cedge.c2)); | |
58 | }, | |
59 | [](const Comment& comment) { | |
60 | return "0 " + comment.text; | |
61 | }, | |
62 | [](const Empty&) { | |
63 | return QStringLiteral(""); | |
64 | }, | |
65 | [](const ParseError& parseError) { | |
66 | return parseError.code; | |
67 | }, | |
68 | }, element); | |
69 | } | |
70 | ||
148 | 71 | Model::Model(QObject *parent) : |
72 | QAbstractListModel{parent} | |
86
4bec0525ef1b
PolygonObjectEditor can now modify the object properly
Teemu Piippo <teemu@hecknology.net>
parents:
76
diff
changeset
|
73 | { |
4bec0525ef1b
PolygonObjectEditor can now modify the object properly
Teemu Piippo <teemu@hecknology.net>
parents:
76
diff
changeset
|
74 | } |
3 | 75 | |
200 | 76 | Model::~Model() |
77 | { | |
78 | } | |
79 | ||
80 | ModelId Model::append(const ModelElement &value) | |
3 | 81 | { |
200 | 82 | const int position = static_cast<int>(this->body.size()); |
83 | const ModelId id = this->runningId; | |
84 | this->runningId.value += 1; | |
85 | Q_EMIT this->beginInsertRows({}, position, position); | |
86 | this->body.push_back({value, id}); | |
87 | this->positions[id] = position; | |
88 | Q_EMIT this->endInsertRows(); | |
89 | return id; | |
90 | } | |
91 | ||
92 | const ModelElement &Model::at(int position) const | |
93 | { | |
94 | return this->body[position].data; | |
3 | 95 | } |
96 | ||
200 | 97 | ModelId Model::idAt(int position) const |
98 | { | |
99 | return this->body[position].id; | |
100 | } | |
101 | ||
102 | void Model::assignAt(int position, const ModelElement &element) | |
133
e39326ee48dc
Begin work on edit history
Teemu Piippo <teemu@hecknology.net>
parents:
112
diff
changeset
|
103 | { |
200 | 104 | this->body[position].data = element; |
105 | const QModelIndex index = this->index(position); | |
106 | Q_EMIT this->dataChanged(index, index); | |
107 | } | |
108 | ||
109 | std::optional<int> Model::find(ModelId id) const | |
110 | { | |
111 | return pointerToOptional(findInMap(this->positions, id)); | |
112 | } | |
113 | ||
204
52e10e8d88cc
Concentrate model editing into one coroutine inside main()
Teemu Piippo <teemu@hecknology.net>
parents:
200
diff
changeset
|
114 | template<typename K, typename V> |
52e10e8d88cc
Concentrate model editing into one coroutine inside main()
Teemu Piippo <teemu@hecknology.net>
parents:
200
diff
changeset
|
115 | 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
|
116 | { |
52e10e8d88cc
Concentrate model editing into one coroutine inside main()
Teemu Piippo <teemu@hecknology.net>
parents:
200
diff
changeset
|
117 | const auto it = map.find(key); |
52e10e8d88cc
Concentrate model editing into one coroutine inside main()
Teemu Piippo <teemu@hecknology.net>
parents:
200
diff
changeset
|
118 | if (it != map.end()) { |
52e10e8d88cc
Concentrate model editing into one coroutine inside main()
Teemu Piippo <teemu@hecknology.net>
parents:
200
diff
changeset
|
119 | map.erase(it); |
52e10e8d88cc
Concentrate model editing into one coroutine inside main()
Teemu Piippo <teemu@hecknology.net>
parents:
200
diff
changeset
|
120 | } |
52e10e8d88cc
Concentrate model editing into one coroutine inside main()
Teemu Piippo <teemu@hecknology.net>
parents:
200
diff
changeset
|
121 | } |
52e10e8d88cc
Concentrate model editing into one coroutine inside main()
Teemu Piippo <teemu@hecknology.net>
parents:
200
diff
changeset
|
122 | |
200 | 123 | void Model::remove(int index) |
124 | { | |
125 | if (index >= 0 and index < this->size()) { | |
126 | 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
|
127 | removeFromMap(this->positions, this->body[index].id); |
200 | 128 | 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
|
129 | 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
|
130 | 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
|
131 | } |
200 | 132 | Q_EMIT this->endRemoveRows(); |
133
e39326ee48dc
Begin work on edit history
Teemu Piippo <teemu@hecknology.net>
parents:
112
diff
changeset
|
133 | } |
e39326ee48dc
Begin work on edit history
Teemu Piippo <teemu@hecknology.net>
parents:
112
diff
changeset
|
134 | } |
e39326ee48dc
Begin work on edit history
Teemu Piippo <teemu@hecknology.net>
parents:
112
diff
changeset
|
135 | |
200 | 136 | int Model::rowCount(const QModelIndex &) const |
8
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
3
diff
changeset
|
137 | { |
137
fb9990772357
Add documentation to model.cpp
Teemu Piippo <teemu@hecknology.net>
parents:
133
diff
changeset
|
138 | return this->size(); |
8
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
3
diff
changeset
|
139 | } |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
3
diff
changeset
|
140 | |
200 | 141 | 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
|
142 | { |
200 | 143 | const int i = index.row(); |
8
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
3
diff
changeset
|
144 | switch(role) |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
3
diff
changeset
|
145 | { |
200 | 146 | /* |
158
5bd755eaa5a8
Add icons from ionicons
Teemu Piippo <teemu@hecknology.net>
parents:
153
diff
changeset
|
147 | case Qt::DecorationRole: |
5bd755eaa5a8
Add icons from ionicons
Teemu Piippo <teemu@hecknology.net>
parents:
153
diff
changeset
|
148 | return QPixmap{object->iconName()}.scaledToHeight(24); |
200 | 149 | */ |
8
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
3
diff
changeset
|
150 | case Qt::DisplayRole: |
200 | 151 | return modelElementToString(this->body[i].data); |
152 | /* | |
8
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
3
diff
changeset
|
153 | case Qt::ForegroundRole: |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
3
diff
changeset
|
154 | return object->textRepresentationForeground(); |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
3
diff
changeset
|
155 | case Qt::BackgroundRole: |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
3
diff
changeset
|
156 | return object->textRepresentationBackground(); |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
3
diff
changeset
|
157 | case Qt::FontRole: |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
3
diff
changeset
|
158 | return object->textRepresentationFont(); |
200 | 159 | */ |
8
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
3
diff
changeset
|
160 | default: |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
3
diff
changeset
|
161 | return {}; |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
3
diff
changeset
|
162 | } |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
3
diff
changeset
|
163 | } |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
3
diff
changeset
|
164 | |
200 | 165 | const ModelElement &Model::operator[](int index) const |
166 | { | |
167 | return this->body[index].data; | |
168 | } | |
169 | ||
170 | int Model::size() const | |
51 | 171 | { |
200 | 172 | return this->body.size(); |
173 | } | |
174 | ||
175 | void save(const Model &model, QIODevice *device) | |
176 | { | |
177 | QTextStream out{device}; | |
178 | for (int i = 0; i < model.size(); ++i) { | |
179 | out << modelElementToString(model[i]) << "\r\n"; | |
173
8a3047468994
Fix performance issues in Model::find
Teemu Piippo <teemu@hecknology.net>
parents:
159
diff
changeset
|
180 | } |
51 | 181 | } |
182 | ||
137
fb9990772357
Add documentation to model.cpp
Teemu Piippo <teemu@hecknology.net>
parents:
133
diff
changeset
|
183 | /** |
141 | 184 | * @brief Sets the path to the model |
185 | * @param path New path to use | |
186 | */ | |
200 | 187 | void updateHeaderNameField(Model& model, const QString &name) |
141 | 188 | { |
189 | // Update the "Name: 1234.dat" comment | |
200 | 190 | if (model.size() >= 2) { |
191 | if (const Comment* nameObject = std::get_if<Comment>(&model[1])) { | |
192 | if (nameObject->text.startsWith("Name: ")) { | |
193 | model[1] = Comment{"Name: " + name}; | |
141 | 194 | } |
195 | } | |
196 | } | |
197 | } |