diff -r 204dc77e5654 -r 7c4a63a02632 src/modeleditcontext.cpp --- a/src/modeleditcontext.cpp Sat Mar 07 01:25:37 2020 +0200 +++ b/src/modeleditcontext.cpp Mon Mar 09 22:12:50 2020 +0200 @@ -31,12 +31,21 @@ return id; } +void Model::EditContext::remove(int position) +{ + this->model().remove(position); +} + void Model::EditContext::setObjectProperty( - ldraw::Object* object, + ldraw::id_t id, ldraw::Property property, const QVariant& value) { - object->setProperty(property, value); + ldraw::Object* object = this->model().objectAt(id); + if (object != nullptr) + { + object->setProperty(property, value); + } } void Model::EditContext::invertObject(ldraw::id_t id) @@ -54,17 +63,61 @@ return this->storedModel; } +namespace +{ + using PropertyTriplet = std::tuple; +} + +static std::array diagonalToPointProperties(ldraw::Diagonal diagonal) +{ + std::array result; + switch (diagonal) + { + case ldraw::Diagonal::Diagonal_13: + result = { + std::make_tuple(ldraw::Property::Point1, ldraw::Property::Point2, ldraw::Property::Point3), + std::make_tuple(ldraw::Property::Point1, ldraw::Property::Point3, ldraw::Property::Point4) + }; + break; + case ldraw::Diagonal::Diagonal_24: + result = { + std::make_tuple(ldraw::Property::Point1, ldraw::Property::Point2, ldraw::Property::Point4), + std::make_tuple(ldraw::Property::Point2, ldraw::Property::Point3, ldraw::Property::Point4) + }; + break; + } + return result; +} + auto ldraw::splitQuadrilateral( Model::EditContext& editor, ldraw::quadrilateralid_t quadrilateral_id, - ldraw::QuadrilateralSplit splitType + ldraw::Diagonal splitType ) -> std::optional> { std::optional> result; - const ldraw::Quadrilateral* quadrilateral = editor.model().get(quadrilateral_id); + QModelIndex index; + const ldraw::Quadrilateral* quadrilateral = editor.model().get(quadrilateral_id, &index); if (quadrilateral != nullptr) { - + Q_ASSERT(index.isValid()); + int position = index.row(); + editor.remove(position); + std::vector triangles; + triangles.reserve(2); + const std::array split = diagonalToPointProperties(splitType); + for (const PropertyTriplet& properties : split) + { + const ldraw::triangleid_t triangle = editor.insert( + position, + quadrilateral->getProperty(std::get<0>(properties)).value(), + quadrilateral->getProperty(std::get<1>(properties)).value(), + quadrilateral->getProperty(std::get<2>(properties)).value(), + ldraw::Color{quadrilateral->getProperty(ldraw::Property::Color).toInt()}); + triangles.push_back(triangle); + position += 1; + } + result = {triangles[0], triangles[1]}; } return result; }