# HG changeset patch # User Teemu Piippo # Date 1627379094 -10800 # Node ID aad3e897bc323bb40a8f18477bc0b2ec18a65207 # Parent ed884a2fb009a628692be39ebfeae5e435a6b939 refactor diff -r ed884a2fb009 -r aad3e897bc32 src/model.h --- a/src/model.h Tue Jul 27 11:11:32 2021 +0300 +++ b/src/model.h Tue Jul 27 12:44:54 2021 +0300 @@ -49,7 +49,15 @@ template ldraw::Id checkType(ldraw::id_t id) const; template - const R* get(ldraw::Id id, QModelIndex* index_out = nullptr) const; + const R* get(ldraw::Id id) const; + template + struct Get2Result + { + QModelIndex index; + const R* object; + }; + template + Get2Result get2(ldraw::Id id) const; Q_SIGNALS: void objectAdded(ldraw::id_t id, int position); private: @@ -131,19 +139,23 @@ } template -const R* Model::get(ldraw::Id id, QModelIndex* index_out) const +const R* Model::get(ldraw::Id id) const { - QModelIndex index = this->lookup(id); - if (index_out != nullptr) + return this->get2(id).object; +} + +template +Model::Get2Result Model::get2(const ldraw::Id id) const +{ + Get2Result result; + result.index = this->lookup(id); + if (result.index.isValid()) { - *index_out = index; - } - if (index.isValid()) - { - return static_cast(this->objectAt(index)); + result.object = static_cast(this->objectAt(result.index)); } else { - return nullptr; + result.object = nullptr; } + return result; } diff -r ed884a2fb009 -r aad3e897bc32 src/modeleditcontext.cpp --- a/src/modeleditcontext.cpp Tue Jul 27 11:11:32 2021 +0300 +++ b/src/modeleditcontext.cpp Tue Jul 27 12:44:54 2021 +0300 @@ -113,13 +113,12 @@ ) -> std::optional> { std::optional> result; - QModelIndex index; - const ldraw::Quadrilateral* quadrilateral = editor.model().get(quadrilateral_id, &index); - if (quadrilateral != nullptr) + const auto resolved = editor.model().get2(quadrilateral_id); + if (resolved.object != nullptr) { - const ldraw::Color color = quadrilateral->colorIndex; - const std::array split = splitTriangles(splitType, quadrilateral->points); - const int position = index.row(); + const ldraw::Color color = resolved.object->colorIndex; + const std::array split = splitTriangles(splitType, resolved.object->points); + const int position = resolved.index.row(); editor.remove(position); result = std::make_pair( editor.insert(position, split[0].points, color),