src/ui/polygonobjecteditor.cpp

changeset 180
5b7a8f2270ff
parent 179
7b9b85b459de
child 181
79de20dc6a1e
equal deleted inserted replaced
179:7b9b85b459de 180:5b7a8f2270ff
1 #include <QFormLayout>
2 #include <QSplitter>
3 #include "document.h"
4 #include "modeleditor.h"
5 #include "widgets/vec3editor.h"
6 #include "ui/polygonobjecteditor.h"
7
8 static constexpr char INDEX_NAME[] = "_ldforge_index";
9 static constexpr char LABEL_NAME[] = "_ldforge_label";
10
11 PolygonObjectEditor::PolygonObjectEditor(Document* document, ldraw::id_t id) :
12 QWidget{document},
13 document{document},
14 storedObjectId{ldraw::NULL_ID.value}
15 {
16 this->splitter.emplace(Qt::Vertical, this);
17 this->buildWidgets();
18 this->setObjectId(id);
19 }
20
21 // destructor needed for std::unique_ptr
22 PolygonObjectEditor::~PolygonObjectEditor()
23 {
24 }
25
26 ldraw::id_t PolygonObjectEditor::objectId() const
27 {
28 return this->storedObjectId;
29 }
30
31 void PolygonObjectEditor::setObjectId(ldraw::id_t id)
32 {
33 this->storedObjectId = id;
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);
63 }
64
65 void PolygonObjectEditor::buildWidgets()
66 {
67 this->formLayout = new QFormLayout{this};
68 this->setLayout(this->formLayout);
69 for (int n : {0, 1, 2, 3})
70 {
71 this->setupPointWidget(n);
72 }
73 for (std::unique_ptr<Vec3Editor>& widget : this->widgets)
74 {
75 const QString label = widget->property(LABEL_NAME).toString();
76 this->formLayout->addRow(label, widget.get());
77 }
78 this->formLayout->addRow("", &*this->splitter);
79 }
80
81 void PolygonObjectEditor::setupPointWidget(int n)
82 {
83 std::unique_ptr<Vec3Editor> editor = std::make_unique<Vec3Editor>(glm::vec3{}, this);
84 QObject::connect(editor.get(), &Vec3Editor::valueChanged, this, &PolygonObjectEditor::pointChanged);
85 editor->setProperty(INDEX_NAME, QVariant::fromValue(n));
86 editor->setProperty(LABEL_NAME, &ldraw::traits(ldraw::pointProperty(n)).name[0]);
87 this->widgets.push_back(std::move(editor));
88 }
89
90 void PolygonObjectEditor::pointChanged(const glm::vec3& value)
91 {
92 std::unique_ptr<ModelEditor> modelEditor = this->document->editModel();
93 const int n = this->sender()->property(INDEX_NAME).toInt();
94 const ldraw::Property property = ldraw::pointProperty(n);
95 modelEditor->setObjectProperty(this->objectId(), property, QVariant::fromValue(value));
96 }

mercurial