Sun, 13 Mar 2022 14:51:39 +0200
fix saving
add color editing to object editor
|
81
62373840e33a
object editor widgets start to form up
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
1 | #include <QVBoxLayout> |
| 178 | 2 | #include <QFormLayout> |
| 3 | #include <QPushButton> | |
|
177
f69d53c053df
Show type of object in the object editor
Teemu Piippo <teemu@hecknology.net>
parents:
152
diff
changeset
|
4 | #include <QLabel> |
| 178 | 5 | #include <QSpinBox> |
|
81
62373840e33a
object editor widgets start to form up
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
6 | #include "objecteditor.h" |
| 152 | 7 | #include "document.h" |
| 178 | 8 | #include "modeleditor.h" |
| 9 | #include "widgets/colorbutton.h" | |
| 10 | #include "widgets/colorindexinput.h" | |
| 11 | ||
| 12 | template<ldraw::Property property> | |
| 13 | static void makeColorEditor() | |
| 14 | { | |
| 15 | QString propertyName = ldraw::PropertyTraits<property>::name; | |
| 16 | } | |
|
81
62373840e33a
object editor widgets start to form up
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
17 | |
| 152 | 18 | ObjectEditor::ObjectEditor(Document* document, const ldraw::id_t id) : |
| 19 | QWidget{document}, | |
|
177
f69d53c053df
Show type of object in the object editor
Teemu Piippo <teemu@hecknology.net>
parents:
152
diff
changeset
|
20 | document{document}, |
| 178 | 21 | formContainer{new QWidget{this}}, |
|
177
f69d53c053df
Show type of object in the object editor
Teemu Piippo <teemu@hecknology.net>
parents:
152
diff
changeset
|
22 | objectTypeNameLabel{new QLabel{this}}, |
|
f69d53c053df
Show type of object in the object editor
Teemu Piippo <teemu@hecknology.net>
parents:
152
diff
changeset
|
23 | objectTypeIconLabel{new QLabel{this}} |
|
81
62373840e33a
object editor widgets start to form up
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
24 | { |
| 178 | 25 | this->polygonEditor.emplace(this->document, id); |
|
81
62373840e33a
object editor widgets start to form up
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
26 | this->setLayout(new QVBoxLayout{this}); |
|
177
f69d53c053df
Show type of object in the object editor
Teemu Piippo <teemu@hecknology.net>
parents:
152
diff
changeset
|
27 | QWidget* objectTitleLayoutContainer = new QWidget{this}; |
|
f69d53c053df
Show type of object in the object editor
Teemu Piippo <teemu@hecknology.net>
parents:
152
diff
changeset
|
28 | QLayout* objectTitleLayout = new QHBoxLayout{objectTitleLayoutContainer}; |
|
f69d53c053df
Show type of object in the object editor
Teemu Piippo <teemu@hecknology.net>
parents:
152
diff
changeset
|
29 | objectTitleLayoutContainer->setLayout(objectTitleLayout); |
|
f69d53c053df
Show type of object in the object editor
Teemu Piippo <teemu@hecknology.net>
parents:
152
diff
changeset
|
30 | objectTitleLayout->addWidget(this->objectTypeIconLabel); |
|
f69d53c053df
Show type of object in the object editor
Teemu Piippo <teemu@hecknology.net>
parents:
152
diff
changeset
|
31 | objectTitleLayout->addWidget(this->objectTypeNameLabel); |
|
f69d53c053df
Show type of object in the object editor
Teemu Piippo <teemu@hecknology.net>
parents:
152
diff
changeset
|
32 | objectTitleLayout->addWidget(new QSplitter{Qt::Horizontal, this}); |
|
f69d53c053df
Show type of object in the object editor
Teemu Piippo <teemu@hecknology.net>
parents:
152
diff
changeset
|
33 | this->layout()->addWidget(objectTitleLayoutContainer); |
| 178 | 34 | this->layout()->addWidget(&*this->polygonEditor); |
| 35 | this->layout()->addWidget(formContainer); | |
| 36 | this->setObjectId(id); | |
| 37 | ||
| 38 | QWidget* const parent = this->formContainer; | |
| 39 | QFormLayout* formLayout = new QFormLayout{parent}; | |
| 40 | this->formContainer->setLayout(formLayout); | |
| 41 | QLabel* colorLabel = new QLabel{"Color", parent}; | |
| 42 | ColorIndexInput* colorWidget = new ColorIndexInput{document, {0}, parent}; | |
| 43 | formLayout->addRow(colorLabel, colorWidget); | |
| 44 | connect(colorWidget, &ColorIndexInput::colorChanged, [this](ldraw::Color color) | |
| 45 | { | |
| 46 | const QModelIndex index = this->document->getModel().find(this->objectId); | |
| 47 | if (index.isValid()) | |
| 48 | { | |
| 49 | this->document->editModel()->modifyObjectAt<ldraw::ColoredObject>( | |
| 50 | index.row(), | |
| 51 | [color](ldraw::ColoredObject* object) | |
| 52 | { | |
| 53 | object->colorIndex = color; | |
| 54 | } | |
| 55 | ); | |
| 56 | } | |
| 57 | }); | |
| 58 | this->propertyWidgets[ldraw::Property::Color] = {colorLabel, colorWidget}; | |
|
177
f69d53c053df
Show type of object in the object editor
Teemu Piippo <teemu@hecknology.net>
parents:
152
diff
changeset
|
59 | } |
|
f69d53c053df
Show type of object in the object editor
Teemu Piippo <teemu@hecknology.net>
parents:
152
diff
changeset
|
60 | |
|
f69d53c053df
Show type of object in the object editor
Teemu Piippo <teemu@hecknology.net>
parents:
152
diff
changeset
|
61 | QString titleCase(const QString& string) |
|
f69d53c053df
Show type of object in the object editor
Teemu Piippo <teemu@hecknology.net>
parents:
152
diff
changeset
|
62 | { |
|
f69d53c053df
Show type of object in the object editor
Teemu Piippo <teemu@hecknology.net>
parents:
152
diff
changeset
|
63 | return string.left(1).toUpper() + string.mid(1); |
|
81
62373840e33a
object editor widgets start to form up
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
64 | } |
|
62373840e33a
object editor widgets start to form up
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
65 | |
|
62373840e33a
object editor widgets start to form up
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
66 | void ObjectEditor::setObjectId(const ldraw::id_t id) |
|
62373840e33a
object editor widgets start to form up
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
67 | { |
|
62373840e33a
object editor widgets start to form up
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
68 | this->objectId = id; |
| 152 | 69 | const ldraw::Object* object = this->document->getModel().get(id); |
| 178 | 70 | this->clear(); |
| 71 | if (object != nullptr) | |
|
81
62373840e33a
object editor widgets start to form up
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
72 | { |
|
177
f69d53c053df
Show type of object in the object editor
Teemu Piippo <teemu@hecknology.net>
parents:
152
diff
changeset
|
73 | this->objectTypeNameLabel->setText("<b>" + titleCase(object->typeName()) + "</b>"); |
|
f69d53c053df
Show type of object in the object editor
Teemu Piippo <teemu@hecknology.net>
parents:
152
diff
changeset
|
74 | this->objectTypeIconLabel->setPixmap(QPixmap{object->iconName()}.scaledToWidth(24)); |
| 178 | 75 | if (object->numPoints() > 0) |
|
81
62373840e33a
object editor widgets start to form up
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
76 | { |
| 178 | 77 | this->polygonEditor->setObjectId(id); |
|
81
62373840e33a
object editor widgets start to form up
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
78 | } |
|
62373840e33a
object editor widgets start to form up
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
79 | else |
|
62373840e33a
object editor widgets start to form up
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
80 | { |
| 178 | 81 | this->polygonEditor->clear(); |
| 82 | } | |
| 83 | constexpr ldraw::Property prop = ldraw::Property::Color; | |
| 84 | QVariant color = object->getProperty(prop); | |
| 85 | this->propertyWidgets[prop].first->setVisible(not color.isNull()); | |
| 86 | this->propertyWidgets[prop].second->setVisible(not color.isNull()); | |
| 87 | if (not color.isNull()) | |
| 88 | { | |
| 89 | static_cast<ColorIndexInput*>(this->propertyWidgets[prop].second)->setSelectedColor(color.value<ldraw::Color>()); | |
|
81
62373840e33a
object editor widgets start to form up
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
90 | } |
|
62373840e33a
object editor widgets start to form up
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
91 | } |
|
62373840e33a
object editor widgets start to form up
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
92 | else |
|
62373840e33a
object editor widgets start to form up
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
93 | { |
| 178 | 94 | for (auto& pair : this->propertyWidgets) |
| 95 | { | |
| 96 | pair.first->setVisible(false); | |
| 97 | pair.second->setVisible(false); | |
| 98 | } | |
|
81
62373840e33a
object editor widgets start to form up
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
99 | } |
|
62373840e33a
object editor widgets start to form up
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
100 | } |
| 178 | 101 | |
| 102 | void ObjectEditor::clear() | |
| 103 | { | |
| 104 | this->objectTypeNameLabel->clear(); | |
| 105 | this->objectTypeIconLabel->clear(); | |
| 106 | this->polygonEditor->clear(); | |
| 107 | delete this->formContainer->layout(); | |
| 108 | } |