src/ui/objecteditor.cpp

Wed, 22 Jun 2022 16:53:35 +0300

author
Teemu Piippo <teemu.s.piippo@gmail.com>
date
Wed, 22 Jun 2022 16:53:35 +0300
changeset 250
2837b549e616
parent 249
37d3c819cafa
child 252
da4876bfd822
permissions
-rw-r--r--

I felt that the compiler was too kind to me, so I enabled a big pile of warnings

#include <QCheckBox>
#include <QLineEdit>
#include <QFormLayout>
#include "objecteditor.h"
#include "document.h"
#include "widgets/colorbutton.h"
#include "widgets/colorindexinput.h"
#include "widgets/vec3editor.h"
#include "ui_objecteditor.h"

using PropertyPointer = std::variant<
	glm::vec3*,
	glm::mat4*,
	QString*,
	ldraw::Color*,
	CircularFraction*>;

enum class PropertyKey
{
	Point1,
	Point2,
	Point3,
	Point4,
	Control1,
	Control2,
	Color,
	Transformation,
	Name,
	Text,
	Code,
	Fraction,
};

constexpr std::size_t NUM_PROPERTIES = static_cast<int>(PropertyKey::Fraction) + 1;

static QString propertyName(PropertyKey key)
{
	switch (key) {
	case PropertyKey::Point1:
		return ObjectEditor::tr("Vertex 1");
	case PropertyKey::Point2:
		return ObjectEditor::tr("Vertex 2");
	case PropertyKey::Point3:
		return ObjectEditor::tr("Vertex 3");
	case PropertyKey::Point4:
		return ObjectEditor::tr("Vertex 4");
	case PropertyKey::Control1:
		return ObjectEditor::tr("Control point 1");
	case PropertyKey::Control2:
		return ObjectEditor::tr("Control point 2");
	case PropertyKey::Color:
		return ObjectEditor::tr("Colour");
	case PropertyKey::Transformation:
		return ObjectEditor::tr("Transformation");
	case PropertyKey::Name:
		return ObjectEditor::tr("Name");
	case PropertyKey::Text:
		return ObjectEditor::tr("Text");
	case PropertyKey::Code:
		return ObjectEditor::tr("Code");
	case PropertyKey::Fraction:
		return ObjectEditor::tr("Fraction");
	}
	return "";
}

static std::map<PropertyKey, PropertyPointer> collectProperties(ModelElement& element)
{
	std::map<PropertyKey, PropertyPointer> result;
	std::visit<void>(overloaded{
		[&](Colored<LineSegment>& edge) {
			result[PropertyKey::Point1] = &edge.p1;
			result[PropertyKey::Point2] = &edge.p2;
			result[PropertyKey::Color] = &edge.color;
		},
		[&](Colored<Triangle>& tri) {
			result[PropertyKey::Point1] = &tri.p1;
			result[PropertyKey::Point2] = &tri.p2;
			result[PropertyKey::Point3] = &tri.p3;
			result[PropertyKey::Color] = &tri.color;
		},
		[&](Colored<Quadrilateral>& quad) {
			result[PropertyKey::Point1] = &quad.p1;
			result[PropertyKey::Point2] = &quad.p2;
			result[PropertyKey::Point3] = &quad.p3;
			result[PropertyKey::Point4] = &quad.p4;
			result[PropertyKey::Color] = &quad.color;
		},
		[&](Colored<ConditionalEdge>& cedge) {
			result[PropertyKey::Point1] = &cedge.p1;
			result[PropertyKey::Point2] = &cedge.p2;
			result[PropertyKey::Control1] = &cedge.c1;
			result[PropertyKey::Control2] = &cedge.c2;
			result[PropertyKey::Color] = &cedge.color;
		},
		[&](Colored<SubfileReference>& ref) {
			result[PropertyKey::Transformation] = &ref.transformation;
			result[PropertyKey::Name] = &ref.name;
			result[PropertyKey::Color] = &ref.color;
		},
		[&](Colored<CircularPrimitive>& circ) {
			result[PropertyKey::Transformation] = &circ.transformation;
			result[PropertyKey::Fraction] = &circ.fraction;
			result[PropertyKey::Color] = &circ.color;
		},
		[&](Empty) {},
		[&](Comment& comment) {
			result[PropertyKey::Text] = &comment.text;
		},
		[&](ParseError& parseError) {
			result[PropertyKey::Code] = &parseError.code;
		},
	}, element);
	return result;
}


ObjectEditor::ObjectEditor(QWidget* parent) :
	QWidget{parent}
{
	this->ui.setupUi(this);
	this->editorwidgets.resize(NUM_PROPERTIES);
	QFormLayout* layout = new QFormLayout{this};
	for (std::size_t i = 0; i < NUM_PROPERTIES; ++i) {
		const auto key = static_cast<PropertyKey>(i);
		QLabel* const label = new QLabel{propertyName(key), this};
		QWidget* const field = new QWidget{this};
		this->editorwidgets[i] = field;
		layout->addRow(label, field);
	}
	this->ui.properties->setLayout(layout);
}

mercurial