From aebe5a42e6b6f4acb6edde667ed346beb95a207f Mon Sep 17 00:00:00 2001 From: Nyall Dawson Date: Tue, 10 Oct 2017 15:04:09 +1000 Subject: [PATCH] Allow reseting ratio manually for linked ratio lock buttons --- python/gui/qgsratiolockbutton.sip | 6 ++++ src/gui/qgsratiolockbutton.cpp | 6 ++++ src/gui/qgsratiolockbutton.h | 6 ++++ tests/src/python/test_qgsratiolockbutton.py | 31 +++++++++++++++++++++ 4 files changed, 49 insertions(+) diff --git a/python/gui/qgsratiolockbutton.sip b/python/gui/qgsratiolockbutton.sip index e73af56986d..89c5ec5fabf 100644 --- a/python/gui/qgsratiolockbutton.sip +++ b/python/gui/qgsratiolockbutton.sip @@ -67,6 +67,12 @@ class QgsRatioLockButton : QToolButton .. seealso:: setWidthSpinBox() %End + void resetRatio(); +%Docstring + Resets the current width/height ratio, taking the width and height + from the current values of the width and height spin boxes. +%End + signals: void lockChanged( const bool locked ); diff --git a/src/gui/qgsratiolockbutton.cpp b/src/gui/qgsratiolockbutton.cpp index 7af07597764..11a574dd86a 100644 --- a/src/gui/qgsratiolockbutton.cpp +++ b/src/gui/qgsratiolockbutton.cpp @@ -156,3 +156,9 @@ void QgsRatioLockButton::setHeightSpinBox( QDoubleSpinBox *widget ) mPrevHeight = widget->value(); connect( mHeightSpinBox, static_cast( &QDoubleSpinBox::valueChanged ), this, &QgsRatioLockButton::heightSpinBoxChanged ); } + +void QgsRatioLockButton::resetRatio() +{ + mPrevWidth = mWidthSpinBox ? mWidthSpinBox->value() : 0.0; + mPrevHeight = mHeightSpinBox ? mHeightSpinBox->value() : 0.0; +} diff --git a/src/gui/qgsratiolockbutton.h b/src/gui/qgsratiolockbutton.h index 0831808d14c..613466635da 100644 --- a/src/gui/qgsratiolockbutton.h +++ b/src/gui/qgsratiolockbutton.h @@ -82,6 +82,12 @@ class GUI_EXPORT QgsRatioLockButton : public QToolButton */ void setHeightSpinBox( QDoubleSpinBox *widget ); + /** + * Resets the current width/height ratio, taking the width and height + * from the current values of the width and height spin boxes. + */ + void resetRatio(); + signals: /** diff --git a/tests/src/python/test_qgsratiolockbutton.py b/tests/src/python/test_qgsratiolockbutton.py index 64514b3b17a..e1f2739b7eb 100644 --- a/tests/src/python/test_qgsratiolockbutton.py +++ b/tests/src/python/test_qgsratiolockbutton.py @@ -99,6 +99,37 @@ class TestQgsRatioLockButton(unittest.TestCase): self.assertEqual(spin_width.value(), 200) self.assertEqual(spin_height.value(), 1000) + def testResetRatio(self): + w = qgis.gui.QgsRatioLockButton() + + spin_width = QDoubleSpinBox() + spin_width.setMaximum(100000) + spin_height = QDoubleSpinBox() + spin_height.setMaximum(100000) + + spin_width.setValue(1000) + w.setWidthSpinBox(spin_width) + spin_height.setValue(500) + w.setHeightSpinBox(spin_height) + + w.setLocked(True) + spin_width.setValue(2000) + self.assertEqual(spin_height.value(), 1000) + + spin_width.blockSignals(True) + spin_width.setValue(1000) + spin_width.blockSignals(False) + + spin_height.setValue(2000) + self.assertEqual(spin_width.value(), 4000) # signals were blocked, so ratio wasn't updated + + spin_width.blockSignals(True) + spin_width.setValue(2000) + spin_width.blockSignals(False) + w.resetRatio() # since signals were blocked, we need to manually reset ratio + spin_height.setValue(1000) + self.assertEqual(spin_width.value(), 1000) + if __name__ == '__main__': unittest.main()