1 #include <QVBoxLayout> |
1 #include <QFormLayout> |
2 #include <QSplitter> |
2 #include <QSplitter> |
3 #include "model.h" |
3 #include "model.h" |
4 #include "modeleditcontext.h" |
4 #include "modeleditcontext.h" |
5 #include "widgets/vec3editor.h" |
5 #include "widgets/vec3editor.h" |
6 #include "ui/polygonobjecteditor.h" |
6 #include "ui/polygonobjecteditor.h" |
7 |
7 |
8 constexpr char COLUMN_PROPERTY[] = "_ldforge_column"; |
8 static constexpr char INDEX_NAME[] = "_ldforge_index"; |
|
9 static constexpr char PROPERTY_NAME[] = "_ldforge_property"; |
|
10 static constexpr char OBJECT_ID_NAME[] = "_ldforge_id"; |
|
11 static constexpr char LABEL_NAME[] = "_ldforge_label"; |
9 |
12 |
10 PolygonObjectEditor::PolygonObjectEditor(Model* model, ldraw::id_t id, QWidget* parent) : |
13 PolygonObjectEditor::PolygonObjectEditor(Model* model, ldraw::id_t id, QWidget* parent) : |
11 QWidget{parent}, |
14 QWidget{parent}, |
12 model{model}, |
15 model{model}, |
13 storedObjectId{ldraw::NULL_ID.value} |
16 storedObjectId{ldraw::NULL_ID.value} |
14 { |
17 { |
15 this->setLayout(new QVBoxLayout{this}); |
18 this->splitter.emplace(Qt::Vertical, this); |
16 for (int i = 0; i < countof(this->vec3Editors); i += 1) |
|
17 { |
|
18 std::optional<Vec3Editor>& editorPointer = this->vec3Editors[i]; |
|
19 editorPointer.emplace(glm::vec3{}, this); |
|
20 editorPointer->setProperty(COLUMN_PROPERTY, QVariant::fromValue(i)); |
|
21 this->layout()->addWidget(&*editorPointer); |
|
22 connect(&*editorPointer, &Vec3Editor::valueChanged, this, &PolygonObjectEditor::vectorChanged); |
|
23 } |
|
24 this->layout()->addWidget(new QSplitter{Qt::Vertical, this}); |
|
25 this->setObjectId(id); |
19 this->setObjectId(id); |
26 } |
20 } |
27 |
21 |
28 // destructor needed for std::unique_ptr |
22 // destructor needed for std::unique_ptr |
29 PolygonObjectEditor::~PolygonObjectEditor() |
23 PolygonObjectEditor::~PolygonObjectEditor() |
36 } |
30 } |
37 |
31 |
38 void PolygonObjectEditor::setObjectId(ldraw::id_t id) |
32 void PolygonObjectEditor::setObjectId(ldraw::id_t id) |
39 { |
33 { |
40 this->storedObjectId = id; |
34 this->storedObjectId = id; |
41 this->updateNumRows(); |
35 this->buildWidgets(); |
42 } |
36 } |
43 |
37 |
44 void PolygonObjectEditor::vectorChanged(const glm::vec3& value) |
38 void PolygonObjectEditor::buildWidgets() |
45 { |
39 { |
46 bool ok; |
40 this->widgets.clear(); |
47 const int num = sender()->property(COLUMN_PROPERTY).toInt(&ok); |
41 delete this->layout(); |
48 if (ok and num >= 0 and num < countof(this->vec3Editors)) |
42 QFormLayout* layout = new QFormLayout{this}; |
|
43 this->setLayout(layout); |
|
44 for (int n : {0, 1, 2, 3}) |
49 { |
45 { |
50 Model::EditContext editor = this->model->edit(); |
46 this->setupPointWidget(n); |
51 editor.setObjectPoint(this->objectId(), num, value); |
47 } |
|
48 for (std::unique_ptr<QWidget>& widget : this->widgets) |
|
49 { |
|
50 const QString label = widget->property(LABEL_NAME).toString(); |
|
51 layout->addRow(label, widget.get()); |
|
52 } |
|
53 layout->addRow("", &*this->splitter); |
|
54 } |
|
55 |
|
56 void PolygonObjectEditor::setupPointWidget(int n) |
|
57 { |
|
58 const ldraw::Object* const object = this->model->get(this->objectId()); |
|
59 const ldraw::Property property = ldraw::pointProperty(n); |
|
60 const QVariant value = object->getProperty(property); |
|
61 if (value.isValid()) |
|
62 { |
|
63 std::unique_ptr<Vec3Editor> editor = std::make_unique<Vec3Editor>(value.value<glm::vec3>(), this); |
|
64 QObject::connect(editor.get(), &Vec3Editor::valueChanged, this, &PolygonObjectEditor::pointChanged); |
|
65 editor->setProperty(INDEX_NAME, QVariant::fromValue(n)); |
|
66 editor->setProperty(LABEL_NAME, &ldraw::traits(property).name[0]); |
|
67 this->widgets.push_back(std::move(editor)); |
52 } |
68 } |
53 } |
69 } |
54 |
70 |
55 void PolygonObjectEditor::updateNumRows() |
71 void PolygonObjectEditor::pointChanged(const glm::vec3& value) |
56 { |
72 { |
57 const ldraw::Object* object = model->get(this->storedObjectId); |
73 Model::EditContext editcontext = this->model->edit(); |
58 const int numPoints = object != nullptr ? object->numPoints() : 0; |
74 const int n = this->sender()->property(INDEX_NAME).toInt(); |
59 for (int i = 0; i < countof(this->vec3Editors); i += 1) |
75 const ldraw::Property property = ldraw::pointProperty(n); |
60 { |
76 editcontext.setObjectProperty(this->objectId(), property, QVariant::fromValue(value)); |
61 Vec3Editor& editor = *this->vec3Editors[i]; |
|
62 QSignalBlocker blocker{&editor}; |
|
63 if (i < numPoints) |
|
64 { |
|
65 editor.setVisible(true); |
|
66 editor.setValue(object->getPoint(i)); |
|
67 } |
|
68 else |
|
69 { |
|
70 editor.setVisible(false); |
|
71 editor.setValue(glm::vec3{}); |
|
72 } |
|
73 } |
|
74 } |
77 } |