diff --git a/src/gui/qgsscalecombobox.cpp b/src/gui/qgsscalecombobox.cpp index 2eeb00811e5..087161ef6e7 100644 --- a/src/gui/qgsscalecombobox.cpp +++ b/src/gui/qgsscalecombobox.cpp @@ -106,6 +106,26 @@ QString QgsScaleComboBox::scaleString() { return toString( mScale ); } + +//! Function to set the selected scale from text +// @note added in 2.0 +bool QgsScaleComboBox::setScaleString( QString scaleTxt ) +{ + bool ok; + double newScale = toDouble( scaleTxt, &ok ); + if ( ! ok ) + { + return false; + } + else + { + mScale = newScale; + setEditText( toString( mScale ) ); + clearFocus(); + return true; + } +} + //! Function to read the selected scale as double // @note added in 2.0 double QgsScaleComboBox::scale() @@ -113,6 +133,13 @@ double QgsScaleComboBox::scale() return mScale; } +//! Function to set the selected scale from double +// @note added in 2.0 +void QgsScaleComboBox::setScale( double scale ) +{ + setScaleString( toString( scale ) ); +} + //! Slot called when QComboBox has changed void QgsScaleComboBox::fixupScale() { @@ -128,7 +155,7 @@ void QgsScaleComboBox::fixupScale() } // We set to the new string representation // or reset to the old - setEditText( toString( mScale ) ); + setScale( mScale ); if ( oldScale != mScale ) { diff --git a/src/gui/qgsscalecombobox.h b/src/gui/qgsscalecombobox.h index ec076f52736..2a00e68f861 100644 --- a/src/gui/qgsscalecombobox.h +++ b/src/gui/qgsscalecombobox.h @@ -33,9 +33,16 @@ class GUI_EXPORT QgsScaleComboBox : public QComboBox //! Function to read the selected scale as text // @note added in 2.0 QString scaleString(); + //! Function to set the selected scale from text + // @note added in 2.0 + bool setScaleString( QString scaleTxt ); //! Function to read the selected scale as double // @note added in 2.0 double scale(); + //! Function to set the selected scale from double + // @note added in 2.0 + void setScale( double scale ); + //! Helper function to convert a double to scale string // Performs rounding, so an exact representation is not to // be expected. @@ -45,9 +52,9 @@ class GUI_EXPORT QgsScaleComboBox : public QComboBox // @note added in 2.0 static double toDouble( QString scaleString, bool *ok = NULL ); - //! Signals is emited when user has finished editing/selecting a new scale. - // @note added in 2.0 signals: + //! Signal is emitted when *user* has finished editing/selecting a new scale. + // @note added in 2.0 void scaleChanged(); public slots: diff --git a/tests/src/gui/testqgsscalecombobox.cpp b/tests/src/gui/testqgsscalecombobox.cpp index 22fbe5533a4..605e0a98d7a 100644 --- a/tests/src/gui/testqgsscalecombobox.cpp +++ b/tests/src/gui/testqgsscalecombobox.cpp @@ -94,6 +94,21 @@ void TestQgsScaleComboBox::basic() QCOMPARE( s->scaleString(), QString( "1:4" ) ); QCOMPARE( s->scale(), ( double ) 0.25 ); + // Test setting programatically + s->setScale(( double ) 0.19 ); + QCOMPARE( s->scaleString(), QString( "1:5" ) ); + QCOMPARE( s->scale(), ( double ) 0.2 ); + + // Test setting programatically + s->setScaleString( QString( "1:240" ) ); + QCOMPARE( s->scaleString(), QString( "1:240" ) ); + QCOMPARE( s->scale(), ( double ) 1.0 / ( double ) 240.0 ); + + // Test setting programatically illegal string + s->setScaleString( QString( "1:2.4" ) ); + QCOMPARE( s->scaleString(), QString( "1:240" ) ); + QCOMPARE( s->scale(), ( double ) 1.0 / ( double ) 240.0 ); + }; void TestQgsScaleComboBox::slot_test()