src/ui/objecteditor.cpp

Sun, 13 Mar 2022 14:51:39 +0200

author
Teemu Piippo <teemu@hecknology.net>
date
Sun, 13 Mar 2022 14:51:39 +0200
changeset 178
a23024fc98e0
parent 177
f69d53c053df
child 180
5b7a8f2270ff
permissions
-rw-r--r--

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
a23024fc98e0 fix saving
Teemu Piippo <teemu@hecknology.net>
parents: 177
diff changeset
2 #include <QFormLayout>
a23024fc98e0 fix saving
Teemu Piippo <teemu@hecknology.net>
parents: 177
diff changeset
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
a23024fc98e0 fix saving
Teemu Piippo <teemu@hecknology.net>
parents: 177
diff changeset
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
03f8e6d42e13 Major refactoring
Teemu Piippo <teemu@hecknology.net>
parents: 81
diff changeset
7 #include "document.h"
178
a23024fc98e0 fix saving
Teemu Piippo <teemu@hecknology.net>
parents: 177
diff changeset
8 #include "modeleditor.h"
a23024fc98e0 fix saving
Teemu Piippo <teemu@hecknology.net>
parents: 177
diff changeset
9 #include "widgets/colorbutton.h"
a23024fc98e0 fix saving
Teemu Piippo <teemu@hecknology.net>
parents: 177
diff changeset
10 #include "widgets/colorindexinput.h"
a23024fc98e0 fix saving
Teemu Piippo <teemu@hecknology.net>
parents: 177
diff changeset
11
a23024fc98e0 fix saving
Teemu Piippo <teemu@hecknology.net>
parents: 177
diff changeset
12 template<ldraw::Property property>
a23024fc98e0 fix saving
Teemu Piippo <teemu@hecknology.net>
parents: 177
diff changeset
13 static void makeColorEditor()
a23024fc98e0 fix saving
Teemu Piippo <teemu@hecknology.net>
parents: 177
diff changeset
14 {
a23024fc98e0 fix saving
Teemu Piippo <teemu@hecknology.net>
parents: 177
diff changeset
15 QString propertyName = ldraw::PropertyTraits<property>::name;
a23024fc98e0 fix saving
Teemu Piippo <teemu@hecknology.net>
parents: 177
diff changeset
16 }
81
62373840e33a object editor widgets start to form up
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
17
152
03f8e6d42e13 Major refactoring
Teemu Piippo <teemu@hecknology.net>
parents: 81
diff changeset
18 ObjectEditor::ObjectEditor(Document* document, const ldraw::id_t id) :
03f8e6d42e13 Major refactoring
Teemu Piippo <teemu@hecknology.net>
parents: 81
diff changeset
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
a23024fc98e0 fix saving
Teemu Piippo <teemu@hecknology.net>
parents: 177
diff changeset
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
a23024fc98e0 fix saving
Teemu Piippo <teemu@hecknology.net>
parents: 177
diff changeset
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
a23024fc98e0 fix saving
Teemu Piippo <teemu@hecknology.net>
parents: 177
diff changeset
34 this->layout()->addWidget(&*this->polygonEditor);
a23024fc98e0 fix saving
Teemu Piippo <teemu@hecknology.net>
parents: 177
diff changeset
35 this->layout()->addWidget(formContainer);
a23024fc98e0 fix saving
Teemu Piippo <teemu@hecknology.net>
parents: 177
diff changeset
36 this->setObjectId(id);
a23024fc98e0 fix saving
Teemu Piippo <teemu@hecknology.net>
parents: 177
diff changeset
37
a23024fc98e0 fix saving
Teemu Piippo <teemu@hecknology.net>
parents: 177
diff changeset
38 QWidget* const parent = this->formContainer;
a23024fc98e0 fix saving
Teemu Piippo <teemu@hecknology.net>
parents: 177
diff changeset
39 QFormLayout* formLayout = new QFormLayout{parent};
a23024fc98e0 fix saving
Teemu Piippo <teemu@hecknology.net>
parents: 177
diff changeset
40 this->formContainer->setLayout(formLayout);
a23024fc98e0 fix saving
Teemu Piippo <teemu@hecknology.net>
parents: 177
diff changeset
41 QLabel* colorLabel = new QLabel{"Color", parent};
a23024fc98e0 fix saving
Teemu Piippo <teemu@hecknology.net>
parents: 177
diff changeset
42 ColorIndexInput* colorWidget = new ColorIndexInput{document, {0}, parent};
a23024fc98e0 fix saving
Teemu Piippo <teemu@hecknology.net>
parents: 177
diff changeset
43 formLayout->addRow(colorLabel, colorWidget);
a23024fc98e0 fix saving
Teemu Piippo <teemu@hecknology.net>
parents: 177
diff changeset
44 connect(colorWidget, &ColorIndexInput::colorChanged, [this](ldraw::Color color)
a23024fc98e0 fix saving
Teemu Piippo <teemu@hecknology.net>
parents: 177
diff changeset
45 {
a23024fc98e0 fix saving
Teemu Piippo <teemu@hecknology.net>
parents: 177
diff changeset
46 const QModelIndex index = this->document->getModel().find(this->objectId);
a23024fc98e0 fix saving
Teemu Piippo <teemu@hecknology.net>
parents: 177
diff changeset
47 if (index.isValid())
a23024fc98e0 fix saving
Teemu Piippo <teemu@hecknology.net>
parents: 177
diff changeset
48 {
a23024fc98e0 fix saving
Teemu Piippo <teemu@hecknology.net>
parents: 177
diff changeset
49 this->document->editModel()->modifyObjectAt<ldraw::ColoredObject>(
a23024fc98e0 fix saving
Teemu Piippo <teemu@hecknology.net>
parents: 177
diff changeset
50 index.row(),
a23024fc98e0 fix saving
Teemu Piippo <teemu@hecknology.net>
parents: 177
diff changeset
51 [color](ldraw::ColoredObject* object)
a23024fc98e0 fix saving
Teemu Piippo <teemu@hecknology.net>
parents: 177
diff changeset
52 {
a23024fc98e0 fix saving
Teemu Piippo <teemu@hecknology.net>
parents: 177
diff changeset
53 object->colorIndex = color;
a23024fc98e0 fix saving
Teemu Piippo <teemu@hecknology.net>
parents: 177
diff changeset
54 }
a23024fc98e0 fix saving
Teemu Piippo <teemu@hecknology.net>
parents: 177
diff changeset
55 );
a23024fc98e0 fix saving
Teemu Piippo <teemu@hecknology.net>
parents: 177
diff changeset
56 }
a23024fc98e0 fix saving
Teemu Piippo <teemu@hecknology.net>
parents: 177
diff changeset
57 });
a23024fc98e0 fix saving
Teemu Piippo <teemu@hecknology.net>
parents: 177
diff changeset
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
03f8e6d42e13 Major refactoring
Teemu Piippo <teemu@hecknology.net>
parents: 81
diff changeset
69 const ldraw::Object* object = this->document->getModel().get(id);
178
a23024fc98e0 fix saving
Teemu Piippo <teemu@hecknology.net>
parents: 177
diff changeset
70 this->clear();
a23024fc98e0 fix saving
Teemu Piippo <teemu@hecknology.net>
parents: 177
diff changeset
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
a23024fc98e0 fix saving
Teemu Piippo <teemu@hecknology.net>
parents: 177
diff changeset
75 if (object->numPoints() > 0)
81
62373840e33a object editor widgets start to form up
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
76 {
178
a23024fc98e0 fix saving
Teemu Piippo <teemu@hecknology.net>
parents: 177
diff changeset
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
a23024fc98e0 fix saving
Teemu Piippo <teemu@hecknology.net>
parents: 177
diff changeset
81 this->polygonEditor->clear();
a23024fc98e0 fix saving
Teemu Piippo <teemu@hecknology.net>
parents: 177
diff changeset
82 }
a23024fc98e0 fix saving
Teemu Piippo <teemu@hecknology.net>
parents: 177
diff changeset
83 constexpr ldraw::Property prop = ldraw::Property::Color;
a23024fc98e0 fix saving
Teemu Piippo <teemu@hecknology.net>
parents: 177
diff changeset
84 QVariant color = object->getProperty(prop);
a23024fc98e0 fix saving
Teemu Piippo <teemu@hecknology.net>
parents: 177
diff changeset
85 this->propertyWidgets[prop].first->setVisible(not color.isNull());
a23024fc98e0 fix saving
Teemu Piippo <teemu@hecknology.net>
parents: 177
diff changeset
86 this->propertyWidgets[prop].second->setVisible(not color.isNull());
a23024fc98e0 fix saving
Teemu Piippo <teemu@hecknology.net>
parents: 177
diff changeset
87 if (not color.isNull())
a23024fc98e0 fix saving
Teemu Piippo <teemu@hecknology.net>
parents: 177
diff changeset
88 {
a23024fc98e0 fix saving
Teemu Piippo <teemu@hecknology.net>
parents: 177
diff changeset
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
a23024fc98e0 fix saving
Teemu Piippo <teemu@hecknology.net>
parents: 177
diff changeset
94 for (auto& pair : this->propertyWidgets)
a23024fc98e0 fix saving
Teemu Piippo <teemu@hecknology.net>
parents: 177
diff changeset
95 {
a23024fc98e0 fix saving
Teemu Piippo <teemu@hecknology.net>
parents: 177
diff changeset
96 pair.first->setVisible(false);
a23024fc98e0 fix saving
Teemu Piippo <teemu@hecknology.net>
parents: 177
diff changeset
97 pair.second->setVisible(false);
a23024fc98e0 fix saving
Teemu Piippo <teemu@hecknology.net>
parents: 177
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 }
178
a23024fc98e0 fix saving
Teemu Piippo <teemu@hecknology.net>
parents: 177
diff changeset
101
a23024fc98e0 fix saving
Teemu Piippo <teemu@hecknology.net>
parents: 177
diff changeset
102 void ObjectEditor::clear()
a23024fc98e0 fix saving
Teemu Piippo <teemu@hecknology.net>
parents: 177
diff changeset
103 {
a23024fc98e0 fix saving
Teemu Piippo <teemu@hecknology.net>
parents: 177
diff changeset
104 this->objectTypeNameLabel->clear();
a23024fc98e0 fix saving
Teemu Piippo <teemu@hecknology.net>
parents: 177
diff changeset
105 this->objectTypeIconLabel->clear();
a23024fc98e0 fix saving
Teemu Piippo <teemu@hecknology.net>
parents: 177
diff changeset
106 this->polygonEditor->clear();
a23024fc98e0 fix saving
Teemu Piippo <teemu@hecknology.net>
parents: 177
diff changeset
107 delete this->formContainer->layout();
a23024fc98e0 fix saving
Teemu Piippo <teemu@hecknology.net>
parents: 177
diff changeset
108 }

mercurial