Skip to content

Commit 01c4ac7

Browse files
committed
Added setting of scale programatially
1 parent dbbb3c3 commit 01c4ac7

3 files changed

Lines changed: 52 additions & 3 deletions

File tree

‎src/gui/qgsscalecombobox.cpp‎

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,13 +106,40 @@ QString QgsScaleComboBox::scaleString()
106106
{
107107
return toString( mScale );
108108
}
109+
110+
//! Function to set the selected scale from text
111+
// @note added in 2.0
112+
bool QgsScaleComboBox::setScaleString( QString scaleTxt )
113+
{
114+
bool ok;
115+
double newScale = toDouble( scaleTxt, &ok );
116+
if ( ! ok )
117+
{
118+
return false;
119+
}
120+
else
121+
{
122+
mScale = newScale;
123+
setEditText( toString( mScale ) );
124+
clearFocus();
125+
return true;
126+
}
127+
}
128+
109129
//! Function to read the selected scale as double
110130
// @note added in 2.0
111131
double QgsScaleComboBox::scale()
112132
{
113133
return mScale;
114134
}
115135

136+
//! Function to set the selected scale from double
137+
// @note added in 2.0
138+
void QgsScaleComboBox::setScale( double scale )
139+
{
140+
setScaleString( toString( scale ) );
141+
}
142+
116143
//! Slot called when QComboBox has changed
117144
void QgsScaleComboBox::fixupScale()
118145
{
@@ -128,7 +155,7 @@ void QgsScaleComboBox::fixupScale()
128155
}
129156
// We set to the new string representation
130157
// or reset to the old
131-
setEditText( toString( mScale ) );
158+
setScale( mScale );
132159

133160
if ( oldScale != mScale )
134161
{

‎src/gui/qgsscalecombobox.h‎

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,16 @@ class GUI_EXPORT QgsScaleComboBox : public QComboBox
3333
//! Function to read the selected scale as text
3434
// @note added in 2.0
3535
QString scaleString();
36+
//! Function to set the selected scale from text
37+
// @note added in 2.0
38+
bool setScaleString( QString scaleTxt );
3639
//! Function to read the selected scale as double
3740
// @note added in 2.0
3841
double scale();
42+
//! Function to set the selected scale from double
43+
// @note added in 2.0
44+
void setScale( double scale );
45+
3946
//! Helper function to convert a double to scale string
4047
// Performs rounding, so an exact representation is not to
4148
// be expected.
@@ -45,9 +52,9 @@ class GUI_EXPORT QgsScaleComboBox : public QComboBox
4552
// @note added in 2.0
4653
static double toDouble( QString scaleString, bool *ok = NULL );
4754

48-
//! Signals is emited when user has finished editing/selecting a new scale.
49-
// @note added in 2.0
5055
signals:
56+
//! Signal is emitted when *user* has finished editing/selecting a new scale.
57+
// @note added in 2.0
5158
void scaleChanged();
5259

5360
public slots:

‎tests/src/gui/testqgsscalecombobox.cpp‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,21 @@ void TestQgsScaleComboBox::basic()
9494
QCOMPARE( s->scaleString(), QString( "1:4" ) );
9595
QCOMPARE( s->scale(), ( double ) 0.25 );
9696

97+
// Test setting programatically
98+
s->setScale(( double ) 0.19 );
99+
QCOMPARE( s->scaleString(), QString( "1:5" ) );
100+
QCOMPARE( s->scale(), ( double ) 0.2 );
101+
102+
// Test setting programatically
103+
s->setScaleString( QString( "1:240" ) );
104+
QCOMPARE( s->scaleString(), QString( "1:240" ) );
105+
QCOMPARE( s->scale(), ( double ) 1.0 / ( double ) 240.0 );
106+
107+
// Test setting programatically illegal string
108+
s->setScaleString( QString( "1:2.4" ) );
109+
QCOMPARE( s->scaleString(), QString( "1:240" ) );
110+
QCOMPARE( s->scale(), ( double ) 1.0 / ( double ) 240.0 );
111+
97112
};
98113

99114
void TestQgsScaleComboBox::slot_test()

0 commit comments

Comments
 (0)