mirror of
https://github.com/qgis/QGIS.git
synced 2025-04-15 00:04:00 -04:00
Added static helper functions and more tests
This commit is contained in:
parent
09d6399b23
commit
576c0a5e83
@ -19,7 +19,9 @@
|
||||
#include "qgsscalecombobox.h"
|
||||
|
||||
#include <QAbstractItemView>
|
||||
#include <QLocale>
|
||||
#include <QSettings>
|
||||
#include <QLineEdit>
|
||||
|
||||
QgsScaleComboBox::QgsScaleComboBox( QWidget* parent ) : QComboBox( parent )
|
||||
{
|
||||
@ -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()
|
||||
@ -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;
|
||||
}
|
||||
|
@ -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
|
||||
|
@ -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()
|
||||
@ -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()
|
||||
@ -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()
|
||||
{
|
||||
};
|
||||
|
Loading…
x
Reference in New Issue
Block a user