Skip to content

Commit

Permalink
Added setting of scale programatially
Browse files Browse the repository at this point in the history
  • Loading branch information
homann committed Sep 21, 2012
1 parent dbbb3c3 commit 01c4ac7
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 3 deletions.
29 changes: 28 additions & 1 deletion src/gui/qgsscalecombobox.cpp
Expand Up @@ -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()
{
Expand All @@ -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 )
{
Expand Down
11 changes: 9 additions & 2 deletions src/gui/qgsscalecombobox.h
Expand Up @@ -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.
Expand All @@ -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:
Expand Down
15 changes: 15 additions & 0 deletions tests/src/gui/testqgsscalecombobox.cpp
Expand Up @@ -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()
Expand Down

0 comments on commit 01c4ac7

Please sign in to comment.