1 #include <QHBoxLayout> |
1 #include <QHBoxLayout> |
2 #include "model.h" |
2 #include "model.h" |
3 #include "modeleditcontext.h" |
3 #include "modeleditcontext.h" |
4 #include "linetypes/object.h" |
4 #include "linetypes/object.h" |
5 #include "transformtool.h" |
5 #include "transformtool.h" |
|
6 #include "document.h" |
6 |
7 |
7 TransformTool::TransformTool(Model* model, QWidget* parent) : |
8 TransformTool::TransformTool(Document* document) : |
8 BaseTool{model, parent}, |
9 BaseTool{document}, |
9 model{model}, |
10 matrixEditor{new MatrixEditor{document}}, |
10 matrixEditor{new MatrixEditor{parent}}, |
11 button{new QPushButton{"Apply", document}}, |
11 button{new QPushButton{"Apply", parent}}, |
12 widget{new QWidget{document}} |
12 widget{new QWidget{parent}} |
|
13 { |
13 { |
14 widget->setLayout(new QHBoxLayout{widget}); |
14 widget->setLayout(new QHBoxLayout{widget}); |
15 widget->layout()->addWidget(this->matrixEditor); |
15 widget->layout()->addWidget(this->matrixEditor); |
16 widget->layout()->addWidget(button); |
16 widget->layout()->addWidget(button); |
17 connect(button, &QPushButton::clicked, this, &TransformTool::applyButtonClicked); |
17 connect(button, &QPushButton::clicked, this, &TransformTool::applyButtonClicked); |
37 return this->widget; |
37 return this->widget; |
38 } |
38 } |
39 |
39 |
40 void TransformTool::applyButtonClicked() |
40 void TransformTool::applyButtonClicked() |
41 { |
41 { |
42 Model::EditContext editcontext = this->model->edit(); |
42 std::unique_ptr<ModelEditor> editor = this->document->editModel(); |
43 const glm::mat4 matrix = this->matrixEditor->value(); |
43 const glm::mat4 matrix = this->matrixEditor->value(); |
44 for (ldraw::id_t id : this->selection) |
44 for (ldraw::id_t id : this->selection) |
45 { |
45 { |
46 const ldraw::Object* object = model->get(id); |
46 editor->modifyObject(id, [&](ldraw::Object* object){ |
47 for (int i = 0; i < object->numPoints(); i += 1) |
47 for (int i = 0; i < object->numPoints(); i += 1) |
48 { |
48 { |
49 const ldraw::Property property = ldraw::pointProperty(i); |
49 const ldraw::Property property = ldraw::pointProperty(i); |
50 const glm::vec3& vec = matrix * glm::vec4{object->getPoint(i), 1}; |
50 const glm::vec3& vec = matrix * glm::vec4{object->getPoint(i), 1}; |
51 editcontext.setObjectProperty(id, property, QVariant::fromValue(vec)); |
51 object->setProperty({property, QVariant::fromValue(vec)}); |
52 } |
52 } |
53 QVariant transformMatrix = object->getProperty(ldraw::Property::Transformation); |
53 QVariant transformMatrix = object->getProperty(ldraw::Property::Transformation); |
54 if (not transformMatrix.isNull()) |
54 if (not transformMatrix.isNull()) |
55 { |
55 { |
56 editcontext.setObjectProperty( |
56 object->setProperty<ldraw::Property::Transformation>(matrix * transformMatrix.value<glm::mat4>()); |
57 id, |
57 } |
58 ldraw::Property::Transformation, |
58 }); |
59 QVariant::fromValue(matrix * transformMatrix.value<glm::mat4>()) |
|
60 ); |
|
61 } |
|
62 } |
59 } |
63 } |
60 } |