src/tools/transformtool.cpp

changeset 127
f64bfb7f5d26
child 143
7b62c52835a1
equal deleted inserted replaced
126:a7c720aff97c 127:f64bfb7f5d26
1 #include <QHBoxLayout>
2 #include "model.h"
3 #include "modeleditcontext.h"
4 #include "linetypes/object.h"
5 #include "transformtool.h"
6
7 TransformTool::TransformTool(Model* model, QObject* parent) :
8 BaseTool{model, parent},
9 model{model},
10 button{new QPushButton{"Apply"}},
11 widget{new QWidget}
12 {
13 widget->setLayout(new QHBoxLayout{widget});
14 widget->layout()->addWidget(&this->matrixEditor);
15 widget->layout()->addWidget(button);
16 connect(button, &QPushButton::clicked, this, &TransformTool::applyButtonClicked);
17 }
18
19 TransformTool::~TransformTool()
20 {
21 delete this->widget;
22 delete this->button;
23 }
24
25 QString TransformTool::name() const
26 {
27 return "Transform";
28 }
29
30 QString TransformTool::toolTip() const
31 {
32 return "Transforms the selection using a matrix";
33 }
34
35 void TransformTool::selectionChanged(const QSet<ldraw::id_t> &newSelection)
36 {
37 this->selection = newSelection;
38 }
39
40 QWidget* TransformTool::toolWidget()
41 {
42 return this->widget;
43 }
44
45 void TransformTool::applyButtonClicked()
46 {
47 Model::EditContext editcontext = this->model->edit();
48 const glm::mat4 matrix = this->matrixEditor.value();
49 for (ldraw::id_t id : this->selection)
50 {
51 const ldraw::Object* object = model->get(id);
52 for (int i = 0; i < object->numPoints(); i += 1)
53 {
54 const ldraw::Property property = ldraw::pointProperty(i);
55 const glm::vec3& vec = matrix * glm::vec4{object->getPoint(i), 1};
56 editcontext.setObjectProperty(id, property, QVariant::fromValue(vec));
57 }
58 QVariant transformMatrix = object->getProperty(ldraw::Property::Transformation);
59 if (not transformMatrix.isNull())
60 {
61 editcontext.setObjectProperty(
62 id,
63 ldraw::Property::Transformation,
64 QVariant::fromValue(matrix * transformMatrix.value<glm::mat4>())
65 );
66 }
67 }
68 }

mercurial