widgets/multiplyfactordialog.cpp

Sun, 26 Jun 2022 21:32:51 +0300

author
Teemu Piippo <teemu.s.piippo@gmail.com>
date
Sun, 26 Jun 2022 21:32:51 +0300
changeset 264
76a025db4948
parent 254
b7b29cb82360
permissions
-rw-r--r--

Convert all includes to be relative to project root directory. Files that cannot be found in this manner use angle brackets.

#include <ui_multiplyfactordialog.h>
#include "widgets/multiplyfactordialog.h"

MultiplyFactorDialog::MultiplyFactorDialog(const glm::vec3& baseVector, QWidget* parent) :
	QDialog{parent},
	baseVector{baseVector},
	preview{baseVector, parent, VectorInput::NoMultiplyButton}
{
	ui = std::make_unique<Ui::MultiplyFactorDialog>();
	ui->setupUi(this);
	this->preview.setEnabled(false);
	this->ui->previewGroupBox->setLayout(new QVBoxLayout{parent});
	this->ui->previewGroupBox->layout()->addWidget(&this->preview);
	connect(
		this->ui->invert,
		&QCheckBox::clicked,
		this,
		&MultiplyFactorDialog::updatePreview);
	connect(
		this->ui->factor,
		qOverload<double>(&DoubleSpinBox::valueChanged),
		this,
		&MultiplyFactorDialog::updatePreview);
}

/**
 * @brief empty destructor, necessary because std::unique_ptr is used with a forward declaration
 */
MultiplyFactorDialog::~MultiplyFactorDialog()
{
}

/**
 * @brief Computes the resulting vector
 * @return the input vector multiplied by the specified vector
 */
glm::vec3 MultiplyFactorDialog::value() const
{
	glm::vec3 result = baseVector;
	if (this->ui->invert->isChecked())
	{
		if (qFuzzyIsNull(this->ui->factor->value()))
		{
			constexpr double infinity = std::numeric_limits<double>::quiet_NaN();
			result = {infinity, infinity, infinity};
		}
		else
		{
			result /= this->ui->factor->value();
		}
	}
	else
	{
		result *= this->ui->factor->value();
	}
	return result;
}

/**
 * @brief Makes a string that is prefixed to the factor input.
 * @param ui
 * @return prefix string
 */
QString prefixForFactorInput(const Ui::MultiplyFactorDialog& ui)
{
	if (ui.invert->isChecked())
	{
		return "1 : ";
	}
	else
	{
		return "";
	}
}

/**
 * @brief Makes a string that is suffixed to the factor input.
 * @param ui
 * @return prefix string
 */
QString suffixForFactorInput(const Ui::MultiplyFactorDialog& ui)
{
	if (ui.invert->isChecked())
	{
		// render the actual factor that stuff gets effectively multiplied by
		return " = " + QString::number(1.0 / (ui.factor->value()));
	}
	else
	{
		return "";
	}
}

/**
 * @brief Responds to changes in the value and updates previews accordingly
 */
void MultiplyFactorDialog::updatePreview()
{
	this->ui->factor->setPrefix(::prefixForFactorInput(*this->ui));
	this->ui->factor->setSuffix(::suffixForFactorInput(*this->ui));
	this->preview.setValue(this->value());
}

mercurial