Skip to content
Permalink
Browse files

Better layout for composer legend

  • Loading branch information
Marco Hugentobler authored and mhugent committed Jul 2, 2011
1 parent e0eb456 commit 4215d51730aa97e2d31c1a5d7faae0dfcebb8368
Showing with 27 additions and 14 deletions.
  1. +27 −14 src/core/composer/qgscomposerlegend.cpp
@@ -101,6 +101,7 @@ QSizeF QgsComposerLegend::paintAndDetermineSize( QPainter* painter )
drawText( painter, mBoxSpace, currentYCoordinate, mTitle, mTitleFont );
}

double currentMaxXCoord;
maxXCoord = 2 * mBoxSpace + textWidthMillimeters( mTitleFont, mTitle );

for ( int i = 0; i < numLayerItems; ++i )
@@ -112,11 +113,13 @@ QSizeF QgsComposerLegend::paintAndDetermineSize( QPainter* painter )
QgsComposerLegendItem::ItemType type = currentLegendItem->itemType();
if ( type == QgsComposerLegendItem::GroupItem )
{
drawGroupItem( painter, dynamic_cast<QgsComposerGroupItem*>( currentLegendItem ), currentYCoordinate, maxXCoord );
drawGroupItem( painter, dynamic_cast<QgsComposerGroupItem*>( currentLegendItem ), currentYCoordinate, currentMaxXCoord );
maxXCoord = qMax( maxXCoord, currentMaxXCoord );
}
else if ( type == QgsComposerLegendItem::LayerItem )
{
drawLayerItem( painter, dynamic_cast<QgsComposerLayerItem*>( currentLegendItem ), currentYCoordinate, maxXCoord );
drawLayerItem( painter, dynamic_cast<QgsComposerLayerItem*>( currentLegendItem ), currentYCoordinate, currentMaxXCoord );
maxXCoord = qMax( maxXCoord, currentMaxXCoord );
}
}
}
@@ -163,7 +166,8 @@ void QgsComposerLegend::drawGroupItem( QPainter* p, QgsComposerGroupItem* groupI

p->setPen( QColor( 0, 0, 0 ) );
drawText( p, mBoxSpace, currentYCoord, groupItem->text(), mGroupFont );
maxXCoord = qMax( maxXCoord, 2 * mBoxSpace + textWidthMillimeters( mGroupFont, groupItem->text() ) );
double currentMaxXCoord = 2 * mBoxSpace + textWidthMillimeters( mGroupFont, groupItem->text() );
maxXCoord = qMax( currentMaxXCoord, maxXCoord );

//children can be other group items or layer items
int numChildItems = groupItem->rowCount();
@@ -176,11 +180,13 @@ void QgsComposerLegend::drawGroupItem( QPainter* p, QgsComposerGroupItem* groupI
QgsComposerLegendItem::ItemType type = currentLegendItem->itemType();
if ( type == QgsComposerLegendItem::GroupItem )
{
drawGroupItem( p, dynamic_cast<QgsComposerGroupItem*>( currentLegendItem ), currentYCoord, maxXCoord );
drawGroupItem( p, dynamic_cast<QgsComposerGroupItem*>( currentLegendItem ), currentYCoord, currentMaxXCoord );
maxXCoord = qMax( currentMaxXCoord, maxXCoord );
}
else if ( type == QgsComposerLegendItem::LayerItem )
{
drawLayerItem( p, dynamic_cast<QgsComposerLayerItem*>( currentLegendItem ), currentYCoord, maxXCoord );
drawLayerItem( p, dynamic_cast<QgsComposerLayerItem*>( currentLegendItem ), currentYCoord, currentMaxXCoord );
qMax( currentMaxXCoord, maxXCoord );
}
}
}
@@ -240,11 +246,15 @@ void QgsComposerLegend::drawLayerChildItems( QPainter* p, QStandardItem* layerIt
return;
}

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

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

double textAlignCoord = 0; //alignment for legend text

QStandardItem* currentItem;

int numChildren = layerItem->rowCount();
@@ -314,18 +324,21 @@ void QgsComposerLegend::drawLayerChildItems( QPainter* p, QStandardItem* layerIt
}
}

//finally draw text
childYCoords.push_back( currentYCoord );
realItemHeights.push_back( realItemHeight );
currentYCoord += realItemHeight;
textAlignCoord = qMax( currentXCoord, textAlignCoord );
}

maxXCoord = textAlignCoord;
for ( int i = 0; i < numChildren; ++i )
{
if ( p )
{
p->setPen( QColor( 0, 0, 0 ) );
drawText( p, currentXCoord, currentYCoord + textHeight + ( realItemHeight - textHeight ) / 2, currentItem->text(), mItemFont );
currentXCoord += textWidthMillimeters( mItemFont, currentItem->text() );
drawText( p, textAlignCoord, childYCoords.at( i ) + textHeight + ( realItemHeights.at( i ) - textHeight ) / 2, layerItem->child( i, 0 )->text(), mItemFont );
maxXCoord = qMax( maxXCoord, textAlignCoord + mBoxSpace + textWidthMillimeters( mItemFont, layerItem->child( i, 0 )->text() ) );
}
currentXCoord += mBoxSpace;

maxXCoord = qMax( maxXCoord, currentXCoord );

currentYCoord += realItemHeight;
}
}

0 comments on commit 4215d51

Please sign in to comment.
You can’t perform that action at this time.