src/widgets/circularsectioneditor.cpp

Sat, 04 Aug 2018 21:46:58 +0300

author
Teemu Piippo <teemu@hecknology.net>
date
Sat, 04 Aug 2018 21:46:58 +0300
changeset 1428
ece049033adc
parent 1417
ed39bfca7a67
permissions
-rw-r--r--

fixed a crash when trying to open a document for the 3rd time after closing it 2 times

#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