Skip to content
Permalink
Browse files

Add QgsUnitTypes::toAbbreviatedString

  • Loading branch information
m-kuhn committed Feb 28, 2017
1 parent b1c653b commit bb617a1d96b20ef45a141631d6c1739e818bd875
Showing with 79 additions and 10 deletions.
  1. +4 −10 python/core/qgsunittypes.sip
  2. +59 −0 src/core/qgsunittypes.cpp
  3. +16 −0 src/core/qgsunittypes.h
@@ -101,11 +101,8 @@ class QgsUnitTypes
*/
static QString toString( QgsUnitTypes::DistanceUnit unit );

/** Converts a translated string to a distance 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 QString toAbbreviatedString( QgsUnitTypes::DistanceUnit unit );

static QgsUnitTypes::DistanceUnit stringToDistanceUnit( const QString& string, bool *ok /Out/ = 0 );

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

/** Converts a translated string to an areal 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 QString toAbbreviatedString( AreaUnit unit );

static AreaUnit stringToAreaUnit( const QString& string, bool *ok /Out/ = 0 );

/** Returns the conversion factor between the specified areal units.
@@ -166,6 +166,37 @@ QString QgsUnitTypes::toString( DistanceUnit unit )
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
* full unit tests in test_qgsunittypes.py.
@@ -485,6 +516,34 @@ QString QgsUnitTypes::toString( QgsUnitTypes::AreaUnit unit )
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 )
{
QString normalized = string.trimmed().toLower();
@@ -130,6 +130,14 @@ class CORE_EXPORT QgsUnitTypes
*/
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.
* @param string string representing a distance unit
* @param ok optional boolean, will be set to true if string was converted successfully
@@ -171,6 +179,14 @@ class CORE_EXPORT QgsUnitTypes
*/
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.
* @param string string representing an areal unit
* @param ok optional boolean, will be set to true if string was converted successfully

0 comments on commit bb617a1

Please sign in to comment.
You can’t perform that action at this time.