# HG changeset patch # User Teemu Piippo # Date 1630264482 -10800 # Node ID f64bfb7f5d26c47b0ff1665a3a505423c0cffb4e # Parent a7c720aff97cb190b76346fcb4af6c676199c1cc added a simple matrix transformation tool diff -r a7c720aff97c -r f64bfb7f5d26 CMakeLists.txt --- a/CMakeLists.txt Sun Aug 29 21:28:08 2021 +0300 +++ b/CMakeLists.txt Sun Aug 29 22:14:42 2021 +0300 @@ -74,6 +74,7 @@ src/tools/basetool.cpp src/tools/selecttool.cpp src/tools/drawtool.cpp + src/tools/transformtool.cpp ) set (LDFORGE_HEADERS src/basics.h @@ -130,6 +131,7 @@ src/tools/selecttool.h src/tools/basetool.h src/tools/drawtool.h + src/tools/transformtool.h ) set (LDFORGE_FORMS src/document.ui diff -r a7c720aff97c -r f64bfb7f5d26 src/document.cpp --- a/src/document.cpp Sun Aug 29 21:28:08 2021 +0300 +++ b/src/document.cpp Sun Aug 29 22:14:42 2021 +0300 @@ -25,10 +25,12 @@ #include "tools/basetool.h" #include "tools/drawtool.h" #include "tools/selecttool.h" +#include "tools/transformtool.h" static const QMetaObject* const toolMetaObjects[] = { &SelectTool::staticMetaObject, &DrawTool::staticMetaObject, + &TransformTool::staticMetaObject, }; Document::Document( @@ -148,9 +150,9 @@ void Document::selectionChanged(const QSet& newSelection) { - if (this->selectedTool != nullptr) + for (BaseTool* tool : this->tools) { - this->selectedTool->selectionChanged(newSelection); + tool->selectionChanged(newSelection); } } diff -r a7c720aff97c -r f64bfb7f5d26 src/tools/transformtool.cpp --- /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 +#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 &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()) + ); + } + } +} diff -r a7c720aff97c -r f64bfb7f5d26 src/tools/transformtool.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/tools/transformtool.h Sun Aug 29 22:14:42 2021 +0300 @@ -0,0 +1,23 @@ +#pragma once +#include +#include "basetool.h" +#include "widgets/matrixeditor.h" + +class TransformTool : public BaseTool +{ + Q_OBJECT +public: + Q_INVOKABLE TransformTool(Model *model, QObject *parent = nullptr); + ~TransformTool(); + virtual QString name() const override; + virtual QString toolTip() const override; + void selectionChanged(const QSet &newSelection) override; + QWidget *toolWidget() override; +private: + Q_SLOT void applyButtonClicked(); + Model* const model; + MatrixEditor matrixEditor; + QPushButton* button; + QWidget* widget; + QSet selection; +};