src/tools/transformtool.cpp

Sat, 05 Mar 2022 15:40:43 +0200

author
Teemu Piippo <teemu@hecknology.net>
date
Sat, 05 Mar 2022 15:40:43 +0200
changeset 167
c1ff4f107569
parent 163
36ea1a8aee33
permissions
-rw-r--r--

the grid now has less lines

#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;
}

QString TransformTool::iconName() const
{
	return ":/icons/grid-outline.png";
}

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