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