Sat, 26 May 2018 17:28:15 +0300
refactored signal blocking
--- a/src/basics.cpp Thu May 10 15:54:26 2018 +0300 +++ b/src/basics.cpp Sat May 26 17:28:15 2018 +0300 @@ -180,3 +180,15 @@ { return superscript(numerator) + "⁄" + subscript(denominator); } + +/* + * Temporarily blocks the signals of `object` and calls `function`. Returns whatever `function` + * returns. Assumes that the function does not change the signal blocking status of `object`. + */ +void withSignalsBlocked(QObject* object, std::function<void()> function) +{ + bool wasBlockingSignals = object->signalsBlocked(); + object->blockSignals(true); + function(); + object->blockSignals(wasBlockingSignals); +}
--- a/src/basics.h Thu May 10 15:54:26 2018 +0300 +++ b/src/basics.h Sat May 26 17:28:15 2018 +0300 @@ -100,3 +100,4 @@ QString superscript(int number); QString subscript(int number); QString fractionRep(int numerator, int denominator); +void withSignalsBlocked(QObject* object, std::function<void()> function);
--- a/src/dialogs/subfilereferenceeditor.cpp Thu May 10 15:54:26 2018 +0300 +++ b/src/dialogs/subfilereferenceeditor.cpp Sat May 26 17:28:15 2018 +0300 @@ -49,9 +49,10 @@ { QLayoutItem* item = this->ui.matrixLayout->itemAtPosition(i, j); QDoubleSpinBox* spinbox = item ? qobject_cast<QDoubleSpinBox*>(item->widget()) : nullptr; - spinbox->blockSignals(true); - spinbox->setValue(reference->transformationMatrix()(i, j)); - spinbox->blockSignals(false); + withSignalsBlocked(spinbox, [&]() + { + spinbox->setValue(reference->transformationMatrix()(i, j)); + }); connect( spinbox, qOverload<double>(&QDoubleSpinBox::valueChanged), @@ -86,9 +87,10 @@ for (int column : {0, 1, 2}) { QDoubleSpinBox* spinbox = this->vectorElement(column); - spinbox->blockSignals(true); - spinbox->setValue(this->matrixScaling(column)); - spinbox->blockSignals(false); + withSignalsBlocked(spinbox, [&]() + { + spinbox->setValue(this->matrixScaling(column)); + }); } } @@ -188,9 +190,10 @@ double cellValue = this->matrixCell(row, column)->value(); cellValue *= newScaling / oldScaling; QDoubleSpinBox* cellWidget = this->matrixCell(row, column); - cellWidget->blockSignals(true); - cellWidget->setValue(cellValue); - cellWidget->blockSignals(false); + withSignalsBlocked(cellWidget, [&]() + { + cellWidget->setValue(cellValue); + }); } } @@ -225,9 +228,10 @@ { int column = this->cellPosition(cellWidget).second; QDoubleSpinBox* spinbox = this->vectorElement(column); - spinbox->blockSignals(true); - spinbox->setValue(this->matrixScaling(column)); - spinbox->blockSignals(false); + withSignalsBlocked(spinbox, [&]() + { + spinbox->setValue(this->matrixScaling(column)); + }); } catch (const std::out_of_range&) {} }