Skip to content
Permalink
Browse files

Added setting of scale programatially

  • Loading branch information
homann committed Sep 21, 2012
1 parent dbbb3c3 commit 01c4ac7d5403cc8986e8beb4eddc356b55895329
Showing with 52 additions and 3 deletions.
  1. +28 −1 src/gui/qgsscalecombobox.cpp
  2. +9 −2 src/gui/qgsscalecombobox.h
  3. +15 −0 tests/src/gui/testqgsscalecombobox.cpp
@@ -106,13 +106,40 @@ 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()
{
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 )
{
@@ -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:
@@ -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()

0 comments on commit 01c4ac7

Please sign in to comment.
You can’t perform that action at this time.