@@ -103,6 +103,7 @@ QSizeF QgsComposerLegend::paintAndDetermineSize( QPainter* painter )
103
103
drawText ( painter, mBoxSpace , currentYCoordinate, mTitle , mTitleFont );
104
104
}
105
105
106
+ double currentMaxXCoord;
106
107
maxXCoord = 2 * mBoxSpace + textWidthMillimeters ( mTitleFont , mTitle );
107
108
108
109
for ( int i = 0 ; i < numLayerItems; ++i )
@@ -114,11 +115,13 @@ QSizeF QgsComposerLegend::paintAndDetermineSize( QPainter* painter )
114
115
QgsComposerLegendItem::ItemType type = currentLegendItem->itemType ();
115
116
if ( type == QgsComposerLegendItem::GroupItem )
116
117
{
117
- drawGroupItem ( painter, dynamic_cast <QgsComposerGroupItem*>( currentLegendItem ), currentYCoordinate, maxXCoord );
118
+ drawGroupItem ( painter, dynamic_cast <QgsComposerGroupItem*>( currentLegendItem ), currentYCoordinate, currentMaxXCoord );
119
+ maxXCoord = qMax ( maxXCoord, currentMaxXCoord );
118
120
}
119
121
else if ( type == QgsComposerLegendItem::LayerItem )
120
122
{
121
- drawLayerItem ( painter, dynamic_cast <QgsComposerLayerItem*>( currentLegendItem ), currentYCoordinate, maxXCoord );
123
+ drawLayerItem ( painter, dynamic_cast <QgsComposerLayerItem*>( currentLegendItem ), currentYCoordinate, currentMaxXCoord );
124
+ maxXCoord = qMax ( maxXCoord, currentMaxXCoord );
122
125
}
123
126
}
124
127
}
@@ -165,7 +168,8 @@ void QgsComposerLegend::drawGroupItem( QPainter* p, QgsComposerGroupItem* groupI
165
168
166
169
p->setPen ( QColor ( 0 , 0 , 0 ) );
167
170
drawText ( p, mBoxSpace , currentYCoord, groupItem->text (), mGroupFont );
168
- maxXCoord = qMax ( maxXCoord, 2 * mBoxSpace + textWidthMillimeters ( mGroupFont , groupItem->text () ) );
171
+ double currentMaxXCoord = 2 * mBoxSpace + textWidthMillimeters ( mGroupFont , groupItem->text () );
172
+ maxXCoord = qMax ( currentMaxXCoord, maxXCoord );
169
173
170
174
// children can be other group items or layer items
171
175
int numChildItems = groupItem->rowCount ();
@@ -178,11 +182,13 @@ void QgsComposerLegend::drawGroupItem( QPainter* p, QgsComposerGroupItem* groupI
178
182
QgsComposerLegendItem::ItemType type = currentLegendItem->itemType ();
179
183
if ( type == QgsComposerLegendItem::GroupItem )
180
184
{
181
- drawGroupItem ( p, dynamic_cast <QgsComposerGroupItem*>( currentLegendItem ), currentYCoord, maxXCoord );
185
+ drawGroupItem ( p, dynamic_cast <QgsComposerGroupItem*>( currentLegendItem ), currentYCoord, currentMaxXCoord );
186
+ maxXCoord = qMax ( currentMaxXCoord, maxXCoord );
182
187
}
183
188
else if ( type == QgsComposerLegendItem::LayerItem )
184
189
{
185
- drawLayerItem ( p, dynamic_cast <QgsComposerLayerItem*>( currentLegendItem ), currentYCoord, maxXCoord );
190
+ drawLayerItem ( p, dynamic_cast <QgsComposerLayerItem*>( currentLegendItem ), currentYCoord, currentMaxXCoord );
191
+ qMax ( currentMaxXCoord, maxXCoord );
186
192
}
187
193
}
188
194
}
@@ -242,11 +248,15 @@ void QgsComposerLegend::drawLayerChildItems( QPainter* p, QStandardItem* layerIt
242
248
return ;
243
249
}
244
250
245
- // standerd item height
251
+ // Draw all symbols first and the texts after (to find out the x coordinate to have the text aligned)
252
+ QList<double > childYCoords;
253
+ QList<double > realItemHeights;
246
254
247
255
double textHeight = fontHeightCharacterMM ( mItemFont , QChar ( ' 0' ) );
248
256
double itemHeight = qMax ( mSymbolHeight , textHeight );
249
257
258
+ double textAlignCoord = 0 ; // alignment for legend text
259
+
250
260
QStandardItem* currentItem;
251
261
252
262
int numChildren = layerItem->rowCount ();
@@ -316,18 +326,21 @@ void QgsComposerLegend::drawLayerChildItems( QPainter* p, QStandardItem* layerIt
316
326
}
317
327
}
318
328
319
- // finally draw text
329
+ childYCoords.push_back ( currentYCoord );
330
+ realItemHeights.push_back ( realItemHeight );
331
+ currentYCoord += realItemHeight;
332
+ textAlignCoord = qMax ( currentXCoord, textAlignCoord );
333
+ }
334
+
335
+ maxXCoord = textAlignCoord;
336
+ for ( int i = 0 ; i < numChildren; ++i )
337
+ {
320
338
if ( p )
321
339
{
322
340
p->setPen ( QColor ( 0 , 0 , 0 ) );
323
- drawText ( p, currentXCoord, currentYCoord + textHeight + ( realItemHeight - textHeight ) / 2 , currentItem ->text (), mItemFont );
324
- currentXCoord += textWidthMillimeters ( mItemFont , currentItem-> text () );
341
+ drawText ( p, textAlignCoord, childYCoords. at ( i ) + textHeight + ( realItemHeights. at ( i ) - textHeight ) / 2 , layerItem-> child ( i, 0 ) ->text (), mItemFont );
342
+ maxXCoord = qMax ( maxXCoord, textAlignCoord + mBoxSpace + textWidthMillimeters ( mItemFont , layerItem-> child ( i, 0 )-> text () ) );
325
343
}
326
- currentXCoord += mBoxSpace ;
327
-
328
- maxXCoord = qMax ( maxXCoord, currentXCoord );
329
-
330
- currentYCoord += realItemHeight;
331
344
}
332
345
}
333
346
0 commit comments