Skip to content

Commit

Permalink
Move some methods out of Qgis class
Browse files Browse the repository at this point in the history
  • Loading branch information
nyalldawson committed Jul 21, 2016
1 parent 1a2231f commit 713e22a
Show file tree
Hide file tree
Showing 6 changed files with 59 additions and 56 deletions.
2 changes: 2 additions & 0 deletions doc/api_break.dox
Expand Up @@ -45,6 +45,8 @@ This page tries to maintain a list with incompatible changes that happened in pr


<ul> <ul>
<li>The QGis class was renamed to Qgis for capitalisation consistency with other class names</li> <li>The QGis class was renamed to Qgis for capitalisation consistency with other class names</li>
<li>permissiveToDouble() and permissiveToInt() where moved out of the QGis class and renamed to qgsPermissiveToDouble() and
qgsPermissiveToInt()</li>
</ul> </ul>


\subsection qgis_api_break_3_0_QgsAuthConfigUriEdit QgsAuthConfigUriEdit \subsection qgis_api_break_3_0_QgsAuthConfigUriEdit QgsAuthConfigUriEdit
Expand Down
41 changes: 21 additions & 20 deletions python/core/qgis.sip
Expand Up @@ -180,26 +180,6 @@ class Qgis
//! @deprecated use QgsUnitTyoes::fromUnitToUnitFactor() instead //! @deprecated use QgsUnitTyoes::fromUnitToUnitFactor() instead
static double fromUnitToUnitFactor( Qgis::UnitType fromUnit, Qgis::UnitType toUnit ) /Deprecated/; static double fromUnitToUnitFactor( Qgis::UnitType fromUnit, Qgis::UnitType toUnit ) /Deprecated/;


/** Converts a string to a double in a permissive way, eg allowing for incorrect
* numbers of digits between thousand separators
* @param string string to convert
* @param ok will be set to true if conversion was successful
* @returns string converted to double if possible
* @note added in version 2.9
* @see permissiveToInt
*/
static double permissiveToDouble( QString string, bool& ok );

/** Converts a string to an integer in a permissive way, eg allowing for incorrect
* numbers of digits between thousand separators
* @param string string to convert
* @param ok will be set to true if conversion was successful
* @returns string converted to int if possible
* @note added in version 2.9
* @see permissiveToDouble
*/
static int permissiveToInt( QString string, bool& ok );

//! User defined event types //! User defined event types
enum UserEvent enum UserEvent
{ {
Expand Down Expand Up @@ -243,6 +223,27 @@ class Qgis
static double SCALE_PRECISION; static double SCALE_PRECISION;
}; };



/** Converts a string to a double in a permissive way, eg allowing for incorrect
* numbers of digits between thousand separators
* @param string string to convert
* @param ok will be set to true if conversion was successful
* @returns string converted to double if possible
* @note added in version 2.9
* @see permissiveToInt
*/
double qgsPermissiveToDouble( QString string, bool& ok );

/** Converts a string to an integer in a permissive way, eg allowing for incorrect
* numbers of digits between thousand separators
* @param string string to convert
* @param ok will be set to true if conversion was successful
* @returns string converted to int if possible
* @note added in version 2.9
* @see permissiveToDouble
*/
int qgsPermissiveToInt( QString string, bool& ok );

//! Compares two QVariant values and returns whether the first is less than the second. //! Compares two QVariant values and returns whether the first is less than the second.
//! Useful for sorting lists of variants, correctly handling sorting of the various //! Useful for sorting lists of variants, correctly handling sorting of the various
//! QVariant data types (such as strings, numeric values, dates and times) //! QVariant data types (such as strings, numeric values, dates and times)
Expand Down
4 changes: 2 additions & 2 deletions src/core/qgis.cpp
Expand Up @@ -217,14 +217,14 @@ double Qgis::fromUnitToUnitFactor( Qgis::UnitType fromUnit, Qgis::UnitType toUni
return QgsUnitTypes::fromUnitToUnitFactor( fromUnit, toUnit ); return QgsUnitTypes::fromUnitToUnitFactor( fromUnit, toUnit );
} }


double Qgis::permissiveToDouble( QString string, bool &ok ) double qgsPermissiveToDouble( QString string, bool &ok )
{ {
//remove any thousands separators //remove any thousands separators
string.remove( QLocale::system().groupSeparator() ); string.remove( QLocale::system().groupSeparator() );
return QLocale::system().toDouble( string, &ok ); return QLocale::system().toDouble( string, &ok );
} }


