Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Allow legend to have other than the "ideal" minimum size
  • Loading branch information
wonder-sk committed Jul 1, 2014
1 parent 438f3d4 commit c0ac0ac
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 4 deletions.
6 changes: 4 additions & 2 deletions src/core/composer/qgscomposerlegend.cpp
Expand Up @@ -58,6 +58,8 @@ void QgsComposerLegend::paint( QPainter* painter, const QStyleOptionGraphicsItem
if ( mComposerMap )
mLegendRenderer->setMmPerMapUnit( mComposerMap->mapUnitsToMM() );

mLegendRenderer->setLegendSize( rect().size() );

if ( !painter )
return;

Expand All @@ -81,7 +83,7 @@ void QgsComposerLegend::paint( QPainter* painter, const QStyleOptionGraphicsItem

QSizeF QgsComposerLegend::paintAndDetermineSize( QPainter* painter )
{
QSizeF size = mLegendRenderer->legendSize();
QSizeF size = mLegendRenderer->minimumSize();
if ( !painter )
mLegendRenderer->drawLegend( painter );
return size;
Expand All @@ -90,7 +92,7 @@ QSizeF QgsComposerLegend::paintAndDetermineSize( QPainter* painter )

void QgsComposerLegend::adjustBoxSize()
{
QSizeF size = mLegendRenderer->legendSize();
QSizeF size = mLegendRenderer->minimumSize();
QgsDebugMsg( QString( "width = %1 height = %2" ).arg( size.width() ).arg( size.height() ) );
if ( size.isValid() )
{
Expand Down
10 changes: 9 additions & 1 deletion src/core/qgslegendrenderer.cpp
Expand Up @@ -40,7 +40,7 @@ QgsLegendRenderer::QgsLegendRenderer( QgsLegendModel* legendModel )
rstyle( QgsComposerLegendStyle::SymbolLabel ).rfont().setPointSizeF( 12.0 );
}

QSizeF QgsLegendRenderer::legendSize()
QSizeF QgsLegendRenderer::minimumSize()
{
return paintAndDetermineSize( 0 );
}
Expand Down Expand Up @@ -121,6 +121,14 @@ QSizeF QgsLegendRenderer::paintAndDetermineSize( QPainter* painter )
size.rwidth() = qMax( titleSize.width(), size.width() );
}

// override the size if it was set by the user
if ( mLegendSize.isValid() )
{
qreal w = qMax( size.width(), mLegendSize.width() );
qreal h = qMax( size.height(), mLegendSize.height() );
size = QSizeF( w, h );
}

// Now we have set the correct total item width and can draw the title centered
if ( !mTitle.isEmpty() )
{
Expand Down
10 changes: 9 additions & 1 deletion src/core/qgslegendrenderer.h
Expand Up @@ -32,7 +32,13 @@ class QgsLegendRenderer
explicit QgsLegendRenderer( QgsLegendModel* legendModel );

/** Run the layout algorithm and determine the size required for legend */
QSizeF legendSize();
QSizeF minimumSize();

/** Set the preferred resulting legend size. */
void setLegendSize( QSizeF s ) { mLegendSize = s; }

/** Find out preferred legend size set by the client. If null, the legend will be drawn with the minimum size */
QSizeF legendSize() const { return mLegendSize; }

/** Draw the legend with given painter. It will occupy the area reported in legendSize().
* Painter should be scaled beforehand so that units correspond to millimeters.
Expand Down Expand Up @@ -209,6 +215,8 @@ class QgsLegendRenderer
private:
QgsLegendModel* mLegendModel;

QSizeF mLegendSize;

QString mTitle;

/** Title alignment, one of Qt::AlignLeft, Qt::AlignHCenter, Qt::AlignRight) */
Expand Down

0 comments on commit c0ac0ac

Please sign in to comment.