src/widgets/matrixeditor.cpp

changeset 85
40e2940605a3
child 112
5760cbb32bc0
equal deleted inserted replaced
84:7137c20979af 85:40e2940605a3
1 #include "main.h"
2 #include "matrixeditor.h"
3 #include "ui_matrixeditor.h"
4 #include "../ui/multiplyfactordialog.h"
5
6 constexpr char BUTTON_COLUMN_PROPERTY[] = "_ldforge_column";
7
8 MatrixEditor::MatrixEditor(const glm::mat4 value, QWidget* parent) :
9 QWidget(parent),
10 ui(new Ui::MatrixEditor)
11 {
12 ui->setupUi(this);
13 for (int column = 0; column < countof(this->spinboxes); column += 1)
14 {
15 for (int row = 0; row < countof(this->spinboxes[0]); row += 1)
16 {
17 const QString name = "cell"_q + QString::number(column) + QString::number(row);
18 QDoubleSpinBox** spinbox = &this->spinboxes[column][row];
19 *spinbox = this->findChild<QDoubleSpinBox*>(name);
20 connect(*spinbox, qOverload<double>(&QDoubleSpinBox::valueChanged), [&]()
21 {
22 emit this->valueChanged(this->value());
23 });
24 Q_ASSERT(*spinbox != nullptr);
25 }
26 QAbstractButton* button = this->findChild<QAbstractButton*>("multiply"_q + QString::number(column));
27 button->setProperty(BUTTON_COLUMN_PROPERTY, column);
28 connect(button, &QAbstractButton::clicked, this, &MatrixEditor::multiplyButtonPressed);
29 }
30 this->setValue(value);
31 }
32
33 MatrixEditor::MatrixEditor(QWidget *parent) :
34 MatrixEditor{glm::mat4{1}, parent}
35 {
36 }
37
38 MatrixEditor::~MatrixEditor()
39 {
40 delete ui;
41 }
42
43 glm::mat4 MatrixEditor::value() const
44 {
45 glm::mat4 result{1};
46 for (int column = 0; column < countof(this->spinboxes); column += 1)
47 {
48 for (int row = 0; row < countof(this->spinboxes[0]); row += 1)
49 {
50 result[column][row] = this->spinboxes[column][row]->value();
51 }
52 }
53 return result;
54 }
55
56 void MatrixEditor::setValue(const glm::mat4& value)
57 {
58 for (int column = 0; column < countof(this->spinboxes); column += 1)
59 {
60 for (int row = 0; row < countof(this->spinboxes[0]); row += 1)
61 {
62 QDoubleSpinBox* spinbox = this->spinboxes[column][row];
63 QSignalBlocker blocker{spinbox};
64 spinbox->setValue(value[column][row]);
65 }
66 }
67 }
68
69 void MatrixEditor::multiplyButtonPressed()
70 {
71 QAbstractButton* button = qobject_cast<QAbstractButton*>(this->sender());
72 if (button != nullptr)
73 {
74 bool ok;
75 const int column = button->property(BUTTON_COLUMN_PROPERTY).toInt(&ok);
76 if (ok and column >= 0 and column < this->matrixSize())
77 {
78 glm::mat4 newValue = this->value();
79 MultiplyFactorDialog dialog{newValue[column], this};
80 const int result = dialog.exec();
81 if (result == QDialog::Accepted)
82 {
83 newValue[column] = glm::vec4{dialog.value(), (column == 3) ? 1 : 0};
84 this->setValue(newValue);
85 emit valueChanged(newValue);
86 }
87 }
88 }
89 }

mercurial