Skip to content

Commit a083c5e

Browse files
author
Marco Hugentobler
committed
Fix for composer legend issues, e.g. ticket #3346
1 parent 99b584e commit a083c5e

File tree

2 files changed

+22
-6
lines changed

2 files changed

+22
-6
lines changed

src/core/composer/qgscomposerlegend.cpp

+16-3
Original file line numberDiff line numberDiff line change
@@ -243,7 +243,9 @@ void QgsComposerLegend::drawLayerChildItems( QPainter* p, QStandardItem* layerIt
243243
}
244244

245245
//standerd item height
246-
double itemHeight = qMax( mSymbolHeight, fontAscentMillimeters( mItemFont ) );
246+
247+
double textHeight = fontHeightCharacterMM( mItemFont, QChar( '0' ) );
248+
double itemHeight = qMax( mSymbolHeight, textHeight );
247249

248250
QStandardItem* currentItem;
249251

@@ -278,6 +280,7 @@ void QgsComposerLegend::drawLayerChildItems( QPainter* p, QStandardItem* layerIt
278280
{
279281
symbolNg = symbolV2Item->symbolV2();
280282
}
283+
QgsComposerRasterSymbolItem* rasterItem = dynamic_cast<QgsComposerRasterSymbolItem*>( currentItem );
281284

282285
if ( symbol ) //item with symbol?
283286
{
@@ -292,12 +295,22 @@ void QgsComposerLegend::drawLayerChildItems( QPainter* p, QStandardItem* layerIt
292295
realItemHeight = qMax( realSymbolHeight, itemHeight );
293296
currentXCoord += mIconLabelSpace;
294297
}
298+
else if ( rasterItem )
299+
{
300+
if ( p )
301+
{
302+
p->setBrush( rasterItem->color() );
303+
p->drawRect( QRectF( currentXCoord, currentYCoord + ( itemHeight - mSymbolHeight ) / 2, mSymbolWidth, mSymbolHeight ) );
304+
}
305+
currentXCoord += mSymbolWidth;
306+
currentXCoord += mIconLabelSpace;
307+
}
295308
else //item with icon?
296309
{
297310
QIcon symbolIcon = currentItem->icon();
298311
if ( !symbolIcon.isNull() && p )
299312
{
300-
symbolIcon.paint( p, currentXCoord, currentYCoord, mSymbolWidth, mSymbolHeight );
313+
symbolIcon.paint( p, currentXCoord, currentYCoord + ( itemHeight - mSymbolHeight ) / 2, mSymbolWidth, mSymbolHeight );
301314
currentXCoord += mSymbolWidth;
302315
currentXCoord += mIconLabelSpace;
303316
}
@@ -307,7 +320,7 @@ void QgsComposerLegend::drawLayerChildItems( QPainter* p, QStandardItem* layerIt
307320
if ( p )
308321
{
309322
p->setPen( QColor( 0, 0, 0 ) );
310-
drawText( p, currentXCoord, currentYCoord + fontAscentMillimeters( mItemFont ) + ( realItemHeight - fontAscentMillimeters( mItemFont ) ) / 2, currentItem->text(), mItemFont );
323+
drawText( p, currentXCoord, currentYCoord + textHeight + ( realItemHeight - textHeight ) / 2, currentItem->text(), mItemFont );
311324
currentXCoord += textWidthMillimeters( mItemFont, currentItem->text() );
312325
}
313326
currentXCoord += mBoxSpace;

src/core/composer/qgscomposerlegenditem.cpp

+6-3
Original file line numberDiff line numberDiff line change
@@ -263,6 +263,7 @@ void QgsComposerRasterSymbolItem::writeXML( QDomElement& elem, QDomDocument& doc
263263
QDomElement rasterClassElem = doc.createElement( "RasterClassificationItem" );
264264
rasterClassElem.setAttribute( "layerId", mLayerID );
265265
rasterClassElem.setAttribute( "text", text() );
266+
rasterClassElem.setAttribute( "color", mColor.name() );
266267
elem.appendChild( rasterClassElem );
267268
}
268269

@@ -274,11 +275,13 @@ void QgsComposerRasterSymbolItem::readXML( const QDomElement& itemElem, bool xSe
274275
}
275276
setText( itemElem.attribute( "text", "" ) );
276277
setLayerID( itemElem.attribute( "layerId", "" ) );
278+
setColor( QColor( itemElem.attribute( "color" ) ) );
277279

278-
QgsRasterLayer* rLayer = qobject_cast<QgsRasterLayer*>( QgsMapLayerRegistry::instance()->mapLayer( mLayerID ) );
279-
if ( rLayer && xServerAvailable )
280+
if ( xServerAvailable )
280281
{
281-
setIcon( QIcon( rLayer->legendAsPixmap( true ) ) );
282+
QPixmap itemPixmap( 20, 20 );
283+
itemPixmap.fill( mColor );
284+
setIcon( QIcon( itemPixmap ) );
282285
}
283286
}
284287

0 commit comments

Comments
 (0)