Skip to content

Commit

Permalink
Calculate correct legend size when using expression based labels
Browse files Browse the repository at this point in the history
  • Loading branch information
nyalldawson committed Jan 18, 2019
1 parent d70b60d commit 7ba5998
Show file tree
Hide file tree
Showing 7 changed files with 18 additions and 17 deletions.
2 changes: 1 addition & 1 deletion python/core/auto_generated/qgslegendrenderer.sip.in
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ Constructor for QgsLegendRenderer. The ownership of the legend model is not chan
and the model must exist for the lifetime of this renderer.
%End

QSizeF minimumSize();
QSizeF minimumSize( QgsRenderContext *renderContext = 0 );
%Docstring
Runs the layout algorithm and returns the minimum size required for the legend.

Expand Down
18 changes: 9 additions & 9 deletions src/core/layertree/qgslayertreemodellegendnode.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ QSizeF QgsLayerTreeModelLegendNode::drawSymbol( const QgsLegendSettings &setting
if ( symbolIcon.isNull() )
return QSizeF();

if ( ctx )
if ( ctx && ctx->painter )
symbolIcon.paint( ctx->painter, ctx->point.x(), ctx->point.y() + ( itemHeight - settings.symbolSize().height() ) / 2,
settings.symbolSize().width(), settings.symbolSize().height() );
return settings.symbolSize();
Expand All @@ -99,7 +99,7 @@ QSizeF QgsLayerTreeModelLegendNode::drawSymbolText( const QgsLegendSettings &set
labelSize.rheight() = lines.count() * textHeight + ( lines.count() - 1 ) * ( settings.lineSpacing() + textDescent );

double labelX = 0.0, labelY = 0.0;
if ( ctx )
if ( ctx && ctx->painter )
{
ctx->painter->setPen( settings.fontColor() );

Expand All @@ -117,7 +117,7 @@ QSizeF QgsLayerTreeModelLegendNode::drawSymbolText( const QgsLegendSettings &set
{
labelSize.rwidth() = std::max( settings.textWidthMillimeters( symbolLabelFont, *itemPart ), double( labelSize.width() ) );

if ( ctx )
if ( ctx && ctx->painter )
{
settings.drawText( ctx->painter, labelX, labelY, *itemPart, symbolLabelFont );
if ( itemPart != ( lines.end() - 1 ) )
Expand Down Expand Up @@ -424,7 +424,7 @@ QSizeF QgsSymbolLegendNode::drawSymbol( const QgsLegendSettings &settings, ItemC
}
}

if ( ctx )
if ( ctx && ctx->painter )
{
double currentXPosition = ctx->point.x();
double currentYCoord = ctx->point.y() + ( itemHeight - settings.symbolSize().height() ) / 2;
Expand Down Expand Up @@ -579,7 +579,7 @@ QSizeF QgsImageLegendNode::drawSymbol( const QgsLegendSettings &settings, ItemCo
{
Q_UNUSED( itemHeight );

if ( ctx )
if ( ctx && ctx->painter )
{
ctx->painter->drawImage( QRectF( ctx->point.x(), ctx->point.y(), settings.wmsLegendSize().width(), settings.wmsLegendSize().height() ),
mImage, QRectF( 0, 0, mImage.width(), mImage.height() ) );
Expand Down Expand Up @@ -614,7 +614,7 @@ QVariant QgsRasterSymbolLegendNode::data( int role ) const

QSizeF QgsRasterSymbolLegendNode::drawSymbol( const QgsLegendSettings &settings, ItemContext *ctx, double itemHeight ) const
{
if ( ctx )
if ( ctx && ctx->painter )
{
QColor itemColor = mColor;
if ( QgsRasterLayer *rasterLayer = dynamic_cast<QgsRasterLayer *>( layerNode()->layer() ) )
Expand Down Expand Up @@ -702,7 +702,7 @@ QSizeF QgsWmsLegendNode::drawSymbol( const QgsLegendSettings &settings, ItemCont
{
Q_UNUSED( itemHeight );

if ( ctx )
if ( ctx && ctx->painter )
{
ctx->painter->drawImage( QRectF( ctx->point, settings.wmsLegendSize() ),
mImage,
Expand Down Expand Up @@ -819,7 +819,7 @@ QgsLayerTreeModelLegendNode::ItemMetrics QgsDataDefinedSizeLegendNode::draw( con
context.setMapToPixel( QgsMapToPixel( 1 / ( settings.mmPerMapUnit() * context.scaleFactor() ) ) );
context.setForceVectorOutput( true );

if ( ctx )
if ( ctx && ctx->painter )
{
context.setPainter( ctx->painter );
ctx->painter->save();
Expand All @@ -836,7 +836,7 @@ QgsLayerTreeModelLegendNode::ItemMetrics QgsDataDefinedSizeLegendNode::draw( con
int labelXOffset;
ddsLegend.drawCollapsedLegend( context, &contentSize, &labelXOffset );

if ( ctx )
if ( ctx && ctx->painter )
ctx->painter->restore();

ItemMetrics im;
Expand Down
7 changes: 5 additions & 2 deletions src/core/layout/qgslayoutitemlegend.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
#include "qgsproject.h"
#include "qgssymbollayerutils.h"
#include "qgslayertreeutils.h"
#include "qgslayoututils.h"
#include <QDomDocument>
#include <QDomElement>
#include <QPainter>
Expand Down Expand Up @@ -114,7 +115,8 @@ void QgsLayoutItemLegend::paint( QPainter *painter, const QStyleOptionGraphicsIt
//adjust box if width or height is too small
if ( mSizeToContents )
{
QSizeF size = legendRenderer.minimumSize();
QgsRenderContext context = QgsLayoutUtils::createRenderContextForLayout( mLayout, nullptr );
QSizeF size = legendRenderer.minimumSize( &context );
if ( mForceResize )
{
mForceResize = false;
Expand Down Expand Up @@ -193,8 +195,9 @@ void QgsLayoutItemLegend::adjustBoxSize()
return;
}

QgsRenderContext context = QgsLayoutUtils::createRenderContextForLayout( mLayout, nullptr );
QgsLegendRenderer legendRenderer( mLegendModel.get(), mSettings );
QSizeF size = legendRenderer.minimumSize();
QSizeF size = legendRenderer.minimumSize( &context );
QgsDebugMsg( QStringLiteral( "width = %1 height = %2" ).arg( size.width() ).arg( size.height() ) );
if ( size.isValid() )
{
Expand Down
4 changes: 2 additions & 2 deletions src/core/qgslegendrenderer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,9 @@ QgsLegendRenderer::QgsLegendRenderer( QgsLayerTreeModel *legendModel, const QgsL
{
}

QSizeF QgsLegendRenderer::minimumSize()
QSizeF QgsLegendRenderer::minimumSize( QgsRenderContext *renderContext )
{
return paintAndDetermineSize();
return paintAndDetermineSize( renderContext );
}

void QgsLegendRenderer::drawLegend( QPainter *painter )
Expand Down
2 changes: 1 addition & 1 deletion src/core/qgslegendrenderer.h
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ class CORE_EXPORT QgsLegendRenderer
* \see setLegendSize()
* \see legendSize()
*/
QSizeF minimumSize();
QSizeF minimumSize( QgsRenderContext *renderContext = nullptr );

/**
* Sets the preferred resulting legend size.
Expand Down
2 changes: 0 additions & 2 deletions tests/src/python/test_qgslayoutlegend.py
Original file line number Diff line number Diff line change
Expand Up @@ -340,8 +340,6 @@ def testExpressionInText(self):
legend.setStyleFont(QgsLegendStyle.Symbol, QgsFontUtils.getStandardTestFont('Bold', 16))
legend.setStyleFont(QgsLegendStyle.SymbolLabel, QgsFontUtils.getStandardTestFont('Bold', 16))

# disable auto resizing
legend.setResizeToContents(False)
legend.setAutoUpdateModel(False)

QgsProject.instance().addMapLayers([point_layer])
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 7ba5998

Please sign in to comment.