Wed, 12 Apr 2023 01:53:42 +0300
Circular primitive type is now an enum class
264
76a025db4948
Convert all includes to be relative to project root directory. Files that cannot be found in this manner use angle brackets.
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
250
diff
changeset
|
1 | #include "src/ui/circletooloptionswidget.h" |
233 | 2 | |
379
8d88adffb779
Circular primitive type is now an enum class
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
264
diff
changeset
|
3 | static QString circularPrimitiveTypeName(circular_primitive_type_e type) |
233 | 4 | { |
5 | switch (type) { | |
379
8d88adffb779
Circular primitive type is now an enum class
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
264
diff
changeset
|
6 | case circular_primitive_type_e::circle: |
233 | 7 | return CircleToolOptionsWidget::tr("Circle"); |
379
8d88adffb779
Circular primitive type is now an enum class
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
264
diff
changeset
|
8 | case circular_primitive_type_e::disc: |
233 | 9 | return CircleToolOptionsWidget::tr("Disc"); |
379
8d88adffb779
Circular primitive type is now an enum class
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
264
diff
changeset
|
10 | case circular_primitive_type_e::cylinder: |
233 | 11 | return CircleToolOptionsWidget::tr("Cylinder"); |
379
8d88adffb779
Circular primitive type is now an enum class
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
264
diff
changeset
|
12 | case circular_primitive_type_e::cylinder_open: |
233 | 13 | return CircleToolOptionsWidget::tr("Cylinder open"); |
379
8d88adffb779
Circular primitive type is now an enum class
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
264
diff
changeset
|
14 | case circular_primitive_type_e::cylinder_closed: |
233 | 15 | return CircleToolOptionsWidget::tr("Cylinder closed"); |
379
8d88adffb779
Circular primitive type is now an enum class
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
264
diff
changeset
|
16 | case circular_primitive_type_e::disc_negative: |
233 | 17 | return CircleToolOptionsWidget::tr("Disc negative"); |
379
8d88adffb779
Circular primitive type is now an enum class
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
264
diff
changeset
|
18 | case circular_primitive_type_e::chord: |
233 | 19 | return CircleToolOptionsWidget::tr("Chord"); |
20 | } | |
21 | return ""; | |
22 | } | |
23 | ||
24 | CircleToolOptionsWidget::CircleToolOptionsWidget(QWidget *parent) : | |
25 | QWidget{parent} | |
26 | { | |
27 | this->ui.setupUi(this); | |
379
8d88adffb779
Circular primitive type is now an enum class
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
264
diff
changeset
|
28 | for (int i = 0; i < circular_primitive::NUM_TYPES; ++i) { |
8d88adffb779
Circular primitive type is now an enum class
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
264
diff
changeset
|
29 | const auto type = static_cast<circular_primitive_type_e>(i); |
8d88adffb779
Circular primitive type is now an enum class
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
264
diff
changeset
|
30 | this->ui.type->addItem(circularPrimitiveTypeName(type), QVariant::fromValue(type)); |
233 | 31 | } |
32 | connect(ui.segments, qOverload<int>(&QSpinBox::valueChanged), this, &CircleToolOptionsWidget::handleInputChange); | |
33 | connect(ui.divisions, &QComboBox::currentTextChanged, this, &CircleToolOptionsWidget::handleInputChange); | |
34 | connect(ui.type, &QComboBox::currentTextChanged, this, &CircleToolOptionsWidget::handleInputChange); | |
35 | } | |
36 | ||
37 | CircleToolOptionsWidget::~CircleToolOptionsWidget() | |
38 | { | |
39 | } | |
40 | ||
41 | unsigned int CircleToolOptionsWidget::segments() const | |
42 | { | |
250
2837b549e616
I felt that the compiler was too kind to me, so I enabled a big pile of warnings
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
233
diff
changeset
|
43 | return unsigned_cast(this->ui.segments->value()); |
233 | 44 | } |
45 | ||
46 | unsigned int CircleToolOptionsWidget::divisions() const | |
47 | { | |
48 | bool ok; | |
250
2837b549e616
I felt that the compiler was too kind to me, so I enabled a big pile of warnings
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
233
diff
changeset
|
49 | const unsigned int divs = this->ui.divisions->currentText().toUInt(&ok); |
233 | 50 | if (ok) { |
51 | return divs; | |
52 | } | |
53 | else { | |
54 | return 16; | |
55 | } | |
56 | } | |
57 | ||
379
8d88adffb779
Circular primitive type is now an enum class
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
264
diff
changeset
|
58 | circular_primitive_type_e CircleToolOptionsWidget::type() const |
233 | 59 | { |
379
8d88adffb779
Circular primitive type is now an enum class
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
264
diff
changeset
|
60 | return this->ui.type->currentData().value<circular_primitive_type_e>(); |
233 | 61 | } |
62 | ||
63 | void CircleToolOptionsWidget::handleInputChange() | |
64 | { | |
250
2837b549e616
I felt that the compiler was too kind to me, so I enabled a big pile of warnings
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
233
diff
changeset
|
65 | const unsigned int olddivs = unsigned_cast(this->ui.segments->maximum()); |
2837b549e616
I felt that the compiler was too kind to me, so I enabled a big pile of warnings
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
233
diff
changeset
|
66 | const unsigned int newdivs = this->divisions(); |
233 | 67 | if (olddivs != newdivs) { |
250
2837b549e616
I felt that the compiler was too kind to me, so I enabled a big pile of warnings
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
233
diff
changeset
|
68 | this->ui.segments->setMaximum(signed_cast(newdivs)); |
2837b549e616
I felt that the compiler was too kind to me, so I enabled a big pile of warnings
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
233
diff
changeset
|
69 | this->ui.segments->setValue(signed_cast(this->segments() * newdivs / olddivs)); |
233 | 70 | } |
71 | const qreal ratio = static_cast<qreal>(this->segments()) / newdivs; | |
72 | this->ui.ratio->setText(QString::number(ratio, 'g', 4)); | |
73 | Q_EMIT this->optionsChanged(CircleToolOptions{ | |
74 | .fraction = CircularFraction{this->segments(), this->divisions()}, | |
75 | .type = this->type(), | |
76 | }); | |
77 | } |