Sun, 29 Aug 2021 22:14:42 +0300
added a simple matrix transformation tool
--- 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
--- 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<ldraw::id_t>& newSelection) { - if (this->selectedTool != nullptr) + for (BaseTool* tool : this->tools) { - this->selectedTool->selectionChanged(newSelection); + tool->selectionChanged(newSelection); } }
--- /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>()) + ); + } + } +}
--- /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 <QPushButton> +#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<ldraw::id_t> &newSelection) override; + QWidget *toolWidget() override; +private: + Q_SLOT void applyButtonClicked(); + Model* const model; + MatrixEditor matrixEditor; + QPushButton* button; + QWidget* widget; + QSet<ldraw::id_t> selection; +};