src/tools/transformtool.cpp

Fri, 04 Mar 2022 11:40:19 +0200

author
Teemu Piippo <teemu@hecknology.net>
date
Fri, 04 Mar 2022 11:40:19 +0200
changeset 153
2f79053c2e9a
parent 152
03f8e6d42e13
child 163
36ea1a8aee33
permissions
-rw-r--r--

Renamed modeleditcontext.cpp -> modeleditor.cpp

#include <QHBoxLayout>
#include "model.h"
#include "modeleditor.h"
#include "linetypes/object.h"
#include "transformtool.h"
#include "document.h"

TransformTool::TransformTool(Document* document) :
	BaseTool{document},
	matrixEditor{new MatrixEditor{document}},
	button{new QPushButton{"Apply", document}},
	widget{new QWidget{document}}
{
	widget->setLayout(new QHBoxLayout{widget});
	widget->layout()->addWidget(this->matrixEditor);
	widget->layout()->addWidget(button);
	connect(button, &QPushButton::clicked, this, &TransformTool::applyButtonClicked);
}

QString TransformTool::name() const
{
	return "Transform";
}

QString TransformTool::toolTip() const
{
	return "Transforms the selection using a matrix";
}

void TransformTool::selectionChanged(const QSet<ldraw::id_t> &newSelection)
{
	this->selection = newSelection;
}

QWidget* TransformTool::toolWidget()
{
	return this->widget;
}

void TransformTool::applyButtonClicked()
{
	std::unique_ptr<ModelEditor> editor = this->document->editModel();
	const glm::mat4 matrix = this->matrixEditor->value();
	for (ldraw::id_t id : this->selection)
	{
		editor->modifyObject(id, [&](ldraw::Object* object){
			for (int i = 0; i < object->numPoints(); i += 1)
			{
				const ldraw::Property property = ldraw::pointProperty(i);
				const glm::vec3& vec = matrix * glm::vec4{object->getPoint(i), 1};
				object->setProperty({property, QVariant::fromValue(vec)});
			}
			QVariant transformMatrix = object->getProperty(ldraw::Property::Transformation);
			if (not transformMatrix.isNull())
			{
				object->setProperty<ldraw::Property::Transformation>(matrix * transformMatrix.value<glm::mat4>());
			}
		});
	}
}

mercurial