Skip to content
Permalink
Browse files

Add Points as valid render unit (for font sizes)

  • Loading branch information
nyalldawson committed Oct 24, 2016
1 parent 7967110 commit 687c66f5d6766ffcc391464171390024626fcef2
@@ -68,6 +68,7 @@ class QgsUnitTypes
RenderMapUnits, //!< map units
RenderPixels, //!< pixels
RenderPercentage, //!< percentage of another measurement (eg canvas size, feature size)
RenderPoints, //! points (eg for font sizes)
RenderUnknownUnit, //!< mixed or unknown units
};

@@ -236,6 +236,10 @@ QPointF QgsMapToolOffsetPointSymbol::calculateOffset( const QgsPoint& startPoint
factor = 25.4 / mCanvas->mapSettings().outputDpi() / mCanvas->mapSettings().mapUnitsPerPixel() ;
break;

case QgsUnitTypes::RenderPoints:
factor = 2.83464567 * 25.4 / mCanvas->mapSettings().outputDpi() / mCanvas->mapSettings().mapUnitsPerPixel() ;
break;

case QgsUnitTypes::RenderPixels:
factor = 1.0 / mCanvas->mapSettings().mapUnitsPerPixel();
break;
@@ -1079,6 +1079,8 @@ QString QgsUnitTypes::encodeUnit( RenderUnit unit )
return "Pixel";
case RenderPercentage:
return "Percentage";
case RenderPoints:
return "Point";
default:
return "MM";
}
@@ -1099,6 +1101,8 @@ QgsUnitTypes::RenderUnit QgsUnitTypes::decodeRenderUnit( const QString& string,
return RenderPixels;
if ( normalized == encodeUnit( RenderPercentage ).toLower() )
return RenderPercentage;
if ( normalized == encodeUnit( RenderPoints ).toLower() )
return RenderPoints;

if ( ok )
*ok = false;
@@ -94,6 +94,7 @@ class CORE_EXPORT QgsUnitTypes
RenderMapUnits, //!< map units
RenderPixels, //!< pixels
RenderPercentage, //!< percentage of another measurement (eg canvas size, feature size)
RenderPoints, //! points (eg for font sizes)
RenderUnknownUnit, //!< mixed or unknown units
};

@@ -40,6 +40,8 @@
#include <QRegExp>
#include <QPicture>

#define POINTS_TO_MM 2.83464567

