Allow reseting ratio manually for linked ratio lock buttons

This commit is contained in:
Nyall Dawson 2017-10-10 15:04:09 +10:00
parent ea453beb73
commit aebe5a42e6
4 changed files with 49 additions and 0 deletions

View File

@ -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 );

View File

@ -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;
}

View File

@ -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:
/**

View File

@ -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()