@@ -101,6 +101,7 @@ QSizeF QgsComposerLegend::paintAndDetermineSize( QPainter* painter )
101101 drawText ( painter, mBoxSpace , currentYCoordinate, mTitle , mTitleFont );
102102 }
103103
104+ double currentMaxXCoord;
104105 maxXCoord = 2 * mBoxSpace + textWidthMillimeters ( mTitleFont , mTitle );
105106
106107 for ( int i = 0 ; i < numLayerItems; ++i )
@@ -112,11 +113,13 @@ QSizeF QgsComposerLegend::paintAndDetermineSize( QPainter* painter )
112113 QgsComposerLegendItem::ItemType type = currentLegendItem->itemType ();
113114 if ( type == QgsComposerLegendItem::GroupItem )
114115 {
115- drawGroupItem ( painter, dynamic_cast <QgsComposerGroupItem*>( currentLegendItem ), currentYCoordinate, maxXCoord );
116+ drawGroupItem ( painter, dynamic_cast <QgsComposerGroupItem*>( currentLegendItem ), currentYCoordinate, currentMaxXCoord );
117+ maxXCoord = qMax ( maxXCoord, currentMaxXCoord );
116118 }
117119 else if ( type == QgsComposerLegendItem::LayerItem )
118120 {
119- drawLayerItem ( painter, dynamic_cast <QgsComposerLayerItem*>( currentLegendItem ), currentYCoordinate, maxXCoord );
121+ drawLayerItem ( painter, dynamic_cast <QgsComposerLayerItem*>( currentLegendItem ), currentYCoordinate, currentMaxXCoord );
122+ maxXCoord = qMax ( maxXCoord, currentMaxXCoord );
120123 }
121124 }
122125 }
@@ -163,7 +166,8 @@ void QgsComposerLegend::drawGroupItem( QPainter* p, QgsComposerGroupItem* groupI
163166
164167 p->setPen ( QColor ( 0 , 0 , 0 ) );
165168 drawText ( p, mBoxSpace , currentYCoord, groupItem->text (), mGroupFont );
166- maxXCoord = qMax ( maxXCoord, 2 * mBoxSpace + textWidthMillimeters ( mGroupFont , groupItem->text () ) );
169+ double currentMaxXCoord = 2 * mBoxSpace + textWidthMillimeters ( mGroupFont , groupItem->text () );
170+ maxXCoord = qMax ( currentMaxXCoord, maxXCoord );
167171
168172 // children can be other group items or layer items
169173 int numChildItems = groupItem->rowCount ();
@@ -176,11 +180,13 @@ void QgsComposerLegend::drawGroupItem( QPainter* p, QgsComposerGroupItem* groupI
176180 QgsComposerLegendItem::ItemType type = currentLegendItem->itemType ();
177181 if ( type == QgsComposerLegendItem::GroupItem )
178182 {
179- drawGroupItem ( p, dynamic_cast <QgsComposerGroupItem*>( currentLegendItem ), currentYCoord, maxXCoord );
183+ drawGroupItem ( p, dynamic_cast <QgsComposerGroupItem*>( currentLegendItem ), currentYCoord, currentMaxXCoord );
184+ maxXCoord = qMax ( currentMaxXCoord, maxXCoord );
180185 }
181186 else if ( type == QgsComposerLegendItem::LayerItem )
182187 {
183- drawLayerItem ( p, dynamic_cast <QgsComposerLayerItem*>( currentLegendItem ), currentYCoord, maxXCoord );
188+ drawLayerItem ( p, dynamic_cast <QgsComposerLayerItem*>( currentLegendItem ), currentYCoord, currentMaxXCoord );
189+ qMax ( currentMaxXCoord, maxXCoord );
184190 }
185191 }
186192}
@@ -240,11 +246,15 @@ void QgsComposerLegend::drawLayerChildItems( QPainter* p, QStandardItem* layerIt
240246 return ;
241247 }
242248
243- // standerd item height
249+ // Draw all symbols first and the texts after (to find out the x coordinate to have the text aligned)
250+ QList<double > childYCoords;
251+ QList<double > realItemHeights;
244252
245253 double textHeight = fontHeightCharacterMM ( mItemFont , QChar ( ' 0' ) );
246254 double itemHeight = qMax ( mSymbolHeight , textHeight );
247255
256+ double textAlignCoord = 0 ; // alignment for legend text
257+
248258 QStandardItem* currentItem;
249259
250260 int numChildren = layerItem->rowCount ();
@@ -314,18 +324,21 @@ void QgsComposerLegend::drawLayerChildItems( QPainter* p, QStandardItem* layerIt
314324 }
315325 }
316326
317- // finally draw text
327+ childYCoords.push_back ( currentYCoord );
328+ realItemHeights.push_back ( realItemHeight );
329+ currentYCoord += realItemHeight;
330+ textAlignCoord = qMax ( currentXCoord, textAlignCoord );
331+ }
332+
333+ maxXCoord = textAlignCoord;
334+ for ( int i = 0 ; i < numChildren; ++i )
335+ {
318336 if ( p )
319337 {
320338 p->setPen ( QColor ( 0 , 0 , 0 ) );
321- drawText ( p, currentXCoord, currentYCoord + textHeight + ( realItemHeight - textHeight ) / 2 , currentItem ->text (), mItemFont );
322- currentXCoord += textWidthMillimeters ( mItemFont , currentItem-> text () );
339+ drawText ( p, textAlignCoord, childYCoords. at ( i ) + textHeight + ( realItemHeights. at ( i ) - textHeight ) / 2 , layerItem-> child ( i, 0 ) ->text (), mItemFont );
340+ maxXCoord = qMax ( maxXCoord, textAlignCoord + mBoxSpace + textWidthMillimeters ( mItemFont , layerItem-> child ( i, 0 )-> text () ) );
323341 }
324- currentXCoord += mBoxSpace ;
325-
326- maxXCoord = qMax ( maxXCoord, currentXCoord );
327-
328- currentYCoord += realItemHeight;
329342 }
330343}
331344
0 commit comments