Navigation Menu

Skip to content

Commit

Permalink
Improve layout legend rendering by preserving aspect ratio of legend …
Browse files Browse the repository at this point in the history
…image size
  • Loading branch information
manisandro committed Nov 22, 2021
1 parent a21530b commit 4599111
Showing 1 changed file with 19 additions and 6 deletions.
25 changes: 19 additions & 6 deletions src/core/layertree/qgslayertreemodellegendnode.cpp
Expand Up @@ -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
Expand Down

0 comments on commit 4599111

Please sign in to comment.