src/tools/transformtool.cpp

Sat, 05 Mar 2022 16:57:28 +0200

author
Teemu Piippo <teemu@hecknology.net>
date
Sat, 05 Mar 2022 16:57:28 +0200
changeset 168
24590af32ad6
parent 163
36ea1a8aee33
permissions
-rw-r--r--

Draw tool now renders the winding of the new polygon

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"
153
2f79053c2e9a Renamed modeleditcontext.cpp -> modeleditor.cpp
Teemu Piippo <teemu@hecknology.net>
parents: 152
diff changeset
3 #include "modeleditor.h"
127
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"
152
03f8e6d42e13 Major refactoring
Teemu Piippo <teemu@hecknology.net>
parents: 143
diff changeset
6 #include "document.h"
127
f64bfb7f5d26 added a simple matrix transformation tool
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
7
152
03f8e6d42e13 Major refactoring
Teemu Piippo <teemu@hecknology.net>
parents: 143
diff changeset
8 TransformTool::TransformTool(Document* document) :
03f8e6d42e13 Major refactoring
Teemu Piippo <teemu@hecknology.net>
parents: 143
diff changeset
9 BaseTool{document},
03f8e6d42e13 Major refactoring
Teemu Piippo <teemu@hecknology.net>
parents: 143
diff changeset
10 matrixEditor{new MatrixEditor{document}},
03f8e6d42e13 Major refactoring
Teemu Piippo <teemu@hecknology.net>
parents: 143
diff changeset
11 button{new QPushButton{"Apply", document}},
03f8e6d42e13 Major refactoring
Teemu Piippo <teemu@hecknology.net>
parents: 143
diff changeset
12 widget{new QWidget{document}}
127
f64bfb7f5d26 added a simple matrix transformation tool
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
13 {
f64bfb7f5d26 added a simple matrix transformation tool
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
14 widget->setLayout(new QHBoxLayout{widget});
143
7b62c52835a1 Fix memory corruption involving document tools.
Teemu Piippo <teemu@hecknology.net>
parents: 127
diff changeset
15 widget->layout()->addWidget(this->matrixEditor);
127
f64bfb7f5d26 added a simple matrix transformation tool
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
16 widget->layout()->addWidget(button);
f64bfb7f5d26 added a simple matrix transformation tool
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
17 connect(button, &QPushButton::clicked, this, &TransformTool::applyButtonClicked);
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
f64bfb7f5d26 added a simple matrix transformation tool
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
20 QString TransformTool::name() const
f64bfb7f5d26 added a simple matrix transformation tool
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
21 {
f64bfb7f5d26 added a simple matrix transformation tool
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
22 return "Transform";
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::toolTip() 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 "Transforms the selection using a matrix";
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 void TransformTool::selectionChanged(const QSet<ldraw::id_t> &newSelection)
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 this->selection = newSelection;
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 QWidget* TransformTool::toolWidget()
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 return this->widget;
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
163
36ea1a8aee33 Add icons for tools
Teemu Piippo <teemu@hecknology.net>
parents: 153
diff changeset
40 QString TransformTool::iconName() const
36ea1a8aee33 Add icons for tools
Teemu Piippo <teemu@hecknology.net>
parents: 153
diff changeset
41 {
36ea1a8aee33 Add icons for tools
Teemu Piippo <teemu@hecknology.net>
parents: 153
diff changeset
42 return ":/icons/grid-outline.png";
36ea1a8aee33 Add icons for tools
Teemu Piippo <teemu@hecknology.net>
parents: 153
diff changeset
43 }
36ea1a8aee33 Add icons for tools
Teemu Piippo <teemu@hecknology.net>
parents: 153
diff changeset
44
127
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 {
152
03f8e6d42e13 Major refactoring
Teemu Piippo <teemu@hecknology.net>
parents: 143
diff changeset
47 std::unique_ptr<ModelEditor> editor = this->document->editModel();
143
7b62c52835a1 Fix memory corruption involving document tools.
Teemu Piippo <teemu@hecknology.net>
parents: 127
diff changeset
48 const glm::mat4 matrix = this->matrixEditor->value();
127
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 {
152
03f8e6d42e13 Major refactoring
Teemu Piippo <teemu@hecknology.net>
parents: 143
diff changeset
51 editor->modifyObject(id, [&](ldraw::Object* object){
03f8e6d42e13 Major refactoring
Teemu Piippo <teemu@hecknology.net>
parents: 143
diff changeset
52 for (int i = 0; i < object->numPoints(); i += 1)
03f8e6d42e13 Major refactoring
Teemu Piippo <teemu@hecknology.net>
parents: 143
diff changeset
53 {
03f8e6d42e13 Major refactoring
Teemu Piippo <teemu@hecknology.net>
parents: 143
diff changeset
54 const ldraw::Property property = ldraw::pointProperty(i);
03f8e6d42e13 Major refactoring
Teemu Piippo <teemu@hecknology.net>
parents: 143
diff changeset
55 const glm::vec3& vec = matrix * glm::vec4{object->getPoint(i), 1};
03f8e6d42e13 Major refactoring
Teemu Piippo <teemu@hecknology.net>
parents: 143
diff changeset
56 object->setProperty({property, QVariant::fromValue(vec)});
03f8e6d42e13 Major refactoring
Teemu Piippo <teemu@hecknology.net>
parents: 143
diff changeset
57 }
03f8e6d42e13 Major refactoring
Teemu Piippo <teemu@hecknology.net>
parents: 143
diff changeset
58 QVariant transformMatrix = object->getProperty(ldraw::Property::Transformation);
03f8e6d42e13 Major refactoring
Teemu Piippo <teemu@hecknology.net>
parents: 143
diff changeset
59 if (not transformMatrix.isNull())
03f8e6d42e13 Major refactoring
Teemu Piippo <teemu@hecknology.net>
parents: 143
diff changeset
60 {
03f8e6d42e13 Major refactoring
Teemu Piippo <teemu@hecknology.net>
parents: 143
diff changeset
61 object->setProperty<ldraw::Property::Transformation>(matrix * transformMatrix.value<glm::mat4>());
03f8e6d42e13 Major refactoring
Teemu Piippo <teemu@hecknology.net>
parents: 143
diff changeset
62 }
03f8e6d42e13 Major refactoring
Teemu Piippo <teemu@hecknology.net>
parents: 143
diff changeset
63 });
127
f64bfb7f5d26 added a simple matrix transformation tool
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
64 }
f64bfb7f5d26 added a simple matrix transformation tool
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
65 }

mercurial