--- a/src/ui/objecteditor.cpp Sun Mar 13 14:53:14 2022 +0200 +++ b/src/ui/objecteditor.cpp Sun Mar 13 18:46:10 2022 +0200 @@ -22,7 +22,6 @@ 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}; @@ -31,31 +30,26 @@ 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) + + for (const ldraw::Property property : ldraw::ALL_PROPERTIES) { - const QModelIndex index = this->document->getModel().find(this->objectId); - if (index.isValid()) + QWidget* editorWidget = this->makeEditorWidgetForProperty(property); + if (editorWidget != nullptr) { - this->document->editModel()->modifyObjectAt<ldraw::ColoredObject>( - index.row(), - [color](ldraw::ColoredObject* object) - { - object->colorIndex = color; - } - ); + 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->propertyWidgets[ldraw::Property::Color] = {colorLabel, colorWidget}; + } + + this->setObjectId(ldraw::NULL_ID); } QString titleCase(const QString& string) @@ -63,34 +57,50 @@ return string.left(1).toUpper() + string.mid(1); } +void setValueToWidget(QWidget* widget, const QVariant& value) +{ + 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>()); + } + } +} + 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 + for (const ldraw::Property property : ldraw::ALL_PROPERTIES) { - 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>()); + const QVariant value = object->getProperty(property); + const auto it = this->propertyWidgets.find(property); + if (it != this->propertyWidgets.end()) + { + it->first->setVisible(not value.isNull()); + it->second->setVisible(not value.isNull()); + if (not value.isNull()) + { + setValueToWidget(it->second, value); + } + } } } else { + this->objectTypeNameLabel->clear(); + this->objectTypeIconLabel->clear(); for (auto& pair : this->propertyWidgets) { pair.first->setVisible(false); @@ -99,10 +109,43 @@ } } -void ObjectEditor::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)); +} + +QWidget* ObjectEditor::makeEditorWidgetForProperty(ldraw::Property property) { - this->objectTypeNameLabel->clear(); - this->objectTypeIconLabel->clear(); - this->polygonEditor->clear(); - delete this->formContainer->layout(); + 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 + { + 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