Tue, 27 Jul 2021 12:44:54 +0300
refactor
src/model.h | file | annotate | diff | comparison | revisions | |
src/modeleditcontext.cpp | file | annotate | diff | comparison | revisions |
--- 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<typename R> ldraw::Id<R> checkType(ldraw::id_t id) const; template<typename R> - const R* get(ldraw::Id<R> id, QModelIndex* index_out = nullptr) const; + const R* get(ldraw::Id<R> id) const; + template<typename R> + struct Get2Result + { + QModelIndex index; + const R* object; + }; + template<typename R> + Get2Result<R> get2(ldraw::Id<R> id) const; Q_SIGNALS: void objectAdded(ldraw::id_t id, int position); private: @@ -131,19 +139,23 @@ } template<typename R> -const R* Model::get(ldraw::Id<R> id, QModelIndex* index_out) const +const R* Model::get(ldraw::Id<R> id) const { - QModelIndex index = this->lookup(id); - if (index_out != nullptr) + return this->get2(id).object; +} + +template<typename R> +Model::Get2Result<R> Model::get2(const ldraw::Id<R> id) const +{ + Get2Result<R> result; + result.index = this->lookup(id); + if (result.index.isValid()) { - *index_out = index; - } - if (index.isValid()) - { - return static_cast<const R*>(this->objectAt(index)); + result.object = static_cast<const R*>(this->objectAt(result.index)); } else { - return nullptr; + result.object = nullptr; } + return result; }
--- 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::pair<ldraw::triangleid_t, ldraw::triangleid_t>> { std::optional<std::pair<ldraw::triangleid_t, ldraw::triangleid_t>> 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<geom::Triangle, 2> split = splitTriangles(splitType, quadrilateral->points); - const int position = index.row(); + const ldraw::Color color = resolved.object->colorIndex; + const std::array<geom::Triangle, 2> split = splitTriangles(splitType, resolved.object->points); + const int position = resolved.index.row(); editor.remove(position); result = std::make_pair( editor.insert<ldraw::Triangle>(position, split[0].points, color),