Skip to content
Permalink
Browse files
Consolidate all render context unit conversion to single methods
within QgsRenderContext

Fix invalid conversions, update unit tests
  • Loading branch information
nyalldawson committed Jan 22, 2017
1 parent dd187a3 commit b7d89a73afa2fdb110f07c8180a1285dd118586e
Show file tree
Hide file tree
Showing 45 changed files with 602 additions and 762 deletions.
@@ -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}
@@ -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
@@ -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;

};
@@ -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
@@ -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 );

@@ -6374,6 +6374,7 @@ specialise:specialize
speciallized:specialized
speciefied:specified
specifc:specific
specied:specified
specifed:specified
specificatin:specification
specificaton:specification
@@ -164,6 +164,7 @@ SET(QGIS_CORE_SRCS
qgsmapsettings.cpp
qgsmaptopixel.cpp
qgsmaptopixelgeometrysimplifier.cpp
qgsmapunitscale.cpp
qgsmessagelog.cpp
qgsmessageoutput.cpp
qgsmimedatautils.cpp
@@ -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 )
@@ -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
@@ -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;
@@ -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 );
@@ -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 ),
@@ -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 );
@@ -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 );
@@ -21,6 +21,7 @@
#include "qgspainteffect.h"
#include "qgis.h"
#include "qgsmapunitscale.h"
#include "qgsunittypes.h"
#include <QPainter>

/** \ingroup core
@@ -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() )
@@ -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 );
}
@@ -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
@@ -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,8 +19,10 @@
#define QGSMAPUNITSCALE_H

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

class QgsRenderContext;

/** \ingroup core
* \class QgsMapUnitScale
@@ -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
{
@@ -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();
@@ -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() ) )
@@ -66,7 +66,7 @@ class QgsPalLayerSettings;
class QgsVectorLayerLabelProvider;
class QgsDxfExport;
class QgsVectorLayerDiagramProvider;

class QgsExpressionContext;


/** \ingroup core

0 comments on commit b7d89a7

Please sign in to comment.