src/ui/multiplyfactordialog.cpp

changeset 252
da4876bfd822
parent 251
94b0a30a1886
child 253
8b994c917f69
equal deleted inserted replaced
251:94b0a30a1886 252:da4876bfd822
1 #include "multiplyfactordialog.h"
2 #include "ui_multiplyfactordialog.h"
3
4 MultiplyFactorDialog::MultiplyFactorDialog(const glm::vec3& baseVector, QWidget* parent) :
5 QDialog{parent},
6 baseVector{baseVector},
7 preview{baseVector, parent, Vec3Editor::NoMultiplyButton}
8 {
9 ui = std::make_unique<Ui::MultiplyFactorDialog>();
10 ui->setupUi(this);
11 this->preview.setEnabled(false);
12 this->ui->previewGroupBox->setLayout(new QVBoxLayout{parent});
13 this->ui->previewGroupBox->layout()->addWidget(&this->preview);
14 connect(
15 this->ui->invert,
16 &QCheckBox::clicked,
17 this,
18 &MultiplyFactorDialog::updatePreview);
19 connect(
20 this->ui->factor,
21 qOverload<double>(&DoubleSpinBox::valueChanged),
22 this,
23 &MultiplyFactorDialog::updatePreview);
24 }
25
26 /**
27 * @brief empty destructor, necessary because std::unique_ptr is used with a forward declaration
28 */
29 MultiplyFactorDialog::~MultiplyFactorDialog()
30 {
31 }
32
33 /**
34 * @brief Computes the resulting vector
35 * @return the input vector multiplied by the specified vector
36 */
37 glm::vec3 MultiplyFactorDialog::value() const
38 {
39 glm::vec3 result = baseVector;
40 if (this->ui->invert->isChecked())
41 {
42 if (qFuzzyIsNull(this->ui->factor->value()))
43 {
44 constexpr double infinity = std::numeric_limits<double>::quiet_NaN();
45 result = {infinity, infinity, infinity};
46 }
47 else
48 {
49 result /= this->ui->factor->value();
50 }
51 }
52 else
53 {
54 result *= this->ui->factor->value();
55 }
56 return result;
57 }
58
59 /**
60 * @brief Makes a string that is prefixed to the factor input.
61 * @param ui
62 * @return prefix string
63 */
64 QString prefixForFactorInput(const Ui::MultiplyFactorDialog& ui)
65 {
66 if (ui.invert->isChecked())
67 {
68 return "1 : ";
69 }
70 else
71 {
72 return "";
73 }
74 }
75
76 /**
77 * @brief Makes a string that is suffixed to the factor input.
78 * @param ui
79 * @return prefix string
80 */
81 QString suffixForFactorInput(const Ui::MultiplyFactorDialog& ui)
82 {
83 if (ui.invert->isChecked())
84 {
85 // render the actual factor that stuff gets effectively multiplied by
86 return " = " + QString::number(1.0 / (ui.factor->value()));
87 }
88 else
89 {
90 return "";
91 }
92 }
93
94 /**
95 * @brief Responds to changes in the value and updates previews accordingly
96 */
97 void MultiplyFactorDialog::updatePreview()
98 {
99 this->ui->factor->setPrefix(::prefixForFactorInput(*this->ui));
100 this->ui->factor->setSuffix(::suffixForFactorInput(*this->ui));
101 this->preview.setValue(this->value());
102 }

mercurial