Skip to content

Commit

Permalink
Simplified, and more test
Browse files Browse the repository at this point in the history
  • Loading branch information
homann committed Sep 21, 2012
1 parent 576c0a5 commit 08d28df
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 45 deletions.
72 changes: 27 additions & 45 deletions src/gui/qgsscalecombobox.cpp
Expand Up @@ -119,38 +119,13 @@ void QgsScaleComboBox::fixupScale()
bool ok;
QStringList txtList;

newScale = QLocale::system().toDouble( currentText(), &ok );
newScale = toDouble( currentText(), &ok );
if ( ok )
{
// Create a text version and set that text and rescan again
// Idea is to get the same rounding.
setEditText( toString( newScale ) );
}
ok = false;
// Is now either X:Y or not valid
txtList = currentText().split( ':' );
if ( 2 == txtList.size() )
{
bool okX = false;
bool okY = false;
int x = QLocale::system().toInt( txtList[ 0 ], &okX );
int y = QLocale::system().toInt( txtList[ 1 ], &okY );
if ( okX && okY )
{
// New scale is fraction of x and y
// Text should be OK now.
// Just update the scale.
mScale = ( double )x / ( double )y;
// And emit a signal that something has changed.
emit scaleChanged();
ok = true;
}
}
if ( ! ok )
{
// Set to what it whas before
setEditText( toString( mScale ) );
mScale = newScale;
}
// We set to the new string representation.
setEditText( toString( mScale ) );
}

QString QgsScaleComboBox::toString( double scale )
Expand All @@ -168,26 +143,33 @@ QString QgsScaleComboBox::toString( double scale )
double QgsScaleComboBox::toDouble( QString scaleString, bool * returnOk )
{
bool ok = false;
double scale = QLocale::system().toDouble( scaleString, &ok );
if ( ! ok )
QString scaleTxt( scaleString );

double scale = QLocale::system().toDouble( scaleTxt, &ok );
if ( ok )
{
// Create a text version and set that text and rescan
// Idea is to get the same rounding.
scaleTxt = toString( scale );
}
// It is now either X:Y or not valid
ok = false;
QStringList txtList = scaleTxt.split( ':' );
if ( 2 == txtList.size() )
{
// It wasn't a decimal scale
// It is now either X:Y or not valid
QStringList txtList = scaleString.split( ':' );
if ( 2 == txtList.size() )
bool okX = false;
bool okY = false;
int x = QLocale::system().toInt( txtList[ 0 ], &okX );
int y = QLocale::system().toInt( txtList[ 1 ], &okY );
if ( okX && okY )
{
bool okX = false;
bool okY = false;
int x = QLocale::system().toInt( txtList[ 0 ], &okX );
int y = QLocale::system().toInt( txtList[ 1 ], &okY );
if ( okX && okY )
{
// Scale is fraction of x and y
scale = ( double )x / ( double )y;
ok = true;
}
// Scale is fraction of x and y
scale = ( double )x / ( double )y;
ok = true;
}
}

// Set up optional return flag
if ( returnOk )
{
*returnOk = ok;
Expand Down
7 changes: 7 additions & 0 deletions tests/src/gui/testqgsscalecombobox.cpp
Expand Up @@ -79,6 +79,13 @@ void TestQgsScaleComboBox::basic()
QCOMPARE( s->scaleString(), QString( "42:1" ) );
QCOMPARE( s->scale(), ( double ) 42 );

// Testing conversion from illegal
l->setText( "" );
QTest::keyClicks( l, "1:x:2" );
QTest::keyClick( l, Qt::Key_Return );
QCOMPARE( s->scaleString(), QString( "42:1" ) );
QCOMPARE( s->scale(), ( double ) 42 );

};

void TestQgsScaleComboBox::slot_test()
Expand Down

0 comments on commit 08d28df

Please sign in to comment.