src/tools/transformtool.cpp

Tue, 28 Sep 2021 00:10:29 +0300

author
Teemu Piippo <teemu@hecknology.net>
date
Tue, 28 Sep 2021 00:10:29 +0300
changeset 141
185eb297dc1e
parent 127
f64bfb7f5d26
child 143
7b62c52835a1
permissions
-rw-r--r--

Saving works now

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

mercurial