--- a/src/modeleditcontext.cpp Thu Mar 03 21:13:16 2022 +0200 +++ b/src/modeleditcontext.cpp Fri Mar 04 11:37:50 2022 +0200 @@ -20,40 +20,42 @@ #include "linetypes/triangle.h" #include "linetypes/quadrilateral.h" -Model::EditContext::EditContext(Model& model) : +ModelEditor::ModelEditor(Model& model) : storedModel{model} { } -Model::EditContext::~EditContext() +ModelEditor::~ModelEditor() { for (ldraw::id_t id : this->modifiedObjects) { - this->model().objectModified(id); + const QModelIndex index = this->model().find(id); + if (index.isValid()) + { + Q_EMIT this->objectModified(index.row()); + } } - this->model().editFinished(); } -ldraw::id_t Model::EditContext::append(std::unique_ptr<ldraw::Object>&& object) +ldraw::id_t ModelEditor::append(std::unique_ptr<ldraw::Object>&& object) { - const ldraw::id_t id = object->id; - this->model().objectsById[id] = object.get(); - this->model().append(std::move(object)); - return id; + this->storedModel.append(std::move(object)); + Q_EMIT this->objectAdded(this->model().size() - 1); + return object->id; } -void Model::EditContext::remove(int position) +void ModelEditor::remove(int position) { - this->model().remove(position); + this->storedModel.remove(position); } -auto Model::EditContext::setObjectProperty( +auto ModelEditor::setObjectProperty( const ldraw::id_t id, const ldraw::Property property, const QVariant& value) -> ldraw::Object::SetPropertyResult { - ldraw::Object* const object = this->model().findObjectById(id); + ldraw::Object* const object = this->storedModel.findObjectById(id); if (object != nullptr) { const ldraw::Object::SetPropertyResult result = object->setProperty(ldraw::PropertyKeyValue{property, value}); @@ -66,9 +68,9 @@ } } -void Model::EditContext::setObjectPoint(ldraw::id_t id, int pointId, const glm::vec3& value) +void ModelEditor::setObjectPoint(ldraw::id_t id, int pointId, const glm::vec3& value) { - ldraw::Object* object = this->model().findObjectById(id); + ldraw::Object* object = this->storedModel.findObjectById(id); if (object != nullptr) { object->setProperty(ldraw::PropertyKeyValue{ldraw::pointProperty(pointId), QVariant::fromValue(value)}); @@ -76,53 +78,8 @@ } } -void Model::EditContext::invertObject(ldraw::id_t id) -{ - auto it = this->model().objectsById.find(id); - if (it != this->model().objectsById.end()) - { - ldraw::Object* object = it->second; - object->invert(); - } -} - -Model& Model::EditContext::model() +const Model &ModelEditor::model() { return this->storedModel; } -static std::array<geom::Triangle, 2> splitTriangles(ldraw::Diagonal diagonal, const std::array<glm::vec3, 4>& points) -{ - std::array<geom::Triangle, 2> result; - switch (diagonal) - { - case ldraw::Diagonal::Diagonal_13: - result = {geom::Triangle{points[0], points[1], points[2]}, {points[0], points[2], points[3]}}; - break; - case ldraw::Diagonal::Diagonal_24: - result = {geom::Triangle{points[0], points[1], points[3]}, {points[1], points[2], points[3]}}; - break; - } - return result; -} - -auto ldraw::splitQuadrilateral( - Model::EditContext& editor, - ldraw::quadrilateralid_t quadrilateral_id, - ldraw::Diagonal splitType -) -> std::optional<std::pair<ldraw::triangleid_t, ldraw::triangleid_t>> -{ - std::optional<std::pair<ldraw::triangleid_t, ldraw::triangleid_t>> result; - const auto resolved = editor.model().get2(quadrilateral_id); - if (resolved.object != nullptr) - { - 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), - editor.insert<ldraw::Triangle>(position, split[1].points, color)); - } - return result; -}