|
1 #include "circletooloptionswidget.h" |
|
2 |
|
3 static QString circularPrimitiveTypeName(CircularPrimitive::Type type) |
|
4 { |
|
5 switch (type) { |
|
6 case CircularPrimitive::Circle: |
|
7 return CircleToolOptionsWidget::tr("Circle"); |
|
8 case CircularPrimitive::Disc: |
|
9 return CircleToolOptionsWidget::tr("Disc"); |
|
10 case CircularPrimitive::Cylinder: |
|
11 return CircleToolOptionsWidget::tr("Cylinder"); |
|
12 case CircularPrimitive::CylinderOpen: |
|
13 return CircleToolOptionsWidget::tr("Cylinder open"); |
|
14 case CircularPrimitive::CylinderClosed: |
|
15 return CircleToolOptionsWidget::tr("Cylinder closed"); |
|
16 case CircularPrimitive::DiscNegative: |
|
17 return CircleToolOptionsWidget::tr("Disc negative"); |
|
18 case CircularPrimitive::Chord: |
|
19 return CircleToolOptionsWidget::tr("Chord"); |
|
20 } |
|
21 return ""; |
|
22 } |
|
23 |
|
24 CircleToolOptionsWidget::CircleToolOptionsWidget(QWidget *parent) : |
|
25 QWidget{parent} |
|
26 { |
|
27 this->ui.setupUi(this); |
|
28 for (int i = 0; i < CircularPrimitive::NUM_TYPES; ++i) { |
|
29 const auto type = static_cast<CircularPrimitive::Type>(i); |
|
30 this->ui.type->addItem(circularPrimitiveTypeName(type), type); |
|
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 { |
|
43 return this->ui.segments->value(); |
|
44 } |
|
45 |
|
46 unsigned int CircleToolOptionsWidget::divisions() const |
|
47 { |
|
48 bool ok; |
|
49 const int divs = this->ui.divisions->currentText().toInt(&ok); |
|
50 if (ok) { |
|
51 return divs; |
|
52 } |
|
53 else { |
|
54 return 16; |
|
55 } |
|
56 } |
|
57 |
|
58 CircularPrimitive::Type CircleToolOptionsWidget::type() const |
|
59 { |
|
60 return this->ui.type->currentData().value<CircularPrimitive::Type>(); |
|
61 } |
|
62 |
|
63 void CircleToolOptionsWidget::handleInputChange() |
|
64 { |
|
65 const int olddivs = this->ui.segments->maximum(); |
|
66 const int newdivs = this->divisions(); |
|
67 if (olddivs != newdivs) { |
|
68 this->ui.segments->setMaximum(newdivs); |
|
69 this->ui.segments->setValue(this->segments() * newdivs / olddivs); |
|
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 } |