Skip to content
Permalink
Browse files
Improve layout legend rendering by preserving aspect ratio of legend …
…image size
  • Loading branch information
manisandro committed Nov 22, 2021
1 parent a21530b commit 459911112d763f71d7ed78f0824379d5ee1c2cfc
Showing with 19 additions and 6 deletions.
  1. +19 −6 src/core/layertree/qgslayertreemodellegendnode.cpp
@@ -1248,33 +1248,46 @@ QSizeF QgsWmsLegendNode::drawSymbol( const QgsLegendSettings &settings, ItemCont
{
Q_UNUSED( itemHeight )

const QImage image = getLegendGraphic();

QSize targetSize = image.size();
if ( settings.wmsLegendSize().width() < image.width() )
{
double targetHeight = image.height() * settings.wmsLegendSize().width() / image.width();
targetSize = QSize( settings.wmsLegendSize().width(), targetHeight );
}
else if ( settings.wmsLegendSize().height() < image.height() )
{
double targetWidth = image.width() * settings.wmsLegendSize().height() / image.height();
targetSize = QSize( targetWidth, settings.wmsLegendSize().height() );
}

if ( ctx && ctx->painter )
{
const QImage image = getLegendGraphic();

switch ( settings.symbolAlignment() )
{
case Qt::AlignLeft:
default:
ctx->painter->drawImage( QRectF( ctx->columnLeft,
ctx->top,
settings.wmsLegendSize().width(),
settings.wmsLegendSize().height() ),
targetSize.width(),
targetSize.height() ),
image,
QRectF( QPointF( 0, 0 ), image.size() ) );
break;

case Qt::AlignRight:
ctx->painter->drawImage( QRectF( ctx->columnRight - settings.wmsLegendSize().width(),
ctx->top,
settings.wmsLegendSize().width(),
settings.wmsLegendSize().height() ),
targetSize.width(),
targetSize.height() ),
image,
QRectF( QPointF( 0, 0 ), image.size() ) );
break;
}
}
return settings.wmsLegendSize();
return targetSize;
}

QJsonObject QgsWmsLegendNode::exportSymbolToJson( const QgsLegendSettings &, const QgsRenderContext & ) const

0 comments on commit 4599111

Please sign in to comment.