widgets/vec3editor.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 264
76a025db4948
permissions
-rw-r--r--

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

#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