Sun, 13 Mar 2022 20:11:06 +0200
fix build
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> |
181 | 6 | #include <QSplitter> |
81
62373840e33a
object editor widgets start to form up
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
7 | #include "objecteditor.h" |
152 | 8 | #include "document.h" |
178 | 9 | #include "modeleditor.h" |
10 | #include "widgets/colorbutton.h" | |
11 | #include "widgets/colorindexinput.h" | |
181 | 12 | #include "widgets/vec3editor.h" |
178 | 13 | |
14 | template<ldraw::Property property> | |
15 | static void makeColorEditor() | |
16 | { | |
17 | QString propertyName = ldraw::PropertyTraits<property>::name; | |
18 | } | |
81
62373840e33a
object editor widgets start to form up
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
19 | |
152 | 20 | ObjectEditor::ObjectEditor(Document* document, const ldraw::id_t id) : |
21 | QWidget{document}, | |
177
f69d53c053df
Show type of object in the object editor
Teemu Piippo <teemu@hecknology.net>
parents:
152
diff
changeset
|
22 | document{document}, |
178 | 23 | formContainer{new QWidget{this}}, |
177
f69d53c053df
Show type of object in the object editor
Teemu Piippo <teemu@hecknology.net>
parents:
152
diff
changeset
|
24 | objectTypeNameLabel{new QLabel{this}}, |
f69d53c053df
Show type of object in the object editor
Teemu Piippo <teemu@hecknology.net>
parents:
152
diff
changeset
|
25 | objectTypeIconLabel{new QLabel{this}} |
81
62373840e33a
object editor widgets start to form up
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
26 | { |
62373840e33a
object editor widgets start to form up
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
27 | this->setLayout(new QVBoxLayout{this}); |
177
f69d53c053df
Show type of object in the object editor
Teemu Piippo <teemu@hecknology.net>
parents:
152
diff
changeset
|
28 | QWidget* objectTitleLayoutContainer = new QWidget{this}; |
f69d53c053df
Show type of object in the object editor
Teemu Piippo <teemu@hecknology.net>
parents:
152
diff
changeset
|
29 | QLayout* objectTitleLayout = new QHBoxLayout{objectTitleLayoutContainer}; |
f69d53c053df
Show type of object in the object editor
Teemu Piippo <teemu@hecknology.net>
parents:
152
diff
changeset
|
30 | objectTitleLayoutContainer->setLayout(objectTitleLayout); |
f69d53c053df
Show type of object in the object editor
Teemu Piippo <teemu@hecknology.net>
parents:
152
diff
changeset
|
31 | objectTitleLayout->addWidget(this->objectTypeIconLabel); |
f69d53c053df
Show type of object in the object editor
Teemu Piippo <teemu@hecknology.net>
parents:
152
diff
changeset
|
32 | objectTitleLayout->addWidget(this->objectTypeNameLabel); |
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(formContainer); |
35 | this->setObjectId(id); | |
180
5b7a8f2270ff
Handle properties in a generic manner in the object editor
Teemu Piippo <teemu@hecknology.net>
parents:
178
diff
changeset
|
36 | |
178 | 37 | QWidget* const parent = this->formContainer; |
38 | QFormLayout* formLayout = new QFormLayout{parent}; | |
39 | this->formContainer->setLayout(formLayout); | |
180
5b7a8f2270ff
Handle properties in a generic manner in the object editor
Teemu Piippo <teemu@hecknology.net>
parents:
178
diff
changeset
|
40 | |
5b7a8f2270ff
Handle properties in a generic manner in the object editor
Teemu Piippo <teemu@hecknology.net>
parents:
178
diff
changeset
|
41 | for (const ldraw::Property property : ldraw::ALL_PROPERTIES) |
178 | 42 | { |
180
5b7a8f2270ff
Handle properties in a generic manner in the object editor
Teemu Piippo <teemu@hecknology.net>
parents:
178
diff
changeset
|
43 | QWidget* editorWidget = this->makeEditorWidgetForProperty(property); |
5b7a8f2270ff
Handle properties in a generic manner in the object editor
Teemu Piippo <teemu@hecknology.net>
parents:
178
diff
changeset
|
44 | if (editorWidget != nullptr) |
178 | 45 | { |
180
5b7a8f2270ff
Handle properties in a generic manner in the object editor
Teemu Piippo <teemu@hecknology.net>
parents:
178
diff
changeset
|
46 | editorWidget->setProperty("_property_id", static_cast<int>(property)); |
5b7a8f2270ff
Handle properties in a generic manner in the object editor
Teemu Piippo <teemu@hecknology.net>
parents:
178
diff
changeset
|
47 | QLabel* propertyLabel = new QLabel{ldraw::traits(property).name.data()}; |
5b7a8f2270ff
Handle properties in a generic manner in the object editor
Teemu Piippo <teemu@hecknology.net>
parents:
178
diff
changeset
|
48 | formLayout->addRow(propertyLabel, editorWidget); |
5b7a8f2270ff
Handle properties in a generic manner in the object editor
Teemu Piippo <teemu@hecknology.net>
parents:
178
diff
changeset
|
49 | this->propertyWidgets[property] = {propertyLabel, editorWidget}; |
178 | 50 | } |
180
5b7a8f2270ff
Handle properties in a generic manner in the object editor
Teemu Piippo <teemu@hecknology.net>
parents:
178
diff
changeset
|
51 | } |
5b7a8f2270ff
Handle properties in a generic manner in the object editor
Teemu Piippo <teemu@hecknology.net>
parents:
178
diff
changeset
|
52 | |
5b7a8f2270ff
Handle properties in a generic manner in the object editor
Teemu Piippo <teemu@hecknology.net>
parents:
178
diff
changeset
|
53 | this->setObjectId(ldraw::NULL_ID); |
177
f69d53c053df
Show type of object in the object editor
Teemu Piippo <teemu@hecknology.net>
parents:
152
diff
changeset
|
54 | } |
f69d53c053df
Show type of object in the object editor
Teemu Piippo <teemu@hecknology.net>
parents:
152
diff
changeset
|
55 | |
f69d53c053df
Show type of object in the object editor
Teemu Piippo <teemu@hecknology.net>
parents:
152
diff
changeset
|
56 | QString titleCase(const QString& string) |
f69d53c053df
Show type of object in the object editor
Teemu Piippo <teemu@hecknology.net>
parents:
152
diff
changeset
|
57 | { |
f69d53c053df
Show type of object in the object editor
Teemu Piippo <teemu@hecknology.net>
parents:
152
diff
changeset
|
58 | 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
|
59 | } |
62373840e33a
object editor widgets start to form up
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
60 | |
180
5b7a8f2270ff
Handle properties in a generic manner in the object editor
Teemu Piippo <teemu@hecknology.net>
parents:
178
diff
changeset
|
61 | void setValueToWidget(QWidget* widget, const QVariant& value) |
5b7a8f2270ff
Handle properties in a generic manner in the object editor
Teemu Piippo <teemu@hecknology.net>
parents:
178
diff
changeset
|
62 | { |
5b7a8f2270ff
Handle properties in a generic manner in the object editor
Teemu Piippo <teemu@hecknology.net>
parents:
178
diff
changeset
|
63 | ColorIndexInput* colorIndexInput = qobject_cast<ColorIndexInput*>(widget); |
5b7a8f2270ff
Handle properties in a generic manner in the object editor
Teemu Piippo <teemu@hecknology.net>
parents:
178
diff
changeset
|
64 | if (colorIndexInput != nullptr) |
5b7a8f2270ff
Handle properties in a generic manner in the object editor
Teemu Piippo <teemu@hecknology.net>
parents:
178
diff
changeset
|
65 | { |
5b7a8f2270ff
Handle properties in a generic manner in the object editor
Teemu Piippo <teemu@hecknology.net>
parents:
178
diff
changeset
|
66 | colorIndexInput->setSelectedColor(value.value<ldraw::Color>()); |
5b7a8f2270ff
Handle properties in a generic manner in the object editor
Teemu Piippo <teemu@hecknology.net>
parents:
178
diff
changeset
|
67 | } |
5b7a8f2270ff
Handle properties in a generic manner in the object editor
Teemu Piippo <teemu@hecknology.net>
parents:
178
diff
changeset
|
68 | else |
5b7a8f2270ff
Handle properties in a generic manner in the object editor
Teemu Piippo <teemu@hecknology.net>
parents:
178
diff
changeset
|
69 | { |
5b7a8f2270ff
Handle properties in a generic manner in the object editor
Teemu Piippo <teemu@hecknology.net>
parents:
178
diff
changeset
|
70 | Vec3Editor* vec3Editor = qobject_cast<Vec3Editor*>(widget); |
5b7a8f2270ff
Handle properties in a generic manner in the object editor
Teemu Piippo <teemu@hecknology.net>
parents:
178
diff
changeset
|
71 | if (vec3Editor != nullptr) |
5b7a8f2270ff
Handle properties in a generic manner in the object editor
Teemu Piippo <teemu@hecknology.net>
parents:
178
diff
changeset
|
72 | { |
5b7a8f2270ff
Handle properties in a generic manner in the object editor
Teemu Piippo <teemu@hecknology.net>
parents:
178
diff
changeset
|
73 | vec3Editor->setValue(value.value<glm::vec3>()); |
5b7a8f2270ff
Handle properties in a generic manner in the object editor
Teemu Piippo <teemu@hecknology.net>
parents:
178
diff
changeset
|
74 | } |
5b7a8f2270ff
Handle properties in a generic manner in the object editor
Teemu Piippo <teemu@hecknology.net>
parents:
178
diff
changeset
|
75 | } |
5b7a8f2270ff
Handle properties in a generic manner in the object editor
Teemu Piippo <teemu@hecknology.net>
parents:
178
diff
changeset
|
76 | } |
5b7a8f2270ff
Handle properties in a generic manner in the object editor
Teemu Piippo <teemu@hecknology.net>
parents:
178
diff
changeset
|
77 | |
81
62373840e33a
object editor widgets start to form up
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
78 | void ObjectEditor::setObjectId(const ldraw::id_t id) |
62373840e33a
object editor widgets start to form up
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
79 | { |
62373840e33a
object editor widgets start to form up
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
80 | this->objectId = id; |
152 | 81 | const ldraw::Object* object = this->document->getModel().get(id); |
178 | 82 | if (object != nullptr) |
81
62373840e33a
object editor widgets start to form up
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
83 | { |
177
f69d53c053df
Show type of object in the object editor
Teemu Piippo <teemu@hecknology.net>
parents:
152
diff
changeset
|
84 | 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
|
85 | this->objectTypeIconLabel->setPixmap(QPixmap{object->iconName()}.scaledToWidth(24)); |
180
5b7a8f2270ff
Handle properties in a generic manner in the object editor
Teemu Piippo <teemu@hecknology.net>
parents:
178
diff
changeset
|
86 | for (const ldraw::Property property : ldraw::ALL_PROPERTIES) |
81
62373840e33a
object editor widgets start to form up
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
87 | { |
180
5b7a8f2270ff
Handle properties in a generic manner in the object editor
Teemu Piippo <teemu@hecknology.net>
parents:
178
diff
changeset
|
88 | const QVariant value = object->getProperty(property); |
5b7a8f2270ff
Handle properties in a generic manner in the object editor
Teemu Piippo <teemu@hecknology.net>
parents:
178
diff
changeset
|
89 | const auto it = this->propertyWidgets.find(property); |
5b7a8f2270ff
Handle properties in a generic manner in the object editor
Teemu Piippo <teemu@hecknology.net>
parents:
178
diff
changeset
|
90 | if (it != this->propertyWidgets.end()) |
5b7a8f2270ff
Handle properties in a generic manner in the object editor
Teemu Piippo <teemu@hecknology.net>
parents:
178
diff
changeset
|
91 | { |
5b7a8f2270ff
Handle properties in a generic manner in the object editor
Teemu Piippo <teemu@hecknology.net>
parents:
178
diff
changeset
|
92 | it->first->setVisible(not value.isNull()); |
5b7a8f2270ff
Handle properties in a generic manner in the object editor
Teemu Piippo <teemu@hecknology.net>
parents:
178
diff
changeset
|
93 | it->second->setVisible(not value.isNull()); |
5b7a8f2270ff
Handle properties in a generic manner in the object editor
Teemu Piippo <teemu@hecknology.net>
parents:
178
diff
changeset
|
94 | if (not value.isNull()) |
5b7a8f2270ff
Handle properties in a generic manner in the object editor
Teemu Piippo <teemu@hecknology.net>
parents:
178
diff
changeset
|
95 | { |
5b7a8f2270ff
Handle properties in a generic manner in the object editor
Teemu Piippo <teemu@hecknology.net>
parents:
178
diff
changeset
|
96 | setValueToWidget(it->second, value); |
5b7a8f2270ff
Handle properties in a generic manner in the object editor
Teemu Piippo <teemu@hecknology.net>
parents:
178
diff
changeset
|
97 | } |
5b7a8f2270ff
Handle properties in a generic manner in the object editor
Teemu Piippo <teemu@hecknology.net>
parents:
178
diff
changeset
|
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 | } |
62373840e33a
object editor widgets start to form up
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
101 | else |
62373840e33a
object editor widgets start to form up
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
102 | { |
180
5b7a8f2270ff
Handle properties in a generic manner in the object editor
Teemu Piippo <teemu@hecknology.net>
parents:
178
diff
changeset
|
103 | this->objectTypeNameLabel->clear(); |
5b7a8f2270ff
Handle properties in a generic manner in the object editor
Teemu Piippo <teemu@hecknology.net>
parents:
178
diff
changeset
|
104 | this->objectTypeIconLabel->clear(); |
178 | 105 | for (auto& pair : this->propertyWidgets) |
106 | { | |
107 | pair.first->setVisible(false); | |
108 | pair.second->setVisible(false); | |
109 | } | |
81
62373840e33a
object editor widgets start to form up
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
110 | } |
62373840e33a
object editor widgets start to form up
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
111 | } |
178 | 112 | |
180
5b7a8f2270ff
Handle properties in a generic manner in the object editor
Teemu Piippo <teemu@hecknology.net>
parents:
178
diff
changeset
|
113 | void ObjectEditor::handleColorChange(ldraw::Color value) |
5b7a8f2270ff
Handle properties in a generic manner in the object editor
Teemu Piippo <teemu@hecknology.net>
parents:
178
diff
changeset
|
114 | { |
5b7a8f2270ff
Handle properties in a generic manner in the object editor
Teemu Piippo <teemu@hecknology.net>
parents:
178
diff
changeset
|
115 | this->handlePropertyChange(this->sender(), QVariant::fromValue(value)); |
5b7a8f2270ff
Handle properties in a generic manner in the object editor
Teemu Piippo <teemu@hecknology.net>
parents:
178
diff
changeset
|
116 | } |
5b7a8f2270ff
Handle properties in a generic manner in the object editor
Teemu Piippo <teemu@hecknology.net>
parents:
178
diff
changeset
|
117 | |
5b7a8f2270ff
Handle properties in a generic manner in the object editor
Teemu Piippo <teemu@hecknology.net>
parents:
178
diff
changeset
|
118 | void ObjectEditor::handleVec3Change(const glm::vec3 &value) |
5b7a8f2270ff
Handle properties in a generic manner in the object editor
Teemu Piippo <teemu@hecknology.net>
parents:
178
diff
changeset
|
119 | { |
5b7a8f2270ff
Handle properties in a generic manner in the object editor
Teemu Piippo <teemu@hecknology.net>
parents:
178
diff
changeset
|
120 | this->handlePropertyChange(this->sender(), QVariant::fromValue(value)); |
5b7a8f2270ff
Handle properties in a generic manner in the object editor
Teemu Piippo <teemu@hecknology.net>
parents:
178
diff
changeset
|
121 | } |
5b7a8f2270ff
Handle properties in a generic manner in the object editor
Teemu Piippo <teemu@hecknology.net>
parents:
178
diff
changeset
|
122 | |
5b7a8f2270ff
Handle properties in a generic manner in the object editor
Teemu Piippo <teemu@hecknology.net>
parents:
178
diff
changeset
|
123 | QWidget* ObjectEditor::makeEditorWidgetForProperty(ldraw::Property property) |
178 | 124 | { |
180
5b7a8f2270ff
Handle properties in a generic manner in the object editor
Teemu Piippo <teemu@hecknology.net>
parents:
178
diff
changeset
|
125 | QWidget* const parent = qobject_cast<QWidget*>(this->parent()); |
5b7a8f2270ff
Handle properties in a generic manner in the object editor
Teemu Piippo <teemu@hecknology.net>
parents:
178
diff
changeset
|
126 | if (ldraw::traits(property).type == qMetaTypeId<ldraw::Color>()) |
5b7a8f2270ff
Handle properties in a generic manner in the object editor
Teemu Piippo <teemu@hecknology.net>
parents:
178
diff
changeset
|
127 | { |
5b7a8f2270ff
Handle properties in a generic manner in the object editor
Teemu Piippo <teemu@hecknology.net>
parents:
178
diff
changeset
|
128 | ColorIndexInput* colorWidget = new ColorIndexInput{this->document, {0}, parent}; |
5b7a8f2270ff
Handle properties in a generic manner in the object editor
Teemu Piippo <teemu@hecknology.net>
parents:
178
diff
changeset
|
129 | connect(colorWidget, &ColorIndexInput::colorChanged, this, &ObjectEditor::handleColorChange); |
5b7a8f2270ff
Handle properties in a generic manner in the object editor
Teemu Piippo <teemu@hecknology.net>
parents:
178
diff
changeset
|
130 | return colorWidget; |
5b7a8f2270ff
Handle properties in a generic manner in the object editor
Teemu Piippo <teemu@hecknology.net>
parents:
178
diff
changeset
|
131 | } |
5b7a8f2270ff
Handle properties in a generic manner in the object editor
Teemu Piippo <teemu@hecknology.net>
parents:
178
diff
changeset
|
132 | else if (ldraw::traits(property).type == qMetaTypeId<glm::vec3>()) |
5b7a8f2270ff
Handle properties in a generic manner in the object editor
Teemu Piippo <teemu@hecknology.net>
parents:
178
diff
changeset
|
133 | { |
5b7a8f2270ff
Handle properties in a generic manner in the object editor
Teemu Piippo <teemu@hecknology.net>
parents:
178
diff
changeset
|
134 | Vec3Editor* editor = new Vec3Editor{{}, parent}; |
5b7a8f2270ff
Handle properties in a generic manner in the object editor
Teemu Piippo <teemu@hecknology.net>
parents:
178
diff
changeset
|
135 | connect(editor, &Vec3Editor::valueChanged, this, &ObjectEditor::handleVec3Change); |
5b7a8f2270ff
Handle properties in a generic manner in the object editor
Teemu Piippo <teemu@hecknology.net>
parents:
178
diff
changeset
|
136 | return editor; |
5b7a8f2270ff
Handle properties in a generic manner in the object editor
Teemu Piippo <teemu@hecknology.net>
parents:
178
diff
changeset
|
137 | } |
5b7a8f2270ff
Handle properties in a generic manner in the object editor
Teemu Piippo <teemu@hecknology.net>
parents:
178
diff
changeset
|
138 | else |
5b7a8f2270ff
Handle properties in a generic manner in the object editor
Teemu Piippo <teemu@hecknology.net>
parents:
178
diff
changeset
|
139 | { |
5b7a8f2270ff
Handle properties in a generic manner in the object editor
Teemu Piippo <teemu@hecknology.net>
parents:
178
diff
changeset
|
140 | return nullptr; |
5b7a8f2270ff
Handle properties in a generic manner in the object editor
Teemu Piippo <teemu@hecknology.net>
parents:
178
diff
changeset
|
141 | } |
178 | 142 | } |
180
5b7a8f2270ff
Handle properties in a generic manner in the object editor
Teemu Piippo <teemu@hecknology.net>
parents:
178
diff
changeset
|
143 | |
5b7a8f2270ff
Handle properties in a generic manner in the object editor
Teemu Piippo <teemu@hecknology.net>
parents:
178
diff
changeset
|
144 | void ObjectEditor::handlePropertyChange(QObject *caller, const QVariant &value) |
5b7a8f2270ff
Handle properties in a generic manner in the object editor
Teemu Piippo <teemu@hecknology.net>
parents:
178
diff
changeset
|
145 | { |
5b7a8f2270ff
Handle properties in a generic manner in the object editor
Teemu Piippo <teemu@hecknology.net>
parents:
178
diff
changeset
|
146 | QVariant propertyVariant = caller->property("_property_id"); |
5b7a8f2270ff
Handle properties in a generic manner in the object editor
Teemu Piippo <teemu@hecknology.net>
parents:
178
diff
changeset
|
147 | if (not propertyVariant.isNull()) |
5b7a8f2270ff
Handle properties in a generic manner in the object editor
Teemu Piippo <teemu@hecknology.net>
parents:
178
diff
changeset
|
148 | { |
5b7a8f2270ff
Handle properties in a generic manner in the object editor
Teemu Piippo <teemu@hecknology.net>
parents:
178
diff
changeset
|
149 | ldraw::Property const property = static_cast<ldraw::Property>(propertyVariant.toInt()); |
5b7a8f2270ff
Handle properties in a generic manner in the object editor
Teemu Piippo <teemu@hecknology.net>
parents:
178
diff
changeset
|
150 | this->document->editModel()->setObjectProperty(this->objectId, property, value); |
5b7a8f2270ff
Handle properties in a generic manner in the object editor
Teemu Piippo <teemu@hecknology.net>
parents:
178
diff
changeset
|
151 | } |
5b7a8f2270ff
Handle properties in a generic manner in the object editor
Teemu Piippo <teemu@hecknology.net>
parents:
178
diff
changeset
|
152 | } |