Skip to content
Permalink
Browse files

Add method to convert QgsUnitTypes::RenderUnit to translated string

  • Loading branch information
nyalldawson committed Jun 1, 2017
1 parent bbec00f commit 4d936cea2b152f7987076a810adf7de2325e4171
Showing with 52 additions and 0 deletions.
  1. +6 −0 python/core/qgsunittypes.sip
  2. +29 −0 src/core/qgsunittypes.cpp
  3. +5 −0 src/core/qgsunittypes.h
  4. +12 −0 tests/src/python/test_qgsunittypes.py
@@ -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
};

/************************************************************************
@@ -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();
}
@@ -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
@@ -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"""

0 comments on commit 4d936ce

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