Skip to content

Commit

Permalink
Better layout for composer legend
Browse files Browse the repository at this point in the history
  • Loading branch information
Marco Hugentobler committed Jun 18, 2011
1 parent bb9e992 commit 03de655
Showing 1 changed file with 27 additions and 14 deletions.
41 changes: 27 additions & 14 deletions src/core/composer/qgscomposerlegend.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,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 )
Expand All @@ -114,11 +115,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 );
}
}
}
Expand Down Expand Up @@ -165,7 +168,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();
Expand All @@ -178,11 +182,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 );
}
}
}
Expand Down Expand Up @@ -242,11 +248,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();
Expand Down Expand Up @@ -316,18 +326,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;
}
}

Expand Down

0 comments on commit 03de655

Please sign in to comment.