src/ui/polygonobjecteditor.cpp

Thu, 05 Nov 2020 14:29:58 +0200

author
Teemu Piippo <teemu@hecknology.net>
date
Thu, 05 Nov 2020 14:29:58 +0200
changeset 95
06a1aef170aa
parent 89
7abaf1d64719
child 100
43ce3672648a
permissions
-rw-r--r--

asioita

#include <QFormLayout>
#include <QSplitter>
#include "model.h"
#include "modeleditcontext.h"
#include "widgets/vec3editor.h"
#include "ui/polygonobjecteditor.h"

static constexpr char INDEX_NAME[] = "_ldforge_index";
static constexpr char PROPERTY_NAME[] = "_ldforge_property";
static constexpr char OBJECT_ID_NAME[] = "_ldforge_id";
static constexpr char LABEL_NAME[] = "_ldforge_label";

PolygonObjectEditor::PolygonObjectEditor(Model* model, ldraw::id_t id, QWidget* parent) :
	QWidget{parent},
	model{model},
	storedObjectId{ldraw::NULL_ID.value}
{
	this->splitter.emplace(Qt::Vertical, this);
	this->setObjectId(id);
}

// destructor needed for std::unique_ptr
PolygonObjectEditor::~PolygonObjectEditor()
{
}

ldraw::id_t PolygonObjectEditor::objectId() const
{
	return this->storedObjectId;
}

void PolygonObjectEditor::setObjectId(ldraw::id_t id)
{
	this->storedObjectId = id;
	this->buildWidgets();
}

void PolygonObjectEditor::buildWidgets()
{
	this->widgets.clear();
	delete this->layout();
	QFormLayout* layout = new QFormLayout{this};
	this->setLayout(layout);
	for (int n : {0, 1, 2, 3})
	{
		this->setupPointWidget(n);
	}
	for (std::unique_ptr<QWidget>& widget : this->widgets)
	{
		const QString label = widget->property(LABEL_NAME).toString();
		layout->addRow(label, widget.get());
	}
	layout->addRow("", &*this->splitter);
}

void PolygonObjectEditor::setupPointWidget(int n)
{
	const ldraw::Object* const object = this->model->get(this->objectId());
	const ldraw::Property property = ldraw::pointProperty(n);
	const QVariant value = object->getProperty(property);
	if (value.isValid())
	{
		std::unique_ptr<Vec3Editor> editor = std::make_unique<Vec3Editor>(value.value<glm::vec3>(), this);
		QObject::connect(editor.get(), &Vec3Editor::valueChanged, this, &PolygonObjectEditor::pointChanged);
		editor->setProperty(INDEX_NAME, QVariant::fromValue(n));
		editor->setProperty(LABEL_NAME, &ldraw::traits(property).name[0]);
		this->widgets.push_back(std::move(editor));
	}
}

void PolygonObjectEditor::pointChanged(const glm::vec3& value)
{
	Model::EditContext editcontext = this->model->edit();
	const int n = this->sender()->property(INDEX_NAME).toInt();
	const ldraw::Property property = ldraw::pointProperty(n);
	editcontext.setObjectProperty(this->objectId(), property, QVariant::fromValue(value));
}

mercurial