src/ui/objecteditor.cpp

changeset 200
ca23936b455b
parent 183
97b591813c8b
child 232
8efa3a33172e
--- a/src/ui/objecteditor.cpp	Wed May 25 20:36:34 2022 +0300
+++ b/src/ui/objecteditor.cpp	Mon Jun 06 22:01:22 2022 +0300
@@ -3,168 +3,73 @@
 #include <QFormLayout>
 #include "objecteditor.h"
 #include "document.h"
-#include "modeleditor.h"
 #include "widgets/colorbutton.h"
 #include "widgets/colorindexinput.h"
 #include "widgets/vec3editor.h"
 #include "ui_objecteditor.h"
 
-ObjectEditor::ObjectEditor(Document* document, const ldraw::id_t id) :
-	QWidget{document},
-	ui{*new Ui_ObjectEditor}, 
-	document{document}
-{
-	this->ui.setupUi(this);
-	this->setObjectId(id);
-	QFormLayout* formLayout = new QFormLayout{ui.properties};
-	this->ui.properties->setLayout(formLayout);
-	for (const ldraw::Property property : ldraw::ALL_PROPERTIES)
-	{
-		QWidget* editorWidget = this->makeEditorWidgetForProperty(property);
-		if (editorWidget != nullptr)
-		{
-			editorWidget->setProperty("_property_id", static_cast<int>(property));
-			QLabel* propertyLabel = new QLabel{ldraw::traits(property).name.data()};
-			formLayout->addRow(propertyLabel, editorWidget);
-			this->propertyWidgets[property] = {propertyLabel, editorWidget};
-		}
-	}
-	this->setObjectId(ldraw::NULL_ID);
-}
-
-ObjectEditor::~ObjectEditor()
-{
-	delete &this->ui;
-}
-
-QString titleCase(const QString& string)
-{
-	return string.left(1).toUpper() + string.mid(1);
-}
+using PropertyValue = std::variant<
+	const glm::vec3*,
+	const glm::mat4*,
+	const QString*,
+	ldraw::Color>;
 
-void setValueToWidget(QWidget* widget, const QVariant& value)
+enum PropertyKey
 {
-	ColorIndexInput* colorIndexInput = qobject_cast<ColorIndexInput*>(widget);
-	if (colorIndexInput != nullptr)
-	{
-		colorIndexInput->setSelectedColor(value.value<ldraw::Color>());
-	}
-	else
-	{
-		Vec3Editor* vec3Editor = qobject_cast<Vec3Editor*>(widget);
-		if (vec3Editor != nullptr)
-		{
-			vec3Editor->setValue(value.value<glm::vec3>());
-		}
-		else
-		{
-			QCheckBox* checkBox = qobject_cast<QCheckBox*>(widget);
-			if (checkBox != nullptr)
-			{
-				checkBox->setChecked(value.toBool());
-			}
-			else
-			{
-				QLineEdit* lineEdit = qobject_cast<QLineEdit*>(widget);
-				if (lineEdit != nullptr)
-				{
-					lineEdit->setText(value.toString());
-				}
-			}
-		}
-	}
-}
+	Point1,
+	Point2,
+	Point3,
+	Point4,
+	Control1,
+	Control2,
+	Color,
+	Transformation,
+	Name,
+	Text,
+	Code,
+};
 
