Skip to content

Commit

Permalink
Add method to convert QgsUnitTypes::RenderUnit to translated string
Browse files Browse the repository at this point in the history
  • Loading branch information
nyalldawson committed Jun 1, 2017
1 parent bbec00f commit 4d936ce
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 0 deletions.
6 changes: 6 additions & 0 deletions python/core/qgsunittypes.sip
Original file line number Diff line number Diff line change
Expand Up @@ -362,6 +362,12 @@ class QgsUnitTypes
:rtype: RenderUnit
%End

static QString toString( RenderUnit unit );
%Docstring
Returns a translated string representing a render ``unit``.
.. versionadded:: 3.0
:rtype: str
%End
};

/************************************************************************
Expand Down
29 changes: 29 additions & 0 deletions src/core/qgsunittypes.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1744,3 +1744,32 @@ QgsUnitTypes::RenderUnit QgsUnitTypes::decodeRenderUnit( const QString &string,
// millimeters are default
return RenderMillimeters;
}

QString QgsUnitTypes::toString( QgsUnitTypes::RenderUnit unit )
{
switch ( unit )
{
case RenderMillimeters:
return QObject::tr( "millimeters", "render" );

case RenderMapUnits:
return QObject::tr( "map units", "render" );

case RenderPixels:
return QObject::tr( "pixels", "render" );

case RenderPercentage:
return QObject::tr( "percent", "render" );

case RenderPoints:
return QObject::tr( "points", "render" );

case RenderInches:
return QObject::tr( "inches", "render" );

case RenderUnknownUnit:
return QObject::tr( "<unknown>", "render" );

}
return QString();
}
5 changes: 5 additions & 0 deletions src/core/qgsunittypes.h
Original file line number Diff line number Diff line change
Expand Up @@ -349,6 +349,11 @@ class CORE_EXPORT QgsUnitTypes
*/
Q_INVOKABLE static RenderUnit decodeRenderUnit( const QString &string, bool *ok SIP_OUT = 0 );

/**
* Returns a translated string representing a render \a unit.
* \since QGIS 3.0
*/
Q_INVOKABLE static QString toString( RenderUnit unit );
};

#endif // QGSUNITTYPES_H
12 changes: 12 additions & 0 deletions tests/src/python/test_qgsunittypes.py
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,18 @@ def testEncodeDecodeRenderUnits(self):
assert ok
self.assertEqual(res, QgsUnitTypes.RenderPoints)

def testRenderUnitsString(self):
"""Test converting render units to strings"""
units = [QgsUnitTypes.RenderMillimeters,
QgsUnitTypes.RenderMapUnits,
QgsUnitTypes.RenderPixels,
QgsUnitTypes.RenderPercentage,
QgsUnitTypes.RenderPoints,
QgsUnitTypes.RenderInches]

for u in units:
self.assertTrue(QgsUnitTypes.toString(u))

def testFromUnitToUnitFactor(self):
"""Test calculation of conversion factor between units"""

Expand Down

0 comments on commit 4d936ce

Please sign in to comment.