src/ui/polygonobjecteditor.cpp

changeset 178
a23024fc98e0
parent 153
2f79053c2e9a
--- a/src/ui/polygonobjecteditor.cpp	Wed Mar 09 14:22:22 2022 +0200
+++ b/src/ui/polygonobjecteditor.cpp	Sun Mar 13 14:51:39 2022 +0200
@@ -14,6 +14,7 @@
 	storedObjectId{ldraw::NULL_ID.value}
 {
 	this->splitter.emplace(Qt::Vertical, this);
+	this->buildWidgets();
 	this->setObjectId(id);
 }
 
@@ -30,44 +31,60 @@
 void PolygonObjectEditor::setObjectId(ldraw::id_t id)
 {
 	this->storedObjectId = id;
-	this->buildWidgets();
+	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->widgets.clear();
-	delete this->layout();
-	QFormLayout* layout = new QFormLayout{this};
-	this->setLayout(layout);
+	this->formLayout = new QFormLayout{this};
+	this->setLayout(this->formLayout);
 	for (int n : {0, 1, 2, 3})
 	{
 		this->setupPointWidget(n);
 	}
-	for (std::unique_ptr<QWidget>& widget : this->widgets)
+	for (std::unique_ptr<Vec3Editor>& widget : this->widgets)
 	{
 		const QString label = widget->property(LABEL_NAME).toString();
-		layout->addRow(label, widget.get());
+		this->formLayout->addRow(label, widget.get());
 	}
-	layout->addRow("", &*this->splitter);
+	this->formLayout->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));
-		}
-	}
+	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)

mercurial