# HG changeset patch # User Teemu Piippo # Date 1646334796 -7200 # Node ID e628fc2e0c72e77bd5942ef6be2065ecc91147f3 # Parent b6cbba6e29a19eee8423749664eec2172aadb28f Clean up Model diff -r b6cbba6e29a1 -r e628fc2e0c72 src/documentmanager.cpp --- 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)) diff -r b6cbba6e29a1 -r e628fc2e0c72 src/model.cpp --- 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(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) diff -r b6cbba6e29a1 -r e628fc2e0c72 src/model.h --- 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::PropertyType 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 @@ -59,10 +57,8 @@ }; template Get2Result get2(ldraw::Id id) const; - template - 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 ldraw::Id 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 - T* objectAt(ldraw::Id id); - template - const T* objectAt(ldraw::Id id) const; void editFinished(); void objectModified(ldraw::id_t id); bool modified = false; - LDHeader header; std::vector body; std::map 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 -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(object.get()); + const ldraw::Object* object = model[i]; + const R* subobject = dynamic_cast(object); if (subobject != nullptr) { f(subobject); @@ -151,7 +142,7 @@ result.index = this->find(id); if (result.index.isValid()) { - result.object = static_cast(this->objectAt(result.index)); + result.object = static_cast((*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 -T* Model::objectAt(ldraw::Id id) -{ - return static_cast(this->objectAt(this->find(id))); -} - -template -const T* Model::objectAt(ldraw::Id id) const -{ - return static_cast(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::PropertyType Model::getObjectProperty(const ldraw::id_t id) const -{ - const ldraw::Object* const object = this->objectAt(id); - ldraw::PropertyType result; - if (object != nullptr) - { - result = object->getProperty(); - } - return result; -} diff -r b6cbba6e29a1 -r e628fc2e0c72 src/modeleditcontext.cpp --- 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)}); diff -r b6cbba6e29a1 -r e628fc2e0c72 src/modeleditcontext.h --- 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 void Model::EditContext::setObjectProperty(const ldraw::id_t id, const ldraw::PropertyType& value) { - ldraw::Object* object = this->model().objectAt(id); + ldraw::Object* object = this->model().findObjectById(id); if (object != nullptr) { object->setProperty(value); diff -r b6cbba6e29a1 -r e628fc2e0c72 src/vertexmap.cpp --- 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([&](const ldraw::Object* object) + applyToModel(*this->model, [&](const ldraw::Object* object) { glm::mat4 matrix; if (object->numPoints() > 2)