Sun, 13 Mar 2022 14:51:39 +0200
fix saving
add color editing to object editor
| 89 | 1 | #include <QFormLayout> |
|
83
b8bd338eb602
refactor, added splitter
Teemu Piippo <teemu@hecknology.net>
parents:
81
diff
changeset
|
2 | #include <QSplitter> |
| 152 | 3 | #include "document.h" |
|
153
2f79053c2e9a
Renamed modeleditcontext.cpp -> modeleditor.cpp
Teemu Piippo <teemu@hecknology.net>
parents:
152
diff
changeset
|
4 | #include "modeleditor.h" |
|
81
62373840e33a
object editor widgets start to form up
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
5 | #include "widgets/vec3editor.h" |
|
86
4bec0525ef1b
PolygonObjectEditor can now modify the object properly
Teemu Piippo <teemu@hecknology.net>
parents:
84
diff
changeset
|
6 | #include "ui/polygonobjecteditor.h" |
|
4bec0525ef1b
PolygonObjectEditor can now modify the object properly
Teemu Piippo <teemu@hecknology.net>
parents:
84
diff
changeset
|
7 | |
| 89 | 8 | static constexpr char INDEX_NAME[] = "_ldforge_index"; |
| 9 | static constexpr char LABEL_NAME[] = "_ldforge_label"; | |
|
81
62373840e33a
object editor widgets start to form up
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
10 | |
| 152 | 11 | PolygonObjectEditor::PolygonObjectEditor(Document* document, ldraw::id_t id) : |
| 12 | QWidget{document}, | |
| 13 | document{document}, | |
|
81
62373840e33a
object editor widgets start to form up
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
14 | storedObjectId{ldraw::NULL_ID.value} |
|
62373840e33a
object editor widgets start to form up
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
15 | { |
| 89 | 16 | this->splitter.emplace(Qt::Vertical, this); |
| 178 | 17 | this->buildWidgets(); |
|
81
62373840e33a
object editor widgets start to form up
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
18 | this->setObjectId(id); |
|
62373840e33a
object editor widgets start to form up
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
19 | } |
|
62373840e33a
object editor widgets start to form up
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
20 | |
|
62373840e33a
object editor widgets start to form up
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
21 | // destructor needed for std::unique_ptr |
|
62373840e33a
object editor widgets start to form up
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
22 | PolygonObjectEditor::~PolygonObjectEditor() |
|
62373840e33a
object editor widgets start to form up
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
23 | { |
|
62373840e33a
object editor widgets start to form up
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
24 | } |
|
62373840e33a
object editor widgets start to form up
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
25 | |
|
62373840e33a
object editor widgets start to form up
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
26 | ldraw::id_t PolygonObjectEditor::objectId() const |
|
62373840e33a
object editor widgets start to form up
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
27 | { |
|
62373840e33a
object editor widgets start to form up
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
28 | return this->storedObjectId; |
|
62373840e33a
object editor widgets start to form up
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
29 | } |
|
62373840e33a
object editor widgets start to form up
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
30 | |
|
62373840e33a
object editor widgets start to form up
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
31 | void PolygonObjectEditor::setObjectId(ldraw::id_t id) |
|
62373840e33a
object editor widgets start to form up
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
32 | { |
|
62373840e33a
object editor widgets start to form up
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
33 | this->storedObjectId = id; |
| 178 | 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); | |
|
81
62373840e33a
object editor widgets start to form up
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
63 | } |
|
62373840e33a
object editor widgets start to form up
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
64 | |
| 89 | 65 | void PolygonObjectEditor::buildWidgets() |
|
86
4bec0525ef1b
PolygonObjectEditor can now modify the object properly
Teemu Piippo <teemu@hecknology.net>
parents:
84
diff
changeset
|
66 | { |
| 178 | 67 | this->formLayout = new QFormLayout{this}; |
| 68 | this->setLayout(this->formLayout); | |
| 89 | 69 | for (int n : {0, 1, 2, 3}) |
| 70 | { | |
| 71 | this->setupPointWidget(n); | |
| 72 | } | |
| 178 | 73 | for (std::unique_ptr<Vec3Editor>& widget : this->widgets) |
|
86
4bec0525ef1b
PolygonObjectEditor can now modify the object properly
Teemu Piippo <teemu@hecknology.net>
parents:
84
diff
changeset
|
74 | { |
| 89 | 75 | const QString label = widget->property(LABEL_NAME).toString(); |
| 178 | 76 | this->formLayout->addRow(label, widget.get()); |
| 89 | 77 | } |
| 178 | 78 | this->formLayout->addRow("", &*this->splitter); |
| 89 | 79 | } |
| 80 | ||
| 81 | void PolygonObjectEditor::setupPointWidget(int n) | |
| 82 | { | |
| 178 | 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)); | |
|
86
4bec0525ef1b
PolygonObjectEditor can now modify the object properly
Teemu Piippo <teemu@hecknology.net>
parents:
84
diff
changeset
|
88 | } |
|
4bec0525ef1b
PolygonObjectEditor can now modify the object properly
Teemu Piippo <teemu@hecknology.net>
parents:
84
diff
changeset
|
89 | |
| 89 | 90 | void PolygonObjectEditor::pointChanged(const glm::vec3& value) |
|
81
62373840e33a
object editor widgets start to form up
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
91 | { |
| 152 | 92 | std::unique_ptr<ModelEditor> modelEditor = this->document->editModel(); |
| 89 | 93 | const int n = this->sender()->property(INDEX_NAME).toInt(); |
| 94 | const ldraw::Property property = ldraw::pointProperty(n); | |
| 152 | 95 | modelEditor->setObjectProperty(this->objectId(), property, QVariant::fromValue(value)); |
|
81
62373840e33a
object editor widgets start to form up
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
96 | } |