Skip to content

Commit

Permalink
Allow reseting ratio manually for linked ratio lock buttons
Browse files Browse the repository at this point in the history
  • Loading branch information
nyalldawson committed Nov 7, 2017
1 parent ea453be commit aebe5a4
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 0 deletions.
6 changes: 6 additions & 0 deletions python/gui/qgsratiolockbutton.sip
Original file line number Diff line number Diff line change
Expand Up @@ -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 );
Expand Down
6 changes: 6 additions & 0 deletions src/gui/qgsratiolockbutton.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -156,3 +156,9 @@ void QgsRatioLockButton::setHeightSpinBox( QDoubleSpinBox *widget )
mPrevHeight = widget->value();
connect( mHeightSpinBox, static_cast<void ( QDoubleSpinBox::* )( double )>( &QDoubleSpinBox::valueChanged ), this, &QgsRatioLockButton::heightSpinBoxChanged );
}

void QgsRatioLockButton::resetRatio()
{
mPrevWidth = mWidthSpinBox ? mWidthSpinBox->value() : 0.0;
mPrevHeight = mHeightSpinBox ? mHeightSpinBox->value() : 0.0;
}
6 changes: 6 additions & 0 deletions src/gui/qgsratiolockbutton.h
Original file line number Diff line number Diff line change
Expand Up @@ -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:

/**
Expand Down
31 changes: 31 additions & 0 deletions tests/src/python/test_qgsratiolockbutton.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,37 @@ def testLinkedWidgets(self):
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()

0 comments on commit aebe5a4

Please sign in to comment.