src/ui/polygonobjecteditor.cpp

Thu, 19 Mar 2020 21:06:06 +0200

author
Teemu Piippo <teemu@hecknology.net>
date
Thu, 19 Mar 2020 21:06:06 +0200
changeset 86
4bec0525ef1b
parent 84
7137c20979af
child 89
7abaf1d64719
permissions
-rw-r--r--

PolygonObjectEditor can now modify the object properly

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

constexpr char COLUMN_PROPERTY[] = "_ldforge_column";

PolygonObjectEditor::PolygonObjectEditor(Model* model, ldraw::id_t id, QWidget* parent) :
	QWidget{parent},
	model{model},
	storedObjectId{ldraw::NULL_ID.value}
{
	this->setLayout(new QVBoxLayout{this});
	for (int i = 0; i < countof(this->vec3Editors); i += 1)
	{
		std::optional<Vec3Editor>& editorPointer = this->vec3Editors[i];
		editorPointer.emplace(glm::vec3{}, this);
		editorPointer->setProperty(COLUMN_PROPERTY, QVariant::fromValue(i));
		this->layout()->addWidget(&*editorPointer);
		connect(&*editorPointer, &Vec3Editor::valueChanged, this, &PolygonObjectEditor::vectorChanged);
	}
	this->layout()->addWidget(new QSplitter{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->updateNumRows();
}

void PolygonObjectEditor::vectorChanged(const glm::vec3& value)
{
	bool ok;
	const int num = sender()->property(COLUMN_PROPERTY).toInt(&ok);
	if (ok and num >= 0 and num < countof(this->vec3Editors))
	{
		Model::EditContext editor = this->model->edit();
		editor.setObjectPoint(this->objectId(), num, value);
	}
}

void PolygonObjectEditor::updateNumRows()
{
	const ldraw::Object* object = model->get(this->storedObjectId);
	const int numPoints = object != nullptr ? object->numPoints() : 0;
	for (int i = 0; i < countof(this->vec3Editors); i += 1)
	{
		Vec3Editor& editor = *this->vec3Editors[i];
		QSignalBlocker blocker{&editor};
		if (i < numPoints)
		{
			editor.setVisible(true);
			editor.setValue(object->getPoint(i));
		}
		else
		{
			editor.setVisible(false);
			editor.setValue(glm::vec3{});
		}
	}
}

mercurial