Sun, 24 Oct 2021 11:33:32 +0300
update
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" | |
26 | ||
137
fb9990772357
Add documentation to model.cpp
Teemu Piippo <teemu@hecknology.net>
parents:
133
diff
changeset
|
27 | /** |
fb9990772357
Add documentation to model.cpp
Teemu Piippo <teemu@hecknology.net>
parents:
133
diff
changeset
|
28 | * @brief Constructs a model |
fb9990772357
Add documentation to model.cpp
Teemu Piippo <teemu@hecknology.net>
parents:
133
diff
changeset
|
29 | * @param parent QObject parent to pass forward |
fb9990772357
Add documentation to model.cpp
Teemu Piippo <teemu@hecknology.net>
parents:
133
diff
changeset
|
30 | */ |
140 | 31 | Model::Model(QObject* parent) : |
32 | Model{"", parent} {} | |
33 | ||
34 | /** | |
35 | * @brief Constructs a model | |
36 | * @param path Path that was used to open the model | |
37 | * @param parent QObject parent to pass forward | |
38 | */ | |
39 | Model::Model(const QString& path, QObject *parent) : | |
40 | QAbstractListModel{parent}, | |
141 | 41 | storedPath{path} |
86
4bec0525ef1b
PolygonObjectEditor can now modify the object properly
Teemu Piippo <teemu@hecknology.net>
parents:
76
diff
changeset
|
42 | { |
4bec0525ef1b
PolygonObjectEditor can now modify the object properly
Teemu Piippo <teemu@hecknology.net>
parents:
76
diff
changeset
|
43 | connect(this, &Model::dataChanged, [&](){ this->needRecache = true; }); |
4bec0525ef1b
PolygonObjectEditor can now modify the object properly
Teemu Piippo <teemu@hecknology.net>
parents:
76
diff
changeset
|
44 | } |
3 | 45 | |
137
fb9990772357
Add documentation to model.cpp
Teemu Piippo <teemu@hecknology.net>
parents:
133
diff
changeset
|
46 | /** |
fb9990772357
Add documentation to model.cpp
Teemu Piippo <teemu@hecknology.net>
parents:
133
diff
changeset
|
47 | * @returns the amount of elements in the model |
fb9990772357
Add documentation to model.cpp
Teemu Piippo <teemu@hecknology.net>
parents:
133
diff
changeset
|
48 | */ |
3 | 49 | int Model::size() const |
50 | { | |
51 | 51 | return static_cast<int>(this->body.size()); |
3 | 52 | } |
53 | ||
133
e39326ee48dc
Begin work on edit history
Teemu Piippo <teemu@hecknology.net>
parents:
112
diff
changeset
|
54 | /** |
e39326ee48dc
Begin work on edit history
Teemu Piippo <teemu@hecknology.net>
parents:
112
diff
changeset
|
55 | * @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
|
56 | * @param index Index of object to look up |
e39326ee48dc
Begin work on edit history
Teemu Piippo <teemu@hecknology.net>
parents:
112
diff
changeset
|
57 | * @return object ID |
e39326ee48dc
Begin work on edit history
Teemu Piippo <teemu@hecknology.net>
parents:
112
diff
changeset
|
58 | */ |
e39326ee48dc
Begin work on edit history
Teemu Piippo <teemu@hecknology.net>
parents:
112
diff
changeset
|
59 | ldraw::id_t Model::at(int index) const |
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 | if (index >= 0 and index < this->size()) |
e39326ee48dc
Begin work on edit history
Teemu Piippo <teemu@hecknology.net>
parents:
112
diff
changeset
|
62 | { |
e39326ee48dc
Begin work on edit history
Teemu Piippo <teemu@hecknology.net>
parents:
112
diff
changeset
|
63 | return this->body[index]->id; |
e39326ee48dc
Begin work on edit history
Teemu Piippo <teemu@hecknology.net>
parents:
112
diff
changeset
|
64 | } |
e39326ee48dc
Begin work on edit history
Teemu Piippo <teemu@hecknology.net>
parents:
112
diff
changeset
|
65 | else |
e39326ee48dc
Begin work on edit history
Teemu Piippo <teemu@hecknology.net>
parents:
112
diff
changeset
|
66 | { |
e39326ee48dc
Begin work on edit history
Teemu Piippo <teemu@hecknology.net>
parents:
112
diff
changeset
|
67 | return ldraw::NULL_ID; |
e39326ee48dc
Begin work on edit history
Teemu Piippo <teemu@hecknology.net>
parents:
112
diff
changeset
|
68 | } |
e39326ee48dc
Begin work on edit history
Teemu Piippo <teemu@hecknology.net>
parents:
112
diff
changeset
|
69 | } |
e39326ee48dc
Begin work on edit history
Teemu Piippo <teemu@hecknology.net>
parents:
112
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 Obtains an editing context for this model. |
fb9990772357
Add documentation to model.cpp
Teemu Piippo <teemu@hecknology.net>
parents:
133
diff
changeset
|
73 | * Editing contexts are used to perform modifications to the model. |
fb9990772357
Add documentation to model.cpp
Teemu Piippo <teemu@hecknology.net>
parents:
133
diff
changeset
|
74 | * @return editing context |
fb9990772357
Add documentation to model.cpp
Teemu Piippo <teemu@hecknology.net>
parents:
133
diff
changeset
|
75 | */ |
3 | 76 | Model::EditContext Model::edit() |
77 | { | |
133
e39326ee48dc
Begin work on edit history
Teemu Piippo <teemu@hecknology.net>
parents:
112
diff
changeset
|
78 | this->editCounter += 1; |
3 | 79 | return {*this}; |
80 | } | |
8
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
3
diff
changeset
|
81 | |
137
fb9990772357
Add documentation to model.cpp
Teemu Piippo <teemu@hecknology.net>
parents:
133
diff
changeset
|
82 | /** |
fb9990772357
Add documentation to model.cpp
Teemu Piippo <teemu@hecknology.net>
parents:
133
diff
changeset
|
83 | * @brief @overload QAbstractListModel::rowCount |
fb9990772357
Add documentation to model.cpp
Teemu Piippo <teemu@hecknology.net>
parents:
133
diff
changeset
|
84 | * @return size |
fb9990772357
Add documentation to model.cpp
Teemu Piippo <teemu@hecknology.net>
parents:
133
diff
changeset
|
85 | */ |
8
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
3
diff
changeset
|
86 | int Model::rowCount(const QModelIndex&) const |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
3
diff
changeset
|
87 | { |
137
fb9990772357
Add documentation to model.cpp
Teemu Piippo <teemu@hecknology.net>
parents:
133
diff
changeset
|
88 | return this->size(); |
8
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
3
diff
changeset
|
89 | } |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
3
diff
changeset
|
90 | |
137
fb9990772357
Add documentation to model.cpp
Teemu Piippo <teemu@hecknology.net>
parents:
133
diff
changeset
|
91 | /** |
fb9990772357
Add documentation to model.cpp
Teemu Piippo <teemu@hecknology.net>
parents:
133
diff
changeset
|
92 | * @brief @overload QAbstractListModel::data |
fb9990772357
Add documentation to model.cpp
Teemu Piippo <teemu@hecknology.net>
parents:
133
diff
changeset
|
93 | * @param index |
fb9990772357
Add documentation to model.cpp
Teemu Piippo <teemu@hecknology.net>
parents:
133
diff
changeset
|
94 | * @param role |
fb9990772357
Add documentation to model.cpp
Teemu Piippo <teemu@hecknology.net>
parents:
133
diff
changeset
|
95 | * @return QVariant |
fb9990772357
Add documentation to model.cpp
Teemu Piippo <teemu@hecknology.net>
parents:
133
diff
changeset
|
96 | */ |
8
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
3
diff
changeset
|
97 | QVariant Model::data(const QModelIndex& index, int role) const |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
3
diff
changeset
|
98 | { |
51 | 99 | const ldraw::Object* object = this->objectAt(index); |
8
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
3
diff
changeset
|
100 | switch(role) |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
3
diff
changeset
|
101 | { |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
3
diff
changeset
|
102 | case Qt::DisplayRole: |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
3
diff
changeset
|
103 | return object->textRepresentation(); |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
3
diff
changeset
|
104 | case Qt::ForegroundRole: |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
3
diff
changeset
|
105 | return object->textRepresentationForeground(); |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
3
diff
changeset
|
106 | case Qt::BackgroundRole: |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
3
diff
changeset
|
107 | return object->textRepresentationBackground(); |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
3
diff
changeset
|
108 | case Qt::FontRole: |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
3
diff
changeset
|
109 | return object->textRepresentationFont(); |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
3
diff
changeset
|
110 | default: |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
3
diff
changeset
|
111 | return {}; |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
3
diff
changeset
|
112 | } |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
3
diff
changeset
|
113 | } |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
3
diff
changeset
|
114 | |
137
fb9990772357
Add documentation to model.cpp
Teemu Piippo <teemu@hecknology.net>
parents:
133
diff
changeset
|
115 | /** |
fb9990772357
Add documentation to model.cpp
Teemu Piippo <teemu@hecknology.net>
parents:
133
diff
changeset
|
116 | * @brief Gets a property of the header |
fb9990772357
Add documentation to model.cpp
Teemu Piippo <teemu@hecknology.net>
parents:
133
diff
changeset
|
117 | * @param property |
fb9990772357
Add documentation to model.cpp
Teemu Piippo <teemu@hecknology.net>
parents:
133
diff
changeset
|
118 | * @return QVariant |
fb9990772357
Add documentation to model.cpp
Teemu Piippo <teemu@hecknology.net>
parents:
133
diff
changeset
|
119 | */ |
8
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
3
diff
changeset
|
120 | QVariant Model::getHeaderProperty(const HeaderProperty property) |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
3
diff
changeset
|
121 | { |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
3
diff
changeset
|
122 | switch (property) |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
3
diff
changeset
|
123 | { |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
3
diff
changeset
|
124 | case HeaderProperty::Name: |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
3
diff
changeset
|
125 | return header.name; |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
3
diff
changeset
|
126 | } |
51 | 127 | return {}; |
8
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
3
diff
changeset
|
128 | } |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
3
diff
changeset
|
129 | |
137
fb9990772357
Add documentation to model.cpp
Teemu Piippo <teemu@hecknology.net>
parents:
133
diff
changeset
|
130 | /** |
fb9990772357
Add documentation to model.cpp
Teemu Piippo <teemu@hecknology.net>
parents:
133
diff
changeset
|
131 | * @brief Gets the specified property from the object at the specified index in the model. |
fb9990772357
Add documentation to model.cpp
Teemu Piippo <teemu@hecknology.net>
parents:
133
diff
changeset
|
132 | * @param index Index of object in the model |
fb9990772357
Add documentation to model.cpp
Teemu Piippo <teemu@hecknology.net>
parents:
133
diff
changeset
|
133 | * @param property Property to look up |
fb9990772357
Add documentation to model.cpp
Teemu Piippo <teemu@hecknology.net>
parents:
133
diff
changeset
|
134 | * @return QVariant |
fb9990772357
Add documentation to model.cpp
Teemu Piippo <teemu@hecknology.net>
parents:
133
diff
changeset
|
135 | */ |
35
98906a94732f
renamed the linetypes namespace to ldraw namespace and added more structures to it
Teemu Piippo <teemu@hecknology.net>
parents:
24
diff
changeset
|
136 | QVariant Model::getObjectProperty(const int index, const ldraw::Property property) const |
12 | 137 | { |
51 | 138 | const ldraw::Object* object = this->body[unsigned_cast(index)].get(); |
12 | 139 | return object->getProperty(property); |
140 | } | |
141 | ||
137
fb9990772357
Add documentation to model.cpp
Teemu Piippo <teemu@hecknology.net>
parents:
133
diff
changeset
|
142 | /** |
fb9990772357
Add documentation to model.cpp
Teemu Piippo <teemu@hecknology.net>
parents:
133
diff
changeset
|
143 | * @brief Gets a list of GL polygons that are used to represent this model. |
fb9990772357
Add documentation to model.cpp
Teemu Piippo <teemu@hecknology.net>
parents:
133
diff
changeset
|
144 | * @details Will build polygons if polygons are outdated. |
fb9990772357
Add documentation to model.cpp
Teemu Piippo <teemu@hecknology.net>
parents:
133
diff
changeset
|
145 | * @param documents Documents to use to resolve subfile references |
fb9990772357
Add documentation to model.cpp
Teemu Piippo <teemu@hecknology.net>
parents:
133
diff
changeset
|
146 | * @return vector of GL polygons |
fb9990772357
Add documentation to model.cpp
Teemu Piippo <teemu@hecknology.net>
parents:
133
diff
changeset
|
147 | */ |
21 | 148 | std::vector<gl::Polygon> Model::getPolygons(DocumentManager* documents) const |
149 | { | |
150 | if (this->needRecache) | |
151 | { | |
152 | this->cachedPolygons.clear(); | |
35
98906a94732f
renamed the linetypes namespace to ldraw namespace and added more structures to it
Teemu Piippo <teemu@hecknology.net>
parents:
24
diff
changeset
|
153 | ldraw::GetPolygonsContext context{documents}; |
21 | 154 | for (int i = 0; i < this->size(); i += 1) |
155 | { | |
156 | this->getObjectPolygons(i, this->cachedPolygons, &context); | |
157 | } | |
158 | this->needRecache = false; | |
159 | } | |
160 | return this->cachedPolygons; | |
161 | } | |
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 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
|
165 | * @param id Object id to look for |
fb9990772357
Add documentation to model.cpp
Teemu Piippo <teemu@hecknology.net>
parents:
133
diff
changeset
|
166 | * @return model index |
fb9990772357
Add documentation to model.cpp
Teemu Piippo <teemu@hecknology.net>
parents:
133
diff
changeset
|
167 | */ |
73
97df974b5ed5
ldraw::Id is now templated for extra type safety
Teemu Piippo <teemu@hecknology.net>
parents:
51
diff
changeset
|
168 | QModelIndex Model::lookup(ldraw::id_t id) const |
51 | 169 | { |
170 | // FIXME: This linear search will probably cause performance issues | |
171 | for (std::size_t i = 0; i < this->body.size(); i += 1) | |
172 | { | |
173 | if (this->body[i]->id == id) | |
174 | { | |
175 | return this->index(static_cast<int>(i)); | |
176 | } | |
177 | } | |
178 | return {}; | |
179 | } | |
180 | ||
137
fb9990772357
Add documentation to model.cpp
Teemu Piippo <teemu@hecknology.net>
parents:
133
diff
changeset
|
181 | /** |
fb9990772357
Add documentation to model.cpp
Teemu Piippo <teemu@hecknology.net>
parents:
133
diff
changeset
|
182 | * @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
|
183 | * @param index Position of the object in the model |
fb9990772357
Add documentation to model.cpp
Teemu Piippo <teemu@hecknology.net>
parents:
133
diff
changeset
|
184 | * @return id |
fb9990772357
Add documentation to model.cpp
Teemu Piippo <teemu@hecknology.net>
parents:
133
diff
changeset
|
185 | */ |
73
97df974b5ed5
ldraw::Id is now templated for extra type safety
Teemu Piippo <teemu@hecknology.net>
parents:
51
diff
changeset
|
186 | ldraw::id_t Model::resolve(const QModelIndex& index) const |
51 | 187 | { |
188 | return this->objectAt(index)->id; | |
189 | } | |
190 | ||
137
fb9990772357
Add documentation to model.cpp
Teemu Piippo <teemu@hecknology.net>
parents:
133
diff
changeset
|
191 | /** |
141 | 192 | * @brief Gets the path to the model |
193 | * @return path | |
194 | */ | |
195 | const QString& Model::path() const | |
196 | { | |
197 | return this->storedPath; | |
198 | } | |
199 | ||
200 | /** | |
201 | * @brief Sets the path to the model | |
202 | * @param path New path to use | |
203 | */ | |
204 | void Model::setPath(const QString &path) | |
205 | { | |
206 | this->storedPath = path; | |
207 | this->header.name = QFileInfo{path}.fileName(); | |
208 | // Update the "Name: 1234.dat" comment | |
209 | if (this->body.size() >= 2) | |
210 | { | |
211 | const ldraw::id_t id = this->body[1]->id; | |
212 | if (this->isA<ldraw::MetaCommand>(id)) | |
213 | { | |
214 | const QString& textBody = this->body[1]->getProperty<ldraw::Property::Text>(); | |
215 | if (textBody.startsWith("Name: ")) | |
216 | { | |
217 | auto editor = this->edit(); | |
218 | editor.setObjectProperty<ldraw::Property::Text>(id, "Name: " + this->header.name); | |
219 | } | |
220 | } | |
221 | } | |
222 | } | |
223 | ||
224 | /** | |
137
fb9990772357
Add documentation to model.cpp
Teemu Piippo <teemu@hecknology.net>
parents:
133
diff
changeset
|
225 | * @brief Gets the GL polygons of the object at the specified position in the model |
fb9990772357
Add documentation to model.cpp
Teemu Piippo <teemu@hecknology.net>
parents:
133
diff
changeset
|
226 | * @param index Index of object in the model |
fb9990772357
Add documentation to model.cpp
Teemu Piippo <teemu@hecknology.net>
parents:
133
diff
changeset
|
227 | * @param polygons_out Vector to add polygons into |
fb9990772357
Add documentation to model.cpp
Teemu Piippo <teemu@hecknology.net>
parents:
133
diff
changeset
|
228 | * @param context Context to use to resolve subfile references |
fb9990772357
Add documentation to model.cpp
Teemu Piippo <teemu@hecknology.net>
parents:
133
diff
changeset
|
229 | */ |
21 | 230 | void Model::getObjectPolygons( |
231 | const int index, | |
232 | std::vector<gl::Polygon>& polygons_out, | |
35
98906a94732f
renamed the linetypes namespace to ldraw namespace and added more structures to it
Teemu Piippo <teemu@hecknology.net>
parents:
24
diff
changeset
|
233 | ldraw::GetPolygonsContext* context) const |
21 | 234 | { |
51 | 235 | const ldraw::Object* object = this->body[unsigned_cast(index)].get(); |
21 | 236 | object->getPolygons(polygons_out, context); |
237 | } | |
238 | ||
137
fb9990772357
Add documentation to model.cpp
Teemu Piippo <teemu@hecknology.net>
parents:
133
diff
changeset
|
239 | /** |
fb9990772357
Add documentation to model.cpp
Teemu Piippo <teemu@hecknology.net>
parents:
133
diff
changeset
|
240 | * @brief Called by the editing context to signal to the model that editing is done. |
fb9990772357
Add documentation to model.cpp
Teemu Piippo <teemu@hecknology.net>
parents:
133
diff
changeset
|
241 | */ |
133
e39326ee48dc
Begin work on edit history
Teemu Piippo <teemu@hecknology.net>
parents:
112
diff
changeset
|
242 | void Model::editFinished() |
e39326ee48dc
Begin work on edit history
Teemu Piippo <teemu@hecknology.net>
parents:
112
diff
changeset
|
243 | { |
e39326ee48dc
Begin work on edit history
Teemu Piippo <teemu@hecknology.net>
parents:
112
diff
changeset
|
244 | this->editCounter -= 1; |
e39326ee48dc
Begin work on edit history
Teemu Piippo <teemu@hecknology.net>
parents:
112
diff
changeset
|
245 | } |
e39326ee48dc
Begin work on edit history
Teemu Piippo <teemu@hecknology.net>
parents:
112
diff
changeset
|
246 | |
137
fb9990772357
Add documentation to model.cpp
Teemu Piippo <teemu@hecknology.net>
parents:
133
diff
changeset
|
247 | /** |
fb9990772357
Add documentation to model.cpp
Teemu Piippo <teemu@hecknology.net>
parents:
133
diff
changeset
|
248 | * @brief Called by the editing context to indicate that the specified object has been modified. |
fb9990772357
Add documentation to model.cpp
Teemu Piippo <teemu@hecknology.net>
parents:
133
diff
changeset
|
249 | * @param id ID of the object that has been modified |
fb9990772357
Add documentation to model.cpp
Teemu Piippo <teemu@hecknology.net>
parents:
133
diff
changeset
|
250 | */ |
133
e39326ee48dc
Begin work on edit history
Teemu Piippo <teemu@hecknology.net>
parents:
112
diff
changeset
|
251 | void Model::objectModified(ldraw::id_t id) |
e39326ee48dc
Begin work on edit history
Teemu Piippo <teemu@hecknology.net>
parents:
112
diff
changeset
|
252 | { |
e39326ee48dc
Begin work on edit history
Teemu Piippo <teemu@hecknology.net>
parents:
112
diff
changeset
|
253 | const QModelIndex index = this->lookup(id); |
e39326ee48dc
Begin work on edit history
Teemu Piippo <teemu@hecknology.net>
parents:
112
diff
changeset
|
254 | Q_EMIT this->dataChanged(index, index); |
e39326ee48dc
Begin work on edit history
Teemu Piippo <teemu@hecknology.net>
parents:
112
diff
changeset
|
255 | } |
e39326ee48dc
Begin work on edit history
Teemu Piippo <teemu@hecknology.net>
parents:
112
diff
changeset
|
256 | |
137
fb9990772357
Add documentation to model.cpp
Teemu Piippo <teemu@hecknology.net>
parents:
133
diff
changeset
|
257 | /** |
fb9990772357
Add documentation to model.cpp
Teemu Piippo <teemu@hecknology.net>
parents:
133
diff
changeset
|
258 | * @brief Adds the given object into the model. |
fb9990772357
Add documentation to model.cpp
Teemu Piippo <teemu@hecknology.net>
parents:
133
diff
changeset
|
259 | * @param object r-value reference to the object |
fb9990772357
Add documentation to model.cpp
Teemu Piippo <teemu@hecknology.net>
parents:
133
diff
changeset
|
260 | */ |
8
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
3
diff
changeset
|
261 | void Model::append(ModelObjectPointer&& object) |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
3
diff
changeset
|
262 | { |
76
7c4a63a02632
finished splitQuadrilateral theoretically (untested)
Teemu Piippo <teemu@hecknology.net>
parents:
73
diff
changeset
|
263 | const int position = static_cast<int>(this->body.size()); |
112 | 264 | Q_EMIT beginInsertRows({}, position, position); |
8
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
3
diff
changeset
|
265 | this->body.push_back(std::move(object)); |
112 | 266 | Q_EMIT endInsertRows(); |
111
1f42c03fafca
Draw tool actually adds objects now
Teemu Piippo <teemu@hecknology.net>
parents:
86
diff
changeset
|
267 | this->needRecache = true; |
76
7c4a63a02632
finished splitQuadrilateral theoretically (untested)
Teemu Piippo <teemu@hecknology.net>
parents:
73
diff
changeset
|
268 | } |
7c4a63a02632
finished splitQuadrilateral theoretically (untested)
Teemu Piippo <teemu@hecknology.net>
parents:
73
diff
changeset
|
269 | |
137
fb9990772357
Add documentation to model.cpp
Teemu Piippo <teemu@hecknology.net>
parents:
133
diff
changeset
|
270 | /** |
fb9990772357
Add documentation to model.cpp
Teemu Piippo <teemu@hecknology.net>
parents:
133
diff
changeset
|
271 | * @brief Removes the object at the specified position |
fb9990772357
Add documentation to model.cpp
Teemu Piippo <teemu@hecknology.net>
parents:
133
diff
changeset
|
272 | * @param position |
fb9990772357
Add documentation to model.cpp
Teemu Piippo <teemu@hecknology.net>
parents:
133
diff
changeset
|
273 | */ |
76
7c4a63a02632
finished splitQuadrilateral theoretically (untested)
Teemu Piippo <teemu@hecknology.net>
parents:
73
diff
changeset
|
274 | void Model::remove(int position) |
7c4a63a02632
finished splitQuadrilateral theoretically (untested)
Teemu Piippo <teemu@hecknology.net>
parents:
73
diff
changeset
|
275 | { |
7c4a63a02632
finished splitQuadrilateral theoretically (untested)
Teemu Piippo <teemu@hecknology.net>
parents:
73
diff
changeset
|
276 | if (position >= 0 and position < signed_cast(this->body.size())) |
7c4a63a02632
finished splitQuadrilateral theoretically (untested)
Teemu Piippo <teemu@hecknology.net>
parents:
73
diff
changeset
|
277 | { |
112 | 278 | Q_EMIT beginRemoveRows({}, position, position); |
76
7c4a63a02632
finished splitQuadrilateral theoretically (untested)
Teemu Piippo <teemu@hecknology.net>
parents:
73
diff
changeset
|
279 | this->body.erase(std::begin(this->body) + position); |
112 | 280 | Q_EMIT endRemoveRows(); |
111
1f42c03fafca
Draw tool actually adds objects now
Teemu Piippo <teemu@hecknology.net>
parents:
86
diff
changeset
|
281 | this->needRecache = true; |
76
7c4a63a02632
finished splitQuadrilateral theoretically (untested)
Teemu Piippo <teemu@hecknology.net>
parents:
73
diff
changeset
|
282 | } |
8
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
3
diff
changeset
|
283 | } |
51 | 284 | |
137
fb9990772357
Add documentation to model.cpp
Teemu Piippo <teemu@hecknology.net>
parents:
133
diff
changeset
|
285 | /** |
fb9990772357
Add documentation to model.cpp
Teemu Piippo <teemu@hecknology.net>
parents:
133
diff
changeset
|
286 | * @brief Gets the object pointer at the specified position |
fb9990772357
Add documentation to model.cpp
Teemu Piippo <teemu@hecknology.net>
parents:
133
diff
changeset
|
287 | * @param index Position of the object |
fb9990772357
Add documentation to model.cpp
Teemu Piippo <teemu@hecknology.net>
parents:
133
diff
changeset
|
288 | * @returns object pointer |
fb9990772357
Add documentation to model.cpp
Teemu Piippo <teemu@hecknology.net>
parents:
133
diff
changeset
|
289 | */ |
51 | 290 | ldraw::Object* Model::objectAt(const QModelIndex& index) |
291 | { | |
292 | return this->body[unsigned_cast(index.row())].get(); | |
293 | } | |
294 | ||
137
fb9990772357
Add documentation to model.cpp
Teemu Piippo <teemu@hecknology.net>
parents:
133
diff
changeset
|
295 | /** |
fb9990772357
Add documentation to model.cpp
Teemu Piippo <teemu@hecknology.net>
parents:
133
diff
changeset
|
296 | * @brief Gets the object pointer at the specified position |
fb9990772357
Add documentation to model.cpp
Teemu Piippo <teemu@hecknology.net>
parents:
133
diff
changeset
|
297 | * @param index Position of the object |
fb9990772357
Add documentation to model.cpp
Teemu Piippo <teemu@hecknology.net>
parents:
133
diff
changeset
|
298 | * @returns object pointer |
fb9990772357
Add documentation to model.cpp
Teemu Piippo <teemu@hecknology.net>
parents:
133
diff
changeset
|
299 | */ |
51 | 300 | const ldraw::Object* Model::objectAt(const QModelIndex& index) const |
301 | { | |
302 | return this->body[unsigned_cast(index.row())].get(); | |
303 | } | |
140 | 304 | |
305 | /** | |
145
4dea24d3eda0
Use QSaveFile to save the file more safely
Teemu Piippo <teemu@hecknology.net>
parents:
141
diff
changeset
|
306 | * @brief Attempts the save the model |
4dea24d3eda0
Use QSaveFile to save the file more safely
Teemu Piippo <teemu@hecknology.net>
parents:
141
diff
changeset
|
307 | * @param errors Where to write any errors |
4dea24d3eda0
Use QSaveFile to save the file more safely
Teemu Piippo <teemu@hecknology.net>
parents:
141
diff
changeset
|
308 | * @returns whether it succeeded |
140 | 309 | */ |
141 | 310 | bool Model::save(QTextStream &errors) const |
140 | 311 | { |
145
4dea24d3eda0
Use QSaveFile to save the file more safely
Teemu Piippo <teemu@hecknology.net>
parents:
141
diff
changeset
|
312 | QSaveFile file{this->path()}; |
4dea24d3eda0
Use QSaveFile to save the file more safely
Teemu Piippo <teemu@hecknology.net>
parents:
141
diff
changeset
|
313 | file.setDirectWriteFallback(true); |
4dea24d3eda0
Use QSaveFile to save the file more safely
Teemu Piippo <teemu@hecknology.net>
parents:
141
diff
changeset
|
314 | if (file.open(QSaveFile::WriteOnly)) |
140 | 315 | { |
316 | QTextStream out{&file}; | |
317 | for (const ModelObjectPointer& object : this->body) | |
318 | { | |
141 | 319 | out << object.get()->toLDrawCode() << "\r\n"; |
140 | 320 | } |
145
4dea24d3eda0
Use QSaveFile to save the file more safely
Teemu Piippo <teemu@hecknology.net>
parents:
141
diff
changeset
|
321 | const bool commitSucceeded = file.commit(); |
4dea24d3eda0
Use QSaveFile to save the file more safely
Teemu Piippo <teemu@hecknology.net>
parents:
141
diff
changeset
|
322 | if (not commitSucceeded) |
141 | 323 | { |
145
4dea24d3eda0
Use QSaveFile to save the file more safely
Teemu Piippo <teemu@hecknology.net>
parents:
141
diff
changeset
|
324 | errors << tr("Could not save to %1: %2") |
4dea24d3eda0
Use QSaveFile to save the file more safely
Teemu Piippo <teemu@hecknology.net>
parents:
141
diff
changeset
|
325 | .arg(this->path()) |
4dea24d3eda0
Use QSaveFile to save the file more safely
Teemu Piippo <teemu@hecknology.net>
parents:
141
diff
changeset
|
326 | .arg(file.errorString()); |
141 | 327 | return false; |
328 | } | |
329 | else | |
330 | { | |
331 | return true; | |
332 | } | |
333 | } | |
334 | else | |
335 | { | |
336 | errors << tr("Could not open %1 for writing: %2") | |
145
4dea24d3eda0
Use QSaveFile to save the file more safely
Teemu Piippo <teemu@hecknology.net>
parents:
141
diff
changeset
|
337 | .arg(file.fileName()) |
141 | 338 | .arg(file.errorString()); |
339 | return false; | |
340 | } | |
341 | } | |
342 | ||
343 | /** | |
344 | * @brief Modifies the !LDRAW_ORG line so that it becomes unofficial | |
345 | */ | |
346 | void Model::makeUnofficial() | |
347 | { | |
348 | if (this->body.size() >= 4) | |
349 | { | |
350 | const ldraw::id_t id = this->body[3]->id; | |
351 | if (this->isA<ldraw::MetaCommand>(id)) | |
352 | { | |
353 | const QString& body = this->body[3]->getProperty<ldraw::Property::Text>(); | |
354 | if (body.startsWith("!LDRAW_ORG ") and not body.startsWith("!LDRAW_ORG Unofficial_")) | |
355 | { | |
356 | QStringList tokens = body.split(" "); | |
357 | tokens[1] = "Unofficial_" + tokens[1]; | |
358 | // Remove the UPDATE tag | |
359 | if (tokens.size() >= 4 && tokens[2] == "UPDATE") | |
360 | { | |
361 | tokens.removeAt(3); | |
362 | tokens.removeAt(2); | |
363 | } | |
364 | EditContext editor = this->edit(); | |
365 | editor.setObjectProperty<ldraw::Property::Text>(id, tokens.join(" ")); | |
366 | } | |
367 | } | |
140 | 368 | } |
147 | 369 | } |