widgets/multiplyfactordialog.cpp

Sun, 03 Jul 2022 22:24:12 +0300

author
Teemu Piippo <teemu.s.piippo@gmail.com>
date
Sun, 03 Jul 2022 22:24:12 +0300
changeset 326
65dbfcb859a7
parent 264
76a025db4948
permissions
-rw-r--r--

Add missing 0 BFC INVERTNEXT when writing out subfile references

#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