src/ui/polygonobjecteditor.cpp

Sun, 13 Mar 2022 14:51:39 +0200

author
Teemu Piippo <teemu@hecknology.net>
date
Sun, 13 Mar 2022 14:51:39 +0200
changeset 178
a23024fc98e0
parent 153
2f79053c2e9a
permissions
-rw-r--r--

fix saving
add color editing to object editor

#include <QFormLayout>
#include <QSplitter>
#include "document.h"
#include "modeleditor.h"
#include "widgets/vec3editor.h"
#include "ui/polygonobjecteditor.h"

static constexpr char INDEX_NAME[] = "_ldforge_index";
static constexpr char LABEL_NAME[] = "_ldforge_label";

PolygonObjectEditor::PolygonObjectEditor(Document* document, ldraw::id_t id) :
	QWidget{document},
	document{document},
	storedObjectId{ldraw::NULL_ID.value}
{
	this->splitter.emplace(Qt::Vertical, this);
	this->buildWidgets();
	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;
	const QModelIndex index = this->document->getModel().find(this->objectId());
	if (index.isValid())
	{
		for (int n : {0, 1, 2, 3})
		{
			const ldraw::Object* const object = this->document->getModel()[index.row()];
			const ldraw::Property property = ldraw::pointProperty(n);
			const QVariant value = object->getProperty(property);
			this->widgets[n]->setVisible(value.isValid());
			this->formLayout->itemAt(n, QFormLayout::LabelRole)->widget()->setVisible(value.isValid());
			if (value.isValid())
			{
				this->widgets[n]->setValue(value.value<glm::vec3>());
			}
		}
	}
	else
	{
		for (int n : {0, 1, 2, 3})
		{
			this->widgets[n]->setVisible(false);
			this->formLayout->itemAt(n, QFormLayout::LabelRole)->widget()->setVisible(false);
		}
	}
}

void PolygonObjectEditor::clear()
{
	this->setObjectId(ldraw::NULL_ID);
}

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

void PolygonObjectEditor::setupPointWidget(int n)
{
	std::unique_ptr<Vec3Editor> editor = std::make_unique<Vec3Editor>(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(ldraw::pointProperty(n)).name[0]);
	this->widgets.push_back(std::move(editor));
}

void PolygonObjectEditor::pointChanged(const glm::vec3& value)
{
	std::unique_ptr<ModelEditor> modelEditor = this->document->editModel();
	const int n = this->sender()->property(INDEX_NAME).toInt();
	const ldraw::Property property = ldraw::pointProperty(n);
	modelEditor->setObjectProperty(this->objectId(), property, QVariant::fromValue(value));
}

mercurial