src/ui/circletooloptionswidget.cpp

Thu, 15 Jun 2023 16:18:03 +0300

author
Teemu Piippo <teemu.s.piippo@gmail.com>
date
Thu, 15 Jun 2023 16:18:03 +0300
changeset 383
530d23cd4e97
parent 379
8d88adffb779
permissions
-rw-r--r--

Refactor, make selecting elements from the model select the corresponding line from the editor as well

#include "src/ui/circletooloptionswidget.h"

static QString circularPrimitiveTypeName(circular_primitive_type_e type)
{
	switch (type) {
	case circular_primitive_type_e::circle:
		return CircleToolOptionsWidget::tr("Circle");
	case circular_primitive_type_e::disc:
		return CircleToolOptionsWidget::tr("Disc");
	case circular_primitive_type_e::cylinder:
		return CircleToolOptionsWidget::tr("Cylinder");
	case circular_primitive_type_e::cylinder_open:
		return CircleToolOptionsWidget::tr("Cylinder open");
	case circular_primitive_type_e::cylinder_closed:
		return CircleToolOptionsWidget::tr("Cylinder closed");
	case circular_primitive_type_e::disc_negative:
		return CircleToolOptionsWidget::tr("Disc negative");
	case circular_primitive_type_e::chord:
		return CircleToolOptionsWidget::tr("Chord");
	}
	return "";
}

CircleToolOptionsWidget::CircleToolOptionsWidget(QWidget *parent) :
	QWidget{parent}
{
	this->ui.setupUi(this);
	for (int i = 0; i < circular_primitive::NUM_TYPES; ++i) {
		const auto type = static_cast<circular_primitive_type_e>(i);
		this->ui.type->addItem(circularPrimitiveTypeName(type), QVariant::fromValue(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;
	}
}

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

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