src/widgets/matrixeditor.cpp

changeset 252
da4876bfd822
parent 251
94b0a30a1886
child 253
8b994c917f69
equal deleted inserted replaced
251:94b0a30a1886 252:da4876bfd822
1 #include "basics.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 = QStringLiteral("cell%1%2").arg(column).arg(row);
18 QDoubleSpinBox** spinbox = &this->spinboxes[column][row];
19 *spinbox = this->findChild<QDoubleSpinBox*>(name);
20 connect(*spinbox, qOverload<double>(&QDoubleSpinBox::valueChanged), [&]()
21 {
22 Q_EMIT this->valueChanged(this->value());
23 });
24 Q_ASSERT(*spinbox != nullptr);
25 }
26 const QString multiplyButtonName = QStringLiteral("multiply%1").arg(column);
27 QAbstractButton* button = this->findChild<QAbstractButton*>(multiplyButtonName);
28 button->setProperty(BUTTON_COLUMN_PROPERTY, column);
29 connect(button, &QAbstractButton::clicked, this, &MatrixEditor::multiplyButtonPressed);
30 }
31 this->setValue(value);
32 }
33
34 MatrixEditor::MatrixEditor(QWidget *parent) :
35 MatrixEditor{glm::mat4{1}, parent}
36 {
37 }
38
39 MatrixEditor::~MatrixEditor()
40 {
41 delete ui;
42 }
43
44 glm::mat4 MatrixEditor::value() const
45 {
46 glm::mat4 result{1};
47 for (int column = 0; column < countof(this->spinboxes); column += 1)
48 {
49 for (int row = 0; row < countof(this->spinboxes[0]); row += 1)
50 {
51 result[column][row] = narrow<float>(this->spinboxes[column][row]->value());
52 }
53 }
54 return result;
55 }
56
57 void MatrixEditor::setValue(const glm::mat4& value)
58 {
59 for (int column = 0; column < countof(this->spinboxes); column += 1)
60 {
61 for (int row = 0; row < countof(this->spinboxes[0]); row += 1)
62 {
63 QDoubleSpinBox* spinbox = this->spinboxes[column][row];
64 QSignalBlocker blocker{spinbox};
65 spinbox->setValue(value[column][row]);
66 }
67 }
68 }
69
70 void MatrixEditor::multiplyButtonPressed()
71 {
72 QAbstractButton* button = qobject_cast<QAbstractButton*>(this->sender());
73 if (button != nullptr)
74 {
75 bool ok;
76 const int column = button->property(BUTTON_COLUMN_PROPERTY).toInt(&ok);
77 if (ok and column >= 0 and column < this->matrixSize())
78 {
79 glm::mat4 newValue = this->value();
80 MultiplyFactorDialog dialog{newValue[column], this};
81 const int result = dialog.exec();
82 if (result == QDialog::Accepted)
83 {
84 newValue[column] = glm::vec4{dialog.value(), (column == 3) ? 1 : 0};
85 this->setValue(newValue);
86 Q_EMIT valueChanged(newValue);
87 }
88 }
89 }
90 }

mercurial