Thu, 03 Mar 2022 21:13:16 +0200
Clean up Model
src/documentmanager.cpp | file | annotate | diff | comparison | revisions | |
src/model.cpp | file | annotate | diff | comparison | revisions | |
src/model.h | file | annotate | diff | comparison | revisions | |
src/modeleditcontext.cpp | file | annotate | diff | comparison | revisions | |
src/modeleditcontext.h | file | annotate | diff | comparison | revisions | |
src/vertexmap.cpp | file | annotate | diff | comparison | revisions |
--- a/src/documentmanager.cpp Thu Mar 03 11:42:52 2022 +0200 +++ b/src/documentmanager.cpp Thu Mar 03 21:13:16 2022 +0200 @@ -256,7 +256,7 @@ if (file.open(QSaveFile::WriteOnly)) { // if path is not nullptr, getModelById will always return a value as well - this->getModelById(modelId)->save(&file); + ::save(*this->getModelById(modelId), &file); const bool commitSucceeded = file.commit(); if (not commitSucceeded) { @@ -413,7 +413,7 @@ modelInfo.dependencies.clear(); for (int i = 0; i < modelInfo.model->size(); i += 1) { - const QString referenceName = modelInfo.model->getObjectProperty(i, ldraw::Property::ReferenceName).toString(); + const QString referenceName = (*modelInfo.model)[i]->getProperty(ldraw::Property::ReferenceName).toString(); if (not referenceName.isEmpty() and modelInfo.dependencies.count(referenceName) == 0 and not failedToOpen.contains(referenceName))
--- a/src/model.cpp Thu Mar 03 11:42:52 2022 +0200 +++ b/src/model.cpp Thu Mar 03 21:13:16 2022 +0200 @@ -87,7 +87,7 @@ */ QVariant Model::data(const QModelIndex& index, int role) const { - const ldraw::Object* object = this->objectAt(index); + const ldraw::Object* object = (*this)[index.row()]; switch(role) { case Qt::DisplayRole: @@ -104,33 +104,6 @@ } /** - * @brief Gets a property of the header - * @param property - * @return QVariant - */ -QVariant Model::getHeaderProperty(const HeaderProperty property) -{ - switch (property) - { - case HeaderProperty::Name: - return header.name; - } - return {}; -} - -/** - * @brief Gets the specified property from the object at the specified index in the model. - * @param index Index of object in the model - * @param property Property to look up - * @return QVariant - */ -QVariant Model::getObjectProperty(const int index, const ldraw::Property property) const -{ - const ldraw::Object* object = this->body[unsigned_cast(index)].get(); - return object->getProperty(property); -} - -/** * @brief Finds the position of the specified object in the model * @param id Object id to look for * @return model index @@ -155,7 +128,7 @@ */ ldraw::id_t Model::idAt(const QModelIndex& index) const { - return this->objectAt(index)->id; + return (*this)[index.row()]->id; } #if 0 @@ -236,33 +209,6 @@ * @param index Position of the object * @returns object pointer */ -ldraw::Object* Model::objectAt(const QModelIndex& index) -{ - return this->body[unsigned_cast(index.row())].get(); -} - -/** - * @brief Gets the object pointer at the specified position - * @param index Position of the object - * @returns object pointer - */ -const ldraw::Object* Model::objectAt(const QModelIndex& index) const -{ - return this->body[unsigned_cast(index.row())].get(); -} - -/** - * @brief Attempts the save the model - */ -void Model::save(QIODevice* device) const -{ - QTextStream out{device}; - for (const ModelObjectPointer& object : this->body) - { - out << object.get()->toLDrawCode() << "\r\n"; - } -} - ldraw::Object* Model::operator[](int index) { if (index >= 0 and index < this->size()) @@ -276,6 +222,65 @@ } /** + * @brief Gets the object pointer at the specified position + * @param index Position of the object + * @returns object pointer + */ +const ldraw::Object* Model::operator[](int index) const +{ + if (index >= 0 and index < this->size()) + { + return this->body[index].get(); + } + else + { + throw std::out_of_range{"index out of range"}; + } +} + +/** + * @brief Gets an object pointer by id. Used by the editing context to actually modify objects. + * @param id + * @return object pointer + */ +ldraw::Object* Model::findObjectById(const ldraw::id_t id) +{ + const QModelIndex index = this->find(id); + if (index.isValid()) + { + return (*this)[index.row()]; + } + else + { + return nullptr; + } +} + +const ldraw::Object* Model::findObjectById(const ldraw::id_t id) const +{ + const QModelIndex index = this->find(id); + if (index.isValid()) + { + return (*this)[index.row()]; + } + else + { + return nullptr; + } +} + +/** + * @brief Attempts the save the model + */ +void save(const Model &model, QIODevice *device) +{ + QTextStream out{device}; + applyToModel<ldraw::Object>(model, [&](const ldraw::Object* object) { + out << object->toLDrawCode() << "\r\n"; + }); +} + +/** * @brief Modifies the !LDRAW_ORG line so that it becomes unofficial */ void makeUnofficial(Model& model)
--- a/src/model.h Thu Mar 03 11:42:52 2022 +0200 +++ b/src/model.h Thu Mar 03 21:13:16 2022 +0200 @@ -43,10 +43,8 @@ EditContext edit(); int rowCount(const QModelIndex&) const override; QVariant data(const QModelIndex& index, int role) const override; - QVariant getHeaderProperty(const HeaderProperty property); - QVariant getObjectProperty(const int index, const ldraw::Property property) const; - template<ldraw::Property Property> - ldraw::PropertyType<Property> getObjectProperty(const ldraw::id_t id) const; + ldraw::Object* findObjectById(const ldraw::id_t id); + const ldraw::Object* findObjectById(const ldraw::id_t id) const; QModelIndex find(ldraw::id_t id) const; ldraw::id_t idAt(const QModelIndex& index) const; template<typename R> @@ -59,10 +57,8 @@ }; template<typename R> Get2Result<R> get2(ldraw::Id<R> id) const; - template<typename R, typename Fn> - void apply(Fn f) const; - void save(QIODevice *device) const; ldraw::Object* operator[](int index); + const ldraw::Object* operator[](int index) const; Q_SIGNALS: void objectAdded(int position); void objectModified(int position); @@ -75,16 +71,9 @@ template<typename T, typename... Args> ldraw::Id<T> insert(std::size_t position, Args&&... args); void remove(int position); - ldraw::Object* objectAt(const QModelIndex& index); - const ldraw::Object* objectAt(const QModelIndex& index) const; - template<typename T> - T* objectAt(ldraw::Id<T> id); - template<typename T> - const T* objectAt(ldraw::Id<T> id) const; void editFinished(); void objectModified(ldraw::id_t id); bool modified = false; - LDHeader header; std::vector<ModelObjectPointer> body; std::map<ldraw::id_t, ldraw::Object*> objectsById; /** @@ -94,6 +83,7 @@ }; void makeUnofficial(Model& model); +void save(const Model& model, QIODevice *device); /** * @brief Calls the specified function to all matching objects in the model @@ -101,11 +91,12 @@ * @param fn Function to call. */ template<typename R, typename Fn> -void Model::apply(Fn f) const +void applyToModel(const Model& model, Fn&& f) { - for (const ModelObjectPointer& object : this->body) + for (int i = 0; i < model.size(); i += 1) { - const R* subobject = dynamic_cast<const R*>(object.get()); + const ldraw::Object* object = model[i]; + const R* subobject = dynamic_cast<const R*>(object); if (subobject != nullptr) { f(subobject); @@ -151,7 +142,7 @@ result.index = this->find(id); if (result.index.isValid()) { - result.object = static_cast<const R*>(this->objectAt(result.index)); + result.object = static_cast<const R*>((*this)[result.index.row()]); } else { @@ -159,38 +150,3 @@ } return result; } - -/** - * @brief Gets an object pointer by id. Used by the editing context to actually modify objects. - * @param id - * @return object pointer - */ -template<typename T> -T* Model::objectAt(ldraw::Id<T> id) -{ - return static_cast<T*>(this->objectAt(this->find(id))); -} - -template<typename T> -const T* Model::objectAt(ldraw::Id<T> id) const -{ - return static_cast<const T*>(this->objectAt(this->find(id))); -} - -/** - * @brief Gets an object property by id - * @tparam Property Property to obtain - * @param id ID of object - * @returns property or default value - */ -template<ldraw::Property Property> -ldraw::PropertyType<Property> Model::getObjectProperty(const ldraw::id_t id) const -{ - const ldraw::Object* const object = this->objectAt(id); - ldraw::PropertyType<Property> result; - if (object != nullptr) - { - result = object->getProperty<Property>(); - } - return result; -}
--- a/src/modeleditcontext.cpp Thu Mar 03 11:42:52 2022 +0200 +++ b/src/modeleditcontext.cpp Thu Mar 03 21:13:16 2022 +0200 @@ -53,7 +53,7 @@ const QVariant& value) -> ldraw::Object::SetPropertyResult { - ldraw::Object* const object = this->model().objectAt(id); + ldraw::Object* const object = this->model().findObjectById(id); if (object != nullptr) { const ldraw::Object::SetPropertyResult result = object->setProperty(ldraw::PropertyKeyValue{property, value}); @@ -68,7 +68,7 @@ void Model::EditContext::setObjectPoint(ldraw::id_t id, int pointId, const glm::vec3& value) { - ldraw::Object* object = this->model().objectAt(id); + ldraw::Object* object = this->model().findObjectById(id); if (object != nullptr) { object->setProperty(ldraw::PropertyKeyValue{ldraw::pointProperty(pointId), QVariant::fromValue(value)});
--- a/src/modeleditcontext.h Thu Mar 03 11:42:52 2022 +0200 +++ b/src/modeleditcontext.h Thu Mar 03 21:13:16 2022 +0200 @@ -46,7 +46,7 @@ template<ldraw::Property Property> void Model::EditContext::setObjectProperty(const ldraw::id_t id, const ldraw::PropertyType<Property>& value) { - ldraw::Object* object = this->model().objectAt(id); + ldraw::Object* object = this->model().findObjectById(id); if (object != nullptr) { object->setProperty<Property>(value);
--- a/src/vertexmap.cpp Thu Mar 03 11:42:52 2022 +0200 +++ b/src/vertexmap.cpp Thu Mar 03 21:13:16 2022 +0200 @@ -55,7 +55,7 @@ this->map.clear(); this->vertices.clear(); this->vertexHashes.clear(); - this->model->apply<ldraw::Object>([&](const ldraw::Object* object) + applyToModel<ldraw::Object>(*this->model, [&](const ldraw::Object* object) { glm::mat4 matrix; if (object->numPoints() > 2)