102 return {}; |
102 return {}; |
103 } |
103 } |
104 } |
104 } |
105 |
105 |
106 /** |
106 /** |
107 * @brief Gets a property of the header |
|
108 * @param property |
|
109 * @return QVariant |
|
110 */ |
|
111 QVariant Model::getHeaderProperty(const HeaderProperty property) |
|
112 { |
|
113 switch (property) |
|
114 { |
|
115 case HeaderProperty::Name: |
|
116 return header.name; |
|
117 } |
|
118 return {}; |
|
119 } |
|
120 |
|
121 /** |
|
122 * @brief Gets the specified property from the object at the specified index in the model. |
|
123 * @param index Index of object in the model |
|
124 * @param property Property to look up |
|
125 * @return QVariant |
|
126 */ |
|
127 QVariant Model::getObjectProperty(const int index, const ldraw::Property property) const |
|
128 { |
|
129 const ldraw::Object* object = this->body[unsigned_cast(index)].get(); |
|
130 return object->getProperty(property); |
|
131 } |
|
132 |
|
133 /** |
|
134 * @brief Finds the position of the specified object in the model |
107 * @brief Finds the position of the specified object in the model |
135 * @param id Object id to look for |
108 * @param id Object id to look for |
136 * @return model index |
109 * @return model index |
137 */ |
110 */ |
138 QModelIndex Model::find(ldraw::id_t id) const |
111 QModelIndex Model::find(ldraw::id_t id) const |
234 /** |
207 /** |
235 * @brief Gets the object pointer at the specified position |
208 * @brief Gets the object pointer at the specified position |
236 * @param index Position of the object |
209 * @param index Position of the object |
237 * @returns object pointer |
210 * @returns object pointer |
238 */ |
211 */ |
239 ldraw::Object* Model::objectAt(const QModelIndex& index) |
212 ldraw::Object* Model::operator[](int index) |
240 { |
213 { |
241 return this->body[unsigned_cast(index.row())].get(); |
214 if (index >= 0 and index < this->size()) |
|
215 { |
|
216 return this->body[index].get(); |
|
217 } |
|
218 else |
|
219 { |
|
220 throw std::out_of_range{"index out of range"}; |
|
221 } |
242 } |
222 } |
243 |
223 |
244 /** |
224 /** |
245 * @brief Gets the object pointer at the specified position |
225 * @brief Gets the object pointer at the specified position |
246 * @param index Position of the object |
226 * @param index Position of the object |
247 * @returns object pointer |
227 * @returns object pointer |
248 */ |
228 */ |
249 const ldraw::Object* Model::objectAt(const QModelIndex& index) const |
229 const ldraw::Object* Model::operator[](int index) const |
250 { |
230 { |
251 return this->body[unsigned_cast(index.row())].get(); |
231 if (index >= 0 and index < this->size()) |
|
232 { |
|
233 return this->body[index].get(); |
|
234 } |
|
235 else |
|
236 { |
|
237 throw std::out_of_range{"index out of range"}; |
|
238 } |
|
239 } |
|
240 |
|
241 /** |
|
242 * @brief Gets an object pointer by id. Used by the editing context to actually modify objects. |
|
243 * @param id |
|
244 * @return object pointer |
|
245 */ |
|
246 ldraw::Object* Model::findObjectById(const ldraw::id_t id) |
|
247 { |
|
248 const QModelIndex index = this->find(id); |
|
249 if (index.isValid()) |
|
250 { |
|
251 return (*this)[index.row()]; |
|
252 } |
|
253 else |
|
254 { |
|
255 return nullptr; |
|
256 } |
|
257 } |
|
258 |
|
259 const ldraw::Object* Model::findObjectById(const ldraw::id_t id) const |
|
260 { |
|
261 const QModelIndex index = this->find(id); |
|
262 if (index.isValid()) |
|
263 { |
|
264 return (*this)[index.row()]; |
|
265 } |
|
266 else |
|
267 { |
|
268 return nullptr; |
|
269 } |
252 } |
270 } |
253 |
271 |
254 /** |
272 /** |
255 * @brief Attempts the save the model |
273 * @brief Attempts the save the model |
256 */ |
274 */ |
257 void Model::save(QIODevice* device) const |
275 void save(const Model &model, QIODevice *device) |
258 { |
276 { |
259 QTextStream out{device}; |
277 QTextStream out{device}; |
260 for (const ModelObjectPointer& object : this->body) |
278 applyToModel<ldraw::Object>(model, [&](const ldraw::Object* object) { |
261 { |
279 out << object->toLDrawCode() << "\r\n"; |
262 out << object.get()->toLDrawCode() << "\r\n"; |
280 }); |
263 } |
|
264 } |
|
265 |
|
266 ldraw::Object* Model::operator[](int index) |
|
267 { |
|
268 if (index >= 0 and index < this->size()) |
|
269 { |
|
270 return this->body[index].get(); |
|
271 } |
|
272 else |
|
273 { |
|
274 throw std::out_of_range{"index out of range"}; |
|
275 } |
|
276 } |
281 } |
277 |
282 |
278 /** |
283 /** |
279 * @brief Modifies the !LDRAW_ORG line so that it becomes unofficial |
284 * @brief Modifies the !LDRAW_ORG line so that it becomes unofficial |
280 */ |
285 */ |