widgets/multiplyfactordialog.cpp

Fri, 01 Jul 2022 16:46:43 +0300

author
Teemu Piippo <teemu.s.piippo@gmail.com>
date
Fri, 01 Jul 2022 16:46:43 +0300
changeset 312
2637134bc37c
parent 264
76a025db4948
permissions
-rw-r--r--

Fix right click to delete not really working properly
Instead of removing the point that had been added, it would remove
the point that is being drawn, which would cause it to overwrite the
previous point using the new point, causing a bit of a delay

#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