widgets/vec3editor.cpp

Sun, 09 Apr 2023 15:59:08 +0300

author
Teemu Piippo <teemu.s.piippo@gmail.com>
date
Sun, 09 Apr 2023 15:59:08 +0300
changeset 362
e1d646a4cbd8
parent 264
76a025db4948
permissions
-rw-r--r--

Extracted the state of the program into a MainState structure, and extracted local functions of main() into static functions.
I was planning to make the core logic and state of the program into a Main class, which would be a QObject that would
have lots of signals and slots, but it looks like this works even without it

#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