src/widgets/circularsectioneditor.cpp

Fri, 21 Dec 2018 21:43:06 +0200

author
Teemu Piippo <teemu@hecknology.net>
date
Fri, 21 Dec 2018 21:43:06 +0200
changeset 1431
2e0e2c696605
parent 1417
ed39bfca7a67
permissions
-rw-r--r--

things

#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<int>(&QSpinBox::valueChanged), this, &CircularSectionEditor::segmentsChanged);
	connect(ui.divisions, qOverload<const QString&>(&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<int>(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());
}

mercurial