int Qgis::permissiveToInt( QString string, bool &ok ) int qgsPermissiveToInt( QString string, bool &ok )
{ {
//remove any thousands separators //remove any thousands separators
string.remove( QLocale::system().groupSeparator() ); string.remove( QLocale::system().groupSeparator() );
Expand Down
40 changes: 20 additions & 20 deletions src/core/qgis.h
Expand Up @@ -194,26 +194,6 @@ class CORE_EXPORT Qgis
//! @deprecated use QgsUnitTyoes::fromUnitToUnitFactor() instead //! @deprecated use QgsUnitTyoes::fromUnitToUnitFactor() instead
Q_DECL_DEPRECATED static double fromUnitToUnitFactor( Qgis::UnitType fromUnit, Qgis::UnitType toUnit ); Q_DECL_DEPRECATED static double fromUnitToUnitFactor( Qgis::UnitType fromUnit, Qgis::UnitType toUnit );


/** Converts a string to a double in a permissive way, eg allowing for incorrect
* numbers of digits between thousand separators
* @param string string to convert
* @param ok will be set to true if conversion was successful
* @returns string converted to double if possible
* @note added in version 2.9
* @see permissiveToInt
*/
static double permissiveToDouble( QString string, bool& ok );

/** Converts a string to an integer in a permissive way, eg allowing for incorrect
* numbers of digits between thousand separators
* @param string string to convert
* @param ok will be set to true if conversion was successful
* @returns string converted to int if possible
* @note added in version 2.9
* @see permissiveToDouble
*/
static int permissiveToInt( QString string, bool& ok );

//! User defined event types //! User defined event types
enum UserEvent enum UserEvent
{ {
Expand Down Expand Up @@ -388,6 +368,26 @@ inline double qgsRound( double x )
return x < 0.0 ? std::ceil( x - 0.5 ) : std::floor( x + 0.5 ); return x < 0.0 ? std::ceil( x - 0.5 ) : std::floor( x + 0.5 );
} }


/** Converts a string to a double in a permissive way, eg allowing for incorrect
* numbers of digits between thousand separators
* @param string string to convert
* @param ok will be set to true if conversion was successful
* @returns string converted to double if possible
* @note added in version 2.9
* @see permissiveToInt
*/
CORE_EXPORT double qgsPermissiveToDouble( QString string, bool& ok );

/** Converts a string to an integer in a permissive way, eg allowing for incorrect
* numbers of digits between thousand separators
* @param string string to convert
* @param ok will be set to true if conversion was successful
* @returns string converted to int if possible
* @note added in version 2.9
* @see permissiveToDouble
*/
CORE_EXPORT int qgsPermissiveToInt( QString string, bool& ok );

// Add missing qHash implementation for QDate, QTime, QDateTime // Add missing qHash implementation for QDate, QTime, QDateTime
// implementations taken from upstream Qt5 versions // implementations taken from upstream Qt5 versions
#if QT_VERSION < 0x050000 #if QT_VERSION < 0x050000
Expand Down
6 changes: 3 additions & 3 deletions src/gui/qgsscalecombobox.cpp
Expand Up @@ -209,7 +209,7 @@ double QgsScaleComboBox::toDouble( const QString& scaleString, bool * returnOk )
bool ok = false; bool ok = false;
QString scaleTxt( scaleString ); QString scaleTxt( scaleString );


double scale = Qgis::permissiveToDouble( scaleTxt, ok ); double scale = qgsPermissiveToDouble( scaleTxt, ok );
if ( ok ) if ( ok )
{ {
// Create a text version and set that text and rescan // Create a text version and set that text and rescan
Expand All @@ -224,8 +224,8 @@ double QgsScaleComboBox::toDouble( const QString& scaleString, bool * returnOk )
{ {
bool okX = false; bool okX = false;
bool okY = false; bool okY = false;
int x = Qgis::permissiveToInt( txtList[ 0 ], okX ); int x = qgsPermissiveToInt( txtList[ 0 ], okX );
int y = Qgis::permissiveToInt( txtList[ 1 ], okY ); int y = qgsPermissiveToInt( txtList[ 1 ], okY );
if ( okX && okY ) if ( okX && okY )
{ {
// Scale is fraction of x and y // Scale is fraction of x and y
Expand Down
22 changes: 11 additions & 11 deletions tests/src/core/testqgis.cpp
Expand Up @@ -69,34 +69,34 @@ void TestQgis::permissiveToDouble()
{ {
//good inputs //good inputs
bool ok = false; bool ok = false;
double result = Qgis::permissiveToDouble( QString( "1000" ), ok ); double result = qgsPermissiveToDouble( QString( "1000" ), ok );
QVERIFY( ok ); QVERIFY( ok );
QCOMPARE( result, 1000.0 ); QCOMPARE( result, 1000.0 );
ok = false; ok = false;
result = Qgis::permissiveToDouble( QString( "1" ) + QLocale::system().groupSeparator() + "000", ok ); result = qgsPermissiveToDouble( QString( "1" ) + QLocale::system().groupSeparator() + "000", ok );
QVERIFY( ok ); QVERIFY( ok );
QCOMPARE( result, 1000.0 ); QCOMPARE( result, 1000.0 );
ok = false; ok = false;
result = Qgis::permissiveToDouble( QString( "5" ) + QLocale::system().decimalPoint() + "5", ok ); result = qgsPermissiveToDouble( QString( "5" ) + QLocale::system().decimalPoint() + "5", ok );
QVERIFY( ok ); QVERIFY( ok );
QCOMPARE( result, 5.5 ); QCOMPARE( result, 5.5 );
ok = false; ok = false;
result = Qgis::permissiveToDouble( QString( "1" ) + QLocale::system().groupSeparator() + "000" + QLocale::system().decimalPoint() + "5", ok ); result = qgsPermissiveToDouble( QString( "1" ) + QLocale::system().groupSeparator() + "000" + QLocale::system().decimalPoint() + "5", ok );
QVERIFY( ok ); QVERIFY( ok );
QCOMPARE( result, 1000.5 ); QCOMPARE( result, 1000.5 );


//bad input //bad input
ok = false; ok = false;
( void ) Qgis::permissiveToDouble( QString( "a" ), ok ); ( void ) qgsPermissiveToDouble( QString( "a" ), ok );
QVERIFY( !ok ); QVERIFY( !ok );


//messy input (invalid thousand separator position), should still be converted //messy input (invalid thousand separator position), should still be converted
ok = false; ok = false;
result = Qgis::permissiveToDouble( QString( "10" ) + QLocale::system().groupSeparator() + "00", ok ); result = qgsPermissiveToDouble( QString( "10" ) + QLocale::system().groupSeparator() + "00", ok );
QVERIFY( ok ); QVERIFY( ok );
QCOMPARE( result, 1000.0 ); QCOMPARE( result, 1000.0 );
ok = false; ok = false;
result = Qgis::permissiveToDouble( QString( "10" ) + QLocale::system().groupSeparator() + "00" + QLocale::system().decimalPoint() + "5", ok ); result = qgsPermissiveToDouble( QString( "10" ) + QLocale::system().groupSeparator() + "00" + QLocale::system().decimalPoint() + "5", ok );
QVERIFY( ok ); QVERIFY( ok );
QCOMPARE( result, 1000.5 ); QCOMPARE( result, 1000.5 );
} }
Expand All @@ -105,22 +105,22 @@ void TestQgis::permissiveToInt()
{ {
//good inputs //good inputs
bool ok = false; bool ok = false;
int result = Qgis::permissiveToInt( QString( "1000" ), ok ); int result = qgsPermissiveToInt( QString( "1000" ), ok );
QVERIFY( ok ); QVERIFY( ok );
QCOMPARE( result, 1000 ); QCOMPARE( result, 1000 );
ok = false; ok = false;
result = Qgis::permissiveToInt( QString( "1%01000" ).arg( QLocale::system().groupSeparator() ), ok ); result = qgsPermissiveToInt( QString( "1%01000" ).arg( QLocale::system().groupSeparator() ), ok );
QVERIFY( ok ); QVERIFY( ok );
QCOMPARE( result, 1000 ); QCOMPARE( result, 1000 );


//bad input //bad input
ok = false; ok = false;
( void ) Qgis::permissiveToInt( QString( "a" ), ok ); ( void ) qgsPermissiveToInt( QString( "a" ), ok );
QVERIFY( !ok ); QVERIFY( !ok );


//messy input (invalid thousand separator position), should still be converted //messy input (invalid thousand separator position), should still be converted
ok = false; ok = false;
result = Qgis::permissiveToInt( QString( "10%0100" ).arg( QLocale::system().groupSeparator() ), ok ); result = qgsPermissiveToInt( QString( "10%0100" ).arg( QLocale::system().groupSeparator() ), ok );
QVERIFY( ok ); QVERIFY( ok );
QCOMPARE( result, 1000 ); QCOMPARE( result, 1000 );
} }
Expand Down

0 comments on commit 713e22a

Please sign in to comment.