-void ObjectEditor::setObjectId(const ldraw::id_t id)
+std::map<PropertyKey, PropertyValue> getProperties(const ModelElement& element)
 {
-	this->objectId = id;
-	const ldraw::Object* object = this->document->getModel().get(id);
-	this->ui.properties->setEnabled(object != nullptr);
-	if (object != nullptr)
-	{
-		this->ui.typeNameLabel->setText("<b>" + titleCase(object->typeName()) + "</b>");
-		this->ui.typeIconLabel->setPixmap(QPixmap{object->iconName()}.scaledToWidth(24));
-		for (const ldraw::Property property : ldraw::ALL_PROPERTIES)
-		{
-			const QVariant value = object->getProperty(property);
-			const auto it = this->propertyWidgets.find(property);
-			if (it != this->propertyWidgets.end())
-			{
-				it->first->setEnabled(not value.isNull());
-				it->second->setEnabled(not value.isNull());
-				if (not value.isNull())
-				{
-					setValueToWidget(it->second, value);
-				}
-			}
-		}
-	}
-	else
-	{
-		this->ui.typeNameLabel->setText(tr("No object selected"));
-		this->ui.typeIconLabel->clear();
-	}
-}
-
-void ObjectEditor::handleColorChange(ldraw::Color value)
-{
-	this->handlePropertyChange(this->sender(), QVariant::fromValue(value));
-}
-
-void ObjectEditor::handleVec3Change(const glm::vec3 &value)
-{
-	this->handlePropertyChange(this->sender(), QVariant::fromValue(value));
-}
-
-void ObjectEditor::handleBoolChange(bool value)
-{
-	this->handlePropertyChange(this->sender(), QVariant::fromValue(value));
+	std::map<PropertyKey, PropertyValue> result;
+	std::visit<void>(overloaded{
+		[&](const Colored<LineSegment>& edge) {
+			result[Point1] = &edge.p1;
+			result[Point2] = &edge.p2;
+			result[Color] = edge.color;
+		},
+		[&](const Colored<Triangle>& tri) {
+			result[Point1] = &tri.p1;
+			result[Point2] = &tri.p2;
+			result[Point3] = &tri.p3;
+			result[Color] = tri.color;
+		},
+		[&](const Colored<Quadrilateral>& quad) {
+			result[Point1] = &quad.p1;
+			result[Point2] = &quad.p2;
+			result[Point3] = &quad.p3;
+			result[Point4] = &quad.p4;
+			result[Color] = quad.color;
+		},
+		[&](const Colored<ConditionalEdge>& cedge) {
+			result[Point1] = &cedge.p1;
+			result[Point2] = &cedge.p2;
+			result[Control1] = &cedge.c1;
+			result[Control2] = &cedge.c2;
+			result[Color] = cedge.color;
+		},
+		[&](const Colored<SubfileReference>& ref) {
+			result[Transformation] = &ref.transformation;
+			result[Name] = &ref.name;
+			result[Color] = ref.color;
+		},
+		[&](Empty) {},
+		[&](const Comment& comment) {
+			result[Text] = &comment.text;
+		},
+		[&](const ParseError& parseError) {
+			result[Code] = &parseError.code;
+		},
+	}, element);
+	return result;
 }
-
-void ObjectEditor::handleStringChange(const QString &value)
-{
-	this->handlePropertyChange(this->sender(), QVariant::fromValue(value));
-}
-
-QWidget* ObjectEditor::makeEditorWidgetForProperty(ldraw::Property property)
-{
-	QWidget* const parent = qobject_cast<QWidget*>(this->parent());
-	if (ldraw::traits(property).type == qMetaTypeId<ldraw::Color>())
-	{
-		ColorIndexInput* colorWidget = new ColorIndexInput{this->document, {0}, parent};
-		connect(colorWidget, &ColorIndexInput::colorChanged, this, &ObjectEditor::handleColorChange);
-		return colorWidget;
-	}
-	else if (ldraw::traits(property).type == qMetaTypeId<glm::vec3>())
-	{
-		Vec3Editor* editor = new Vec3Editor{{}, parent};
-		connect(editor, &Vec3Editor::valueChanged, this, &ObjectEditor::handleVec3Change);
-		return editor;
-	}
-	else if (ldraw::traits(property).type == qMetaTypeId<bool>())
-	{
-		QCheckBox* editor = new QCheckBox{{}, parent};
-		connect(editor, &QCheckBox::clicked, this, &ObjectEditor::handleBoolChange);
-		return editor;
-	}
-	else if (ldraw::traits(property).type == qMetaTypeId<QString>())
-	{
-		QLineEdit* editor = new QLineEdit{{}, parent};
-		connect(editor, &QLineEdit::textChanged, this, &ObjectEditor::handleStringChange);
-		return editor;
-	}
-	else
-	{
-		return nullptr;
-	}
-}
-
-void ObjectEditor::handlePropertyChange(QObject *caller, const QVariant &value)
-{
-	QVariant propertyVariant = caller->property("_property_id");
-	if (not propertyVariant.isNull())
-	{
-		ldraw::Property const property = static_cast<ldraw::Property>(propertyVariant.toInt());
-		this->document->editModel()->setObjectProperty(this->objectId, property, value);
-	}
-}
\ No newline at end of file

mercurial