src/ui/polygonobjecteditor.cpp

Sat, 05 Mar 2022 00:44:15 +0200

author
Teemu Piippo <teemu@hecknology.net>
date
Sat, 05 Mar 2022 00:44:15 +0200
changeset 156
65b75beed7e0
parent 153
2f79053c2e9a
child 178
a23024fc98e0
permissions
-rw-r--r--

render the grid with lines

#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->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 QModelIndex index = this->document->getModel().find(this->objectId());
	if (index.isValid())
	{
		const ldraw::Object* const object = this->document->getModel()[index.row()];
		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)
{
	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