Skip to content

Commit

Permalink
Added static helper functions and more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
homann committed Sep 21, 2012
1 parent 09d6399 commit 576c0a5
Show file tree
Hide file tree
Showing 3 changed files with 157 additions and 7 deletions.
100 changes: 100 additions & 0 deletions src/gui/qgsscalecombobox.cpp
Expand Up @@ -19,7 +19,9 @@
#include "qgsscalecombobox.h"

#include <QAbstractItemView>
#include <QLocale>
#include <QSettings>
#include <QLineEdit>

QgsScaleComboBox::QgsScaleComboBox( QWidget* parent ) : QComboBox( parent )
{
Expand All @@ -28,6 +30,8 @@ QgsScaleComboBox::QgsScaleComboBox( QWidget* parent ) : QComboBox( parent )
setEditable( true );
setInsertPolicy( QComboBox::NoInsert );
setCompleter( 0 );
connect( this, SIGNAL( currentIndexChanged( const QString & ) ), this, SLOT( fixupScale() ) );
connect( lineEdit(), SIGNAL( editingFinished() ), this, SLOT( fixupScale() ) );
}

QgsScaleComboBox::~QgsScaleComboBox()
Expand Down Expand Up @@ -94,3 +98,99 @@ void QgsScaleComboBox::showPopup()
view()->setCurrentIndex( model()->index( idx, 0 ) );
blockSignals( false );
}

//! Function to read the selected scale as text
// @note added in 2.0
QString QgsScaleComboBox::scaleString()
{
return toString( mScale );
}
//! Function to read the selected scale as double
// @note added in 2.0
double QgsScaleComboBox::scale()
{
return mScale;
}

//! Slot called when QComboBox has changed
void QgsScaleComboBox::fixupScale()
{
double newScale;
bool ok;
QStringList txtList;

newScale = QLocale::system().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 ) );
}
}

QString QgsScaleComboBox::toString( double scale )
{
if ( scale > 1 )
{
return QString( "%1:1" ).arg( qRound( scale ) );
}
else
{
return QString( "1:%1" ).arg( qRound( 1.0 / scale ) );
}
}

double QgsScaleComboBox::toDouble( QString scaleString, bool * returnOk )
{
bool ok = false;
double scale = QLocale::system().toDouble( scaleString, &ok );
if ( ! ok )
{
// 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 )
{
// Scale is fraction of x and y
scale = ( double )x / ( double )y;
ok = true;
}
}
}
if ( returnOk )
{
*returnOk = ok;
}
return scale;
}
26 changes: 26 additions & 0 deletions src/gui/qgsscalecombobox.h
Expand Up @@ -30,12 +30,38 @@ class GUI_EXPORT QgsScaleComboBox : public QComboBox
public:
QgsScaleComboBox( QWidget* parent = 0 );
virtual ~QgsScaleComboBox();
//! Function to read the selected scale as text
// @note added in 2.0
QString scaleString();
//! Function to read the selected scale as double
// @note added in 2.0
double scale();
//! Helper function to convert a double to scale string
// Performs rounding, so an exact representation is not to
// be expected.
// @note added in 2.0
static QString toString( double scale );
//! Helper function to convert a scale string to double
// @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:
void scaleChanged();

public slots:
void updateScales( const QStringList &scales = QStringList() );


protected:
void showPopup();

private slots:
void fixupScale();

private:
double mScale;
};

#endif // QGSSCALECOMBOBOX_H
38 changes: 31 additions & 7 deletions tests/src/gui/testqgsscalecombobox.cpp
Expand Up @@ -31,8 +31,9 @@ class TestQgsScaleComboBox: public QObject
void init();// will be called before each testfunction is executed.
void cleanup();// will be called after every testfunction.
void basic();
void slot_test();
private:
QgsScaleComboBox *mScaleEdit;
QgsScaleComboBox *s;
};

void TestQgsScaleComboBox::initTestCase()
Expand All @@ -41,12 +42,12 @@ void TestQgsScaleComboBox::initTestCase()
QgsApplication::initQgis();

// Create a combobox, and init with predefined scales.
mScaleEdit = new QgsScaleComboBox();
s = new QgsScaleComboBox();
};

void TestQgsScaleComboBox::cleanupTestCase()
{
delete mScaleEdit;
delete s;
};

void TestQgsScaleComboBox::init()
Expand All @@ -55,11 +56,34 @@ void TestQgsScaleComboBox::init()

void TestQgsScaleComboBox::basic()
{
mScaleEdit->lineEdit()->setText( "" );
QTest::keyClicks( mScaleEdit->lineEdit(), "1:2345");
QCOMPARE( mScaleEdit->lineEdit()->text(), QString("1:2345"));
QLineEdit *l = s->lineEdit();

// Testing conversion from "1:nnn".
l->setText( "" );
QTest::keyClicks( l, "1:2345" );
QTest::keyClick( l, Qt::Key_Return );
QCOMPARE( s->scaleString(), QString( "1:2345" ) );
QCOMPARE( s->scale(), (( double ) 1.0 / ( double ) 2345.0 ) );

// Testing conversion from number to "1:x"
l->setText( "" );
QTest::keyClicks( l, QLocale::system().toString( 0.02 ) );
QTest::keyClick( l, Qt::Key_Return );
QCOMPARE( s->scaleString(), QString( "1:50" ) );
QCOMPARE( s->scale(), ( double ) 0.02 );

// Testing conversion from number to "x:1"
l->setText( "" );
QTest::keyClicks( l, QLocale::system().toString( 42 ) );
QTest::keyClick( l, Qt::Key_Return );
QCOMPARE( s->scaleString(), QString( "42:1" ) );
QCOMPARE( s->scale(), ( double ) 42 );

};


void TestQgsScaleComboBox::slot_test()
{
}
void TestQgsScaleComboBox::cleanup()
{
};
Expand Down

0 comments on commit 576c0a5

Please sign in to comment.