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 | #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 | ||
114 | void Model::remove(int index) | |
115 | { | |
116 | if (index >= 0 and index < this->size()) { | |
117 | Q_EMIT this->beginRemoveRows({}, index, index); | |
118 | this->body.erase(this->body.begin() + index); | |
119 | Q_EMIT this->endRemoveRows(); | |
133
e39326ee48dc
Begin work on edit history
Teemu Piippo <teemu@hecknology.net>
parents:
112
diff
changeset
|
120 | } |
e39326ee48dc
Begin work on edit history
Teemu Piippo <teemu@hecknology.net>
parents:
112
diff
changeset
|
121 | } |
e39326ee48dc
Begin work on edit history
Teemu Piippo <teemu@hecknology.net>
parents:
112
diff
changeset
|
122 | |
200 | 123 | int Model::rowCount(const QModelIndex &) const |
8
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
3
diff
changeset
|
124 | { |
137
fb9990772357
Add documentation to model.cpp
Teemu Piippo <teemu@hecknology.net>
parents:
133
diff
changeset
|
125 | return this->size(); |
8
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
3
diff
changeset
|
126 | } |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
3
diff
changeset
|
127 | |
200 | 128 | 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
|
129 | { |
200 | 130 | const int i = index.row(); |
8
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
3
diff
changeset
|
131 | switch(role) |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
3
diff
changeset
|
132 | { |
200 | 133 | /* |
158
5bd755eaa5a8
Add icons from ionicons
Teemu Piippo <teemu@hecknology.net>
parents:
153
diff
changeset
|
134 | case Qt::DecorationRole: |
5bd755eaa5a8
Add icons from ionicons
Teemu Piippo <teemu@hecknology.net>
parents:
153
diff
changeset
|
135 | return QPixmap{object->iconName()}.scaledToHeight(24); |
200 | 136 | */ |
8
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
3
diff
changeset
|
137 | case Qt::DisplayRole: |
200 | 138 | return modelElementToString(this->body[i].data); |
139 | /* | |
8
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
3
diff
changeset
|
140 | case Qt::ForegroundRole: |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
3
diff
changeset
|
141 | return object->textRepresentationForeground(); |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
3
diff
changeset
|
142 | case Qt::BackgroundRole: |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
3
diff
changeset
|
143 | return object->textRepresentationBackground(); |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
3
diff
changeset
|
144 | case Qt::FontRole: |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
3
diff
changeset
|
145 | return object->textRepresentationFont(); |
200 | 146 | */ |
8
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
3
diff
changeset
|
147 | default: |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
3
diff
changeset
|
148 | return {}; |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
3
diff
changeset
|
149 | } |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
3
diff
changeset
|
150 | } |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
3
diff
changeset
|
151 | |
200 | 152 | const ModelElement &Model::operator[](int index) const |
153 | { | |
154 | return this->body[index].data; | |
155 | } | |
156 | ||
157 | int Model::size() const | |
51 | 158 | { |
200 | 159 | return this->body.size(); |
160 | } | |
161 | ||
162 | void save(const Model &model, QIODevice *device) | |
163 | { | |
164 | QTextStream out{device}; | |
165 | for (int i = 0; i < model.size(); ++i) { | |
166 | out << modelElementToString(model[i]) << "\r\n"; | |
173
8a3047468994
Fix performance issues in Model::find
Teemu Piippo <teemu@hecknology.net>
parents:
159
diff
changeset
|
167 | } |
51 | 168 | } |
169 | ||
137
fb9990772357
Add documentation to model.cpp
Teemu Piippo <teemu@hecknology.net>
parents:
133
diff
changeset
|
170 | /** |
141 | 171 | * @brief Sets the path to the model |
172 | * @param path New path to use | |
173 | */ | |
200 | 174 | void updateHeaderNameField(Model& model, const QString &name) |
141 | 175 | { |
176 | // Update the "Name: 1234.dat" comment | |
200 | 177 | if (model.size() >= 2) { |
178 | if (const Comment* nameObject = std::get_if<Comment>(&model[1])) { | |
179 | if (nameObject->text.startsWith("Name: ")) { | |
180 | model[1] = Comment{"Name: " + name}; | |
141 | 181 | } |
182 | } | |
183 | } | |
184 | } |