@@ -103,6 +103,7 @@ QSizeF QgsComposerLegend::paintAndDetermineSize( QPainter* painter )
103103 drawText ( painter, mBoxSpace , currentYCoordinate, mTitle , mTitleFont );
104104 }
105105
106+ double currentMaxXCoord;
106107 maxXCoord = 2 * mBoxSpace + textWidthMillimeters ( mTitleFont , mTitle );
107108
108109 for ( int i = 0 ; i < numLayerItems; ++i )
@@ -114,11 +115,13 @@ QSizeF QgsComposerLegend::paintAndDetermineSize( QPainter* painter )
114115 QgsComposerLegendItem::ItemType type = currentLegendItem->itemType ();
115116 if ( type == QgsComposerLegendItem::GroupItem )
116117 {
117- drawGroupItem ( painter, dynamic_cast <QgsComposerGroupItem*>( currentLegendItem ), currentYCoordinate, maxXCoord );
118+ drawGroupItem ( painter, dynamic_cast <QgsComposerGroupItem*>( currentLegendItem ), currentYCoordinate, currentMaxXCoord );
119+ maxXCoord = qMax ( maxXCoord, currentMaxXCoord );
118120 }
119121 else if ( type == QgsComposerLegendItem::LayerItem )
120122 {
121- drawLayerItem ( painter, dynamic_cast <QgsComposerLayerItem*>( currentLegendItem ), currentYCoordinate, maxXCoord );
123+ drawLayerItem ( painter, dynamic_cast <QgsComposerLayerItem*>( currentLegendItem ), currentYCoordinate, currentMaxXCoord );
124+ maxXCoord = qMax ( maxXCoord, currentMaxXCoord );
122125 }
123126 }
124127 }
@@ -165,7 +168,8 @@ void QgsComposerLegend::drawGroupItem( QPainter* p, QgsComposerGroupItem* groupI
165168
166169 p->setPen ( QColor ( 0 , 0 , 0 ) );
167170 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 );
169173
170174 // children can be other group items or layer items
171175 int numChildItems = groupItem->rowCount ();
@@ -178,11 +182,13 @@ void QgsComposerLegend::drawGroupItem( QPainter* p, QgsComposerGroupItem* groupI
178182 QgsComposerLegendItem::ItemType type = currentLegendItem->itemType ();
179183 if ( type == QgsComposerLegendItem::GroupItem )
180184 {
181- drawGroupItem ( p, dynamic_cast <QgsComposerGroupItem*>( currentLegendItem ), currentYCoord, maxXCoord );
185+ drawGroupItem ( p, dynamic_cast <QgsComposerGroupItem*>( currentLegendItem ), currentYCoord, currentMaxXCoord );
186+ maxXCoord = qMax ( currentMaxXCoord, maxXCoord );
182187 }
183188 else if ( type == QgsComposerLegendItem::LayerItem )
184189 {
185- drawLayerItem ( p, dynamic_cast <QgsComposerLayerItem*>( currentLegendItem ), currentYCoord, maxXCoord );
190+ drawLayerItem ( p, dynamic_cast <QgsComposerLayerItem*>( currentLegendItem ), currentYCoord, currentMaxXCoord );
191+ qMax ( currentMaxXCoord, maxXCoord );
186192 }
187193 }
188194}
@@ -242,11 +248,15 @@ void QgsComposerLegend::drawLayerChildItems( QPainter* p, QStandardItem* layerIt
242248 return ;
243249 }
244250
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;
246254
247255 double textHeight = fontHeightCharacterMM ( mItemFont , QChar ( ' 0' ) );
248256 double itemHeight = qMax ( mSymbolHeight , textHeight );
249257
258+ double textAlignCoord = 0 ; // alignment for legend text
259+
250260 QStandardItem* currentItem;
251261
252262 int numChildren = layerItem->rowCount ();
@@ -316,18 +326,21 @@ void QgsComposerLegend::drawLayerChildItems( QPainter* p, QStandardItem* layerIt
316326 }
317327 }
318328
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+ {
320338 if ( p )
321339 {
322340 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 () ) );
325343 }
326- currentXCoord += mBoxSpace ;
327-
328- maxXCoord = qMax ( maxXCoord, currentXCoord );
329-
330- currentYCoord += realItemHeight;
331344 }
332345}
333346
0 commit comments