src/ui/polygonobjecteditor.cpp

Sat, 05 Mar 2022 13:19:59 +0200

author
Teemu Piippo <teemu@hecknology.net>
date
Sat, 05 Mar 2022 13:19:59 +0200
changeset 162
3ebe1a0ca818
parent 153
2f79053c2e9a
child 178
a23024fc98e0
permissions
-rw-r--r--

only render svg to png if the png does not exist

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>
152
03f8e6d42e13 Major refactoring
Teemu Piippo <teemu@hecknology.net>
parents: 100
diff changeset
3 #include "document.h"
153
2f79053c2e9a Renamed modeleditcontext.cpp -> modeleditor.cpp
Teemu Piippo <teemu@hecknology.net>
parents: 152
diff changeset
4 #include "modeleditor.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 LABEL_NAME[] = "_ldforge_label";
81
62373840e33a object editor widgets start to form up
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
10
152
03f8e6d42e13 Major refactoring
Teemu Piippo <teemu@hecknology.net>
parents: 100
diff changeset
11 PolygonObjectEditor::PolygonObjectEditor(Document* document, ldraw::id_t id) :
03f8e6d42e13 Major refactoring
Teemu Piippo <teemu@hecknology.net>
parents: 100
diff changeset
12 QWidget{document},
03f8e6d42e13 Major refactoring
Teemu Piippo <teemu@hecknology.net>
parents: 100
diff changeset
13 document{document},
81
62373840e33a object editor widgets start to form up
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
14 storedObjectId{ldraw::NULL_ID.value}
62373840e33a object editor widgets start to form up
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
15 {
89
7abaf1d64719 object editing
Teemu Piippo <teemu@hecknology.net>
parents: 86
diff changeset
16 this->splitter.emplace(Qt::Vertical, this);
81
62373840e33a object editor widgets start to form up
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
17 this->setObjectId(id);
62373840e33a object editor widgets start to form up
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
18 }
62373840e33a object editor widgets start to form up
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
19
62373840e33a object editor widgets start to form up
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
20 // destructor needed for std::unique_ptr
62373840e33a object editor widgets start to form up
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
21 PolygonObjectEditor::~PolygonObjectEditor()
62373840e33a object editor widgets start to form up
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
22 {
62373840e33a object editor widgets start to form up
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
23 }
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 ldraw::id_t PolygonObjectEditor::objectId() const
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 return this->storedObjectId;
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
62373840e33a object editor widgets start to form up
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
30 void PolygonObjectEditor::setObjectId(ldraw::id_t id)
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 this->storedObjectId = id;
89
7abaf1d64719 object editing
Teemu Piippo <teemu@hecknology.net>
parents: 86
diff changeset
33 this->buildWidgets();
81
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
89
7abaf1d64719 object editing
Teemu Piippo <teemu@hecknology.net>
parents: 86
diff changeset
36 void PolygonObjectEditor::buildWidgets()
86
4bec0525ef1b PolygonObjectEditor can now modify the object properly
Teemu Piippo <teemu@hecknology.net>
parents: 84
diff changeset
37 {
89
7abaf1d64719 object editing
Teemu Piippo <teemu@hecknology.net>
parents: 86
diff changeset
38 this->widgets.clear();
7abaf1d64719 object editing
Teemu Piippo <teemu@hecknology.net>
parents: 86
diff changeset
39 delete this->layout();
7abaf1d64719 object editing
Teemu Piippo <teemu@hecknology.net>
parents: 86
diff changeset
40 QFormLayout* layout = new QFormLayout{this};
7abaf1d64719 object editing
Teemu Piippo <teemu@hecknology.net>
parents: 86
diff changeset
41 this->setLayout(layout);
7abaf1d64719 object editing
Teemu Piippo <teemu@hecknology.net>
parents: 86
diff changeset
42 for (int n : {0, 1, 2, 3})
7abaf1d64719 object editing
Teemu Piippo <teemu@hecknology.net>
parents: 86
diff changeset
43 {
7abaf1d64719 object editing
Teemu Piippo <teemu@hecknology.net>
parents: 86
diff changeset
44 this->setupPointWidget(n);
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 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
47 {
89
7abaf1d64719 object editing
Teemu Piippo <teemu@hecknology.net>
parents: 86
diff changeset
48 const QString label = widget->property(LABEL_NAME).toString();
7abaf1d64719 object editing
Teemu Piippo <teemu@hecknology.net>
parents: 86
diff changeset
49 layout->addRow(label, widget.get());
7abaf1d64719 object editing
Teemu Piippo <teemu@hecknology.net>
parents: 86
diff changeset
50 }
7abaf1d64719 object editing
Teemu Piippo <teemu@hecknology.net>
parents: 86
diff changeset
51 layout->addRow("", &*this->splitter);
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
7abaf1d64719 object editing
Teemu Piippo <teemu@hecknology.net>
parents: 86
diff changeset
54 void PolygonObjectEditor::setupPointWidget(int n)
7abaf1d64719 object editing
Teemu Piippo <teemu@hecknology.net>
parents: 86
diff changeset
55 {
152
03f8e6d42e13 Major refactoring
Teemu Piippo <teemu@hecknology.net>
parents: 100
diff changeset
56 const QModelIndex index = this->document->getModel().find(this->objectId());
03f8e6d42e13 Major refactoring
Teemu Piippo <teemu@hecknology.net>
parents: 100
diff changeset
57 if (index.isValid())
89
7abaf1d64719 object editing
Teemu Piippo <teemu@hecknology.net>
parents: 86
diff changeset
58 {
152
03f8e6d42e13 Major refactoring
Teemu Piippo <teemu@hecknology.net>
parents: 100
diff changeset
59 const ldraw::Object* const object = this->document->getModel()[index.row()];
03f8e6d42e13 Major refactoring
Teemu Piippo <teemu@hecknology.net>
parents: 100
diff changeset
60 const ldraw::Property property = ldraw::pointProperty(n);
03f8e6d42e13 Major refactoring
Teemu Piippo <teemu@hecknology.net>
parents: 100
diff changeset
61 const QVariant value = object->getProperty(property);
03f8e6d42e13 Major refactoring
Teemu Piippo <teemu@hecknology.net>
parents: 100
diff changeset
62 if (value.isValid())
03f8e6d42e13 Major refactoring
Teemu Piippo <teemu@hecknology.net>
parents: 100
diff changeset
63 {
03f8e6d42e13 Major refactoring
Teemu Piippo <teemu@hecknology.net>
parents: 100
diff changeset
64 std::unique_ptr<Vec3Editor> editor = std::make_unique<Vec3Editor>(value.value<glm::vec3>(), this);
03f8e6d42e13 Major refactoring
Teemu Piippo <teemu@hecknology.net>
parents: 100
diff changeset
65 QObject::connect(editor.get(), &Vec3Editor::valueChanged, this, &PolygonObjectEditor::pointChanged);
03f8e6d42e13 Major refactoring
Teemu Piippo <teemu@hecknology.net>
parents: 100
diff changeset
66 editor->setProperty(INDEX_NAME, QVariant::fromValue(n));
03f8e6d42e13 Major refactoring
Teemu Piippo <teemu@hecknology.net>
parents: 100
diff changeset
67 editor->setProperty(LABEL_NAME, &ldraw::traits(property).name[0]);
03f8e6d42e13 Major refactoring
Teemu Piippo <teemu@hecknology.net>
parents: 100
diff changeset
68 this->widgets.push_back(std::move(editor));
03f8e6d42e13 Major refactoring
Teemu Piippo <teemu@hecknology.net>
parents: 100
diff changeset
69 }
86
4bec0525ef1b PolygonObjectEditor can now modify the object properly
Teemu Piippo <teemu@hecknology.net>
parents: 84
diff changeset
70 }
4bec0525ef1b PolygonObjectEditor can now modify the object properly
Teemu Piippo <teemu@hecknology.net>
parents: 84
diff changeset
71 }
4bec0525ef1b PolygonObjectEditor can now modify the object properly
Teemu Piippo <teemu@hecknology.net>
parents: 84
diff changeset
72
89
7abaf1d64719 object editing
Teemu Piippo <teemu@hecknology.net>
parents: 86
diff changeset
73 void PolygonObjectEditor::pointChanged(const glm::vec3& value)
81
62373840e33a object editor widgets start to form up
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
74 {
152
03f8e6d42e13 Major refactoring
Teemu Piippo <teemu@hecknology.net>
parents: 100
diff changeset
75 std::unique_ptr<ModelEditor> modelEditor = this->document->editModel();
89
7abaf1d64719 object editing
Teemu Piippo <teemu@hecknology.net>
parents: 86
diff changeset
76 const int n = this->sender()->property(INDEX_NAME).toInt();
7abaf1d64719 object editing
Teemu Piippo <teemu@hecknology.net>
parents: 86
diff changeset
77 const ldraw::Property property = ldraw::pointProperty(n);
152
03f8e6d42e13 Major refactoring
Teemu Piippo <teemu@hecknology.net>
parents: 100
diff changeset
78 modelEditor->setObjectProperty(this->objectId(), property, QVariant::fromValue(value));
81
62373840e33a object editor widgets start to form up
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
79 }

mercurial