Fri, 21 Dec 2018 21:43:06 +0200
things
1417
ed39bfca7a67
refactored the segments/divisions editor in MainWindow to a new widget
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
1 | #include "circularsectioneditor.h" |
ed39bfca7a67
refactored the segments/divisions editor in MainWindow to a new widget
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
2 | #include "ui_circularsectioneditor.h" |
ed39bfca7a67
refactored the segments/divisions editor in MainWindow to a new widget
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
3 | #include "../primitives.h" |
ed39bfca7a67
refactored the segments/divisions editor in MainWindow to a new widget
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
4 | |
ed39bfca7a67
refactored the segments/divisions editor in MainWindow to a new widget
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
5 | CircularSectionEditor::CircularSectionEditor(QWidget* parent) : |
ed39bfca7a67
refactored the segments/divisions editor in MainWindow to a new widget
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
6 | QWidget {parent}, |
ed39bfca7a67
refactored the segments/divisions editor in MainWindow to a new widget
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
7 | ui {*new Ui_CircularSectionEditor} |
ed39bfca7a67
refactored the segments/divisions editor in MainWindow to a new widget
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
8 | { |
ed39bfca7a67
refactored the segments/divisions editor in MainWindow to a new widget
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
9 | ui.setupUi(this); |
ed39bfca7a67
refactored the segments/divisions editor in MainWindow to a new widget
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
10 | ui.divisions->setValidator(new QIntValidator {1, INT_MAX}); |
ed39bfca7a67
refactored the segments/divisions editor in MainWindow to a new widget
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
11 | connect(ui.segments, qOverload<int>(&QSpinBox::valueChanged), this, &CircularSectionEditor::segmentsChanged); |
ed39bfca7a67
refactored the segments/divisions editor in MainWindow to a new widget
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
12 | connect(ui.divisions, qOverload<const QString&>(&QComboBox::activated), this, &CircularSectionEditor::divisionsChanged); |
ed39bfca7a67
refactored the segments/divisions editor in MainWindow to a new widget
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
13 | previousDivisions = ui.divisions->currentText().toInt(); |
ed39bfca7a67
refactored the segments/divisions editor in MainWindow to a new widget
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
14 | updateFractionLabel(); |
ed39bfca7a67
refactored the segments/divisions editor in MainWindow to a new widget
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
15 | } |
ed39bfca7a67
refactored the segments/divisions editor in MainWindow to a new widget
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
16 | |
ed39bfca7a67
refactored the segments/divisions editor in MainWindow to a new widget
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
17 | CircularSectionEditor::~CircularSectionEditor() |
ed39bfca7a67
refactored the segments/divisions editor in MainWindow to a new widget
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
18 | { |
ed39bfca7a67
refactored the segments/divisions editor in MainWindow to a new widget
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
19 | delete &ui; |
ed39bfca7a67
refactored the segments/divisions editor in MainWindow to a new widget
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
20 | } |
ed39bfca7a67
refactored the segments/divisions editor in MainWindow to a new widget
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
21 | |
ed39bfca7a67
refactored the segments/divisions editor in MainWindow to a new widget
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
22 | CircularSection CircularSectionEditor::section() |
ed39bfca7a67
refactored the segments/divisions editor in MainWindow to a new widget
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
23 | { |
ed39bfca7a67
refactored the segments/divisions editor in MainWindow to a new widget
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
24 | return {ui.segments->value(), ui.divisions->currentText().toInt()}; |
ed39bfca7a67
refactored the segments/divisions editor in MainWindow to a new widget
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
25 | } |
ed39bfca7a67
refactored the segments/divisions editor in MainWindow to a new widget
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
26 | |
ed39bfca7a67
refactored the segments/divisions editor in MainWindow to a new widget
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
27 | void CircularSectionEditor::setSection(const CircularSection& newSection) |
ed39bfca7a67
refactored the segments/divisions editor in MainWindow to a new widget
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
28 | { |
ed39bfca7a67
refactored the segments/divisions editor in MainWindow to a new widget
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
29 | ui.divisions->setCurrentText(QString::number(newSection.divisions)); |
ed39bfca7a67
refactored the segments/divisions editor in MainWindow to a new widget
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
30 | ui.segments->setValue(newSection.segments); |
ed39bfca7a67
refactored the segments/divisions editor in MainWindow to a new widget
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
31 | } |
ed39bfca7a67
refactored the segments/divisions editor in MainWindow to a new widget
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
32 | |
ed39bfca7a67
refactored the segments/divisions editor in MainWindow to a new widget
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
33 | void CircularSectionEditor::updateFractionLabel() |
ed39bfca7a67
refactored the segments/divisions editor in MainWindow to a new widget
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
34 | { |
ed39bfca7a67
refactored the segments/divisions editor in MainWindow to a new widget
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
35 | CircularSection section = this->section(); |
ed39bfca7a67
refactored the segments/divisions editor in MainWindow to a new widget
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
36 | int numerator = section.segments; |
ed39bfca7a67
refactored the segments/divisions editor in MainWindow to a new widget
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
37 | int denominator = section.divisions; |
ed39bfca7a67
refactored the segments/divisions editor in MainWindow to a new widget
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
38 | simplify(numerator, denominator); |
ed39bfca7a67
refactored the segments/divisions editor in MainWindow to a new widget
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
39 | ui.fraction->setText(fractionRep(numerator, denominator)); |
ed39bfca7a67
refactored the segments/divisions editor in MainWindow to a new widget
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
40 | } |
ed39bfca7a67
refactored the segments/divisions editor in MainWindow to a new widget
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
41 | |
ed39bfca7a67
refactored the segments/divisions editor in MainWindow to a new widget
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
42 | void CircularSectionEditor::divisionsChanged() |
ed39bfca7a67
refactored the segments/divisions editor in MainWindow to a new widget
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
43 | { |
ed39bfca7a67
refactored the segments/divisions editor in MainWindow to a new widget
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
44 | // Scale the segments value to fit. |
ed39bfca7a67
refactored the segments/divisions editor in MainWindow to a new widget
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
45 | int divisions = ui.divisions->currentText().toInt(); |
ed39bfca7a67
refactored the segments/divisions editor in MainWindow to a new widget
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
46 | |
ed39bfca7a67
refactored the segments/divisions editor in MainWindow to a new widget
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
47 | if (divisions <= 0) |
ed39bfca7a67
refactored the segments/divisions editor in MainWindow to a new widget
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
48 | { |
ed39bfca7a67
refactored the segments/divisions editor in MainWindow to a new widget
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
49 | ui.divisions->setCurrentText(QString::number(1)); |
ed39bfca7a67
refactored the segments/divisions editor in MainWindow to a new widget
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
50 | } |
ed39bfca7a67
refactored the segments/divisions editor in MainWindow to a new widget
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
51 | else |
ed39bfca7a67
refactored the segments/divisions editor in MainWindow to a new widget
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
52 | { |
ed39bfca7a67
refactored the segments/divisions editor in MainWindow to a new widget
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
53 | int newSegments = static_cast<int>(round( |
ed39bfca7a67
refactored the segments/divisions editor in MainWindow to a new widget
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
54 | ui.segments->value() * double(divisions) / this->previousDivisions |
ed39bfca7a67
refactored the segments/divisions editor in MainWindow to a new widget
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
55 | )); |
ed39bfca7a67
refactored the segments/divisions editor in MainWindow to a new widget
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
56 | ui.segments->setMaximum(divisions); |
ed39bfca7a67
refactored the segments/divisions editor in MainWindow to a new widget
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
57 | ui.segments->setValue(newSegments); |
ed39bfca7a67
refactored the segments/divisions editor in MainWindow to a new widget
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
58 | previousDivisions = divisions; |
ed39bfca7a67
refactored the segments/divisions editor in MainWindow to a new widget
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
59 | } |
ed39bfca7a67
refactored the segments/divisions editor in MainWindow to a new widget
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
60 | |
ed39bfca7a67
refactored the segments/divisions editor in MainWindow to a new widget
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
61 | emit sectionChanged(section()); |
ed39bfca7a67
refactored the segments/divisions editor in MainWindow to a new widget
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
62 | } |
ed39bfca7a67
refactored the segments/divisions editor in MainWindow to a new widget
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
63 | |
ed39bfca7a67
refactored the segments/divisions editor in MainWindow to a new widget
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
64 | void CircularSectionEditor::segmentsChanged() |
ed39bfca7a67
refactored the segments/divisions editor in MainWindow to a new widget
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
65 | { |
ed39bfca7a67
refactored the segments/divisions editor in MainWindow to a new widget
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
66 | updateFractionLabel(); |
ed39bfca7a67
refactored the segments/divisions editor in MainWindow to a new widget
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
67 | emit sectionChanged(section()); |
ed39bfca7a67
refactored the segments/divisions editor in MainWindow to a new widget
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
68 | } |