Skip to content

Commit aebe5a4

Browse files
committedNov 7, 2017
Allow reseting ratio manually for linked ratio lock buttons
1 parent ea453be commit aebe5a4

File tree

4 files changed

+49
-0
lines changed

4 files changed

+49
-0
lines changed
 

‎python/gui/qgsratiolockbutton.sip

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,12 @@ class QgsRatioLockButton : QToolButton
6767
.. seealso:: setWidthSpinBox()
6868
%End
6969

70+
void resetRatio();
71+
%Docstring
72+
Resets the current width/height ratio, taking the width and height
73+
from the current values of the width and height spin boxes.
74+
%End
75+
7076
signals:
7177

7278
void lockChanged( const bool locked );

‎src/gui/qgsratiolockbutton.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,3 +156,9 @@ void QgsRatioLockButton::setHeightSpinBox( QDoubleSpinBox *widget )
156156
mPrevHeight = widget->value();
157157
connect( mHeightSpinBox, static_cast<void ( QDoubleSpinBox::* )( double )>( &QDoubleSpinBox::valueChanged ), this, &QgsRatioLockButton::heightSpinBoxChanged );
158158
}
159+
160+
void QgsRatioLockButton::resetRatio()
161+
{
162+
mPrevWidth = mWidthSpinBox ? mWidthSpinBox->value() : 0.0;
163+
mPrevHeight = mHeightSpinBox ? mHeightSpinBox->value() : 0.0;
164+
}

‎src/gui/qgsratiolockbutton.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,12 @@ class GUI_EXPORT QgsRatioLockButton : public QToolButton
8282
*/
8383
void setHeightSpinBox( QDoubleSpinBox *widget );
8484

85+
/**
86+
* Resets the current width/height ratio, taking the width and height
87+
* from the current values of the width and height spin boxes.
88+
*/
89+
void resetRatio();
90+
8591
signals:
8692

8793
/**

‎tests/src/python/test_qgsratiolockbutton.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,37 @@ def testLinkedWidgets(self):
9999
self.assertEqual(spin_width.value(), 200)
100100
self.assertEqual(spin_height.value(), 1000)
101101

102+
def testResetRatio(self):
103+
w = qgis.gui.QgsRatioLockButton()
104+
105+
spin_width = QDoubleSpinBox()
106+
spin_width.setMaximum(100000)
107+
spin_height = QDoubleSpinBox()
108+
spin_height.setMaximum(100000)
109+
110+
spin_width.setValue(1000)
111+
w.setWidthSpinBox(spin_width)
112+
spin_height.setValue(500)
113+
w.setHeightSpinBox(spin_height)
114+
115+
w.setLocked(True)
116+
spin_width.setValue(2000)
117+
self.assertEqual(spin_height.value(), 1000)
118+
119+
spin_width.blockSignals(True)
120+
spin_width.setValue(1000)
121+
spin_width.blockSignals(False)
122+
123+
spin_height.setValue(2000)
124+
self.assertEqual(spin_width.value(), 4000) # signals were blocked, so ratio wasn't updated
125+
126+
spin_width.blockSignals(True)
127+
spin_width.setValue(2000)
128+
spin_width.blockSignals(False)
129+
w.resetRatio() # since signals were blocked, we need to manually reset ratio
130+
spin_height.setValue(1000)
131+
self.assertEqual(spin_width.value(), 1000)
132+
102133

103134
if __name__ == '__main__':
104135
unittest.main()

0 commit comments

Comments
 (0)
Please sign in to comment.