1 #include <QHBoxLayout> |
|
2 #include "model.h" |
|
3 #include "modeleditor.h" |
|
4 #include "linetypes/object.h" |
|
5 #include "transformtool.h" |
|
6 #include "document.h" |
|
7 |
|
8 TransformTool::TransformTool(Document* document) : |
|
9 BaseTool{document}, |
|
10 matrixEditor{new MatrixEditor{document}}, |
|
11 button{new QPushButton{"Apply", document}}, |
|
12 widget{new QWidget{document}} |
|
13 { |
|
14 widget->setLayout(new QHBoxLayout{widget}); |
|
15 widget->layout()->addWidget(this->matrixEditor); |
|
16 widget->layout()->addWidget(button); |
|
17 connect(button, &QPushButton::clicked, this, &TransformTool::applyButtonClicked); |
|
18 } |
|
19 |
|
20 QString TransformTool::name() const |
|
21 { |
|
22 return "Transform"; |
|
23 } |
|
24 |
|
25 QString TransformTool::toolTip() const |
|
26 { |
|
27 return "Transforms the selection using a matrix"; |
|
28 } |
|
29 |
|
30 void TransformTool::selectionChanged(const QSet<ldraw::id_t> &newSelection) |
|
31 { |
|
32 this->selection = newSelection; |
|
33 } |
|
34 |
|
35 QWidget* TransformTool::toolWidget() |
|
36 { |
|
37 return this->widget; |
|
38 } |
|
39 |
|
40 QString TransformTool::iconName() const |
|
41 { |
|
42 return ":/icons/grid-outline.png"; |
|
43 } |
|
44 |
|
45 void TransformTool::applyButtonClicked() |
|
46 { |
|
47 std::unique_ptr<ModelEditor> editor = this->document->editModel(); |
|
48 const glm::mat4 matrix = this->matrixEditor->value(); |
|
49 for (ldraw::id_t id : this->selection) |
|
50 { |
|
51 editor->modifyObject(id, [&](ldraw::Object* object){ |
|
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 object->setProperty({property, QVariant::fromValue(vec)}); |
|
57 } |
|
58 QVariant transformMatrix = object->getProperty(ldraw::Property::Transformation); |
|
59 if (not transformMatrix.isNull()) |
|
60 { |
|
61 object->setProperty<ldraw::Property::Transformation>(matrix * transformMatrix.value<glm::mat4>()); |
|
62 } |
|
63 }); |
|
64 } |
|
65 } |
|