|
1 #include "matrixeditor.h" |
|
2 #include "ui_matrixeditor.h" |
|
3 #include "multiplyfactordialog.h" |
|
4 |
|
5 constexpr char BUTTON_COLUMN_PROPERTY[] = "_ldforge_column"; |
|
6 |
|
7 MatrixEditor::MatrixEditor(const glm::mat4 value, QWidget* parent) : |
|
8 QWidget(parent), |
|
9 ui(new Ui::MatrixEditor) |
|
10 { |
|
11 ui->setupUi(this); |
|
12 for (int column = 0; column < (int)glm::countof(this->spinboxes); column += 1) |
|
13 { |
|
14 for (int row = 0; row < (int)glm::countof(this->spinboxes[0]); row += 1) |
|
15 { |
|
16 const QString name = QStringLiteral("cell%1%2").arg(column).arg(row); |
|
17 QDoubleSpinBox** spinbox = &this->spinboxes[column][row]; |
|
18 *spinbox = this->findChild<QDoubleSpinBox*>(name); |
|
19 connect(*spinbox, qOverload<double>(&QDoubleSpinBox::valueChanged), [&]() |
|
20 { |
|
21 Q_EMIT this->valueChanged(this->value()); |
|
22 }); |
|
23 Q_ASSERT(*spinbox != nullptr); |
|
24 } |
|
25 const QString multiplyButtonName = QStringLiteral("multiply%1").arg(column); |
|
26 QAbstractButton* button = this->findChild<QAbstractButton*>(multiplyButtonName); |
|
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 < (int)glm::countof(this->spinboxes); column += 1) |
|
47 { |
|
48 for (int row = 0; row < (int)glm::countof(this->spinboxes[0]); row += 1) |
|
49 { |
|
50 result[column][row] = static_cast<float>(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 < (int)glm::countof(this->spinboxes); column += 1) |
|
59 { |
|
60 for (int row = 0; row < (int)glm::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 Q_EMIT valueChanged(newValue); |
|
86 } |
|
87 } |
|
88 } |
|
89 } |