QString QgsSymbolLayerUtils::encodeColor( const QColor& color )
{
return QString( "%1,%2,%3,%4" ).arg( color.red() ).arg( color.green() ).arg( color.blue() ).arg( color.alpha() );
@@ -3198,6 +3200,8 @@ double QgsSymbolLayerUtils::lineWidthScaleFactor( const QgsRenderContext& c, Qgs
{
case QgsUnitTypes::RenderMillimeters:
return c.scaleFactor();
case QgsUnitTypes::RenderPoints:
return c.scaleFactor() * POINTS_TO_MM;
case QgsUnitTypes::RenderMapUnits:
{
double mup = scale.computeMapUnitsPerPixel( c );
@@ -3274,6 +3278,10 @@ double QgsSymbolLayerUtils::convertToMapUnits( const QgsRenderContext &c, double
{
return size * c.scaleFactor() * c.rasterScaleFactor() * mup;
}
case QgsUnitTypes::RenderPoints:
{
return size * c.scaleFactor() * c.rasterScaleFactor() * mup / POINTS_TO_MM;
}
case QgsUnitTypes::RenderPixels:
{
return size * mup;
@@ -3293,6 +3301,8 @@ double QgsSymbolLayerUtils::pixelSizeScaleFactor( const QgsRenderContext& c, Qgs
{
case QgsUnitTypes::RenderMillimeters:
return ( c.scaleFactor() * c.rasterScaleFactor() );
case QgsUnitTypes::RenderPoints:
return ( c.scaleFactor() * c.rasterScaleFactor() ) * POINTS_TO_MM;
case QgsUnitTypes::RenderMapUnits:
{
double mup = scale.computeMapUnitsPerPixel( c );
@@ -3321,6 +3331,8 @@ double QgsSymbolLayerUtils::mapUnitScaleFactor( const QgsRenderContext &c, QgsUn
{
case QgsUnitTypes::RenderMillimeters:
return scale.computeMapUnitsPerPixel( c ) * c.scaleFactor() * c.rasterScaleFactor();
case QgsUnitTypes::RenderPoints:
return scale.computeMapUnitsPerPixel( c ) * c.scaleFactor() * c.rasterScaleFactor() * POINTS_TO_MM;
case QgsUnitTypes::RenderMapUnits:
{
return 1.0;
@@ -162,6 +162,10 @@ void QgsUnitSelectionWidget::setUnits( const QgsUnitTypes::RenderUnitList &units
{
mUnitCombo->addItem( tr( "Millimeter" ), QgsUnitTypes::RenderMillimeters );
}
if ( units.contains( QgsUnitTypes::RenderPoints ) )
{
mUnitCombo->addItem( tr( "Points" ), QgsUnitTypes::RenderPoints );
}
if ( units.contains( QgsUnitTypes::RenderPixels ) )
{
mUnitCombo->addItem( tr( "Pixels" ), QgsUnitTypes::RenderPixels );
@@ -140,6 +140,8 @@ def testLineWidthScaleFactor(self):
self.assertAlmostEqual(sf, 0.5, places=5)
sf = QgsSymbolLayerUtils.lineWidthScaleFactor(r, QgsUnitTypes.RenderMillimeters, c)
self.assertAlmostEqual(sf, 11.8110236, places=5)
sf = QgsSymbolLayerUtils.lineWidthScaleFactor(r, QgsUnitTypes.RenderPoints, c)
self.assertAlmostEqual(sf, 33.4800668, places=5)
sf = QgsSymbolLayerUtils.lineWidthScaleFactor(r, QgsUnitTypes.RenderPixels, c)
self.assertAlmostEqual(sf, 1.0, places=5)

@@ -150,6 +152,8 @@ def testLineWidthScaleFactor(self):
# only conversion from mapunits should be affected
sf = QgsSymbolLayerUtils.lineWidthScaleFactor(r, QgsUnitTypes.RenderMillimeters, c)
self.assertAlmostEqual(sf, 11.8110236, places=5)
sf = QgsSymbolLayerUtils.lineWidthScaleFactor(r, QgsUnitTypes.RenderPoints, c)
self.assertAlmostEqual(sf, 33.4800668, places=5)
sf = QgsSymbolLayerUtils.lineWidthScaleFactor(r, QgsUnitTypes.RenderPixels, c)
self.assertAlmostEqual(sf, 1.0, places=5)
c.minScale = 0
@@ -161,6 +165,8 @@ def testLineWidthScaleFactor(self):
# only conversion from mapunits should be affected
sf = QgsSymbolLayerUtils.lineWidthScaleFactor(r, QgsUnitTypes.RenderMillimeters, c)
self.assertAlmostEqual(sf, 11.8110236, places=5)
sf = QgsSymbolLayerUtils.lineWidthScaleFactor(r, QgsUnitTypes.RenderPoints, c)
self.assertAlmostEqual(sf, 33.4800668, places=5)
sf = QgsSymbolLayerUtils.lineWidthScaleFactor(r, QgsUnitTypes.RenderPixels, c)
self.assertAlmostEqual(sf, 1.0, places=5)

@@ -182,6 +188,8 @@ def testConvertToPainterUnits(self):
self.assertAlmostEqual(size, 1.0, places=5)
size = QgsSymbolLayerUtils.convertToPainterUnits(r, 2, QgsUnitTypes.RenderMillimeters, c)
self.assertAlmostEqual(size, 23.622047, places=5)
size = QgsSymbolLayerUtils.convertToPainterUnits(r, 2, QgsUnitTypes.RenderPoints, c)
self.assertAlmostEqual(size, 66.9601332, places=5)
size = QgsSymbolLayerUtils.convertToPainterUnits(r, 2, QgsUnitTypes.RenderPixels, c)
self.assertAlmostEqual(size, 2.0, places=5)

@@ -193,6 +201,8 @@ def testConvertToPainterUnits(self):
# only conversion from mapunits should be affected
size = QgsSymbolLayerUtils.convertToPainterUnits(r, 2, QgsUnitTypes.RenderMillimeters, c)
self.assertAlmostEqual(size, 23.622047, places=5)
size = QgsSymbolLayerUtils.convertToPainterUnits(r, 2, QgsUnitTypes.RenderPoints, c)
self.assertAlmostEqual(size, 66.9601332, places=5)
size = QgsSymbolLayerUtils.convertToPainterUnits(r, 2, QgsUnitTypes.RenderPixels, c)
self.assertAlmostEqual(size, 2.0, places=5)
c.minSizeMMEnabled = False
@@ -205,6 +215,8 @@ def testConvertToPainterUnits(self):
# only conversion from mapunits should be affected
size = QgsSymbolLayerUtils.convertToPainterUnits(r, 2, QgsUnitTypes.RenderMillimeters, c)
self.assertAlmostEqual(size, 23.622047, places=5)
size = QgsSymbolLayerUtils.convertToPainterUnits(r, 2, QgsUnitTypes.RenderPoints, c)
self.assertAlmostEqual(size, 66.9601332, places=5)
size = QgsSymbolLayerUtils.convertToPainterUnits(r, 2, QgsUnitTypes.RenderPixels, c)
self.assertAlmostEqual(size, 2.0, places=5)

@@ -226,6 +238,8 @@ def testConvertToMapUnits(self):
self.assertEqual(size, 2.0)
size = QgsSymbolLayerUtils.convertToMapUnits(r, 2, QgsUnitTypes.RenderMillimeters, c)
self.assertAlmostEqual(size, 47.244094, places=5)
size = QgsSymbolLayerUtils.convertToMapUnits(r, 5.66929, QgsUnitTypes.RenderPoints, c)
self.assertAlmostEqual(size, 47.2440833, places=5)
size = QgsSymbolLayerUtils.convertToMapUnits(r, 2, QgsUnitTypes.RenderPixels, c)
self.assertAlmostEqual(size, 4.0, places=5)

@@ -237,6 +251,8 @@ def testConvertToMapUnits(self):
# only conversion from mapunits should be affected
size = QgsSymbolLayerUtils.convertToMapUnits(r, 2, QgsUnitTypes.RenderMillimeters, c)
self.assertAlmostEqual(size, 47.244094, places=5)
size = QgsSymbolLayerUtils.convertToMapUnits(r, 5.66929, QgsUnitTypes.RenderPoints, c)
self.assertAlmostEqual(size, 47.2440833, places=5)
size = QgsSymbolLayerUtils.convertToMapUnits(r, 2, QgsUnitTypes.RenderPixels, c)
self.assertAlmostEqual(size, 4.0, places=5)
c.minSizeMMEnabled = False
@@ -249,6 +265,8 @@ def testConvertToMapUnits(self):
# only conversion from mapunits should be affected
size = QgsSymbolLayerUtils.convertToMapUnits(r, 2, QgsUnitTypes.RenderMillimeters, c)
self.assertAlmostEqual(size, 47.244094, places=5)
size = QgsSymbolLayerUtils.convertToMapUnits(r, 5.66929, QgsUnitTypes.RenderPoints, c)
self.assertAlmostEqual(size, 47.2440833, places=5)
size = QgsSymbolLayerUtils.convertToMapUnits(r, 2, QgsUnitTypes.RenderPixels, c)
self.assertAlmostEqual(size, 4.0, places=5)
c.maxSizeMMEnabled = False
@@ -260,6 +278,8 @@ def testConvertToMapUnits(self):
# only conversion from mapunits should be affected
size = QgsSymbolLayerUtils.convertToMapUnits(r, 2, QgsUnitTypes.RenderMillimeters, c)
self.assertAlmostEqual(size, 47.244094, places=5)
size = QgsSymbolLayerUtils.convertToMapUnits(r, 5.66929, QgsUnitTypes.RenderPoints, c)
self.assertAlmostEqual(size, 47.2440833, places=5)
size = QgsSymbolLayerUtils.convertToMapUnits(r, 2, QgsUnitTypes.RenderPixels, c)
self.assertAlmostEqual(size, 4.0, places=5)
c.minScale = 0
@@ -271,6 +291,8 @@ def testConvertToMapUnits(self):
# only conversion from mapunits should be affected
size = QgsSymbolLayerUtils.convertToMapUnits(r, 2, QgsUnitTypes.RenderMillimeters, c)
self.assertAlmostEqual(size, 47.244094, places=5)
size = QgsSymbolLayerUtils.convertToMapUnits(r, 5.66929, QgsUnitTypes.RenderPoints, c)
self.assertAlmostEqual(size, 47.2440833, places=5)
size = QgsSymbolLayerUtils.convertToMapUnits(r, 2, QgsUnitTypes.RenderPixels, c)
self.assertAlmostEqual(size, 4.0, places=5)
c.maxScale = 0
@@ -293,6 +315,8 @@ def testPixelSizeScaleFactor(self):
self.assertAlmostEqual(sf, 0.5, places=5)
sf = QgsSymbolLayerUtils.pixelSizeScaleFactor(r, QgsUnitTypes.RenderMillimeters, c)
self.assertAlmostEqual(sf, 11.8110236, places=5)
sf = QgsSymbolLayerUtils.pixelSizeScaleFactor(r, QgsUnitTypes.RenderPoints, c)
self.assertAlmostEqual(sf, 33.48006689762, places=5)
sf = QgsSymbolLayerUtils.pixelSizeScaleFactor(r, QgsUnitTypes.RenderPixels, c)
self.assertAlmostEqual(sf, 1.0, places=5)

@@ -303,6 +327,8 @@ def testPixelSizeScaleFactor(self):
# only conversion from mapunits should be affected
sf = QgsSymbolLayerUtils.pixelSizeScaleFactor(r, QgsUnitTypes.RenderMillimeters, c)
self.assertAlmostEqual(sf, 11.811023, places=5)
sf = QgsSymbolLayerUtils.pixelSizeScaleFactor(r, QgsUnitTypes.RenderPoints, c)
self.assertAlmostEqual(sf, 33.48006689762, places=5)
sf = QgsSymbolLayerUtils.pixelSizeScaleFactor(r, QgsUnitTypes.RenderPixels, c)
self.assertAlmostEqual(sf, 1.0, places=5)
c.minScale = 0
@@ -314,6 +340,8 @@ def testPixelSizeScaleFactor(self):
# only conversion from mapunits should be affected
sf = QgsSymbolLayerUtils.pixelSizeScaleFactor(r, QgsUnitTypes.RenderMillimeters, c)
self.assertAlmostEqual(sf, 11.8110236, places=5)
sf = QgsSymbolLayerUtils.pixelSizeScaleFactor(r, QgsUnitTypes.RenderPoints, c)
self.assertAlmostEqual(sf, 33.48006689762, places=5)
sf = QgsSymbolLayerUtils.pixelSizeScaleFactor(r, QgsUnitTypes.RenderPixels, c)
self.assertAlmostEqual(sf, 1.0, places=5)

@@ -333,6 +361,8 @@ def testMapUnitScaleFactor(self):
self.assertAlmostEqual(sf, 1.0, places=5)
sf = QgsSymbolLayerUtils.mapUnitScaleFactor(r, QgsUnitTypes.RenderMillimeters, c)
self.assertAlmostEqual(sf, 23.622047, places=5)
sf = QgsSymbolLayerUtils.mapUnitScaleFactor(r, QgsUnitTypes.RenderPoints, c)
self.assertAlmostEqual(sf, 66.9601332, places=5)
sf = QgsSymbolLayerUtils.mapUnitScaleFactor(r, QgsUnitTypes.RenderPixels, c)
self.assertAlmostEqual(sf, 2.0, places=5)

@@ -174,7 +174,8 @@ def testEncodeDecodeRenderUnits(self):
units = [QgsUnitTypes.RenderMillimeters,
QgsUnitTypes.RenderMapUnits,
QgsUnitTypes.RenderPixels,
QgsUnitTypes.RenderPercentage]
QgsUnitTypes.RenderPercentage,
QgsUnitTypes.RenderPoints]

for u in units:
res, ok = QgsUnitTypes.decodeRenderUnit(QgsUnitTypes.encodeUnit(u))

0 comments on commit 687c66f

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