Skip to content

Commit bb617a1

Browse files
committed
Add QgsUnitTypes::toAbbreviatedString
1 parent b1c653b commit bb617a1

File tree

3 files changed

+79
-10
lines changed

3 files changed

+79
-10
lines changed

python/core/qgsunittypes.sip

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -101,11 +101,8 @@ class QgsUnitTypes
101101
*/
102102
static QString toString( QgsUnitTypes::DistanceUnit unit );
103103

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

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

143-
/** Converts a translated string to an areal unit.
144-
* @param string string representing an areal unit
145-
* @param ok optional boolean, will be set to true if string was converted successfully
146-
* @see toString()
147-
*/
140+
static QString toAbbreviatedString( AreaUnit unit );
141+
148142
static AreaUnit stringToAreaUnit( const QString& string, bool *ok /Out/ = 0 );
149143

150144
/** Returns the conversion factor between the specified areal units.

src/core/qgsunittypes.cpp

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,37 @@ QString QgsUnitTypes::toString( DistanceUnit unit )
166166
return QString();
167167
}
168168

169+
QString QgsUnitTypes::toAbbreviatedString( QgsUnitTypes::DistanceUnit unit )
170+
{
171+
switch ( unit )
172+
{
173+
case DistanceMeters:
174+
return QCoreApplication::translate( "UnitType", "m" );
175+
176+
case DistanceKilometers:
177+
return QCoreApplication::translate( "UnitType", "km" );
178+
179+
case DistanceFeet:
180+
return QCoreApplication::translate( "UnitType", "ft" );
181+
182+
case DistanceYards:
183+
return QCoreApplication::translate( "UnitType", "yd" );
184+
185+
case DistanceMiles:
186+
return QCoreApplication::translate( "UnitType", "mi" );
187+
188+
case DistanceDegrees:
189+
return QCoreApplication::translate( "UnitType", "deg" );
190+
191+
case DistanceUnknownUnit:
192+
return QString();
193+
194+
case DistanceNauticalMiles:
195+
return QCoreApplication::translate( "UnitType", "NM" );
196+
}
197+
return QString();
198+
}
199+
169200
/***************************************************************************
170201
* This class is considered CRITICAL and any change MUST be accompanied with
171202
* full unit tests in test_qgsunittypes.py.
@@ -485,6 +516,34 @@ QString QgsUnitTypes::toString( QgsUnitTypes::AreaUnit unit )
485516
return QString();
486517
}
487518

519+
QString QgsUnitTypes::toAbbreviatedString( QgsUnitTypes::AreaUnit unit )
520+
{
521+
switch ( unit )
522+
{
523+
case AreaSquareMeters:
524+
return QCoreApplication::translate( "QgsUnitTypes::AreaUnit", "" );
525+
case AreaSquareKilometers:
526+
return QCoreApplication::translate( "QgsUnitTypes::AreaUnit", "km²" );
527+
case AreaSquareFeet:
528+
return QCoreApplication::translate( "QgsUnitTypes::AreaUnit", "ft²" );
529+
case AreaSquareYards:
530+
return QCoreApplication::translate( "QgsUnitTypes::AreaUnit", "yd²" );
531+
case AreaSquareMiles:
532+
return QCoreApplication::translate( "QgsUnitTypes::AreaUnit", "mi²" );
533+
case AreaHectares:
534+
return QCoreApplication::translate( "QgsUnitTypes::AreaUnit", "ha²" );
535+
case AreaAcres:
536+
return QCoreApplication::translate( "QgsUnitTypes::AreaUnit", "ac²" );
537+
case AreaSquareNauticalMiles:
538+
return QCoreApplication::translate( "QgsUnitTypes::AreaUnit", "NM²" );
539+
case AreaSquareDegrees:
540+
return QCoreApplication::translate( "QgsUnitTypes::AreaUnit", "deg²" );
541+
case AreaUnknownUnit:
542+
return QString();
543+
}
544+
return QString();
545+
}
546+
488547
QgsUnitTypes::AreaUnit QgsUnitTypes::stringToAreaUnit( const QString& string, bool* ok )
489548
{
490549
QString normalized = string.trimmed().toLower();

src/core/qgsunittypes.h

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,14 @@ class CORE_EXPORT QgsUnitTypes
130130
*/
131131
Q_INVOKABLE static QString toString( QgsUnitTypes::DistanceUnit unit );
132132

133+
/** Returns a translated abbreviation representing a distance unit.
134+
* @param unit unit to convert to string
135+
* @see stringToDistanceUnit()
136+
*
137+
* @note Added in QGIS 3.0
138+
*/
139+
Q_INVOKABLE static QString toAbbreviatedString( QgsUnitTypes::DistanceUnit unit );
140+
133141
/** Converts a translated string to a distance unit.
134142
* @param string string representing a distance unit
135143
* @param ok optional boolean, will be set to true if string was converted successfully
@@ -171,6 +179,14 @@ class CORE_EXPORT QgsUnitTypes
171179
*/
172180
Q_INVOKABLE static QString toString( AreaUnit unit );
173181

182+
/** Returns a translated abbreviation representing an areal unit.
183+
* @param unit unit to convert to string
184+
* @see stringToAreaUnit()
185+
*
186+
* @note Added in QGIS 3.0
187+
*/
188+
Q_INVOKABLE static QString toAbbreviatedString( AreaUnit unit );
189+
174190
/** Converts a translated string to an areal unit.
175191
* @param string string representing an areal unit
176192
* @param ok optional boolean, will be set to true if string was converted successfully

0 commit comments

Comments
 (0)