src/ui/polygonobjecteditor.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 153
2f79053c2e9a
permissions
-rw-r--r--

fix saving
add color editing to object editor

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);
178
a23024fc98e0 fix saving
Teemu Piippo <teemu@hecknology.net>
parents: 153
diff changeset
17 this->buildWidgets();
81
62373840e33a object editor widgets start to form up
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
18 this->setObjectId(id);
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
62373840e33a object editor widgets start to form up
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
21 // destructor needed for std::unique_ptr
62373840e33a object editor widgets start to form up
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
22 PolygonObjectEditor::~PolygonObjectEditor()
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
62373840e33a object editor widgets start to form up
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
26 ldraw::id_t PolygonObjectEditor::objectId() const
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 return this->storedObjectId;
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
62373840e33a object editor widgets start to form up
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
31 void PolygonObjectEditor::setObjectId(ldraw::id_t id)
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 this->storedObjectId = id;
178
a23024fc98e0 fix saving
Teemu Piippo <teemu@hecknology.net>
parents: 153
diff changeset
34 const QModelIndex index = this->document->getModel().find(this->objectId());
a23024fc98e0 fix saving
Teemu Piippo <teemu@hecknology.net>
parents: 153
diff changeset
35 if (index.isValid())
a23024fc98e0 fix saving
Teemu Piippo <teemu@hecknology.net>
parents: 153
diff changeset
36 {
a23024fc98e0 fix saving
Teemu Piippo <teemu@hecknology.net>
parents: 153
diff changeset
37 for (int n : {0, 1, 2, 3})
a23024fc98e0 fix saving
Teemu Piippo <teemu@hecknology.net>
parents: 153
diff changeset
38 {
a23024fc98e0 fix saving
Teemu Piippo <teemu@hecknology.net>
parents: 153
diff changeset
39 const ldraw::Object* const object = this->document->getModel()[index.row()];
a23024fc98e0 fix saving
Teemu Piippo <teemu@hecknology.net>
parents: 153
diff changeset
40 const ldraw::Property property = ldraw::pointProperty(n);
a23024fc98e0 fix saving
Teemu Piippo <teemu@hecknology.net>
parents: 153
diff changeset
41 const QVariant value = object->getProperty(property);
a23024fc98e0 fix saving
Teemu Piippo <teemu@hecknology.net>
parents: 153
diff changeset
42 this->widgets[n]->setVisible(value.isValid());
a23024fc98e0 fix saving
Teemu Piippo <teemu@hecknology.net>
parents: 153
diff changeset
43 this->formLayout->itemAt(n, QFormLayout::LabelRole)->widget()->setVisible(value.isValid());
a23024fc98e0 fix saving
Teemu Piippo <teemu@hecknology.net>
parents: 153
diff changeset
44 if (value.isValid())
a23024fc98e0 fix saving
Teemu Piippo <teemu@hecknology.net>
parents: 153
diff changeset
45 {
a23024fc98e0 fix saving
Teemu Piippo <teemu@hecknology.net>
parents: 153
diff changeset
46 this->widgets[n]->setValue(value.value<glm::vec3>());
a23024fc98e0 fix saving
Teemu Piippo <teemu@hecknology.net>
parents: 153
diff changeset
47 }
a23024fc98e0 fix saving
Teemu Piippo <teemu@hecknology.net>
parents: 153
diff changeset
48 }
a23024fc98e0 fix saving
Teemu Piippo <teemu@hecknology.net>
parents: 153
diff changeset
49 }
a23024fc98e0 fix saving
Teemu Piippo <teemu@hecknology.net>
parents: 153
diff changeset
50 else
a23024fc98e0 fix saving
Teemu Piippo <teemu@hecknology.net>
parents: 153
diff changeset
51 {
a23024fc98e0 fix saving
Teemu Piippo <teemu@hecknology.net>
parents: 153
diff changeset
52 for (int n : {0, 1, 2, 3})
a23024fc98e0 fix saving
Teemu Piippo <teemu@hecknology.net>
parents: 153
diff changeset
53 {
a23024fc98e0 fix saving
Teemu Piippo <teemu@hecknology.net>
parents: 153
diff changeset
54 this->widgets[n]->setVisible(false);
a23024fc98e0 fix saving
Teemu Piippo <teemu@hecknology.net>
parents: 153
diff changeset
55 this->formLayout->itemAt(n, QFormLayout::LabelRole)->widget()->setVisible(false);
a23024fc98e0 fix saving
Teemu Piippo <teemu@hecknology.net>
parents: 153
diff changeset
56 }
a23024fc98e0 fix saving
Teemu Piippo <teemu@hecknology.net>
parents: 153
diff changeset
57 }
a23024fc98e0 fix saving
Teemu Piippo <teemu@hecknology.net>
parents: 153
diff changeset
58 }
a23024fc98e0 fix saving
Teemu Piippo <teemu@hecknology.net>
parents: 153
diff changeset
59
a23024fc98e0 fix saving
Teemu Piippo <teemu@hecknology.net>
parents: 153
diff changeset
60 void PolygonObjectEditor::clear()
a23024fc98e0 fix saving
Teemu Piippo <teemu@hecknology.net>
parents: 153
diff changeset
61 {
a23024fc98e0 fix saving
Teemu Piippo <teemu@hecknology.net>
parents: 153
diff changeset
62 this->setObjectId(ldraw::NULL_ID);
81
62373840e33a object editor widgets start to form up
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
63 }
62373840e33a object editor widgets start to form up
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
64
89
7abaf1d64719 object editing
Teemu Piippo <teemu@hecknology.net>
parents: 86
diff changeset
65 void PolygonObjectEditor::buildWidgets()
86
4bec0525ef1b PolygonObjectEditor can now modify the object properly
Teemu Piippo <teemu@hecknology.net>
parents: 84
diff changeset
66 {
178
a23024fc98e0 fix saving
Teemu Piippo <teemu@hecknology.net>
parents: 153
diff changeset
67 this->formLayout = new QFormLayout{this};
a23024fc98e0 fix saving
Teemu Piippo <teemu@hecknology.net>
parents: 153
diff changeset
68 this->setLayout(this->formLayout);
89
7abaf1d64719 object editing
Teemu Piippo <teemu@hecknology.net>
parents: 86
diff changeset
69 for (int n : {0, 1, 2, 3})
7abaf1d64719 object editing
Teemu Piippo <teemu@hecknology.net>
parents: 86
diff changeset
70 {
7abaf1d64719 object editing
Teemu Piippo <teemu@hecknology.net>
parents: 86
diff changeset
71 this->setupPointWidget(n);
7abaf1d64719 object editing
Teemu Piippo <teemu@hecknology.net>
parents: 86
diff changeset
72 }
178
a23024fc98e0 fix saving
Teemu Piippo <teemu@hecknology.net>
parents: 153
diff changeset
73 for (std::unique_ptr<Vec3Editor>& widget : this->widgets)
86
4bec0525ef1b PolygonObjectEditor can now modify the object properly
Teemu Piippo <teemu@hecknology.net>
parents: 84
diff changeset
74 {
89
7abaf1d64719 object editing
Teemu Piippo <teemu@hecknology.net>
parents: 86
diff changeset
75 const QString label = widget->property(LABEL_NAME).toString();
178
a23024fc98e0 fix saving
Teemu Piippo <teemu@hecknology.net>
parents: 153
diff changeset
76 this->formLayout->addRow(label, widget.get());
89
7abaf1d64719 object editing
Teemu Piippo <teemu@hecknology.net>
parents: 86
diff changeset
77 }
178
a23024fc98e0 fix saving
Teemu Piippo <teemu@hecknology.net>
parents: 153
diff changeset
78 this->formLayout->addRow("", &*this->splitter);
89
7abaf1d64719 object editing
Teemu Piippo <teemu@hecknology.net>
parents: 86
diff changeset
79 }
7abaf1d64719 object editing
Teemu Piippo <teemu@hecknology.net>
parents: 86
diff changeset
80
7abaf1d64719 object editing
Teemu Piippo <teemu@hecknology.net>
parents: 86
diff changeset
81 void PolygonObjectEditor::setupPointWidget(int n)
7abaf1d64719 object editing
Teemu Piippo <teemu@hecknology.net>
parents: 86
diff changeset
82 {
178
a23024fc98e0 fix saving
Teemu Piippo <teemu@hecknology.net>
parents: 153
diff changeset
83 std::unique_ptr<Vec3Editor> editor = std::make_unique<Vec3Editor>(glm::vec3{}, this);
a23024fc98e0 fix saving
Teemu Piippo <teemu@hecknology.net>
parents: 153
diff changeset
84 QObject::connect(editor.get(), &Vec3Editor::valueChanged, this, &PolygonObjectEditor::pointChanged);
a23024fc98e0 fix saving
Teemu Piippo <teemu@hecknology.net>
parents: 153
diff changeset
85 editor->setProperty(INDEX_NAME, QVariant::fromValue(n));
a23024fc98e0 fix saving
Teemu Piippo <teemu@hecknology.net>
parents: 153
diff changeset
86 editor->setProperty(LABEL_NAME, &ldraw::traits(ldraw::pointProperty(n)).name[0]);
a23024fc98e0 fix saving
Teemu Piippo <teemu@hecknology.net>
parents: 153
diff changeset
87 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
88 }
4bec0525ef1b PolygonObjectEditor can now modify the object properly
Teemu Piippo <teemu@hecknology.net>
parents: 84
diff changeset
89
89
7abaf1d64719 object editing
Teemu Piippo <teemu@hecknology.net>
parents: 86
diff changeset
90 void PolygonObjectEditor::pointChanged(const glm::vec3& value)
81
62373840e33a object editor widgets start to form up
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
91 {
152
03f8e6d42e13 Major refactoring
Teemu Piippo <teemu@hecknology.net>
parents: 100
diff changeset
92 std::unique_ptr<ModelEditor> modelEditor = this->document->editModel();
89
7abaf1d64719 object editing
Teemu Piippo <teemu@hecknology.net>
parents: 86
diff changeset
93 const int n = this->sender()->property(INDEX_NAME).toInt();
7abaf1d64719 object editing
Teemu Piippo <teemu@hecknology.net>
parents: 86
diff changeset
94 const ldraw::Property property = ldraw::pointProperty(n);
152
03f8e6d42e13 Major refactoring
Teemu Piippo <teemu@hecknology.net>
parents: 100
diff changeset
95 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
96 }

mercurial