src/ui/objecteditor.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 177
f69d53c053df
child 180
5b7a8f2270ff
permissions
-rw-r--r--

fix saving
add color editing to object editor

#include <QVBoxLayout>
#include <QFormLayout>
#include <QPushButton>
#include <QLabel>
#include <QSpinBox>
#include "objecteditor.h"
#include "document.h"
#include "modeleditor.h"
#include "widgets/colorbutton.h"
#include "widgets/colorindexinput.h"

template<ldraw::Property property>
static void makeColorEditor()
{
	QString propertyName = ldraw::PropertyTraits<property>::name;
}

ObjectEditor::ObjectEditor(Document* document, const ldraw::id_t id) :
	QWidget{document},
	document{document},
	formContainer{new QWidget{this}},
	objectTypeNameLabel{new QLabel{this}},
	objectTypeIconLabel{new QLabel{this}}
{
	this->polygonEditor.emplace(this->document, id);
	this->setLayout(new QVBoxLayout{this});
	QWidget* objectTitleLayoutContainer = new QWidget{this};
	QLayout* objectTitleLayout = new QHBoxLayout{objectTitleLayoutContainer};
	objectTitleLayoutContainer->setLayout(objectTitleLayout);
	objectTitleLayout->addWidget(this->objectTypeIconLabel);
	objectTitleLayout->addWidget(this->objectTypeNameLabel);
	objectTitleLayout->addWidget(new QSplitter{Qt::Horizontal, this});
	this->layout()->addWidget(objectTitleLayoutContainer);
	this->layout()->addWidget(&*this->polygonEditor);
	this->layout()->addWidget(formContainer);
	this->setObjectId(id);
	
	QWidget* const parent = this->formContainer;
	QFormLayout* formLayout = new QFormLayout{parent};
	this->formContainer->setLayout(formLayout);
	QLabel* colorLabel = new QLabel{"Color", parent};
	ColorIndexInput* colorWidget = new ColorIndexInput{document, {0}, parent};
	formLayout->addRow(colorLabel, colorWidget);
	connect(colorWidget, &ColorIndexInput::colorChanged, [this](ldraw::Color color)
	{
		const QModelIndex index = this->document->getModel().find(this->objectId);
		if (index.isValid())
		{
			this->document->editModel()->modifyObjectAt<ldraw::ColoredObject>(
				index.row(),
				[color](ldraw::ColoredObject* object)
				{
					object->colorIndex = color;
				}
			);
		}
	});
	this->propertyWidgets[ldraw::Property::Color] = {colorLabel, colorWidget};
}

QString titleCase(const QString& string)
{
	return string.left(1).toUpper() + string.mid(1);
}

void ObjectEditor::setObjectId(const ldraw::id_t id)
{
	this->objectId = id;
	const ldraw::Object* object = this->document->getModel().get(id);
	this->clear();
	if (object != nullptr)
	{
		this->objectTypeNameLabel->setText("<b>" + titleCase(object->typeName()) + "</b>");
		this->objectTypeIconLabel->setPixmap(QPixmap{object->iconName()}.scaledToWidth(24));
		if (object->numPoints() > 0)
		{
			this->polygonEditor->setObjectId(id);
		}
		else
		{
			this->polygonEditor->clear();
		}
		constexpr ldraw::Property prop = ldraw::Property::Color;
		QVariant color = object->getProperty(prop);
		this->propertyWidgets[prop].first->setVisible(not color.isNull());
		this->propertyWidgets[prop].second->setVisible(not color.isNull());
		if (not color.isNull())
		{
			static_cast<ColorIndexInput*>(this->propertyWidgets[prop].second)->setSelectedColor(color.value<ldraw::Color>());
		}
	}
	else
	{
		for (auto& pair : this->propertyWidgets)
		{
			pair.first->setVisible(false);
			pair.second->setVisible(false);
		}
	}
}

void ObjectEditor::clear()
{
	this->objectTypeNameLabel->clear();
	this->objectTypeIconLabel->clear();
	this->polygonEditor->clear();
	delete this->formContainer->layout();
}

mercurial