# HG changeset patch # User Teemu Piippo # Date 1529595963 -10800 # Node ID ed39bfca7a6756de97285153a5c7f541e36d0c65 # Parent ba63c7286767af5773db5f24b01d4b6868b44cc2 refactored the segments/divisions editor in MainWindow to a new widget diff -r ba63c7286767 -r ed39bfca7a67 CMakeLists.txt --- a/CMakeLists.txt Thu Jun 21 17:02:58 2018 +0300 +++ b/CMakeLists.txt Thu Jun 21 18:46:03 2018 +0300 @@ -95,6 +95,7 @@ src/toolsets/viewtoolset.cpp src/types/boundingbox.cpp src/types/vertex.cpp + src/widgets/circularsectioneditor.cpp src/widgets/colorbutton.cpp src/widgets/doublespinbox.cpp src/widgets/headeredit.cpp @@ -176,6 +177,7 @@ src/types/boundingbox.h src/types/library.h src/types/vertex.h + src/widgets/circularsectioneditor.h src/widgets/colorbutton.h src/widgets/doublespinbox.h src/widgets/headeredit.h @@ -206,6 +208,7 @@ src/toolsets/fixroundingerrors.ui src/mainwindow.ui src/partdownloader.ui + src/widgets/circularsectioneditor.ui src/widgets/vertexobjecteditor.ui src/widgets/headeredit.ui src/widgets/matrixeditor.ui diff -r ba63c7286767 -r ed39bfca7a67 src/basics.h --- a/src/basics.h Thu Jun 21 17:02:58 2018 +0300 +++ b/src/basics.h Thu Jun 21 18:46:03 2018 +0300 @@ -59,6 +59,12 @@ Clockwise, }; +struct CircularSection +{ + int segments = 16; + int divisions = 16; +}; + /* * Special operator definition that implements the XOR operator for windings. * However, if either winding is NoWinding, then this function returns NoWinding. diff -r ba63c7286767 -r ed39bfca7a67 src/editmodes/circleMode.cpp --- a/src/editmodes/circleMode.cpp Thu Jun 21 17:02:58 2018 +0300 +++ b/src/editmodes/circleMode.cpp Thu Jun 21 18:46:03 2018 +0300 @@ -89,8 +89,7 @@ void CircleMode::endDraw() { Model model {m_documents}; - int segments = m_window->ringToolSegments(); - int divisions = m_window->ringToolDivisions(); + CircularSection section = m_window->circleToolSection(); double dist0 = getCircleDrawDist(0); double dist1 = getCircleDrawDist(1); QVector3D translation = m_drawedVerts.first().toVector(); @@ -105,7 +104,7 @@ QMatrix4x4 transform = renderer()->currentCamera().transformationMatrix(1); transform.scale(dist0); transform.translate(translation); - model.emplace(PrimitiveModel::Circle, segments, divisions, transform); + model.emplace(PrimitiveModel::Circle, section.segments, section.divisions, transform); finishDraw(model); return; } @@ -116,7 +115,7 @@ QMatrix4x4 transform = renderer()->currentCamera().transformationMatrix(1); transform.scale(max(dist0, dist1)); transform.translate(translation); - model.emplace(PrimitiveModel::Disc, segments, divisions, transform); + model.emplace(PrimitiveModel::Disc, section.segments, section.divisions, transform); finishDraw(model); return; } @@ -124,8 +123,9 @@ { // The ring finder found a solution, use that. Add the component rings to the file. PrimitiveModel primitiveModel; - primitiveModel.segments = m_window->ringToolSegments(); - primitiveModel.divisions = m_window->ringToolDivisions(); + CircularSection section = m_window->circleToolSection(); + primitiveModel.segments = section.segments; + primitiveModel.divisions = section.divisions; primitiveModel.type = PrimitiveModel::Ring; for (const RingFinder::Component& component : g_RingFinder.bestSolution()->getComponents()) @@ -151,10 +151,10 @@ templ.setCoordinate(localy, y0); // Calculate circle coords - QVector c0 = makeCircle(segments, divisions, dist0); - QVector c1 = makeCircle(segments, divisions, dist1); + QVector c0 = makeCircle(section.segments, section.divisions, dist0); + QVector c1 = makeCircle(section.segments, section.divisions, dist1); - for (int i = 0; i < segments; ++i) + for (int i = 0; i < section.segments; ++i) { Vertex v0, v1, v2, v3; v0 = v1 = v2 = v3 = templ; @@ -191,7 +191,7 @@ { if (not m_drawedVerts.isEmpty()) { - int divisions = m_window->ringToolDivisions(); + int divisions = m_window->circleToolSection().divisions; QPointF originSpot = renderer()->currentCamera().convert3dTo2d(m_drawedVerts.first()); // Line from the origin of the circle to current mouse position QLineF hand1 = {originSpot, renderer()->mousePositionF()}; @@ -227,15 +227,14 @@ QVector innerverts2d, outerverts2d; double innerdistance = getCircleDrawDist(0); double outerdistance = countof(m_drawedVerts) >= 2 ? getCircleDrawDist (1) : -1; - int divisions = m_window->ringToolDivisions(); - int segments = m_window->ringToolSegments(); - double angleUnit = 2 * pi / divisions; + CircularSection section = m_window->circleToolSection(); + double angleUnit = 2 * pi / section.divisions; Axis relX, relY; renderer()->getRelativeAxes(relX, relY); double angleoffset = (countof(m_drawedVerts) < 3 ? orientation() : m_angleOffset); // Calculate the preview positions of vertices - for (int i = 0; i < segments + 1; ++i) + for (int i = 0; i < section.segments + 1; ++i) { const double sinangle = ldrawsin(angleoffset + i * angleUnit); const double cosangle = ldrawcos(angleoffset + i * angleUnit); @@ -260,10 +259,10 @@ { painter.setBrush(m_polybrush); painter.setPen(Qt::NoPen); - lines.reserve(segments * 2); + lines.reserve(section.segments * 2); // Compile polygons - for (int i = 0; i < segments; ++i) + for (int i = 0; i < section.segments; ++i) { QVector points; points << innerverts2d[i] @@ -276,7 +275,7 @@ } // Add bordering edges for unclosed rings/discs - if (segments != divisions) + if (section.segments != section.divisions) { lines.append({innerverts2d.first(), outerverts2d.first()}); lines.append({innerverts2d.last(), outerverts2d.last()}); @@ -284,9 +283,9 @@ } else { - lines.reserve(segments); + lines.reserve(section.segments); - for (int i = 0; i < segments; ++i) + for (int i = 0; i < section.segments; ++i) lines.append({innerverts2d[i], innerverts2d[i + 1]}); } diff -r ba63c7286767 -r ed39bfca7a67 src/mainwindow.cpp --- a/src/mainwindow.cpp Thu Jun 21 17:02:58 2018 +0300 +++ b/src/mainwindow.cpp Thu Jun 21 18:46:03 2018 +0300 @@ -111,9 +111,7 @@ updateTitle(); loadShortcuts(); setMinimumSize (300, 200); - connect(ui.ringToolDivisions, SIGNAL(currentTextChanged(QString)), this, SLOT(ringToolDivisionsChanged())); - connect(ui.ringToolSegments, SIGNAL(valueChanged(int)), this, SLOT(circleToolSegmentsChanged())); - circleToolSegmentsChanged(); // invoke it manually for initial label text + connect(ui.circleToolSection, &CircularSectionEditor::sectionChanged, [&](){this->renderer()->update();}); // Examine the toolsets and make a dictionary of tools m_toolsets = Toolset::createToolsets (this); @@ -796,41 +794,9 @@ return m_defaultShortcuts[act]; } -int MainWindow::ringToolDivisions() const -{ - return ui.ringToolDivisions->currentText().toInt(); -} - -// --------------------------------------------------------------------------------------------------------------------- -// -int MainWindow::ringToolSegments() const -{ - return ui.ringToolSegments->value(); -} - -// --------------------------------------------------------------------------------------------------------------------- -// -void MainWindow::ringToolDivisionsChanged() +CircularSection MainWindow::circleToolSection() const { - // Scale the segments value to fit. - int divisions = this->ringToolDivisions(); - int newSegments = static_cast(round( - this->ringToolSegments() * double(divisions) / this->previousDivisions - )); - this->ui.ringToolSegments->setMaximum(divisions); - this->ui.ringToolSegments->setValue(newSegments); - this->previousDivisions = divisions; - this->renderer()->update(); -} - -// --------------------------------------------------------------------------------------------------------------------- -// -void MainWindow::circleToolSegmentsChanged() -{ - int numerator = this->ringToolSegments(); - int denominator = this->ringToolDivisions(); - simplify (numerator, denominator); - ui.ringToolSegmentsLabel->setText(fractionRep(numerator, denominator)); + return ui.circleToolSection->section(); } // --------------------------------------------------------------------------------------------------------------------- diff -r ba63c7286767 -r ed39bfca7a67 src/mainwindow.h --- a/src/mainwindow.h Thu Jun 21 17:02:58 2018 +0300 +++ b/src/mainwindow.h Thu Jun 21 18:46:03 2018 +0300 @@ -83,8 +83,7 @@ Canvas* renderer(); void refresh(); void replaceSelection(const QItemSelection& selection); - int ringToolDivisions() const; - int ringToolSegments() const; + CircularSection circleToolSection() const; bool save (LDDocument* doc, bool saveAs); void saveShortcuts(); void select(const QModelIndex& objectIndex); @@ -108,10 +107,8 @@ public slots: void actionTriggered(); - void circleToolSegmentsChanged(); void closeTab (int tabindex); void historyTraversed(); - void ringToolDivisionsChanged(); void tabSelected(); void documentClosed(LDDocument* document); void updateTitle(); diff -r ba63c7286767 -r ed39bfca7a67 src/mainwindow.ui --- a/src/mainwindow.ui Thu Jun 21 17:02:58 2018 +0300 +++ b/src/mainwindow.ui Thu Jun 21 18:46:03 2018 +0300 @@ -40,15 +40,15 @@ - 1 + 2 0 0 - 926 - 336 + 100 + 30 @@ -71,7 +71,7 @@ 0 0 926 - 336 + 366 @@ -102,7 +102,7 @@ 0 0 926 - 336 + 366 @@ -114,78 +114,13 @@ Circle Tool Options - - - - - - - Divisions: - - - - - - - Segments: - - - - - - - - - true - - - 1 - - - 16 - - - 16 - - - - - - - a / b - - - - - - - - - 1 - - - - 8 - - - - - 16 - - - - - 48 - - - - - - - + + + + Qt::Vertical @@ -205,8 +140,8 @@ 0 0 - 926 - 336 + 93 + 93 @@ -239,7 +174,7 @@ 0 0 1010 - 31 + 26 @@ -1789,10 +1724,15 @@
widgets/headeredit.h
1 + + CircularSectionEditor + QWidget +
widgets/circularsectioneditor.h
+ 1 +
- diff -r ba63c7286767 -r ed39bfca7a67 src/widgets/circularsectioneditor.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/widgets/circularsectioneditor.cpp Thu Jun 21 18:46:03 2018 +0300 @@ -0,0 +1,68 @@ +#include "circularsectioneditor.h" +#include "ui_circularsectioneditor.h" +#include "../primitives.h" + +CircularSectionEditor::CircularSectionEditor(QWidget* parent) : + QWidget {parent}, + ui {*new Ui_CircularSectionEditor} +{ + ui.setupUi(this); + ui.divisions->setValidator(new QIntValidator {1, INT_MAX}); + connect(ui.segments, qOverload(&QSpinBox::valueChanged), this, &CircularSectionEditor::segmentsChanged); + connect(ui.divisions, qOverload(&QComboBox::activated), this, &CircularSectionEditor::divisionsChanged); + previousDivisions = ui.divisions->currentText().toInt(); + updateFractionLabel(); +} + +CircularSectionEditor::~CircularSectionEditor() +{ + delete &ui; +} + +CircularSection CircularSectionEditor::section() +{ + return {ui.segments->value(), ui.divisions->currentText().toInt()}; +} + +void CircularSectionEditor::setSection(const CircularSection& newSection) +{ + ui.divisions->setCurrentText(QString::number(newSection.divisions)); + ui.segments->setValue(newSection.segments); +} + +void CircularSectionEditor::updateFractionLabel() +{ + CircularSection section = this->section(); + int numerator = section.segments; + int denominator = section.divisions; + simplify(numerator, denominator); + ui.fraction->setText(fractionRep(numerator, denominator)); +} + +void CircularSectionEditor::divisionsChanged() +{ + // Scale the segments value to fit. + int divisions = ui.divisions->currentText().toInt(); + + if (divisions <= 0) + { + ui.divisions->setCurrentText(QString::number(1)); + } + else + { + int newSegments = static_cast(round( + ui.segments->value() * double(divisions) / this->previousDivisions + )); + ui.segments->setMaximum(divisions); + ui.segments->setValue(newSegments); + previousDivisions = divisions; + } + + emit sectionChanged(section()); +} + +void CircularSectionEditor::segmentsChanged() +{ + updateFractionLabel(); + emit sectionChanged(section()); +} diff -r ba63c7286767 -r ed39bfca7a67 src/widgets/circularsectioneditor.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/widgets/circularsectioneditor.h Thu Jun 21 18:46:03 2018 +0300 @@ -0,0 +1,26 @@ +#pragma once +#include +#include "../basics.h" + +class CircularSectionEditor : public QWidget +{ + Q_OBJECT + +public: + explicit CircularSectionEditor(QWidget *parent = 0); + ~CircularSectionEditor(); + + CircularSection section(); + void setSection(const CircularSection& newSection); + +signals: + void sectionChanged(CircularSection); + +private: + class Ui_CircularSectionEditor& ui; + int previousDivisions; + + Q_SLOT void updateFractionLabel(); + Q_SLOT void segmentsChanged(); + Q_SLOT void divisionsChanged(); +}; diff -r ba63c7286767 -r ed39bfca7a67 src/widgets/circularsectioneditor.ui --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/widgets/circularsectioneditor.ui Thu Jun 21 18:46:03 2018 +0300 @@ -0,0 +1,84 @@ + + + CircularSectionEditor + + + + 0 + 0 + 207 + 86 + + + + Form + + + + + + Segments: + + + + + + + 1 + + + 16 + + + 16 + + + + + + + Divisions: + + + + + + + + + true + + + 1 + + + + 8 + + + + + 16 + + + + + 48 + + + + + + + + 1 / 1 + + + + + + + + + +