src/tools/transformtool.cpp

Sun, 12 Sep 2021 12:14:32 +0300

author
Teemu Piippo <teemu@hecknology.net>
date
Sun, 12 Sep 2021 12:14:32 +0300
changeset 128
7c834fe36b25
parent 127
f64bfb7f5d26
child 143
7b62c52835a1
permissions
-rw-r--r--

Moved automatic grid adjusting into a new action

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

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

TransformTool::~TransformTool()
{
	delete this->widget;
	delete this->button;
}

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