src/ui/circletooloptionswidget.cpp

Tue, 28 Jun 2022 12:18:00 +0300

author
Teemu Piippo <teemu.s.piippo@gmail.com>
date
Tue, 28 Jun 2022 12:18:00 +0300
changeset 282
f2dc3bbecbfa
parent 264
76a025db4948
child 379
8d88adffb779
permissions
-rw-r--r--

Make settings editor a sub window instead of a dialog

#include "src/ui/circletooloptionswidget.h"

static QString circularPrimitiveTypeName(CircularPrimitive::Type type)
{
	switch (type) {
	case CircularPrimitive::Circle:
		return CircleToolOptionsWidget::tr("Circle");
	case CircularPrimitive::Disc:
		return CircleToolOptionsWidget::tr("Disc");
	case CircularPrimitive::Cylinder:
		return CircleToolOptionsWidget::tr("Cylinder");
	case CircularPrimitive::CylinderOpen:
		return CircleToolOptionsWidget::tr("Cylinder open");
	case CircularPrimitive::CylinderClosed:
		return CircleToolOptionsWidget::tr("Cylinder closed");
	case CircularPrimitive::DiscNegative:
		return CircleToolOptionsWidget::tr("Disc negative");
	case CircularPrimitive::Chord:
		return CircleToolOptionsWidget::tr("Chord");
	}
	return "";
}

CircleToolOptionsWidget::CircleToolOptionsWidget(QWidget *parent) :
	QWidget{parent}
{
	this->ui.setupUi(this);
	for (int i = 0; i < CircularPrimitive::NUM_TYPES; ++i) {
		const auto type = static_cast<CircularPrimitive::Type>(i);
		this->ui.type->addItem(circularPrimitiveTypeName(type), type);
	}
	connect(ui.segments, qOverload<int>(&QSpinBox::valueChanged), this, &CircleToolOptionsWidget::handleInputChange);
	connect(ui.divisions, &QComboBox::currentTextChanged, this, &CircleToolOptionsWidget::handleInputChange);
	connect(ui.type, &QComboBox::currentTextChanged, this, &CircleToolOptionsWidget::handleInputChange);
}

CircleToolOptionsWidget::~CircleToolOptionsWidget()
{
}

unsigned int CircleToolOptionsWidget::segments() const
{
	return unsigned_cast(this->ui.segments->value());
}

unsigned int CircleToolOptionsWidget::divisions() const
{
	bool ok;
	const unsigned int divs = this->ui.divisions->currentText().toUInt(&ok);
	if (ok) {
		return divs;
	}
	else {
		return 16;
	}
}

CircularPrimitive::Type CircleToolOptionsWidget::type() const
{
	return this->ui.type->currentData().value<CircularPrimitive::Type>();
}

void CircleToolOptionsWidget::handleInputChange()
{
	const unsigned int olddivs = unsigned_cast(this->ui.segments->maximum());
	const unsigned int newdivs = this->divisions();
	if (olddivs != newdivs) {
		this->ui.segments->setMaximum(signed_cast(newdivs));
		this->ui.segments->setValue(signed_cast(this->segments() * newdivs / olddivs));
	}
	const qreal ratio = static_cast<qreal>(this->segments()) / newdivs;
	this->ui.ratio->setText(QString::number(ratio, 'g', 4));
	Q_EMIT this->optionsChanged(CircleToolOptions{
		.fraction = CircularFraction{this->segments(), this->divisions()},
		.type = this->type(),
	});
}

mercurial