Skip to content
Permalink
Browse files

Support for raster legend in composer

  • Loading branch information
mhugent committed Jun 16, 2011
1 parent 325e25f commit 66dded483264d56c01f8482540b528def6620df5
@@ -125,8 +125,12 @@ class CORE_EXPORT QgsComposerRasterSymbolItem: public QgsComposerLegendItem
QString layerID() const { return mLayerID; }
ItemType itemType() const { return RasterSymbolItem; }

void setColor( const QColor& c ){ mColor = c; }
QColor color() const { return mColor; }

private:
QString mLayerID;
QColor mColor;
};

class CORE_EXPORT QgsComposerLayerItem: public QgsComposerLegendItem
@@ -223,7 +223,7 @@ int QgsLegendModel::addVectorLayerItems( QStandardItem* layerItem, QgsVectorLaye
return 0;
}

int QgsLegendModel::addRasterLayerItem( QStandardItem* layerItem, QgsMapLayer* rlayer )
int QgsLegendModel::addRasterLayerItems( QStandardItem* layerItem, QgsMapLayer* rlayer )
{
if ( !layerItem || !rlayer )
{
@@ -236,15 +236,22 @@ int QgsLegendModel::addRasterLayerItem( QStandardItem* layerItem, QgsMapLayer* r
return 2;
}

QgsComposerRasterSymbolItem* currentSymbolItem = new QgsComposerRasterSymbolItem();
//use a vector symbol item without symbol
if ( mHasTopLevelWindow ) //only use QIcon / QPixmap if we have a running x-server
QList< QPair< QString, QColor > > rasterItemList = rasterLayer->legendSymbologyItems();
QList< QPair< QString, QColor > >::const_iterator itemIt = rasterItemList.constBegin();
for(; itemIt != rasterItemList.constEnd(); ++itemIt )
{
currentSymbolItem->setIcon( QIcon( rasterLayer->legendAsPixmap( true ) ) );
QgsComposerRasterSymbolItem* currentSymbolItem = new QgsComposerRasterSymbolItem( itemIt->first );
if( mHasTopLevelWindow )
{
QPixmap itemPixmap( 20, 20 );
itemPixmap.fill( itemIt->second );
currentSymbolItem->setIcon( QIcon( itemPixmap ) );
}
currentSymbolItem->setLayerID( rasterLayer->id() );
currentSymbolItem->setColor( itemIt->second );
int currentRowCount = layerItem->rowCount();
layerItem->setChild( currentRowCount, 0, currentSymbolItem );
}
currentSymbolItem->setLayerID( rasterLayer->id() );
int currentRowCount = layerItem->rowCount();
layerItem->setChild( currentRowCount, 0, currentSymbolItem );

return 0;
}
@@ -304,7 +311,7 @@ void QgsLegendModel::updateLayer( QStandardItem* layerItem )
QgsRasterLayer* rLayer = qobject_cast<QgsRasterLayer*>( mapLayer );
if ( rLayer )
{
addRasterLayerItem( lItem, rLayer );
addRasterLayerItems( lItem, rLayer );
}
}
}
@@ -362,7 +369,7 @@ void QgsLegendModel::addLayer( QgsMapLayer* theMapLayer )
break;
}
case QgsMapLayer::RasterLayer:
addRasterLayerItem( layerItem, theMapLayer );
addRasterLayerItems( layerItem, theMapLayer );
break;
default:
break;
@@ -104,7 +104,7 @@ class CORE_EXPORT QgsLegendModel: public QStandardItemModel

/**Adds item of raster layer
@return 0 in case of success*/
int addRasterLayerItem( QStandardItem* layerItem, QgsMapLayer* rlayer );
int addRasterLayerItems( QStandardItem* layerItem, QgsMapLayer* rlayer );

/**Creates a model item for a vector symbol. The calling function takes ownership*/
QStandardItem* itemFromSymbol( QgsSymbol* s, int opacity, const QString& layerID );
@@ -1496,33 +1496,8 @@ void QgsWMSServer::drawRasterSymbol( QgsComposerLegendItem* item, QPainter* p, d
return;
}

QPen savedPen = p->pen();
p->setPen( QPen( Qt::NoPen ) );

QgsRasterLayer::DrawingStyle drawingStyle = layer->drawingStyle();
if ( drawingStyle == QgsRasterLayer::SingleBandGray
|| drawingStyle == QgsRasterLayer::PalettedSingleBandGray
|| drawingStyle == QgsRasterLayer::MultiBandSingleGandGray )
{
int grayValue = 0;
for ( int i = 0; i < symbolWidth; ++i )
{
grayValue = 255.0 * ( i / symbolWidth );
p->setBrush( QColor( grayValue, grayValue, grayValue ) );
p->drawRect( QRectF( boxSpace + i, currentY + yDownShift, 1, symbolHeight ) );
}
}
else
{
//red/green/blue
p->setBrush( Qt::red );
p->drawRect( QRectF( boxSpace, currentY + yDownShift, symbolWidth / 3.0, symbolHeight ) );
p->setBrush( Qt::green );
p->drawRect( QRectF( boxSpace + symbolWidth / 3.0, currentY + yDownShift, symbolWidth / 3.0, symbolHeight ) );
p->setBrush( Qt::blue );
p->drawRect( QRectF( boxSpace + symbolWidth - symbolWidth / 3.0, currentY + yDownShift, symbolWidth / 3.0, symbolHeight ) );
}
p->setPen( savedPen );
p->setBrush( QBrush( rasterItem->color() ) );
p->drawRect( QRectF( boxSpace, currentY + yDownShift, symbolWidth, symbolHeight ) );
}

QMap<QString, QString> QgsWMSServer::applyRequestedLayerFilters( const QStringList& layerList, const QStringList& layerIds ) const

0 comments on commit 66dded4

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