src/ui/polygonobjecteditor.cpp

changeset 178
a23024fc98e0
parent 153
2f79053c2e9a
equal deleted inserted replaced
177:f69d53c053df 178:a23024fc98e0
12 QWidget{document}, 12 QWidget{document},
13 document{document}, 13 document{document},
14 storedObjectId{ldraw::NULL_ID.value} 14 storedObjectId{ldraw::NULL_ID.value}
15 { 15 {
16 this->splitter.emplace(Qt::Vertical, this); 16 this->splitter.emplace(Qt::Vertical, this);
17 this->buildWidgets();
17 this->setObjectId(id); 18 this->setObjectId(id);
18 } 19 }
19 20
20 // destructor needed for std::unique_ptr 21 // destructor needed for std::unique_ptr
21 PolygonObjectEditor::~PolygonObjectEditor() 22 PolygonObjectEditor::~PolygonObjectEditor()
28 } 29 }
29 30
30 void PolygonObjectEditor::setObjectId(ldraw::id_t id) 31 void PolygonObjectEditor::setObjectId(ldraw::id_t id)
31 { 32 {
32 this->storedObjectId = id; 33 this->storedObjectId = id;
33 this->buildWidgets(); 34 const QModelIndex index = this->document->getModel().find(this->objectId());
35 if (index.isValid())
36 {
37 for (int n : {0, 1, 2, 3})
38 {
39 const ldraw::Object* const object = this->document->getModel()[index.row()];
40 const ldraw::Property property = ldraw::pointProperty(n);
41 const QVariant value = object->getProperty(property);
42 this->widgets[n]->setVisible(value.isValid());
43 this->formLayout->itemAt(n, QFormLayout::LabelRole)->widget()->setVisible(value.isValid());
44 if (value.isValid())
45 {
46 this->widgets[n]->setValue(value.value<glm::vec3>());
47 }
48 }
49 }
50 else
51 {
52 for (int n : {0, 1, 2, 3})
53 {
54 this->widgets[n]->setVisible(false);
55 this->formLayout->itemAt(n, QFormLayout::LabelRole)->widget()->setVisible(false);
56 }
57 }
58 }
59
60 void PolygonObjectEditor::clear()
61 {
62 this->setObjectId(ldraw::NULL_ID);
34 } 63 }
35 64
36 void PolygonObjectEditor::buildWidgets() 65 void PolygonObjectEditor::buildWidgets()
37 { 66 {
38 this->widgets.clear(); 67 this->formLayout = new QFormLayout{this};
39 delete this->layout(); 68 this->setLayout(this->formLayout);
40 QFormLayout* layout = new QFormLayout{this};
41 this->setLayout(layout);
42 for (int n : {0, 1, 2, 3}) 69 for (int n : {0, 1, 2, 3})
43 { 70 {
44 this->setupPointWidget(n); 71 this->setupPointWidget(n);
45 } 72 }
46 for (std::unique_ptr<QWidget>& widget : this->widgets) 73 for (std::unique_ptr<Vec3Editor>& widget : this->widgets)
47 { 74 {
48 const QString label = widget->property(LABEL_NAME).toString(); 75 const QString label = widget->property(LABEL_NAME).toString();
49 layout->addRow(label, widget.get()); 76 this->formLayout->addRow(label, widget.get());
50 } 77 }
51 layout->addRow("", &*this->splitter); 78 this->formLayout->addRow("", &*this->splitter);
52 } 79 }
53 80
54 void PolygonObjectEditor::setupPointWidget(int n) 81 void PolygonObjectEditor::setupPointWidget(int n)
55 { 82 {
56 const QModelIndex index = this->document->getModel().find(this->objectId()); 83 std::unique_ptr<Vec3Editor> editor = std::make_unique<Vec3Editor>(glm::vec3{}, this);
57 if (index.isValid()) 84 QObject::connect(editor.get(), &Vec3Editor::valueChanged, this, &PolygonObjectEditor::pointChanged);
58 { 85 editor->setProperty(INDEX_NAME, QVariant::fromValue(n));
59 const ldraw::Object* const object = this->document->getModel()[index.row()]; 86 editor->setProperty(LABEL_NAME, &ldraw::traits(ldraw::pointProperty(n)).name[0]);
60 const ldraw::Property property = ldraw::pointProperty(n); 87 this->widgets.push_back(std::move(editor));
61 const QVariant value = object->getProperty(property);
62 if (value.isValid())
63 {
64 std::unique_ptr<Vec3Editor> editor = std::make_unique<Vec3Editor>(value.value<glm::vec3>(), this);
65 QObject::connect(editor.get(), &Vec3Editor::valueChanged, this, &PolygonObjectEditor::pointChanged);
66 editor->setProperty(INDEX_NAME, QVariant::fromValue(n));
67 editor->setProperty(LABEL_NAME, &ldraw::traits(property).name[0]);
68 this->widgets.push_back(std::move(editor));
69 }
70 }
71 } 88 }
72 89
73 void PolygonObjectEditor::pointChanged(const glm::vec3& value) 90 void PolygonObjectEditor::pointChanged(const glm::vec3& value)
74 { 91 {
75 std::unique_ptr<ModelEditor> modelEditor = this->document->editModel(); 92 std::unique_ptr<ModelEditor> modelEditor = this->document->editModel();

mercurial