src/dialogs/subfilereferenceeditor.cpp

changeset 1391
5fa4bf1fc781
parent 1389
3fb8ad4d27b1
child 1392
0541d9b21968
--- a/src/dialogs/subfilereferenceeditor.cpp	Wed May 30 22:31:06 2018 +0300
+++ b/src/dialogs/subfilereferenceeditor.cpp	Mon Jun 04 23:12:40 2018 +0300
@@ -30,11 +30,10 @@
 {
 	this->ui.setupUi(this);
 	this->ui.referenceName->setText(reference->referenceName());
+	this->ui.matrixEditor->setPosition(reference->position());
+	this->ui.matrixEditor->setMatrix(reference->transformationMatrix());
 	this->color = reference->color();
 	::setColorButton(this->ui.colorButton, this->color);
-	this->ui.positionX->setValue(reference->position().x);
-	this->ui.positionY->setValue(reference->position().y);
-	this->ui.positionZ->setValue(reference->position().z);
 	connect(
 		this->ui.colorButton,
 		&QPushButton::clicked,
@@ -44,22 +43,6 @@
 				::setColorButton(this->ui.colorButton, this->color);
 		}
 	);
-	for (int i : {0, 1, 2})
-	for (int j : {0, 1, 2})
-	{
-		QLayoutItem* item = this->ui.matrixLayout->itemAtPosition(i, j);
-		QDoubleSpinBox* spinbox = item ? qobject_cast<QDoubleSpinBox*>(item->widget()) : nullptr;
-		withSignalsBlocked(spinbox, [&]()
-		{
-			spinbox->setValue(reference->transformationMatrix()(i, j));
-		});
-		connect(
-			spinbox,
-			qOverload<double>(&QDoubleSpinBox::valueChanged),
-			this,
-			&SubfileReferenceEditor::matrixChanged
-		);
-	}
 	connect(
 		this->ui.primitivesTreeView,
 		&QTreeView::clicked,
@@ -72,26 +55,6 @@
 				this->ui.referenceName->setText(primitiveName.toString());
 		}
 	);
-
-	for (QDoubleSpinBox* spinbox : {this->ui.scalingX, this->ui.scalingY, this->ui.scalingZ})
-	{
-		connect(
-			spinbox,
-			qOverload<double>(&QDoubleSpinBox::valueChanged),
-			this,
-			&SubfileReferenceEditor::scalingChanged
-		);
-	}
-
-	// Fill in the initial scaling values
-	for (int column : {0, 1, 2})
-	{
-		QDoubleSpinBox* spinbox = this->vectorElement(column);
-		withSignalsBlocked(spinbox, [&]()
-		{
-			spinbox->setValue(this->matrixScaling(column));
-		});
-	}
 }
 
 SubfileReferenceEditor::~SubfileReferenceEditor()
@@ -99,61 +62,12 @@
 	delete &this->ui;
 }
 
-/*
- * Returns a spinbox from the matrix grid at position (row, column).
- * Row and column must be within [0, 2].
- */
-QDoubleSpinBox* SubfileReferenceEditor::matrixCell(int row, int column) const
-{
-	if (qBound(0, row, 2) != row or qBound(0, column, 2) != column)
-	{
-		throw std::out_of_range {"bad row and column values"};
-	}
-	else
-	{
-		QLayoutItem* item = this->ui.matrixLayout->itemAtPosition(row, column);
-		return item ? qobject_cast<QDoubleSpinBox*>(item->widget()) : nullptr;
-	}
-}
-
-/*
- * Returns a spinbox for the vector element at the given position
- * Index must be within [0, 2]
- */
-QDoubleSpinBox* SubfileReferenceEditor::vectorElement(int index)
-{
-	switch (index)
-	{
-	case 0:
-		return this->ui.scalingX;
-
-	case 1:
-		return this->ui.scalingY;
-
-	case 2:
-		return this->ui.scalingZ;
-
-	default:
-		throw std::out_of_range {"bad index"};
-	}
-}
-
 void SubfileReferenceEditor::accept()
 {
 	this->reference->setReferenceName(this->ui.referenceName->text());
-	Matrix transformationMatrix;
-	for (int i : {0, 1, 2})
-	for (int j : {0, 1, 2})
-	{
-		transformationMatrix(i, j) = this->matrixCell(i, j)->value();
-	}
-	this->reference->setTransformationMatrix(transformationMatrix);
-	this->reference->setPosition({
-		this->ui.positionX->value(),
-		this->ui.positionY->value(),
-		this->ui.positionZ->value()
-	});
 	this->reference->setColor(this->color);
+	this->reference->setTransformationMatrix(this->ui.matrixEditor->matrix());
+	this->reference->setPosition(this->ui.matrixEditor->position());
 	QDialog::accept();
 }
 
@@ -161,77 +75,3 @@
 {
 	this->ui.primitivesTreeView->setModel(primitives);
 }
-
-double SubfileReferenceEditor::matrixScaling(int column) const
-{
-	return sqrt(
-		pow(this->matrixCell(0, column)->value(), 2) +
-		pow(this->matrixCell(1, column)->value(), 2) +
-		pow(this->matrixCell(2, column)->value(), 2)
-	);
-}
-
-/*
- * Updates the appropriate matrix column when a scaling vector element is changed.
- */
-void SubfileReferenceEditor::scalingChanged()
-{
-	for (int column : {0, 1, 2})
-	{
-		if (this->sender() == this->vectorElement(column))
-		{
-			double oldScaling = this->matrixScaling(column);
-			double newScaling = static_cast<QDoubleSpinBox*>(this->sender())->value();
-
-			if (not qFuzzyCompare(newScaling, 0.0))
-			{
-				for (int row : {0, 1, 2})
-				{
-					double cellValue = this->matrixCell(row, column)->value();
-					cellValue *= newScaling / oldScaling;
-					QDoubleSpinBox* cellWidget = this->matrixCell(row, column);
-					withSignalsBlocked(cellWidget, [&]()
-					{
-						cellWidget->setValue(cellValue);
-					});
-				}
-			}
-
-			break;
-		}
-	}
-}
-
-/*
- * Finds the position for the given cell widget.
- */
-QPair<int, int> SubfileReferenceEditor::cellPosition(QDoubleSpinBox* cellWidget)
-{
-	for (int row : {0, 1, 2})
-	for (int column : {0, 1, 2})
-	{
-		if (this->matrixCell(row, column) == cellWidget)
-			return {row, column};
-	}
-
-	throw std::out_of_range {"widget is not in the matrix"};
-}
-
-/*
- * Updates the appropriate scaling vector element when a matrix cell is changed.
- */
-void SubfileReferenceEditor::matrixChanged()
-{
-	QDoubleSpinBox* cellWidget = static_cast<QDoubleSpinBox*>(this->sender());
-
-	try
-	{
-		int column = this->cellPosition(cellWidget).second;
-		QDoubleSpinBox* spinbox = this->vectorElement(column);
-		withSignalsBlocked(spinbox, [&]()
-		{
-			spinbox->setValue(this->matrixScaling(column));
-		});
-	}
-	catch (const std::out_of_range&) {}
-}

mercurial