src/ui/polygonobjecteditor.cpp

Wed, 25 Mar 2020 16:07:20 +0200

author
Teemu Piippo <teemu@hecknology.net>
date
Wed, 25 Mar 2020 16:07:20 +0200
changeset 87
93ec4d630346
parent 86
4bec0525ef1b
child 89
7abaf1d64719
permissions
-rw-r--r--

added PolygonObject and refactored away a lot of boilerplate

81
62373840e33a object editor widgets start to form up
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
1 #include <QVBoxLayout>
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
4bec0525ef1b PolygonObjectEditor can now modify the object properly
Teemu Piippo <teemu@hecknology.net>
parents: 84
diff changeset
8 constexpr char COLUMN_PROPERTY[] = "_ldforge_column";
81
62373840e33a object editor widgets start to form up
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
9
62373840e33a object editor widgets start to form up
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
10 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
11 QWidget{parent},
62373840e33a object editor widgets start to form up
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
12 model{model},
62373840e33a object editor widgets start to form up
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
13 storedObjectId{ldraw::NULL_ID.value}
62373840e33a object editor widgets start to form up
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
14 {
62373840e33a object editor widgets start to form up
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
15 this->setLayout(new QVBoxLayout{this});
86
4bec0525ef1b PolygonObjectEditor can now modify the object properly
Teemu Piippo <teemu@hecknology.net>
parents: 84
diff changeset
16 for (int i = 0; i < countof(this->vec3Editors); i += 1)
83
b8bd338eb602 refactor, added splitter
Teemu Piippo <teemu@hecknology.net>
parents: 81
diff changeset
17 {
86
4bec0525ef1b PolygonObjectEditor can now modify the object properly
Teemu Piippo <teemu@hecknology.net>
parents: 84
diff changeset
18 std::optional<Vec3Editor>& editorPointer = this->vec3Editors[i];
83
b8bd338eb602 refactor, added splitter
Teemu Piippo <teemu@hecknology.net>
parents: 81
diff changeset
19 editorPointer.emplace(glm::vec3{}, this);
86
4bec0525ef1b PolygonObjectEditor can now modify the object properly
Teemu Piippo <teemu@hecknology.net>
parents: 84
diff changeset
20 editorPointer->setProperty(COLUMN_PROPERTY, QVariant::fromValue(i));
83
b8bd338eb602 refactor, added splitter
Teemu Piippo <teemu@hecknology.net>
parents: 81
diff changeset
21 this->layout()->addWidget(&*editorPointer);
86
4bec0525ef1b PolygonObjectEditor can now modify the object properly
Teemu Piippo <teemu@hecknology.net>
parents: 84
diff changeset
22 connect(&*editorPointer, &Vec3Editor::valueChanged, this, &PolygonObjectEditor::vectorChanged);
83
b8bd338eb602 refactor, added splitter
Teemu Piippo <teemu@hecknology.net>
parents: 81
diff changeset
23 }
b8bd338eb602 refactor, added splitter
Teemu Piippo <teemu@hecknology.net>
parents: 81
diff changeset
24 this->layout()->addWidget(new QSplitter{Qt::Vertical, this});
81
62373840e33a object editor widgets start to form up
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
25 this->setObjectId(id);
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
62373840e33a object editor widgets start to form up
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
28 // destructor needed for std::unique_ptr
62373840e33a object editor widgets start to form up
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
29 PolygonObjectEditor::~PolygonObjectEditor()
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
62373840e33a object editor widgets start to form up
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
33 ldraw::id_t PolygonObjectEditor::objectId() const
62373840e33a object editor widgets start to form up
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
34 {
62373840e33a object editor widgets start to form up
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
35 return this->storedObjectId;
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
62373840e33a object editor widgets start to form up
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
38 void PolygonObjectEditor::setObjectId(ldraw::id_t id)
62373840e33a object editor widgets start to form up
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
39 {
62373840e33a object editor widgets start to form up
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
40 this->storedObjectId = id;
62373840e33a object editor widgets start to form up
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
41 this->updateNumRows();
62373840e33a object editor widgets start to form up
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
42 }
62373840e33a object editor widgets start to form up
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
43
86
4bec0525ef1b PolygonObjectEditor can now modify the object properly
Teemu Piippo <teemu@hecknology.net>
parents: 84
diff changeset
44 void PolygonObjectEditor::vectorChanged(const glm::vec3& value)
4bec0525ef1b PolygonObjectEditor can now modify the object properly
Teemu Piippo <teemu@hecknology.net>
parents: 84
diff changeset
45 {
4bec0525ef1b PolygonObjectEditor can now modify the object properly
Teemu Piippo <teemu@hecknology.net>
parents: 84
diff changeset
46 bool ok;
4bec0525ef1b PolygonObjectEditor can now modify the object properly
Teemu Piippo <teemu@hecknology.net>
parents: 84
diff changeset
47 const int num = sender()->property(COLUMN_PROPERTY).toInt(&ok);
4bec0525ef1b PolygonObjectEditor can now modify the object properly
Teemu Piippo <teemu@hecknology.net>
parents: 84
diff changeset
48 if (ok and num >= 0 and num < countof(this->vec3Editors))
4bec0525ef1b PolygonObjectEditor can now modify the object properly
Teemu Piippo <teemu@hecknology.net>
parents: 84
diff changeset
49 {
4bec0525ef1b PolygonObjectEditor can now modify the object properly
Teemu Piippo <teemu@hecknology.net>
parents: 84
diff changeset
50 Model::EditContext editor = this->model->edit();
4bec0525ef1b PolygonObjectEditor can now modify the object properly
Teemu Piippo <teemu@hecknology.net>
parents: 84
diff changeset
51 editor.setObjectPoint(this->objectId(), num, value);
4bec0525ef1b PolygonObjectEditor can now modify the object properly
Teemu Piippo <teemu@hecknology.net>
parents: 84
diff changeset
52 }
4bec0525ef1b PolygonObjectEditor can now modify the object properly
Teemu Piippo <teemu@hecknology.net>
parents: 84
diff changeset
53 }
4bec0525ef1b PolygonObjectEditor can now modify the object properly
Teemu Piippo <teemu@hecknology.net>
parents: 84
diff changeset
54
81
62373840e33a object editor widgets start to form up
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
55 void PolygonObjectEditor::updateNumRows()
62373840e33a object editor widgets start to form up
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
56 {
62373840e33a object editor widgets start to form up
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
57 const ldraw::Object* object = model->get(this->storedObjectId);
84
Teemu Piippo <teemu@hecknology.net>
parents: 83
diff changeset
58 const int numPoints = object != nullptr ? object->numPoints() : 0;
83
b8bd338eb602 refactor, added splitter
Teemu Piippo <teemu@hecknology.net>
parents: 81
diff changeset
59 for (int i = 0; i < countof(this->vec3Editors); i += 1)
81
62373840e33a object editor widgets start to form up
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
60 {
83
b8bd338eb602 refactor, added splitter
Teemu Piippo <teemu@hecknology.net>
parents: 81
diff changeset
61 Vec3Editor& editor = *this->vec3Editors[i];
86
4bec0525ef1b PolygonObjectEditor can now modify the object properly
Teemu Piippo <teemu@hecknology.net>
parents: 84
diff changeset
62 QSignalBlocker blocker{&editor};
83
b8bd338eb602 refactor, added splitter
Teemu Piippo <teemu@hecknology.net>
parents: 81
diff changeset
63 if (i < numPoints)
81
62373840e33a object editor widgets start to form up
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
64 {
83
b8bd338eb602 refactor, added splitter
Teemu Piippo <teemu@hecknology.net>
parents: 81
diff changeset
65 editor.setVisible(true);
b8bd338eb602 refactor, added splitter
Teemu Piippo <teemu@hecknology.net>
parents: 81
diff changeset
66 editor.setValue(object->getPoint(i));
b8bd338eb602 refactor, added splitter
Teemu Piippo <teemu@hecknology.net>
parents: 81
diff changeset
67 }
b8bd338eb602 refactor, added splitter
Teemu Piippo <teemu@hecknology.net>
parents: 81
diff changeset
68 else
b8bd338eb602 refactor, added splitter
Teemu Piippo <teemu@hecknology.net>
parents: 81
diff changeset
69 {
b8bd338eb602 refactor, added splitter
Teemu Piippo <teemu@hecknology.net>
parents: 81
diff changeset
70 editor.setVisible(false);
b8bd338eb602 refactor, added splitter
Teemu Piippo <teemu@hecknology.net>
parents: 81
diff changeset
71 editor.setValue(glm::vec3{});
81
62373840e33a object editor widgets start to form up
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
72 }
62373840e33a object editor widgets start to form up
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
73 }
62373840e33a object editor widgets start to form up
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
74 }

mercurial