Skip to content

Commit c0ac0ac

Browse files
committedJul 1, 2014
Allow legend to have other than the "ideal" minimum size
1 parent 438f3d4 commit c0ac0ac

File tree

3 files changed

+22
-4
lines changed

3 files changed

+22
-4
lines changed
 

‎src/core/composer/qgscomposerlegend.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,8 @@ void QgsComposerLegend::paint( QPainter* painter, const QStyleOptionGraphicsItem
5858
if ( mComposerMap )
5959
mLegendRenderer->setMmPerMapUnit( mComposerMap->mapUnitsToMM() );
6060

61+
mLegendRenderer->setLegendSize( rect().size() );
62+
6163
if ( !painter )
6264
return;
6365

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

8284
QSizeF QgsComposerLegend::paintAndDetermineSize( QPainter* painter )
8385
{
84-
QSizeF size = mLegendRenderer->legendSize();
86+
QSizeF size = mLegendRenderer->minimumSize();
8587
if ( !painter )
8688
mLegendRenderer->drawLegend( painter );
8789
return size;
@@ -90,7 +92,7 @@ QSizeF QgsComposerLegend::paintAndDetermineSize( QPainter* painter )
9092

9193
void QgsComposerLegend::adjustBoxSize()
9294
{
93-
QSizeF size = mLegendRenderer->legendSize();
95+
QSizeF size = mLegendRenderer->minimumSize();
9496
QgsDebugMsg( QString( "width = %1 height = %2" ).arg( size.width() ).arg( size.height() ) );
9597
if ( size.isValid() )
9698
{

‎src/core/qgslegendrenderer.cpp

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ QgsLegendRenderer::QgsLegendRenderer( QgsLegendModel* legendModel )
4040
rstyle( QgsComposerLegendStyle::SymbolLabel ).rfont().setPointSizeF( 12.0 );
4141
}
4242

43-
QSizeF QgsLegendRenderer::legendSize()
43+
QSizeF QgsLegendRenderer::minimumSize()
4444
{
4545
return paintAndDetermineSize( 0 );
4646
}
@@ -121,6 +121,14 @@ QSizeF QgsLegendRenderer::paintAndDetermineSize( QPainter* painter )
121121
size.rwidth() = qMax( titleSize.width(), size.width() );
122122
}
123123

124+
// override the size if it was set by the user
125+
if ( mLegendSize.isValid() )
126+
{
127+
qreal w = qMax( size.width(), mLegendSize.width() );
128+
qreal h = qMax( size.height(), mLegendSize.height() );
129+
size = QSizeF( w, h );
130+
}
131+
124132
// Now we have set the correct total item width and can draw the title centered
125133
if ( !mTitle.isEmpty() )
126134
{

‎src/core/qgslegendrenderer.h

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,13 @@ class QgsLegendRenderer
3232
explicit QgsLegendRenderer( QgsLegendModel* legendModel );
3333

3434
/** Run the layout algorithm and determine the size required for legend */
35-
QSizeF legendSize();
35+
QSizeF minimumSize();
36+
37+
/** Set the preferred resulting legend size. */
38+
void setLegendSize( QSizeF s ) { mLegendSize = s; }
39+
40+
/** Find out preferred legend size set by the client. If null, the legend will be drawn with the minimum size */
41+
QSizeF legendSize() const { return mLegendSize; }
3642

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

218+
QSizeF mLegendSize;
219+
212220
QString mTitle;
213221

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

0 commit comments

Comments
 (0)
Please sign in to comment.