--- a/src/ui/polygonobjecteditor.cpp Mon May 11 12:18:04 2020 +0300 +++ b/src/ui/polygonobjecteditor.cpp Mon May 11 12:18:59 2020 +0300 @@ -1,27 +1,21 @@ -#include <QVBoxLayout> +#include <QFormLayout> #include <QSplitter> #include "model.h" #include "modeleditcontext.h" #include "widgets/vec3editor.h" #include "ui/polygonobjecteditor.h" -constexpr char COLUMN_PROPERTY[] = "_ldforge_column"; +static constexpr char INDEX_NAME[] = "_ldforge_index"; +static constexpr char PROPERTY_NAME[] = "_ldforge_property"; +static constexpr char OBJECT_ID_NAME[] = "_ldforge_id"; +static constexpr char LABEL_NAME[] = "_ldforge_label"; PolygonObjectEditor::PolygonObjectEditor(Model* model, ldraw::id_t id, QWidget* parent) : QWidget{parent}, model{model}, storedObjectId{ldraw::NULL_ID.value} { - this->setLayout(new QVBoxLayout{this}); - for (int i = 0; i < countof(this->vec3Editors); i += 1) - { - std::optional<Vec3Editor>& editorPointer = this->vec3Editors[i]; - editorPointer.emplace(glm::vec3{}, this); - editorPointer->setProperty(COLUMN_PROPERTY, QVariant::fromValue(i)); - this->layout()->addWidget(&*editorPointer); - connect(&*editorPointer, &Vec3Editor::valueChanged, this, &PolygonObjectEditor::vectorChanged); - } - this->layout()->addWidget(new QSplitter{Qt::Vertical, this}); + this->splitter.emplace(Qt::Vertical, this); this->setObjectId(id); } @@ -38,37 +32,46 @@ void PolygonObjectEditor::setObjectId(ldraw::id_t id) { this->storedObjectId = id; - this->updateNumRows(); + this->buildWidgets(); } -void PolygonObjectEditor::vectorChanged(const glm::vec3& value) +void PolygonObjectEditor::buildWidgets() { - bool ok; - const int num = sender()->property(COLUMN_PROPERTY).toInt(&ok); - if (ok and num >= 0 and num < countof(this->vec3Editors)) + this->widgets.clear(); + delete this->layout(); + QFormLayout* layout = new QFormLayout{this}; + this->setLayout(layout); + for (int n : {0, 1, 2, 3}) + { + this->setupPointWidget(n); + } + for (std::unique_ptr<QWidget>& widget : this->widgets) { - Model::EditContext editor = this->model->edit(); - editor.setObjectPoint(this->objectId(), num, value); + const QString label = widget->property(LABEL_NAME).toString(); + layout->addRow(label, widget.get()); + } + layout->addRow("", &*this->splitter); +} + +void PolygonObjectEditor::setupPointWidget(int n) +{ + const ldraw::Object* const object = this->model->get(this->objectId()); + 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)); } } -void PolygonObjectEditor::updateNumRows() +void PolygonObjectEditor::pointChanged(const glm::vec3& value) { - const ldraw::Object* object = model->get(this->storedObjectId); - const int numPoints = object != nullptr ? object->numPoints() : 0; - for (int i = 0; i < countof(this->vec3Editors); i += 1) - { - Vec3Editor& editor = *this->vec3Editors[i]; - QSignalBlocker blocker{&editor}; - if (i < numPoints) - { - editor.setVisible(true); - editor.setValue(object->getPoint(i)); - } - else - { - editor.setVisible(false); - editor.setValue(glm::vec3{}); - } - } + Model::EditContext editcontext = this->model->edit(); + const int n = this->sender()->property(INDEX_NAME).toInt(); + const ldraw::Property property = ldraw::pointProperty(n); + editcontext.setObjectProperty(this->objectId(), property, QVariant::fromValue(value)); }