Skip to content

Commit

Permalink
Support for raster legend in composer
Browse files Browse the repository at this point in the history
  • Loading branch information
mhugent committed Jun 16, 2011
1 parent 325e25f commit 66dded4
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 38 deletions.
4 changes: 4 additions & 0 deletions src/core/composer/qgscomposerlegenditem.h
Expand Up @@ -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
Expand Down
27 changes: 17 additions & 10 deletions src/core/composer/qgslegendmodel.cpp
Expand Up @@ -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 )
{
Expand All @@ -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;
}
Expand Down Expand Up @@ -304,7 +311,7 @@ void QgsLegendModel::updateLayer( QStandardItem* layerItem )
QgsRasterLayer* rLayer = qobject_cast<QgsRasterLayer*>( mapLayer );
if ( rLayer )
{
addRasterLayerItem( lItem, rLayer );
addRasterLayerItems( lItem, rLayer );
}
}
}
Expand Down Expand Up @@ -362,7 +369,7 @@ void QgsLegendModel::addLayer( QgsMapLayer* theMapLayer )
break;
}
case QgsMapLayer::RasterLayer:
addRasterLayerItem( layerItem, theMapLayer );
addRasterLayerItems( layerItem, theMapLayer );
break;
default:
break;
Expand Down
2 changes: 1 addition & 1 deletion src/core/composer/qgslegendmodel.h
Expand Up @@ -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 );
Expand Down
29 changes: 2 additions & 27 deletions src/mapserver/qgswmsserver.cpp
Expand Up @@ -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
Expand Down

0 comments on commit 66dded4

Please sign in to comment.