src/widgets/matrixeditor.cpp

Wed, 08 Jun 2022 22:29:44 +0300

author
Teemu Piippo <teemu@hecknology.net>
date
Wed, 08 Jun 2022 22:29:44 +0300
changeset 206
654661eab7f3
parent 112
5760cbb32bc0
child 250
2837b549e616
permissions
-rw-r--r--

More refactor, merged main.h, basics.h and utility.h into one header file basics.h and removed plenty of unused code

206
654661eab7f3 More refactor, merged main.h, basics.h and utility.h into one header file basics.h and removed plenty of unused code
Teemu Piippo <teemu@hecknology.net>
parents: 112
diff changeset
1 #include "basics.h"
85
40e2940605a3 added a matrix editing widget
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
2 #include "matrixeditor.h"
40e2940605a3 added a matrix editing widget
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
3 #include "ui_matrixeditor.h"
40e2940605a3 added a matrix editing widget
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
4 #include "../ui/multiplyfactordialog.h"
40e2940605a3 added a matrix editing widget
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
5
40e2940605a3 added a matrix editing widget
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
6 constexpr char BUTTON_COLUMN_PROPERTY[] = "_ldforge_column";
40e2940605a3 added a matrix editing widget
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
7
40e2940605a3 added a matrix editing widget
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
8 MatrixEditor::MatrixEditor(const glm::mat4 value, QWidget* parent) :
40e2940605a3 added a matrix editing widget
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
9 QWidget(parent),
40e2940605a3 added a matrix editing widget
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
10 ui(new Ui::MatrixEditor)
40e2940605a3 added a matrix editing widget
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
11 {
40e2940605a3 added a matrix editing widget
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
12 ui->setupUi(this);
40e2940605a3 added a matrix editing widget
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
13 for (int column = 0; column < countof(this->spinboxes); column += 1)
40e2940605a3 added a matrix editing widget
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
14 {
40e2940605a3 added a matrix editing widget
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
15 for (int row = 0; row < countof(this->spinboxes[0]); row += 1)
40e2940605a3 added a matrix editing widget
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
16 {
206
654661eab7f3 More refactor, merged main.h, basics.h and utility.h into one header file basics.h and removed plenty of unused code
Teemu Piippo <teemu@hecknology.net>
parents: 112
diff changeset
17 const QString name = QStringLiteral("cell%1%2").arg(column).arg(row);
85
40e2940605a3 added a matrix editing widget
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
18 QDoubleSpinBox** spinbox = &this->spinboxes[column][row];
40e2940605a3 added a matrix editing widget
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
19 *spinbox = this->findChild<QDoubleSpinBox*>(name);
40e2940605a3 added a matrix editing widget
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
20 connect(*spinbox, qOverload<double>(&QDoubleSpinBox::valueChanged), [&]()
40e2940605a3 added a matrix editing widget
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
21 {
112
5760cbb32bc0 use QT_NO_KEYWORDS
Teemu Piippo <teemu@hecknology.net>
parents: 85
diff changeset
22 Q_EMIT this->valueChanged(this->value());
85
40e2940605a3 added a matrix editing widget
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
23 });
40e2940605a3 added a matrix editing widget
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
24 Q_ASSERT(*spinbox != nullptr);
40e2940605a3 added a matrix editing widget
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
25 }
206
654661eab7f3 More refactor, merged main.h, basics.h and utility.h into one header file basics.h and removed plenty of unused code
Teemu Piippo <teemu@hecknology.net>
parents: 112
diff changeset
26 const QString multiplyButtonName = QStringLiteral("multiply%1").arg(column);
654661eab7f3 More refactor, merged main.h, basics.h and utility.h into one header file basics.h and removed plenty of unused code
Teemu Piippo <teemu@hecknology.net>
parents: 112
diff changeset
27 QAbstractButton* button = this->findChild<QAbstractButton*>(multiplyButtonName);
85
40e2940605a3 added a matrix editing widget
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
28 button->setProperty(BUTTON_COLUMN_PROPERTY, column);
40e2940605a3 added a matrix editing widget
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
29 connect(button, &QAbstractButton::clicked, this, &MatrixEditor::multiplyButtonPressed);
40e2940605a3 added a matrix editing widget
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
30 }
40e2940605a3 added a matrix editing widget
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
31 this->setValue(value);
40e2940605a3 added a matrix editing widget
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
32 }
40e2940605a3 added a matrix editing widget
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
33
40e2940605a3 added a matrix editing widget
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
34 MatrixEditor::MatrixEditor(QWidget *parent) :
40e2940605a3 added a matrix editing widget
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
35 MatrixEditor{glm::mat4{1}, parent}
40e2940605a3 added a matrix editing widget
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
36 {
40e2940605a3 added a matrix editing widget
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
37 }
40e2940605a3 added a matrix editing widget
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
38
40e2940605a3 added a matrix editing widget
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
39 MatrixEditor::~MatrixEditor()
40e2940605a3 added a matrix editing widget
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
40 {
40e2940605a3 added a matrix editing widget
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
41 delete ui;
40e2940605a3 added a matrix editing widget
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
42 }
40e2940605a3 added a matrix editing widget
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
43
40e2940605a3 added a matrix editing widget
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
44 glm::mat4 MatrixEditor::value() const
40e2940605a3 added a matrix editing widget
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
45 {
40e2940605a3 added a matrix editing widget
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
46 glm::mat4 result{1};
40e2940605a3 added a matrix editing widget
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
47 for (int column = 0; column < countof(this->spinboxes); column += 1)
40e2940605a3 added a matrix editing widget
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
48 {
40e2940605a3 added a matrix editing widget
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
49 for (int row = 0; row < countof(this->spinboxes[0]); row += 1)
40e2940605a3 added a matrix editing widget
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
50 {
40e2940605a3 added a matrix editing widget
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
51 result[column][row] = this->spinboxes[column][row]->value();
40e2940605a3 added a matrix editing widget
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
52 }
40e2940605a3 added a matrix editing widget
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
53 }
40e2940605a3 added a matrix editing widget
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
54 return result;
40e2940605a3 added a matrix editing widget
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
55 }
40e2940605a3 added a matrix editing widget
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
56
40e2940605a3 added a matrix editing widget
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
57 void MatrixEditor::setValue(const glm::mat4& value)
40e2940605a3 added a matrix editing widget
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
58 {
40e2940605a3 added a matrix editing widget
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
59 for (int column = 0; column < countof(this->spinboxes); column += 1)
40e2940605a3 added a matrix editing widget
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
60 {
40e2940605a3 added a matrix editing widget
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
61 for (int row = 0; row < countof(this->spinboxes[0]); row += 1)
40e2940605a3 added a matrix editing widget
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
62 {
40e2940605a3 added a matrix editing widget
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
63 QDoubleSpinBox* spinbox = this->spinboxes[column][row];
40e2940605a3 added a matrix editing widget
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
64 QSignalBlocker blocker{spinbox};
40e2940605a3 added a matrix editing widget
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
65 spinbox->setValue(value[column][row]);
40e2940605a3 added a matrix editing widget
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
66 }
40e2940605a3 added a matrix editing widget
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
67 }
40e2940605a3 added a matrix editing widget
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
68 }
40e2940605a3 added a matrix editing widget
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
69
40e2940605a3 added a matrix editing widget
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
70 void MatrixEditor::multiplyButtonPressed()
40e2940605a3 added a matrix editing widget
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
71 {
40e2940605a3 added a matrix editing widget
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
72 QAbstractButton* button = qobject_cast<QAbstractButton*>(this->sender());
40e2940605a3 added a matrix editing widget
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
73 if (button != nullptr)
40e2940605a3 added a matrix editing widget
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
74 {
40e2940605a3 added a matrix editing widget
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
75 bool ok;
40e2940605a3 added a matrix editing widget
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
76 const int column = button->property(BUTTON_COLUMN_PROPERTY).toInt(&ok);
40e2940605a3 added a matrix editing widget
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
77 if (ok and column >= 0 and column < this->matrixSize())
40e2940605a3 added a matrix editing widget
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
78 {
40e2940605a3 added a matrix editing widget
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
79 glm::mat4 newValue = this->value();
40e2940605a3 added a matrix editing widget
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
80 MultiplyFactorDialog dialog{newValue[column], this};
40e2940605a3 added a matrix editing widget
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
81 const int result = dialog.exec();
40e2940605a3 added a matrix editing widget
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
82 if (result == QDialog::Accepted)
40e2940605a3 added a matrix editing widget
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
83 {
40e2940605a3 added a matrix editing widget
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
84 newValue[column] = glm::vec4{dialog.value(), (column == 3) ? 1 : 0};
40e2940605a3 added a matrix editing widget
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
85 this->setValue(newValue);
112
5760cbb32bc0 use QT_NO_KEYWORDS
Teemu Piippo <teemu@hecknology.net>
parents: 85
diff changeset
86 Q_EMIT valueChanged(newValue);
85
40e2940605a3 added a matrix editing widget
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
87 }
40e2940605a3 added a matrix editing widget
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
88 }
40e2940605a3 added a matrix editing widget
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
89 }
40e2940605a3 added a matrix editing widget
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
90 }

mercurial