Skip to content

Commit

Permalink
Move min/max symbol size handling to QgsSymbolLayerUtils
Browse files Browse the repository at this point in the history
  • Loading branch information
mhugent committed Jun 30, 2020
1 parent 9ecded8 commit 80b3a81
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 7 deletions.
11 changes: 11 additions & 0 deletions python/core/auto_generated/symbology/qgssymbollayerutils.sip.in
Expand Up @@ -813,6 +813,17 @@ Encodes a reference to a parametric SVG into a path with parameters according to
Converts a set of symbol layer id to a set of pointers to actual symbol layers carried by the feature renderer.

.. versionadded:: 3.12
%End

static bool restrictSymbolSize( double &size, double minSize, double maxSize );
%Docstring
Restricts size value to min/max size

:param size: value will be set so minSize if below minimum or to maxSize if above minimum
:param minSize: the minimum symbolSize
:param maxSize: the maximum symbol size

:return: true if size value has been changed
%End
};

Expand Down
12 changes: 5 additions & 7 deletions src/core/layertree/qgslayertreemodellegendnode.cpp
Expand Up @@ -567,13 +567,12 @@ QSizeF QgsSymbolLegendNode::drawSymbol( const QgsLegendSettings &settings, ItemC
double size = markerSymbol->size( *context ) / context->scaleFactor();

//If marker size exceeds max marker size, clone symbol and set max size in mm
if ( ( maxSymbolSize > 0 && size > maxSymbolSize ) || ( minSymbolSize > 0 && size < minSymbolSize ) )
if ( QgsSymbolLayerUtils::restrictSymbolSize( size, minSymbolSize, maxSymbolSize ) )
{
minMaxSizeMarkerSymbol.reset( dynamic_cast<QgsMarkerSymbol *>( s->clone() ) );
minMaxSizeMarkerSymbol->setSize( size > maxSymbolSize ? maxSymbolSize : minSymbolSize );
minMaxSizeMarkerSymbol->setSize( size );
minMaxSizeMarkerSymbol->setSizeUnit( QgsUnitTypes::RenderMillimeters );
s = minMaxSizeMarkerSymbol.get();
size = ( size > maxSymbolSize ) ? maxSymbolSize : minSymbolSize;
}

height = size;
Expand All @@ -592,15 +591,14 @@ QSizeF QgsSymbolLegendNode::drawSymbol( const QgsLegendSettings &settings, ItemC
if ( QgsLineSymbol *lineSymbol = dynamic_cast<QgsLineSymbol *>( s ) )
{
double width = lineSymbol->width( *context ) / context->scaleFactor();
if ( ( maxSymbolSize > 0 && width > maxSymbolSize ) || ( minSymbolSize > 0 && width < minSymbolSize ) )
if ( QgsSymbolLayerUtils::restrictSymbolSize( width, minSymbolSize, maxSymbolSize ) )
{
minMaxSizeLineSymbol.reset( dynamic_cast<QgsLineSymbol *>( s->clone() ) );
minMaxSizeLineSymbol->setWidth( width > maxSymbolSize ? maxSymbolSize : minSymbolSize );
minMaxSizeLineSymbol->setWidth( width );
minMaxSizeLineSymbol->setWidthUnit( QgsUnitTypes::RenderMillimeters );
s = minMaxSizeLineSymbol.get();
width = ( width > maxSymbolSize ) ? maxSymbolSize : minSymbolSize;
height = width;
}
height = width;
}

if ( ctx && ctx->painter )
Expand Down
15 changes: 15 additions & 0 deletions src/core/symbology/qgssymbollayerutils.cpp
Expand Up @@ -4473,3 +4473,18 @@ QSet<const QgsSymbolLayer *> QgsSymbolLayerUtils::toSymbolLayerPointers( QgsFeat
renderer->accept( &visitor );
return visitor.mSymbolLayers;
}

bool QgsSymbolLayerUtils::restrictSymbolSize( double &size, double minSize, double maxSize )
{
if ( size < minSize )
{
size = minSize;
return true;
}
if ( size > maxSize )
{
size = maxSize;
return true;
}
return false;
}
9 changes: 9 additions & 0 deletions src/core/symbology/qgssymbollayerutils.h
Expand Up @@ -728,6 +728,15 @@ class CORE_EXPORT QgsSymbolLayerUtils
* \since QGIS 3.12
*/
static QSet<const QgsSymbolLayer *> toSymbolLayerPointers( QgsFeatureRenderer *renderer, const QSet<QgsSymbolLayerId> &symbolLayerIds );

/**
* \brief Restricts size value to min/max size
* \param size value will be set so minSize if below minimum or to maxSize if above minimum
* \param minSize the minimum symbolSize
* \param maxSize the maximum symbol size
* \return true if size value has been changed
*/
static bool restrictSymbolSize( double &size, double minSize, double maxSize );
};

class QPolygonF;
Expand Down

0 comments on commit 80b3a81

Please sign in to comment.