Skip to content
Permalink
Browse files

Calculate correct legend size when using expression based labels

  • Loading branch information
nyalldawson committed Jan 18, 2019
1 parent d70b60d commit 7ba5998db2c5d44cd536c6da36073871f8104e10
@@ -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.

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

@@ -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 ) )
@@ -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;
@@ -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() ) );
@@ -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() ) )
@@ -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,
@@ -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();
@@ -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;
@@ -30,6 +30,7 @@
#include "qgsproject.h"
#include "qgssymbollayerutils.h"
#include "qgslayertreeutils.h"
#include "qgslayoututils.h"
#include <QDomDocument>
#include <QDomElement>
#include <QPainter>
@@ -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;
@@ -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() )
{
@@ -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 )
@@ -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.
@@ -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])
Binary file not shown.

0 comments on commit 7ba5998

Please sign in to comment.
You can’t perform that action at this time.