Fri, 04 Mar 2022 11:37:50 +0200
Major refactoring
- Model now just stores objects
- Document contains business logic
- Model::EditContext is now ModelEditor, no longer a nested class
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 | ||
8
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
3
diff
changeset
|
19 | #include <QBrush> |
140 | 20 | #include <QFile> |
141 | 21 | #include <QFileInfo> |
8
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
3
diff
changeset
|
22 | #include <QFont> |
145
4dea24d3eda0
Use QSaveFile to save the file more safely
Teemu Piippo <teemu@hecknology.net>
parents:
141
diff
changeset
|
23 | #include <QSaveFile> |
3 | 24 | #include "model.h" |
25 | #include "modeleditcontext.h" | |
148 | 26 | #include "documentmanager.h" |
3 | 27 | |
137
fb9990772357
Add documentation to model.cpp
Teemu Piippo <teemu@hecknology.net>
parents:
133
diff
changeset
|
28 | /** |
fb9990772357
Add documentation to model.cpp
Teemu Piippo <teemu@hecknology.net>
parents:
133
diff
changeset
|
29 | * @brief Constructs a model |
fb9990772357
Add documentation to model.cpp
Teemu Piippo <teemu@hecknology.net>
parents:
133
diff
changeset
|
30 | * @param parent QObject parent to pass forward |
fb9990772357
Add documentation to model.cpp
Teemu Piippo <teemu@hecknology.net>
parents:
133
diff
changeset
|
31 | */ |
148 | 32 | Model::Model(QObject *parent) : |
33 | QAbstractListModel{parent} | |
86
4bec0525ef1b
PolygonObjectEditor can now modify the object properly
Teemu Piippo <teemu@hecknology.net>
parents:
76
diff
changeset
|
34 | { |
4bec0525ef1b
PolygonObjectEditor can now modify the object properly
Teemu Piippo <teemu@hecknology.net>
parents:
76
diff
changeset
|
35 | } |
3 | 36 | |
137
fb9990772357
Add documentation to model.cpp
Teemu Piippo <teemu@hecknology.net>
parents:
133
diff
changeset
|
37 | /** |
fb9990772357
Add documentation to model.cpp
Teemu Piippo <teemu@hecknology.net>
parents:
133
diff
changeset
|
38 | * @returns the amount of elements in the model |
fb9990772357
Add documentation to model.cpp
Teemu Piippo <teemu@hecknology.net>
parents:
133
diff
changeset
|
39 | */ |
3 | 40 | int Model::size() const |
41 | { | |
51 | 42 | return static_cast<int>(this->body.size()); |
3 | 43 | } |
44 | ||
133
e39326ee48dc
Begin work on edit history
Teemu Piippo <teemu@hecknology.net>
parents:
112
diff
changeset
|
45 | /** |
e39326ee48dc
Begin work on edit history
Teemu Piippo <teemu@hecknology.net>
parents:
112
diff
changeset
|
46 | * @brief Looks up the object ID at the specified index. If out of bounds, returns NULL_ID. |
e39326ee48dc
Begin work on edit history
Teemu Piippo <teemu@hecknology.net>
parents:
112
diff
changeset
|
47 | * @param index Index of object to look up |
e39326ee48dc
Begin work on edit history
Teemu Piippo <teemu@hecknology.net>
parents:
112
diff
changeset
|
48 | * @return object ID |
e39326ee48dc
Begin work on edit history
Teemu Piippo <teemu@hecknology.net>
parents:
112
diff
changeset
|
49 | */ |
e39326ee48dc
Begin work on edit history
Teemu Piippo <teemu@hecknology.net>
parents:
112
diff
changeset
|
50 | ldraw::id_t Model::at(int index) const |
e39326ee48dc
Begin work on edit history
Teemu Piippo <teemu@hecknology.net>
parents:
112
diff
changeset
|
51 | { |
e39326ee48dc
Begin work on edit history
Teemu Piippo <teemu@hecknology.net>
parents:
112
diff
changeset
|
52 | if (index >= 0 and index < this->size()) |
e39326ee48dc
Begin work on edit history
Teemu Piippo <teemu@hecknology.net>
parents:
112
diff
changeset
|
53 | { |
e39326ee48dc
Begin work on edit history
Teemu Piippo <teemu@hecknology.net>
parents:
112
diff
changeset
|
54 | return this->body[index]->id; |
e39326ee48dc
Begin work on edit history
Teemu Piippo <teemu@hecknology.net>
parents:
112
diff
changeset
|
55 | } |
e39326ee48dc
Begin work on edit history
Teemu Piippo <teemu@hecknology.net>
parents:
112
diff
changeset
|
56 | else |
e39326ee48dc
Begin work on edit history
Teemu Piippo <teemu@hecknology.net>
parents:
112
diff
changeset
|
57 | { |
e39326ee48dc
Begin work on edit history
Teemu Piippo <teemu@hecknology.net>
parents:
112
diff
changeset
|
58 | return ldraw::NULL_ID; |
e39326ee48dc
Begin work on edit history
Teemu Piippo <teemu@hecknology.net>
parents:
112
diff
changeset
|
59 | } |
e39326ee48dc
Begin work on edit history
Teemu Piippo <teemu@hecknology.net>
parents:
112
diff
changeset
|
60 | } |
e39326ee48dc
Begin work on edit history
Teemu Piippo <teemu@hecknology.net>
parents:
112
diff
changeset
|
61 | |
137
fb9990772357
Add documentation to model.cpp
Teemu Piippo <teemu@hecknology.net>
parents:
133
diff
changeset
|
62 | /** |
fb9990772357
Add documentation to model.cpp
Teemu Piippo <teemu@hecknology.net>
parents:
133
diff
changeset
|
63 | * @brief @overload QAbstractListModel::rowCount |
fb9990772357
Add documentation to model.cpp
Teemu Piippo <teemu@hecknology.net>
parents:
133
diff
changeset
|
64 | * @return size |
fb9990772357
Add documentation to model.cpp
Teemu Piippo <teemu@hecknology.net>
parents:
133
diff
changeset
|
65 | */ |
8
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
3
diff
changeset
|
66 | int Model::rowCount(const QModelIndex&) const |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
3
diff
changeset
|
67 | { |
137
fb9990772357
Add documentation to model.cpp
Teemu Piippo <teemu@hecknology.net>
parents:
133
diff
changeset
|
68 | return this->size(); |
8
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
3
diff
changeset
|
69 | } |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
3
diff
changeset
|
70 | |
137
fb9990772357
Add documentation to model.cpp
Teemu Piippo <teemu@hecknology.net>
parents:
133
diff
changeset
|
71 | /** |
fb9990772357
Add documentation to model.cpp
Teemu Piippo <teemu@hecknology.net>
parents:
133
diff
changeset
|
72 | * @brief @overload QAbstractListModel::data |
fb9990772357
Add documentation to model.cpp
Teemu Piippo <teemu@hecknology.net>
parents:
133
diff
changeset
|
73 | * @param index |
fb9990772357
Add documentation to model.cpp
Teemu Piippo <teemu@hecknology.net>
parents:
133
diff
changeset
|
74 | * @param role |
fb9990772357
Add documentation to model.cpp
Teemu Piippo <teemu@hecknology.net>
parents:
133
diff
changeset
|
75 | * @return QVariant |
fb9990772357
Add documentation to model.cpp
Teemu Piippo <teemu@hecknology.net>
parents:
133
diff
changeset
|
76 | */ |
8
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
3
diff
changeset
|
77 | QVariant Model::data(const QModelIndex& index, int role) const |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
3
diff
changeset
|
78 | { |
151 | 79 | const ldraw::Object* object = (*this)[index.row()]; |
8
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
3
diff
changeset
|
80 | switch(role) |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
3
diff
changeset
|
81 | { |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
3
diff
changeset
|
82 | case Qt::DisplayRole: |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
3
diff
changeset
|
83 | return object->textRepresentation(); |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
3
diff
changeset
|
84 | case Qt::ForegroundRole: |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
3
diff
changeset
|
85 | return object->textRepresentationForeground(); |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
3
diff
changeset
|
86 | case Qt::BackgroundRole: |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
3
diff
changeset
|
87 | return object->textRepresentationBackground(); |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
3
diff
changeset
|
88 | case Qt::FontRole: |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
3
diff
changeset
|
89 | return object->textRepresentationFont(); |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
3
diff
changeset
|
90 | default: |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
3
diff
changeset
|
91 | return {}; |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
3
diff
changeset
|
92 | } |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
3
diff
changeset
|
93 | } |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
3
diff
changeset
|
94 | |
137
fb9990772357
Add documentation to model.cpp
Teemu Piippo <teemu@hecknology.net>
parents:
133
diff
changeset
|
95 | /** |
fb9990772357
Add documentation to model.cpp
Teemu Piippo <teemu@hecknology.net>
parents:
133
diff
changeset
|
96 | * @brief Finds the position of the specified object in the model |
fb9990772357
Add documentation to model.cpp
Teemu Piippo <teemu@hecknology.net>
parents:
133
diff
changeset
|
97 | * @param id Object id to look for |
fb9990772357
Add documentation to model.cpp
Teemu Piippo <teemu@hecknology.net>
parents:
133
diff
changeset
|
98 | * @return model index |
fb9990772357
Add documentation to model.cpp
Teemu Piippo <teemu@hecknology.net>
parents:
133
diff
changeset
|
99 | */ |
150
b6cbba6e29a1
extract polygon cache out of Model
Teemu Piippo <teemu@hecknology.net>
parents:
148
diff
changeset
|
100 | QModelIndex Model::find(ldraw::id_t id) const |
51 | 101 | { |
102 | // FIXME: This linear search will probably cause performance issues | |
103 | for (std::size_t i = 0; i < this->body.size(); i += 1) | |
104 | { | |
105 | if (this->body[i]->id == id) | |
106 | { | |
107 | return this->index(static_cast<int>(i)); | |
108 | } | |
109 | } | |
110 | return {}; | |
111 | } | |
112 | ||
137
fb9990772357
Add documentation to model.cpp
Teemu Piippo <teemu@hecknology.net>
parents:
133
diff
changeset
|
113 | /** |
fb9990772357
Add documentation to model.cpp
Teemu Piippo <teemu@hecknology.net>
parents:
133
diff
changeset
|
114 | * @brief Gets an object id by position in the model |
fb9990772357
Add documentation to model.cpp
Teemu Piippo <teemu@hecknology.net>
parents:
133
diff
changeset
|
115 | * @param index Position of the object in the model |
fb9990772357
Add documentation to model.cpp
Teemu Piippo <teemu@hecknology.net>
parents:
133
diff
changeset
|
116 | * @return id |
fb9990772357
Add documentation to model.cpp
Teemu Piippo <teemu@hecknology.net>
parents:
133
diff
changeset
|
117 | */ |
150
b6cbba6e29a1
extract polygon cache out of Model
Teemu Piippo <teemu@hecknology.net>
parents:
148
diff
changeset
|
118 | ldraw::id_t Model::idAt(const QModelIndex& index) const |
51 | 119 | { |
151 | 120 | return (*this)[index.row()]->id; |
51 | 121 | } |
122 | ||
148 | 123 | #if 0 |
141 | 124 | /** |
125 | * @brief Sets the path to the model | |
126 | * @param path New path to use | |
127 | */ | |
128 | void Model::setPath(const QString &path) | |
129 | { | |
130 | this->storedPath = path; | |
131 | this->header.name = QFileInfo{path}.fileName(); | |
132 | // Update the "Name: 1234.dat" comment | |
133 | if (this->body.size() >= 2) | |
134 | { | |
135 | const ldraw::id_t id = this->body[1]->id; | |
136 | if (this->isA<ldraw::MetaCommand>(id)) | |
137 | { | |
138 | const QString& textBody = this->body[1]->getProperty<ldraw::Property::Text>(); | |
139 | if (textBody.startsWith("Name: ")) | |
140 | { | |
141 | auto editor = this->edit(); | |
142 | editor.setObjectProperty<ldraw::Property::Text>(id, "Name: " + this->header.name); | |
143 | } | |
144 | } | |
145 | } | |
146 | } | |
148 | 147 | #endif |
141 | 148 | |
149 | /** | |
137
fb9990772357
Add documentation to model.cpp
Teemu Piippo <teemu@hecknology.net>
parents:
133
diff
changeset
|
150 | * @brief Adds the given object into the model. |
fb9990772357
Add documentation to model.cpp
Teemu Piippo <teemu@hecknology.net>
parents:
133
diff
changeset
|
151 | * @param object r-value reference to the object |
fb9990772357
Add documentation to model.cpp
Teemu Piippo <teemu@hecknology.net>
parents:
133
diff
changeset
|
152 | */ |
152 | 153 | ldraw::id_t Model::append(ModelObjectPointer&& object) |
8
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
3
diff
changeset
|
154 | { |
76
7c4a63a02632
finished splitQuadrilateral theoretically (untested)
Teemu Piippo <teemu@hecknology.net>
parents:
73
diff
changeset
|
155 | const int position = static_cast<int>(this->body.size()); |
150
b6cbba6e29a1
extract polygon cache out of Model
Teemu Piippo <teemu@hecknology.net>
parents:
148
diff
changeset
|
156 | Q_EMIT this->beginInsertRows({}, position, position); |
8
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
3
diff
changeset
|
157 | this->body.push_back(std::move(object)); |
150
b6cbba6e29a1
extract polygon cache out of Model
Teemu Piippo <teemu@hecknology.net>
parents:
148
diff
changeset
|
158 | Q_EMIT this->endInsertRows(); |
152 | 159 | this->objectsById[object->id] = object.get(); |
160 | return object->id; | |
76
7c4a63a02632
finished splitQuadrilateral theoretically (untested)
Teemu Piippo <teemu@hecknology.net>
parents:
73
diff
changeset
|
161 | } |
7c4a63a02632
finished splitQuadrilateral theoretically (untested)
Teemu Piippo <teemu@hecknology.net>
parents:
73
diff
changeset
|
162 | |
137
fb9990772357
Add documentation to model.cpp
Teemu Piippo <teemu@hecknology.net>
parents:
133
diff
changeset
|
163 | /** |
fb9990772357
Add documentation to model.cpp
Teemu Piippo <teemu@hecknology.net>
parents:
133
diff
changeset
|
164 | * @brief Removes the object at the specified position |
fb9990772357
Add documentation to model.cpp
Teemu Piippo <teemu@hecknology.net>
parents:
133
diff
changeset
|
165 | * @param position |
fb9990772357
Add documentation to model.cpp
Teemu Piippo <teemu@hecknology.net>
parents:
133
diff
changeset
|
166 | */ |
76
7c4a63a02632
finished splitQuadrilateral theoretically (untested)
Teemu Piippo <teemu@hecknology.net>
parents:
73
diff
changeset
|
167 | void Model::remove(int position) |
7c4a63a02632
finished splitQuadrilateral theoretically (untested)
Teemu Piippo <teemu@hecknology.net>
parents:
73
diff
changeset
|
168 | { |
7c4a63a02632
finished splitQuadrilateral theoretically (untested)
Teemu Piippo <teemu@hecknology.net>
parents:
73
diff
changeset
|
169 | if (position >= 0 and position < signed_cast(this->body.size())) |
7c4a63a02632
finished splitQuadrilateral theoretically (untested)
Teemu Piippo <teemu@hecknology.net>
parents:
73
diff
changeset
|
170 | { |
150
b6cbba6e29a1
extract polygon cache out of Model
Teemu Piippo <teemu@hecknology.net>
parents:
148
diff
changeset
|
171 | Q_EMIT this->beginRemoveRows({}, position, position); |
76
7c4a63a02632
finished splitQuadrilateral theoretically (untested)
Teemu Piippo <teemu@hecknology.net>
parents:
73
diff
changeset
|
172 | this->body.erase(std::begin(this->body) + position); |
150
b6cbba6e29a1
extract polygon cache out of Model
Teemu Piippo <teemu@hecknology.net>
parents:
148
diff
changeset
|
173 | Q_EMIT this->endRemoveRows(); |
76
7c4a63a02632
finished splitQuadrilateral theoretically (untested)
Teemu Piippo <teemu@hecknology.net>
parents:
73
diff
changeset
|
174 | } |
8
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
3
diff
changeset
|
175 | } |
51 | 176 | |
152 | 177 | void Model::emitDataChangedSignal(int position) |
178 | { | |
179 | Q_EMIT this->dataChanged(this->index(position), this->index(position)); | |
180 | } | |
181 | ||
137
fb9990772357
Add documentation to model.cpp
Teemu Piippo <teemu@hecknology.net>
parents:
133
diff
changeset
|
182 | /** |
fb9990772357
Add documentation to model.cpp
Teemu Piippo <teemu@hecknology.net>
parents:
133
diff
changeset
|
183 | * @brief Gets the object pointer at the specified position |
fb9990772357
Add documentation to model.cpp
Teemu Piippo <teemu@hecknology.net>
parents:
133
diff
changeset
|
184 | * @param index Position of the object |
fb9990772357
Add documentation to model.cpp
Teemu Piippo <teemu@hecknology.net>
parents:
133
diff
changeset
|
185 | * @returns object pointer |
fb9990772357
Add documentation to model.cpp
Teemu Piippo <teemu@hecknology.net>
parents:
133
diff
changeset
|
186 | */ |
150
b6cbba6e29a1
extract polygon cache out of Model
Teemu Piippo <teemu@hecknology.net>
parents:
148
diff
changeset
|
187 | ldraw::Object* Model::operator[](int index) |
b6cbba6e29a1
extract polygon cache out of Model
Teemu Piippo <teemu@hecknology.net>
parents:
148
diff
changeset
|
188 | { |
b6cbba6e29a1
extract polygon cache out of Model
Teemu Piippo <teemu@hecknology.net>
parents:
148
diff
changeset
|
189 | if (index >= 0 and index < this->size()) |
b6cbba6e29a1
extract polygon cache out of Model
Teemu Piippo <teemu@hecknology.net>
parents:
148
diff
changeset
|
190 | { |
b6cbba6e29a1
extract polygon cache out of Model
Teemu Piippo <teemu@hecknology.net>
parents:
148
diff
changeset
|
191 | return this->body[index].get(); |
b6cbba6e29a1
extract polygon cache out of Model
Teemu Piippo <teemu@hecknology.net>
parents:
148
diff
changeset
|
192 | } |
b6cbba6e29a1
extract polygon cache out of Model
Teemu Piippo <teemu@hecknology.net>
parents:
148
diff
changeset
|
193 | else |
b6cbba6e29a1
extract polygon cache out of Model
Teemu Piippo <teemu@hecknology.net>
parents:
148
diff
changeset
|
194 | { |
b6cbba6e29a1
extract polygon cache out of Model
Teemu Piippo <teemu@hecknology.net>
parents:
148
diff
changeset
|
195 | throw std::out_of_range{"index out of range"}; |
b6cbba6e29a1
extract polygon cache out of Model
Teemu Piippo <teemu@hecknology.net>
parents:
148
diff
changeset
|
196 | } |
b6cbba6e29a1
extract polygon cache out of Model
Teemu Piippo <teemu@hecknology.net>
parents:
148
diff
changeset
|
197 | } |
b6cbba6e29a1
extract polygon cache out of Model
Teemu Piippo <teemu@hecknology.net>
parents:
148
diff
changeset
|
198 | |
141 | 199 | /** |
151 | 200 | * @brief Gets the object pointer at the specified position |
201 | * @param index Position of the object | |
202 | * @returns object pointer | |
203 | */ | |
204 | const ldraw::Object* Model::operator[](int index) const | |
205 | { | |
206 | if (index >= 0 and index < this->size()) | |
207 | { | |
208 | return this->body[index].get(); | |
209 | } | |
210 | else | |
211 | { | |
212 | throw std::out_of_range{"index out of range"}; | |
213 | } | |
214 | } | |
215 | ||
216 | /** | |
217 | * @brief Gets an object pointer by id. Used by the editing context to actually modify objects. | |
218 | * @param id | |
219 | * @return object pointer | |
220 | */ | |
221 | ldraw::Object* Model::findObjectById(const ldraw::id_t id) | |
222 | { | |
223 | const QModelIndex index = this->find(id); | |
224 | if (index.isValid()) | |
225 | { | |
226 | return (*this)[index.row()]; | |
227 | } | |
228 | else | |
229 | { | |
230 | return nullptr; | |
231 | } | |
232 | } | |
233 | ||
234 | const ldraw::Object* Model::findObjectById(const ldraw::id_t id) const | |
235 | { | |
236 | const QModelIndex index = this->find(id); | |
237 | if (index.isValid()) | |
238 | { | |
239 | return (*this)[index.row()]; | |
240 | } | |
241 | else | |
242 | { | |
243 | return nullptr; | |
244 | } | |
245 | } | |
246 | ||
247 | /** | |
248 | * @brief Attempts the save the model | |
249 | */ | |
250 | void save(const Model &model, QIODevice *device) | |
251 | { | |
252 | QTextStream out{device}; | |
253 | applyToModel<ldraw::Object>(model, [&](const ldraw::Object* object) { | |
254 | out << object->toLDrawCode() << "\r\n"; | |
255 | }); | |
256 | } |