Skip to content

Commit cb41f07

Browse files
committedJun 18, 2011
Merge branch 'master' of github.com:qgis/Quantum-GIS
2 parents 9028bf7 + f70aae6 commit cb41f07

File tree

1 file changed

+30
-14
lines changed

1 file changed

+30
-14
lines changed
 

‎src/core/composer/qgscomposerlegend.cpp

Lines changed: 30 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -103,8 +103,10 @@ QSizeF QgsComposerLegend::paintAndDetermineSize( QPainter* painter )
103103
drawText( painter, mBoxSpace, currentYCoordinate, mTitle, mTitleFont );
104104
}
105105

106+
106107
maxXCoord = 2 * mBoxSpace + textWidthMillimeters( mTitleFont, mTitle );
107108

109+
double currentItemMaxX = 0; //maximum x-coordinate for current item
108110
for ( int i = 0; i < numLayerItems; ++i )
109111
{
110112
currentLayerItem = rootItem->child( i );
@@ -114,11 +116,13 @@ QSizeF QgsComposerLegend::paintAndDetermineSize( QPainter* painter )
114116
QgsComposerLegendItem::ItemType type = currentLegendItem->itemType();
115117
if ( type == QgsComposerLegendItem::GroupItem )
116118
{
117-
drawGroupItem( painter, dynamic_cast<QgsComposerGroupItem*>( currentLegendItem ), currentYCoordinate, maxXCoord );
119+
drawGroupItem( painter, dynamic_cast<QgsComposerGroupItem*>( currentLegendItem ), currentYCoordinate, currentItemMaxX );
120+
maxXCoord = qMax( maxXCoord, currentItemMaxX );
118121
}
119122
else if ( type == QgsComposerLegendItem::LayerItem )
120123
{
121-
drawLayerItem( painter, dynamic_cast<QgsComposerLayerItem*>( currentLegendItem ), currentYCoordinate, maxXCoord );
124+
drawLayerItem( painter, dynamic_cast<QgsComposerLayerItem*>( currentLegendItem ), currentYCoordinate, currentItemMaxX );
125+
maxXCoord = qMax( maxXCoord, currentItemMaxX );
122126
}
123127
}
124128
}
@@ -165,7 +169,10 @@ void QgsComposerLegend::drawGroupItem( QPainter* p, QgsComposerGroupItem* groupI
165169

166170
p->setPen( QColor( 0, 0, 0 ) );
167171
drawText( p, mBoxSpace, currentYCoord, groupItem->text(), mGroupFont );
168-
maxXCoord = qMax( maxXCoord, 2 * mBoxSpace + textWidthMillimeters( mGroupFont, groupItem->text() ) );
172+
173+
//maximum x-coordinate of current item
174+
double currentMaxXCoord = 2 * mBoxSpace + textWidthMillimeters( mGroupFont, groupItem->text() );
175+
maxXCoord = qMax( currentMaxXCoord, maxXCoord );
169176

170177
//children can be other group items or layer items
171178
int numChildItems = groupItem->rowCount();
@@ -178,11 +185,13 @@ void QgsComposerLegend::drawGroupItem( QPainter* p, QgsComposerGroupItem* groupI
178185
QgsComposerLegendItem::ItemType type = currentLegendItem->itemType();
179186
if ( type == QgsComposerLegendItem::GroupItem )
180187
{
181-
drawGroupItem( p, dynamic_cast<QgsComposerGroupItem*>( currentLegendItem ), currentYCoord, maxXCoord );
188+
drawGroupItem( p, dynamic_cast<QgsComposerGroupItem*>( currentLegendItem ), currentYCoord, currentMaxXCoord );
189+
maxXCoord = qMax( currentMaxXCoord, maxXCoord );
182190
}
183191
else if ( type == QgsComposerLegendItem::LayerItem )
184192
{
185-
drawLayerItem( p, dynamic_cast<QgsComposerLayerItem*>( currentLegendItem ), currentYCoord, maxXCoord );
193+
drawLayerItem( p, dynamic_cast<QgsComposerLayerItem*>( currentLegendItem ), currentYCoord, currentMaxXCoord );
194+
maxXCoord = qMax( currentMaxXCoord, maxXCoord );
186195
}
187196
}
188197
}
@@ -242,11 +251,15 @@ void QgsComposerLegend::drawLayerChildItems( QPainter* p, QStandardItem* layerIt
242251
return;
243252
}
244253

245-
//standerd item height
254+
//Draw all symbols first and the texts after (to find out the x coordinate to have the text aligned)
255+
QList<double> childYCoords;
256+
QList<double> realItemHeights;
246257

247258
double textHeight = fontHeightCharacterMM( mItemFont, QChar( '0' ) );
248259
double itemHeight = qMax( mSymbolHeight, textHeight );
249260

261+
double textAlignCoord = 0; //alignment for legend text
262+
250263
QStandardItem* currentItem;
251264

252265
int numChildren = layerItem->rowCount();
@@ -316,18 +329,21 @@ void QgsComposerLegend::drawLayerChildItems( QPainter* p, QStandardItem* layerIt
316329
}
317330
}
318331

319-
//finally draw text
332+
childYCoords.push_back( currentYCoord );
333+
realItemHeights.push_back( realItemHeight );
334+
currentYCoord += realItemHeight;
335+
textAlignCoord = qMax( currentXCoord, textAlignCoord );
336+
}
337+
338+
maxXCoord = textAlignCoord;
339+
for ( int i = 0; i < numChildren; ++i )
340+
{
320341
if ( p )
321342
{
322343
p->setPen( QColor( 0, 0, 0 ) );
323-
drawText( p, currentXCoord, currentYCoord + textHeight + ( realItemHeight - textHeight ) / 2, currentItem->text(), mItemFont );
324-
currentXCoord += textWidthMillimeters( mItemFont, currentItem->text() );
344+
drawText( p, textAlignCoord, childYCoords.at( i ) + textHeight + ( realItemHeights.at( i ) - textHeight ) / 2, layerItem->child( i, 0 )->text(), mItemFont );
345+
maxXCoord = qMax( maxXCoord, textAlignCoord + mBoxSpace + textWidthMillimeters( mItemFont, layerItem->child( i, 0 )->text() ) );
325346
}
326-
currentXCoord += mBoxSpace;
327-
328-
maxXCoord = qMax( maxXCoord, currentXCoord );
329-
330-
currentYCoord += realItemHeight;
331347
}
332348
}
333349

0 commit comments

Comments
 (0)
Please sign in to comment.