Skip to content
Permalink
Browse files
Allow legend to have other than the "ideal" minimum size
  • Loading branch information
wonder-sk committed Jul 1, 2014
1 parent 438f3d4 commit c0ac0ac6b8bd372ac197691145b3180783ebdfbd
Showing with 22 additions and 4 deletions.
  1. +4 −2 src/core/composer/qgscomposerlegend.cpp
  2. +9 −1 src/core/qgslegendrenderer.cpp
  3. +9 −1 src/core/qgslegendrenderer.h
@@ -58,6 +58,8 @@ void QgsComposerLegend::paint( QPainter* painter, const QStyleOptionGraphicsItem
if ( mComposerMap )
mLegendRenderer->setMmPerMapUnit( mComposerMap->mapUnitsToMM() );

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

if ( !painter )
return;

@@ -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;
@@ -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() )
{
@@ -40,7 +40,7 @@ QgsLegendRenderer::QgsLegendRenderer( QgsLegendModel* legendModel )
rstyle( QgsComposerLegendStyle::SymbolLabel ).rfont().setPointSizeF( 12.0 );
}

QSizeF QgsLegendRenderer::legendSize()
QSizeF QgsLegendRenderer::minimumSize()
{
return paintAndDetermineSize( 0 );
}
@@ -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() )
{
@@ -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.
@@ -209,6 +215,8 @@ class QgsLegendRenderer
private:
QgsLegendModel* mLegendModel;

QSizeF mLegendSize;

QString mTitle;

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

0 comments on commit c0ac0ac

Please sign in to comment.