--- a/src/ui/objecteditor.cpp Wed Mar 09 14:22:22 2022 +0200 +++ b/src/ui/objecteditor.cpp Sun Mar 13 14:51:39 2022 +0200 @@ -1,15 +1,28 @@ #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->setObjectId(id); + this->polygonEditor.emplace(this->document, id); this->setLayout(new QVBoxLayout{this}); QWidget* objectTitleLayoutContainer = new QWidget{this}; QLayout* objectTitleLayout = new QHBoxLayout{objectTitleLayoutContainer}; @@ -18,6 +31,31 @@ 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) @@ -29,24 +67,42 @@ { this->objectId = id; const ldraw::Object* object = this->document->getModel().get(id); - if (object != nullptr and object->numPoints() > 0) + this->clear(); + if (object != nullptr) { this->objectTypeNameLabel->setText("<b>" + titleCase(object->typeName()) + "</b>"); this->objectTypeIconLabel->setPixmap(QPixmap{object->iconName()}.scaledToWidth(24)); - if (not this->polygonEditor.has_value()) + if (object->numPoints() > 0) { - this->polygonEditor.emplace(this->document, id); - this->layout()->addWidget(&*this->polygonEditor); + this->polygonEditor->setObjectId(id); } else { - this->polygonEditor->setObjectId(id); + 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 { - this->objectTypeNameLabel->clear(); - this->objectTypeIconLabel->clear(); - this->polygonEditor.reset(); + 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(); +}