widgets/vec3editor.cpp

Fri, 01 Jul 2022 23:51:16 +0300

author
Teemu Piippo <teemu.s.piippo@gmail.com>
date
Fri, 01 Jul 2022 23:51:16 +0300
changeset 314
4642ba1218e8
parent 264
76a025db4948
permissions
-rw-r--r--

Added rudimentary cylinder extrusion into circle tool.
I really had to think which way the vectors are in this one

#include <QDialog>
#include <QCheckBox>
#include <QSignalBlocker>
#include "widgets/vec3editor.h"
#include "widgets/multiplyfactordialog.h"

VectorInput::VectorInput(const glm::vec3& value, QWidget* parent, QFlags<VectorInput::Flag> flags) :
	VectorInput{parent, flags}
{
	this->setValue(value);
}

VectorInput::VectorInput(QWidget *parent, QFlags<Flag> flags) :
	QWidget{parent}
{
	this->ui.setupUi(this);
	if (flags.testFlag(NoMultiplyButton)) {
		this->ui.multiply->setVisible(false);
	}
	else {
		connect(this->ui.multiply, &QPushButton::clicked, this, &VectorInput::multiplyPressed);
	}
	for (QDoubleSpinBox* spinbox : this->spinboxes()) {
		connect(spinbox, qOverload<double>(&QDoubleSpinBox::valueChanged), [&](double){
			Q_EMIT this->valueChanged(this->value());
		});
	}
}

VectorInput::~VectorInput()
{
}

glm::vec3 VectorInput::value() const
{
	auto get = [](DoubleSpinBox* spinbox){ return static_cast<float>(spinbox->value()); };
	return {get(this->ui.x), get(this->ui.y), get(this->ui.z)};
}

void VectorInput::setValue(const glm::vec3& value)
{
	auto set = [](DoubleSpinBox* spinbox, float value){
		QSignalBlocker blocker{spinbox};
		spinbox->setValue(static_cast<qreal>(value));
	};
	set(this->ui.x, value.x);
	set(this->ui.y, value.y);
	set(this->ui.z, value.z);
	Q_EMIT this->valueChanged(value);
}

qreal VectorInput::x() const
{
	return this->ui.x->value();
}

qreal VectorInput::y() const
{
	return this->ui.y->value();
}

qreal VectorInput::z() const
{
	return this->ui.z->value();
}

void VectorInput::setX(qreal x)
{
	this->ui.x->setValue(x);
}

void VectorInput::setY(qreal y)
{
	this->ui.y->setValue(y);
}

void VectorInput::setZ(qreal z)
{
	this->ui.z->setValue(z);
}

std::array<DoubleSpinBox*, 3> VectorInput::spinboxes()
{
	return {this->ui.x, this->ui.y, this->ui.z};
}

void VectorInput::multiplyPressed()
{
	MultiplyFactorDialog dialog{this->value(), this};
	const int dialogResult = dialog.exec();
	if (dialogResult == QDialog::Accepted) {
		this->setValue(dialog.value());
	}
}

mercurial