--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/tools/transformtool.cpp Sun Aug 29 22:14:42 2021 +0300 @@ -0,0 +1,68 @@ +#include <QHBoxLayout> +#include "model.h" +#include "modeleditcontext.h" +#include "linetypes/object.h" +#include "transformtool.h" + +TransformTool::TransformTool(Model* model, QObject* parent) : + BaseTool{model, parent}, + model{model}, + button{new QPushButton{"Apply"}}, + widget{new QWidget} +{ + widget->setLayout(new QHBoxLayout{widget}); + widget->layout()->addWidget(&this->matrixEditor); + widget->layout()->addWidget(button); + connect(button, &QPushButton::clicked, this, &TransformTool::applyButtonClicked); +} + +TransformTool::~TransformTool() +{ + delete this->widget; + delete this->button; +} + +QString TransformTool::name() const +{ + return "Transform"; +} + +QString TransformTool::toolTip() const +{ + return "Transforms the selection using a matrix"; +} + +void TransformTool::selectionChanged(const QSet<ldraw::id_t> &newSelection) +{ + this->selection = newSelection; +} + +QWidget* TransformTool::toolWidget() +{ + return this->widget; +} + +void TransformTool::applyButtonClicked() +{ + Model::EditContext editcontext = this->model->edit(); + const glm::mat4 matrix = this->matrixEditor.value(); + for (ldraw::id_t id : this->selection) + { + const ldraw::Object* object = model->get(id); + for (int i = 0; i < object->numPoints(); i += 1) + { + const ldraw::Property property = ldraw::pointProperty(i); + const glm::vec3& vec = matrix * glm::vec4{object->getPoint(i), 1}; + editcontext.setObjectProperty(id, property, QVariant::fromValue(vec)); + } + QVariant transformMatrix = object->getProperty(ldraw::Property::Transformation); + if (not transformMatrix.isNull()) + { + editcontext.setObjectProperty( + id, + ldraw::Property::Transformation, + QVariant::fromValue(matrix * transformMatrix.value<glm::mat4>()) + ); + } + } +}