src/tools/transformtool.cpp

Tue, 01 Mar 2022 17:00:19 +0200

author
Teemu Piippo <teemu@hecknology.net>
date
Tue, 01 Mar 2022 17:00:19 +0200
changeset 149
008989bc7d6e
parent 143
7b62c52835a1
child 152
03f8e6d42e13
permissions
-rw-r--r--

work on edit history

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

TransformTool::TransformTool(Model* model, QWidget* parent) :
	BaseTool{model, parent},
	model{model},
	matrixEditor{new MatrixEditor{parent}},
	button{new QPushButton{"Apply", parent}},
	widget{new QWidget{parent}}
{
	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()
{
	Model::EditContext editcontext = this->model->edit();
	const glm::mat4 matrix = this->matrixEditor->value();
	for (ldraw::id_t id : this->selection)
	{
		const ldraw::Object* object = model->get(id);
		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};
			editcontext.setObjectProperty(id, property, QVariant::fromValue(vec));
		}
		QVariant transformMatrix = object->getProperty(ldraw::Property::Transformation);
		if (not transformMatrix.isNull())
		{
			editcontext.setObjectProperty(
				id,
				ldraw::Property::Transformation,
				QVariant::fromValue(matrix * transformMatrix.value<glm::mat4>())
			);
		}
	}
}

mercurial