Skip to content

Commit

Permalink
Add QgsUnitTypes::toAbbreviatedString
Browse files Browse the repository at this point in the history
  • Loading branch information
m-kuhn committed Feb 28, 2017
1 parent b1c653b commit bb617a1
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 10 deletions.
14 changes: 4 additions & 10 deletions python/core/qgsunittypes.sip
Expand Up @@ -101,11 +101,8 @@ class QgsUnitTypes
*/ */
static QString toString( QgsUnitTypes::DistanceUnit unit ); static QString toString( QgsUnitTypes::DistanceUnit unit );


/** Converts a translated string to a distance unit. static QString toAbbreviatedString( QgsUnitTypes::DistanceUnit unit );
* @param string string representing a distance unit
* @param ok optional boolean, will be set to true if string was converted successfully
* @see toString()
*/
static QgsUnitTypes::DistanceUnit stringToDistanceUnit( const QString& string, bool *ok /Out/ = 0 ); static QgsUnitTypes::DistanceUnit stringToDistanceUnit( const QString& string, bool *ok /Out/ = 0 );


/** Returns the conversion factor between the specified distance units. /** Returns the conversion factor between the specified distance units.
Expand Down Expand Up @@ -140,11 +137,8 @@ class QgsUnitTypes
*/ */
static QString toString( AreaUnit unit ); static QString toString( AreaUnit unit );


/** Converts a translated string to an areal unit. static QString toAbbreviatedString( AreaUnit unit );
* @param string string representing an areal unit
* @param ok optional boolean, will be set to true if string was converted successfully
* @see toString()
*/
static AreaUnit stringToAreaUnit( const QString& string, bool *ok /Out/ = 0 ); static AreaUnit stringToAreaUnit( const QString& string, bool *ok /Out/ = 0 );


/** Returns the conversion factor between the specified areal units. /** Returns the conversion factor between the specified areal units.
Expand Down
59 changes: 59 additions & 0 deletions src/core/qgsunittypes.cpp
Expand Up @@ -166,6 +166,37 @@ QString QgsUnitTypes::toString( DistanceUnit unit )
return QString(); return QString();
} }


QString QgsUnitTypes::toAbbreviatedString( QgsUnitTypes::DistanceUnit unit )
{
switch ( unit )
{
case DistanceMeters:
return QCoreApplication::translate( "UnitType", "m" );

case DistanceKilometers:
return QCoreApplication::translate( "UnitType", "km" );

case DistanceFeet:
return QCoreApplication::translate( "UnitType", "ft" );

case DistanceYards:
return QCoreApplication::translate( "UnitType", "yd" );

case DistanceMiles:
return QCoreApplication::translate( "UnitType", "mi" );

case DistanceDegrees:
return QCoreApplication::translate( "UnitType", "deg" );

case DistanceUnknownUnit:
return QString();

case DistanceNauticalMiles:
return QCoreApplication::translate( "UnitType", "NM" );
}
return QString();
}

/*************************************************************************** /***************************************************************************
* This class is considered CRITICAL and any change MUST be accompanied with * This class is considered CRITICAL and any change MUST be accompanied with
* full unit tests in test_qgsunittypes.py. * full unit tests in test_qgsunittypes.py.
Expand Down Expand Up @@ -485,6 +516,34 @@ QString QgsUnitTypes::toString( QgsUnitTypes::AreaUnit unit )
return QString(); return QString();
} }


QString QgsUnitTypes::toAbbreviatedString( QgsUnitTypes::AreaUnit unit )
{
switch ( unit )
{
case AreaSquareMeters:
return QCoreApplication::translate( "QgsUnitTypes::AreaUnit", "" );
case AreaSquareKilometers:
return QCoreApplication::translate( "QgsUnitTypes::AreaUnit", "km²" );
case AreaSquareFeet:
return QCoreApplication::translate( "QgsUnitTypes::AreaUnit", "ft²" );
case AreaSquareYards:
return QCoreApplication::translate( "QgsUnitTypes::AreaUnit", "yd²" );
case AreaSquareMiles:
return QCoreApplication::translate( "QgsUnitTypes::AreaUnit", "mi²" );
case AreaHectares:
return QCoreApplication::translate( "QgsUnitTypes::AreaUnit", "ha²" );
case AreaAcres:
return QCoreApplication::translate( "QgsUnitTypes::AreaUnit", "ac²" );
case AreaSquareNauticalMiles:
return QCoreApplication::translate( "QgsUnitTypes::AreaUnit", "NM²" );
case AreaSquareDegrees:
return QCoreApplication::translate( "QgsUnitTypes::AreaUnit", "deg²" );
case AreaUnknownUnit:
return QString();
}
return QString();
}

QgsUnitTypes::AreaUnit QgsUnitTypes::stringToAreaUnit( const QString& string, bool* ok ) QgsUnitTypes::AreaUnit QgsUnitTypes::stringToAreaUnit( const QString& string, bool* ok )
{ {
QString normalized = string.trimmed().toLower(); QString normalized = string.trimmed().toLower();
Expand Down
16 changes: 16 additions & 0 deletions src/core/qgsunittypes.h
Expand Up @@ -130,6 +130,14 @@ class CORE_EXPORT QgsUnitTypes
*/ */
Q_INVOKABLE static QString toString( QgsUnitTypes::DistanceUnit unit ); Q_INVOKABLE static QString toString( QgsUnitTypes::DistanceUnit unit );


/** Returns a translated abbreviation representing a distance unit.
* @param unit unit to convert to string
* @see stringToDistanceUnit()
*
* @note Added in QGIS 3.0
*/
Q_INVOKABLE static QString toAbbreviatedString( QgsUnitTypes::DistanceUnit unit );

/** Converts a translated string to a distance unit. /** Converts a translated string to a distance unit.
* @param string string representing a distance unit * @param string string representing a distance unit
* @param ok optional boolean, will be set to true if string was converted successfully * @param ok optional boolean, will be set to true if string was converted successfully
Expand Down Expand Up @@ -171,6 +179,14 @@ class CORE_EXPORT QgsUnitTypes
*/ */
Q_INVOKABLE static QString toString( AreaUnit unit ); Q_INVOKABLE static QString toString( AreaUnit unit );


/** Returns a translated abbreviation representing an areal unit.
* @param unit unit to convert to string
* @see stringToAreaUnit()
*
* @note Added in QGIS 3.0
*/
Q_INVOKABLE static QString toAbbreviatedString( AreaUnit unit );

/** Converts a translated string to an areal unit. /** Converts a translated string to an areal unit.
* @param string string representing an areal unit * @param string string representing an areal unit
* @param ok optional boolean, will be set to true if string was converted successfully * @param ok optional boolean, will be set to true if string was converted successfully
Expand Down

0 comments on commit bb617a1

Please sign in to comment.