|
1 #include <QDialog> |
|
2 #include <QCheckBox> |
|
3 #include <QSignalBlocker> |
|
4 #include "vec3editor.h" |
|
5 #include "ui_vec3editor.h" |
|
6 #include "multiplyfactordialog.h" |
|
7 |
|
8 Vec3Editor::Vec3Editor(const glm::vec3& value, QWidget *parent, QFlags<Flag> flags) : |
|
9 QWidget{parent}, |
|
10 ui{new Ui::Vec3Editor} |
|
11 { |
|
12 this->ui->setupUi(this); |
|
13 this->setValue(value); |
|
14 if (flags.testFlag(NoMultiplyButton)) |
|
15 { |
|
16 this->ui->multiply->setVisible(false); |
|
17 } |
|
18 else |
|
19 { |
|
20 connect(this->ui->multiply, &QPushButton::clicked, this, &Vec3Editor::multiplyPressed); |
|
21 } |
|
22 for (QDoubleSpinBox* spinbox : this->spinboxes()) |
|
23 { |
|
24 connect(spinbox, qOverload<double>(&QDoubleSpinBox::valueChanged), [&](double) |
|
25 { |
|
26 Q_EMIT this->valueChanged(this->value()); |
|
27 }); |
|
28 } |
|
29 } |
|
30 |
|
31 Vec3Editor::~Vec3Editor() |
|
32 { |
|
33 } |
|
34 |
|
35 glm::vec3 Vec3Editor::value() const |
|
36 { |
|
37 auto get = [](DoubleSpinBox* spinbox){ return static_cast<float>(spinbox->value()); }; |
|
38 return {get(this->ui->x), get(this->ui->y), get(this->ui->z)}; |
|
39 } |
|
40 |
|
41 void Vec3Editor::setValue(const glm::vec3& value) |
|
42 { |
|
43 auto set = [](DoubleSpinBox* spinbox, float value) |
|
44 { |
|
45 QSignalBlocker blocker{spinbox}; |
|
46 spinbox->setValue(static_cast<qreal>(value)); |
|
47 }; |
|
48 set(this->ui->x, value.x); |
|
49 set(this->ui->y, value.y); |
|
50 set(this->ui->z, value.z); |
|
51 Q_EMIT this->valueChanged(value); |
|
52 } |
|
53 |
|
54 std::array<DoubleSpinBox*, 3> Vec3Editor::spinboxes() |
|
55 { |
|
56 return {this->ui->x, this->ui->y, this->ui->z}; |
|
57 } |
|
58 |
|
59 void Vec3Editor::multiplyPressed() |
|
60 { |
|
61 MultiplyFactorDialog dialog{this->value(), this}; |
|
62 const int dialogResult = dialog.exec(); |
|
63 if (dialogResult == QDialog::Accepted) |
|
64 { |
|
65 this->setValue(dialog.value()); |
|
66 } |
|
67 } |