src/ui/polygonobjecteditor.cpp

Mon, 21 Sep 2020 19:48:18 +0300

author
Teemu Piippo <teemu@hecknology.net>
date
Mon, 21 Sep 2020 19:48:18 +0300
changeset 94
164f53fb5921
parent 89
7abaf1d64719
child 100
43ce3672648a
permissions
-rw-r--r--

added a color select dialog

89
7abaf1d64719 object editing
Teemu Piippo <teemu@hecknology.net>
parents: 86
diff changeset
1 #include <QFormLayout>
83
b8bd338eb602 refactor, added splitter
Teemu Piippo <teemu@hecknology.net>
parents: 81
diff changeset
2 #include <QSplitter>
81
62373840e33a object editor widgets start to form up
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
3 #include "model.h"
86
4bec0525ef1b PolygonObjectEditor can now modify the object properly
Teemu Piippo <teemu@hecknology.net>
parents: 84
diff changeset
4 #include "modeleditcontext.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
7abaf1d64719 object editing
Teemu Piippo <teemu@hecknology.net>
parents: 86
diff changeset
8 static constexpr char INDEX_NAME[] = "_ldforge_index";
7abaf1d64719 object editing
Teemu Piippo <teemu@hecknology.net>
parents: 86
diff changeset
9 static constexpr char PROPERTY_NAME[] = "_ldforge_property";
7abaf1d64719 object editing
Teemu Piippo <teemu@hecknology.net>
parents: 86
diff changeset
10 static constexpr char OBJECT_ID_NAME[] = "_ldforge_id";
7abaf1d64719 object editing
Teemu Piippo <teemu@hecknology.net>
parents: 86
diff changeset
11 static constexpr char LABEL_NAME[] = "_ldforge_label";
81
62373840e33a object editor widgets start to form up
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
12
62373840e33a object editor widgets start to form up
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
13 PolygonObjectEditor::PolygonObjectEditor(Model* model, ldraw::id_t id, QWidget* parent) :
62373840e33a object editor widgets start to form up
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
14 QWidget{parent},
62373840e33a object editor widgets start to form up
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
15 model{model},
62373840e33a object editor widgets start to form up
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
16 storedObjectId{ldraw::NULL_ID.value}
62373840e33a object editor widgets start to form up
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
17 {
89
7abaf1d64719 object editing
Teemu Piippo <teemu@hecknology.net>
parents: 86
diff changeset
18 this->splitter.emplace(Qt::Vertical, this);
81
62373840e33a object editor widgets start to form up
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
19 this->setObjectId(id);
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
62373840e33a object editor widgets start to form up
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
22 // destructor needed for std::unique_ptr
62373840e33a object editor widgets start to form up
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
23 PolygonObjectEditor::~PolygonObjectEditor()
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
62373840e33a object editor widgets start to form up
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
27 ldraw::id_t PolygonObjectEditor::objectId() const
62373840e33a object editor widgets start to form up
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
28 {
62373840e33a object editor widgets start to form up
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
29 return this->storedObjectId;
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
62373840e33a object editor widgets start to form up
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
32 void PolygonObjectEditor::setObjectId(ldraw::id_t id)
62373840e33a object editor widgets start to form up
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
33 {
62373840e33a object editor widgets start to form up
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
34 this->storedObjectId = id;
89
7abaf1d64719 object editing
Teemu Piippo <teemu@hecknology.net>
parents: 86
diff changeset
35 this->buildWidgets();
81
62373840e33a object editor widgets start to form up
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
36 }
62373840e33a object editor widgets start to form up
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
37
89
7abaf1d64719 object editing
Teemu Piippo <teemu@hecknology.net>
parents: 86
diff changeset
38 void PolygonObjectEditor::buildWidgets()
86
4bec0525ef1b PolygonObjectEditor can now modify the object properly
Teemu Piippo <teemu@hecknology.net>
parents: 84
diff changeset
39 {
89
7abaf1d64719 object editing
Teemu Piippo <teemu@hecknology.net>
parents: 86
diff changeset
40 this->widgets.clear();
7abaf1d64719 object editing
Teemu Piippo <teemu@hecknology.net>
parents: 86
diff changeset
41 delete this->layout();
7abaf1d64719 object editing
Teemu Piippo <teemu@hecknology.net>
parents: 86
diff changeset
42 QFormLayout* layout = new QFormLayout{this};
7abaf1d64719 object editing
Teemu Piippo <teemu@hecknology.net>
parents: 86
diff changeset
43 this->setLayout(layout);
7abaf1d64719 object editing
Teemu Piippo <teemu@hecknology.net>
parents: 86
diff changeset
44 for (int n : {0, 1, 2, 3})
7abaf1d64719 object editing
Teemu Piippo <teemu@hecknology.net>
parents: 86
diff changeset
45 {
7abaf1d64719 object editing
Teemu Piippo <teemu@hecknology.net>
parents: 86
diff changeset
46 this->setupPointWidget(n);
7abaf1d64719 object editing
Teemu Piippo <teemu@hecknology.net>
parents: 86
diff changeset
47 }
7abaf1d64719 object editing
Teemu Piippo <teemu@hecknology.net>
parents: 86
diff changeset
48 for (std::unique_ptr<QWidget>& widget : this->widgets)
86
4bec0525ef1b PolygonObjectEditor can now modify the object properly
Teemu Piippo <teemu@hecknology.net>
parents: 84
diff changeset
49 {
89
7abaf1d64719 object editing
Teemu Piippo <teemu@hecknology.net>
parents: 86
diff changeset
50 const QString label = widget->property(LABEL_NAME).toString();
7abaf1d64719 object editing
Teemu Piippo <teemu@hecknology.net>
parents: 86
diff changeset
51 layout->addRow(label, widget.get());
7abaf1d64719 object editing
Teemu Piippo <teemu@hecknology.net>
parents: 86
diff changeset
52 }
7abaf1d64719 object editing
Teemu Piippo <teemu@hecknology.net>
parents: 86
diff changeset
53 layout->addRow("", &*this->splitter);
7abaf1d64719 object editing
Teemu Piippo <teemu@hecknology.net>
parents: 86
diff changeset
54 }
7abaf1d64719 object editing
Teemu Piippo <teemu@hecknology.net>
parents: 86
diff changeset
55
7abaf1d64719 object editing
Teemu Piippo <teemu@hecknology.net>
parents: 86
diff changeset
56 void PolygonObjectEditor::setupPointWidget(int n)
7abaf1d64719 object editing
Teemu Piippo <teemu@hecknology.net>
parents: 86
diff changeset
57 {
7abaf1d64719 object editing
Teemu Piippo <teemu@hecknology.net>
parents: 86
diff changeset
58 const ldraw::Object* const object = this->model->get(this->objectId());
7abaf1d64719 object editing
Teemu Piippo <teemu@hecknology.net>
parents: 86
diff changeset
59 const ldraw::Property property = ldraw::pointProperty(n);
7abaf1d64719 object editing
Teemu Piippo <teemu@hecknology.net>
parents: 86
diff changeset
60 const QVariant value = object->getProperty(property);
7abaf1d64719 object editing
Teemu Piippo <teemu@hecknology.net>
parents: 86
diff changeset
61 if (value.isValid())
7abaf1d64719 object editing
Teemu Piippo <teemu@hecknology.net>
parents: 86
diff changeset
62 {
7abaf1d64719 object editing
Teemu Piippo <teemu@hecknology.net>
parents: 86
diff changeset
63 std::unique_ptr<Vec3Editor> editor = std::make_unique<Vec3Editor>(value.value<glm::vec3>(), this);
7abaf1d64719 object editing
Teemu Piippo <teemu@hecknology.net>
parents: 86
diff changeset
64 QObject::connect(editor.get(), &Vec3Editor::valueChanged, this, &PolygonObjectEditor::pointChanged);
7abaf1d64719 object editing
Teemu Piippo <teemu@hecknology.net>
parents: 86
diff changeset
65 editor->setProperty(INDEX_NAME, QVariant::fromValue(n));
7abaf1d64719 object editing
Teemu Piippo <teemu@hecknology.net>
parents: 86
diff changeset
66 editor->setProperty(LABEL_NAME, &ldraw::traits(property).name[0]);
7abaf1d64719 object editing
Teemu Piippo <teemu@hecknology.net>
parents: 86
diff changeset
67 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
68 }
4bec0525ef1b PolygonObjectEditor can now modify the object properly
Teemu Piippo <teemu@hecknology.net>
parents: 84
diff changeset
69 }
4bec0525ef1b PolygonObjectEditor can now modify the object properly
Teemu Piippo <teemu@hecknology.net>
parents: 84
diff changeset
70
89
7abaf1d64719 object editing
Teemu Piippo <teemu@hecknology.net>
parents: 86
diff changeset
71 void PolygonObjectEditor::pointChanged(const glm::vec3& value)
81
62373840e33a object editor widgets start to form up
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
72 {
89
7abaf1d64719 object editing
Teemu Piippo <teemu@hecknology.net>
parents: 86
diff changeset
73 Model::EditContext editcontext = this->model->edit();
7abaf1d64719 object editing
Teemu Piippo <teemu@hecknology.net>
parents: 86
diff changeset
74 const int n = this->sender()->property(INDEX_NAME).toInt();
7abaf1d64719 object editing
Teemu Piippo <teemu@hecknology.net>
parents: 86
diff changeset
75 const ldraw::Property property = ldraw::pointProperty(n);
7abaf1d64719 object editing
Teemu Piippo <teemu@hecknology.net>
parents: 86
diff changeset
76 editcontext.setObjectProperty(this->objectId(), property, QVariant::fromValue(value));
81
62373840e33a object editor widgets start to form up
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
77 }

mercurial