Skip to content

Commit

Permalink
Add Points as valid render unit (for font sizes)
Browse files Browse the repository at this point in the history
  • Loading branch information
nyalldawson committed Oct 24, 2016
1 parent 7967110 commit 687c66f
Show file tree
Hide file tree
Showing 8 changed files with 58 additions and 1 deletion.
1 change: 1 addition & 0 deletions python/core/qgsunittypes.sip
Original file line number Diff line number Diff line change
Expand Up @@ -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
};

Expand Down
4 changes: 4 additions & 0 deletions src/app/qgsmaptooloffsetpointsymbol.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
4 changes: 4 additions & 0 deletions src/core/qgsunittypes.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1079,6 +1079,8 @@ QString QgsUnitTypes::encodeUnit( RenderUnit unit )
return "Pixel";
case RenderPercentage:
return "Percentage";
case RenderPoints:
return "Point";
default:
return "MM";
}
Expand All @@ -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;
Expand Down
1 change: 1 addition & 0 deletions src/core/qgsunittypes.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
};

Expand Down
12 changes: 12 additions & 0 deletions src/core/symbology-ng/qgssymbollayerutils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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() );
Expand Down Expand Up @@ -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 );
Expand Down Expand Up @@ -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;
Expand All @@ -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 );
Expand Down Expand Up @@ -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;
Expand Down
4 changes: 4 additions & 0 deletions src/gui/qgsunitselectionwidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 );
Expand Down
30 changes: 30 additions & 0 deletions tests/src/python/test_qgsmapunitscale.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand All @@ -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
Expand All @@ -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)

Expand All @@ -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)

Expand All @@ -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
Expand All @@ -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)

Expand All @@ -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)

Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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)

Expand All @@ -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
Expand All @@ -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)

Expand All @@ -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)

Expand Down
3 changes: 2 additions & 1 deletion tests/src/python/test_qgsunittypes.py
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down

0 comments on commit 687c66f

Please sign in to comment.