Skip to content

Commit

Permalink
Consolidate all render context unit conversion to single methods
Browse files Browse the repository at this point in the history
within QgsRenderContext

Fix invalid conversions, update unit tests
  • Loading branch information
nyalldawson committed Jan 22, 2017
1 parent dd187a3 commit b7d89a7
Show file tree
Hide file tree
Showing 45 changed files with 602 additions and 762 deletions.
5 changes: 5 additions & 0 deletions doc/api_break.dox
Expand Up @@ -1718,6 +1718,11 @@ QgsSymbolLayerUtils (renamed from QgsSymbolLayerUtilsV2) {#qgis_api_break
- The signatures for wellKnownMarkerToSld() and wellKnownMarkerFromSld() were changed.
- The symbolPreviewPixmap() customContext is now the fourth parameter
- createRenderContext() was moved to QgsRenderContext::fromQPainter()
- lineWidthScaleFactor() and convertToPainterUnits() were moved to QgsRenderContext::convertToPainterUnits()
- convertToMapUnits() was moved to QgsRenderContext::convertToMapUnits()
- convertFromMapUnits() was moved to QgsRenderContext::convertFromMapUnits()
- pixelSizeScaleFactor() was removed. Use QgsRenderContext::convertToPainterUnits() instead.
- mapUnitScaleFactor() was removed. Use QgsRenderContext::convertToMapUnits() instead.


QgsSymbolSelectorWidget {#qgis_api_break_3_0_QgsSymbolSelectorWidget}
Expand Down
2 changes: 1 addition & 1 deletion python/core/diagram/qgsdiagram.sip
Expand Up @@ -61,7 +61,7 @@ class QgsDiagram
*
* @return The converted length for rendering
*/
float sizePainterUnits( float l, const QgsDiagramSettings& s, const QgsRenderContext& c );
double sizePainterUnits( double l, const QgsDiagramSettings& s, const QgsRenderContext& c );

/** Calculates a size to match the current settings and rendering context
* @param s The settings that contain the font size and size type
Expand Down
5 changes: 5 additions & 0 deletions python/core/qgsrendercontext.sip
Expand Up @@ -196,4 +196,9 @@ class QgsRenderContext
void setSegmentationToleranceType( QgsAbstractGeometry::SegmentationToleranceType type );
/** Gets segmentation tolerance type (maximum angle or maximum difference between curve and approximation)*/
QgsAbstractGeometry::SegmentationToleranceType segmentationToleranceType() const;

double convertToPainterUnits( double size, QgsUnitTypes::RenderUnit unit, const QgsMapUnitScale& scale = QgsMapUnitScale() ) const;
double convertToMapUnits( double size, QgsUnitTypes::RenderUnit unit, const QgsMapUnitScale& scale = QgsMapUnitScale() ) const;
double convertFromMapUnits( double sizeInMapUnits, QgsUnitTypes::RenderUnit outputUnit ) const;

};
9 changes: 0 additions & 9 deletions python/core/qgstextrenderer.sip
Expand Up @@ -1054,15 +1054,6 @@ class QgsTextRenderer
*/
static int sizeToPixel( double size, const QgsRenderContext& c, QgsUnitTypes::RenderUnit unit, const QgsMapUnitScale& mapUnitScale = QgsMapUnitScale() );

/** Calculates size (considering output size should be in pixel or map units, scale factors and optionally oversampling)
* @param size size to convert
* @param c rendercontext
* @param unit size units
* @param mapUnitScale a mapUnitScale clamper
* @return size that will render, as double
*/
static double scaleToPixelContext( double size, const QgsRenderContext& c, QgsUnitTypes::RenderUnit unit, const QgsMapUnitScale& mapUnitScale = QgsMapUnitScale() );

/** Draws text within a rectangle using the specified settings.
* @param rect destination rectangle for text
* @param rotation text rotation
Expand Down
50 changes: 0 additions & 50 deletions python/core/symbology-ng/qgssymbollayerutils.sip
Expand Up @@ -380,56 +380,6 @@ class QgsSymbolLayerUtils
*/
static QColor parseColorWithAlpha( const QString& colorStr, bool &containsAlpha, bool strictEval = false );

/** Returns the line width scale factor depending on the unit and the paint device.
* Consider using convertToPainterUnits() instead, as convertToPainterUnits() respects the size limits specified by the scale
* parameter.
* @param c render context
* @param u units to convert from
* @param scale map unit scale, specifying limits for the map units to convert from
* @see convertToPainterUnits()
*/
static double lineWidthScaleFactor( const QgsRenderContext& c, QgsUnitTypes::RenderUnit u, const QgsMapUnitScale& scale = QgsMapUnitScale() );

/** Converts a size from the specied units to painter units. The conversion respects the limits
* specified by the optional scale parameter.
* @param c render context
* @param size size to convert
* @param unit units for specified size
* @param scale map unit scale
* @note added in QGIS 2.12
* @see lineWidthScaleFactor()
* @see convertToMapUnits()
*/
static double convertToPainterUnits( const QgsRenderContext&c, double size, QgsUnitTypes::RenderUnit unit, const QgsMapUnitScale& scale = QgsMapUnitScale() );

/** Converts a size from the specied units to map units. The conversion respects the limits
* specified by the optional scale parameter.
* @param c render context
* @param size size to convert
* @param unit units for specified size
* @param scale map unit scale
* @note added in QGIS 2.16
* @see convertFromMapUnits()
* @see convertToPainterUnits()
*/
static double convertToMapUnits( const QgsRenderContext&c, double size, QgsUnitTypes::RenderUnit unit, const QgsMapUnitScale& scale = QgsMapUnitScale() );

/** Converts a size from map units to the specied units.
* @param context render context
* @param sizeInMapUnits size (in map units) to convert
* @param outputUnit output units
* @note added in QGIS 3.0
* @see convertToMapUnits()
*/
static double convertFromMapUnits( const QgsRenderContext& context, double sizeInMapUnits, QgsUnitTypes::RenderUnit outputUnit );


/** Returns scale factor painter units -> pixel dimensions*/
static double pixelSizeScaleFactor( const QgsRenderContext& c, QgsUnitTypes::RenderUnit u, const QgsMapUnitScale& scale = QgsMapUnitScale() );

/** Returns scale factor painter units -> map units*/
static double mapUnitScaleFactor( const QgsRenderContext& c, QgsUnitTypes::RenderUnit u, const QgsMapUnitScale& scale = QgsMapUnitScale() );

/** Multiplies opacity of image pixel values with a (global) transparency value*/
static void multiplyImageOpacity( QImage* image, qreal alpha );

Expand Down
1 change: 1 addition & 0 deletions scripts/spell_check/spelling.dat
Expand Up @@ -6374,6 +6374,7 @@ specialise:specialize
speciallized:specialized
speciefied:specified
specifc:specific
specied:specified
specifed:specified
specificatin:specification
specificaton:specification
Expand Down
1 change: 1 addition & 0 deletions src/core/CMakeLists.txt
Expand Up @@ -164,6 +164,7 @@ SET(QGIS_CORE_SRCS
qgsmapsettings.cpp
qgsmaptopixel.cpp
qgsmaptopixelgeometrysimplifier.cpp
qgsmapunitscale.cpp
qgsmessagelog.cpp
qgsmessageoutput.cpp
qgsmimedatautils.cpp
Expand Down
8 changes: 4 additions & 4 deletions src/core/diagram/qgsdiagram.cpp
Expand Up @@ -58,18 +58,18 @@ QgsExpression *QgsDiagram::getExpression( const QString &expression, const QgsEx

void QgsDiagram::setPenWidth( QPen& pen, const QgsDiagramSettings& s, const QgsRenderContext& c )
{
pen.setWidthF( QgsSymbolLayerUtils::convertToPainterUnits( c, s.penWidth, s.lineSizeUnit, s.lineSizeScale ) );
pen.setWidthF( c.convertToPainterUnits( s.penWidth, s.lineSizeUnit, s.lineSizeScale ) );
}


QSizeF QgsDiagram::sizePainterUnits( QSizeF size, const QgsDiagramSettings& s, const QgsRenderContext& c )
{
return QSizeF( QgsSymbolLayerUtils::convertToPainterUnits( c, size.width(), s.sizeType, s.sizeScale ), QgsSymbolLayerUtils::convertToPainterUnits( c, size.height(), s.sizeType, s.sizeScale ) );
return QSizeF( c.convertToPainterUnits( size.width(), s.sizeType, s.sizeScale ), c.convertToPainterUnits( size.height(), s.sizeType, s.sizeScale ) );
}

float QgsDiagram::sizePainterUnits( float l, const QgsDiagramSettings& s, const QgsRenderContext& c )
double QgsDiagram::sizePainterUnits( double l, const QgsDiagramSettings& s, const QgsRenderContext& c )
{
return QgsSymbolLayerUtils::convertToPainterUnits( c, l, s.sizeType, s.sizeScale );
return c.convertToPainterUnits( l, s.sizeType, s.sizeScale );
}

QFont QgsDiagram::scaledFont( const QgsDiagramSettings& s, const QgsRenderContext& c )
Expand Down
2 changes: 1 addition & 1 deletion src/core/diagram/qgsdiagram.h
Expand Up @@ -98,7 +98,7 @@ class CORE_EXPORT QgsDiagram
*
* @return The converted length for rendering
*/
float sizePainterUnits( float l, const QgsDiagramSettings& s, const QgsRenderContext& c );
double sizePainterUnits( double l, const QgsDiagramSettings& s, const QgsRenderContext& c );

/** Calculates a size to match the current settings and rendering context
* @param s The settings that contain the font size and size type
Expand Down
4 changes: 2 additions & 2 deletions src/core/effects/qgsgloweffect.cpp
Expand Up @@ -79,7 +79,7 @@ void QgsGlowEffect::draw( QgsRenderContext &context )
}

QgsImageOperation::DistanceTransformProperties dtProps;
dtProps.spread = mSpread * QgsSymbolLayerUtils::pixelSizeScaleFactor( context, mSpreadUnit, mSpreadMapUnitScale );
dtProps.spread = context.convertToPainterUnits( mSpread, mSpreadUnit, mSpreadMapUnitScale );
dtProps.useMaxDistance = false;
dtProps.shadeExterior = shadeExterior();
dtProps.ramp = ramp;
Expand Down Expand Up @@ -213,7 +213,7 @@ QgsGlowEffect &QgsGlowEffect::operator=( const QgsGlowEffect & rhs )
QRectF QgsGlowEffect::boundingRect( const QRectF &rect, const QgsRenderContext& context ) const
{
//spread size
double spread = mSpread * QgsSymbolLayerUtils::pixelSizeScaleFactor( context, mSpreadUnit, mSpreadMapUnitScale );
double spread = context.convertToPainterUnits( mSpread, mSpreadUnit, mSpreadMapUnitScale );
//plus possible extension due to blur, with a couple of extra pixels thrown in for safety
spread += mBlurLevel * 2 + 10;
return rect.adjusted( -spread, -spread, spread, spread );
Expand Down
5 changes: 2 additions & 3 deletions src/core/effects/qgsshadoweffect.cpp
Expand Up @@ -54,8 +54,7 @@ void QgsShadowEffect::draw( QgsRenderContext &context )
QgsImageOperation::overlayColor( colorisedIm, mColor );
QgsImageOperation::stackBlur( colorisedIm, mBlurLevel );

double offsetDist = mOffsetDist *
QgsSymbolLayerUtils::pixelSizeScaleFactor( context, mOffsetUnit, mOffsetMapUnitScale );
double offsetDist = context.convertToPainterUnits( mOffsetDist, mOffsetUnit, mOffsetMapUnitScale );

double angleRad = mOffsetAngle * M_PI / 180; // to radians
QPointF transPt( -offsetDist * cos( angleRad + M_PI / 2 ),
Expand Down Expand Up @@ -145,7 +144,7 @@ void QgsShadowEffect::readProperties( const QgsStringMap &props )
QRectF QgsShadowEffect::boundingRect( const QRectF &rect, const QgsRenderContext& context ) const
{
//offset distance
double spread = mOffsetDist * QgsSymbolLayerUtils::pixelSizeScaleFactor( context, mOffsetUnit, mOffsetMapUnitScale );
double spread = context.convertToPainterUnits( mOffsetDist, mOffsetUnit, mOffsetMapUnitScale );
//plus possible extension due to blur, with a couple of extra pixels thrown in for safety
spread += mBlurLevel * 2 + 10;
return rect.adjusted( -spread, -spread, spread, spread );
Expand Down
6 changes: 2 additions & 4 deletions src/core/effects/qgstransformeffect.cpp
Expand Up @@ -122,10 +122,8 @@ QTransform QgsTransformEffect::createTransform( const QgsRenderContext& context
//remember that the below operations are effectively performed in the opposite order
//so, first the reflection applies, then scale, shear, rotate and lastly translation

double translateX = mTranslateX *
QgsSymbolLayerUtils::pixelSizeScaleFactor( context, mTranslateUnit, mTranslateMapUnitScale );
double translateY = mTranslateY *
QgsSymbolLayerUtils::pixelSizeScaleFactor( context, mTranslateUnit, mTranslateMapUnitScale );
double translateX = context.convertToPainterUnits( mTranslateX, mTranslateUnit, mTranslateMapUnitScale );
double translateY = context.convertToPainterUnits( mTranslateY, mTranslateUnit, mTranslateMapUnitScale );

t.translate( translateX + left + width / 2.0,
translateY + top + height / 2.0 );
Expand Down
1 change: 1 addition & 0 deletions src/core/effects/qgstransformeffect.h
Expand Up @@ -21,6 +21,7 @@
#include "qgspainteffect.h"
#include "qgis.h"
#include "qgsmapunitscale.h"
#include "qgsunittypes.h"
#include <QPainter>

/** \ingroup core
Expand Down
2 changes: 1 addition & 1 deletion src/core/layertree/qgslayertreemodellegendnode.cpp
Expand Up @@ -362,7 +362,7 @@ QSizeF QgsSymbolLegendNode::drawSymbol( const QgsLegendSettings& settings, ItemC
if ( QgsMarkerSymbol* markerSymbol = dynamic_cast<QgsMarkerSymbol*>( s ) )
{
// allow marker symbol to occupy bigger area if necessary
double size = QgsSymbolLayerUtils::convertToPainterUnits( context, markerSymbol->size(), markerSymbol->sizeUnit(), markerSymbol->sizeMapUnitScale() ) / context.scaleFactor();
double size = context.convertToPainterUnits( markerSymbol->size(), markerSymbol->sizeUnit(), markerSymbol->sizeMapUnitScale() ) / context.scaleFactor();
height = size;
width = size;
if ( width < settings.symbolSize().width() )
Expand Down
2 changes: 1 addition & 1 deletion src/core/qgsdiagramrenderer.cpp
Expand Up @@ -424,7 +424,7 @@ QSizeF QgsDiagramRenderer::sizeMapUnits( const QgsFeature& feature, const QgsRen
QSizeF size = diagramSize( feature, c );
if ( size.isValid() )
{
double width = QgsSymbolLayerUtils::convertToMapUnits( c, size.width(), s.sizeType, s.sizeScale );
double width = c.convertToMapUnits( size.width(), s.sizeType, s.sizeScale );
size.rheight() *= width / size.width();
size.setWidth( width );
}
Expand Down
2 changes: 1 addition & 1 deletion src/core/qgslabelingengine.cpp
Expand Up @@ -24,7 +24,7 @@
#include "layer.h"
#include "pal.h"
#include "problem.h"

#include "qgsrendercontext.h"


// helper function for checking for job cancelation within PAL
Expand Down
33 changes: 33 additions & 0 deletions src/core/qgsmapunitscale.cpp
@@ -0,0 +1,33 @@
/***************************************************************************
qgsmapunitscale.cpp
-------------------
begin : April 2014
copyright : (C) Sandro Mani
email : smani at sourcepole dot ch
***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/

#include "qgsmapunitscale.h"
#include "qgsrendercontext.h"

double QgsMapUnitScale::computeMapUnitsPerPixel( const QgsRenderContext& c ) const
{
double mup = c.mapToPixel().mapUnitsPerPixel();
double renderScale = c.rendererScale(); // Note: this value is 1 / scale
if ( !qgsDoubleNear( minScale, 0 ) )
{
mup = qMin( mup / ( minScale * renderScale ), mup );
}
if ( !qgsDoubleNear( maxScale, 0 ) )
{
mup = qMax( mup / ( maxScale * renderScale ), mup );
}
return mup;
}
19 changes: 4 additions & 15 deletions src/core/qgsmapunitscale.h
Expand Up @@ -19,8 +19,10 @@
#define QGSMAPUNITSCALE_H

#include "qgis_core.h"
#include "qgis.h"
#include <QtCore>
#include "qgsrendercontext.h"

class QgsRenderContext;

/** \ingroup core
* \class QgsMapUnitScale
Expand Down Expand Up @@ -67,20 +69,7 @@ class CORE_EXPORT QgsMapUnitScale
* @param c render context
* @returns map units per pixel, limited between minimum and maximum scales
*/
double computeMapUnitsPerPixel( const QgsRenderContext& c ) const
{
double mup = c.mapToPixel().mapUnitsPerPixel();
double renderScale = c.rendererScale(); // Note: this value is 1 / scale
if ( !qgsDoubleNear( minScale, 0 ) )
{
mup = qMin( mup / ( minScale * renderScale ), mup );
}
if ( !qgsDoubleNear( maxScale, 0 ) )
{
mup = qMax( mup / ( maxScale * renderScale ), mup );
}
return mup;
}
double computeMapUnitsPerPixel( const QgsRenderContext& c ) const;

bool operator==( const QgsMapUnitScale& other ) const
{
Expand Down
4 changes: 2 additions & 2 deletions src/core/qgspallabeling.cpp
Expand Up @@ -2704,7 +2704,7 @@ void QgsPalLayerSettings::parseTextStyle( QFont& labelFont,
wordspace = wspacing;
}
}
labelFont.setWordSpacing( QgsTextRenderer::scaleToPixelContext( wordspace, context, fontunits, mFormat.sizeMapUnitScale() ) );
labelFont.setWordSpacing( context.convertToPainterUnits( wordspace, fontunits, mFormat.sizeMapUnitScale() ) );

// data defined letter spacing?
double letterspace = labelFont.letterSpacing();
Expand All @@ -2718,7 +2718,7 @@ void QgsPalLayerSettings::parseTextStyle( QFont& labelFont,
letterspace = lspacing;
}
}
labelFont.setLetterSpacing( QFont::AbsoluteSpacing, QgsTextRenderer::scaleToPixelContext( letterspace, context, fontunits, mFormat.sizeMapUnitScale() ) );
labelFont.setLetterSpacing( QFont::AbsoluteSpacing, context.convertToPainterUnits( letterspace, fontunits, mFormat.sizeMapUnitScale() ) );

// data defined strikeout font style?
if ( dataDefinedEvaluate( QgsPalLayerSettings::Strikeout, exprVal, &context.expressionContext(), labelFont.strikeOut() ) )
Expand Down
2 changes: 1 addition & 1 deletion src/core/qgspallabeling.h
Expand Up @@ -66,7 +66,7 @@ class QgsPalLayerSettings;
class QgsVectorLayerLabelProvider;
class QgsDxfExport;
class QgsVectorLayerDiagramProvider;

class QgsExpressionContext;


/** \ingroup core
Expand Down

0 comments on commit b7d89a7

Please sign in to comment.