src/widgets/circularsectioneditor.cpp

Thu, 20 Jun 2019 08:54:35 +0300

author
Teemu Piippo <teemu@hecknology.net>
date
Thu, 20 Jun 2019 08:54:35 +0300
changeset 1440
265b2e95a8e8
parent 1417
ed39bfca7a67
permissions
-rw-r--r--

uuid 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