@@ -103,8 +103,10 @@ QSizeF QgsComposerLegend::paintAndDetermineSize( QPainter* painter )
103
103
drawText ( painter, mBoxSpace , currentYCoordinate, mTitle , mTitleFont );
104
104
}
105
105
106
+
106
107
maxXCoord = 2 * mBoxSpace + textWidthMillimeters ( mTitleFont , mTitle );
107
108
109
+ double currentItemMaxX = 0 ; // maximum x-coordinate for current item
108
110
for ( int i = 0 ; i < numLayerItems; ++i )
109
111
{
110
112
currentLayerItem = rootItem->child ( i );
@@ -114,11 +116,13 @@ QSizeF QgsComposerLegend::paintAndDetermineSize( QPainter* painter )
114
116
QgsComposerLegendItem::ItemType type = currentLegendItem->itemType ();
115
117
if ( type == QgsComposerLegendItem::GroupItem )
116
118
{
117
- drawGroupItem ( painter, dynamic_cast <QgsComposerGroupItem*>( currentLegendItem ), currentYCoordinate, maxXCoord );
119
+ drawGroupItem ( painter, dynamic_cast <QgsComposerGroupItem*>( currentLegendItem ), currentYCoordinate, currentItemMaxX );
120
+ maxXCoord = qMax ( maxXCoord, currentItemMaxX );
118
121
}
119
122
else if ( type == QgsComposerLegendItem::LayerItem )
120
123
{
121
- drawLayerItem ( painter, dynamic_cast <QgsComposerLayerItem*>( currentLegendItem ), currentYCoordinate, maxXCoord );
124
+ drawLayerItem ( painter, dynamic_cast <QgsComposerLayerItem*>( currentLegendItem ), currentYCoordinate, currentItemMaxX );
125
+ maxXCoord = qMax ( maxXCoord, currentItemMaxX );
122
126
}
123
127
}
124
128
}
@@ -165,7 +169,10 @@ void QgsComposerLegend::drawGroupItem( QPainter* p, QgsComposerGroupItem* groupI
165
169
166
170
p->setPen ( QColor ( 0 , 0 , 0 ) );
167
171
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 );
169
176
170
177
// children can be other group items or layer items
171
178
int numChildItems = groupItem->rowCount ();
@@ -178,11 +185,13 @@ void QgsComposerLegend::drawGroupItem( QPainter* p, QgsComposerGroupItem* groupI
178
185
QgsComposerLegendItem::ItemType type = currentLegendItem->itemType ();
179
186
if ( type == QgsComposerLegendItem::GroupItem )
180
187
{
181
- drawGroupItem ( p, dynamic_cast <QgsComposerGroupItem*>( currentLegendItem ), currentYCoord, maxXCoord );
188
+ drawGroupItem ( p, dynamic_cast <QgsComposerGroupItem*>( currentLegendItem ), currentYCoord, currentMaxXCoord );
189
+ maxXCoord = qMax ( currentMaxXCoord, maxXCoord );
182
190
}
183
191
else if ( type == QgsComposerLegendItem::LayerItem )
184
192
{
185
- drawLayerItem ( p, dynamic_cast <QgsComposerLayerItem*>( currentLegendItem ), currentYCoord, maxXCoord );
193
+ drawLayerItem ( p, dynamic_cast <QgsComposerLayerItem*>( currentLegendItem ), currentYCoord, currentMaxXCoord );
194
+ maxXCoord = qMax ( currentMaxXCoord, maxXCoord );
186
195
}
187
196
}
188
197
}
@@ -242,11 +251,15 @@ void QgsComposerLegend::drawLayerChildItems( QPainter* p, QStandardItem* layerIt
242
251
return ;
243
252
}
244
253
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;
246
257
247
258
double textHeight = fontHeightCharacterMM ( mItemFont , QChar ( ' 0' ) );
248
259
double itemHeight = qMax ( mSymbolHeight , textHeight );
249
260
261
+ double textAlignCoord = 0 ; // alignment for legend text
262
+
250
263
QStandardItem* currentItem;
251
264
252
265
int numChildren = layerItem->rowCount ();
@@ -316,18 +329,21 @@ void QgsComposerLegend::drawLayerChildItems( QPainter* p, QStandardItem* layerIt
316
329
}
317
330
}
318
331
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
+ {
320
341
if ( p )
321
342
{
322
343
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 () ) );
325
346
}
326
- currentXCoord += mBoxSpace ;
327
-
328
- maxXCoord = qMax ( maxXCoord, currentXCoord );
329
-
330
- currentYCoord += realItemHeight;
331
347
}
332
348
}
333
349
0 commit comments