Skip to content

Commit e28d1e4

Browse files
committed
[FEATURE]: Show colormap items in legend
1 parent a954488 commit e28d1e4

File tree

3 files changed

+42
-0
lines changed

3 files changed

+42
-0
lines changed

src/app/legend/qgslegendlayer.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -306,10 +306,22 @@ void QgsLegendLayer::vectorLayerSymbologyV2( QgsVectorLayer* layer )
306306

307307
void QgsLegendLayer::rasterLayerSymbology( QgsRasterLayer* layer )
308308
{
309+
#if 0
309310
SymbologyList itemList;
310311
QPixmap legendpixmap = layer->legendAsPixmap( true ).scaled( 20, 20, Qt::KeepAspectRatio );
311312
itemList.append( qMakePair( QString(), legendpixmap ) );
312313

314+
changeSymbologySettings( layer, itemList );
315+
#endif //0
316+
SymbologyList itemList;
317+
QList< QPair< QString, QColor > > rasterItemList = layer->legendSymbologyItems();
318+
QList< QPair< QString, QColor > >::const_iterator itemIt = rasterItemList.constBegin();
319+
for(; itemIt != rasterItemList.constEnd(); ++itemIt )
320+
{
321+
QPixmap itemPixmap( treeWidget()->iconSize() );
322+
itemPixmap.fill( itemIt->second );
323+
itemList.append( qMakePair( itemIt->first, itemPixmap ) );
324+
}
313325
changeSymbologySettings( layer, itemList );
314326
}
315327

src/core/raster/qgsrasterlayer.cpp

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1209,6 +1209,32 @@ QString QgsRasterLayer::lastErrorTitle()
12091209
return mErrorCaption;
12101210
}
12111211

1212+
QList< QPair< QString, QColor > > QgsRasterLayer::legendSymbologyItems() const
1213+
{
1214+
QList< QPair< QString, QColor > > symbolList;
1215+
1216+
switch ( mColorShadingAlgorithm )
1217+
{
1218+
case ColorRampShader:
1219+
{
1220+
const QgsColorRampShader* crShader = dynamic_cast<QgsColorRampShader*>( mRasterShader->rasterShaderFunction() );
1221+
if( crShader )
1222+
{
1223+
QList<QgsColorRampShader::ColorRampItem> shaderItems = crShader->colorRampItemList();
1224+
QList<QgsColorRampShader::ColorRampItem>::const_iterator itemIt = shaderItems.constBegin();
1225+
for(; itemIt != shaderItems.constEnd(); ++itemIt )
1226+
{
1227+
symbolList.push_back( qMakePair( itemIt->label, itemIt->color ) );
1228+
}
1229+
}
1230+
break;
1231+
}
1232+
default:
1233+
break;
1234+
}
1235+
return symbolList;
1236+
}
1237+
12121238
/**
12131239
* This is an overloaded version of the legendAsPixmap( bool ) assumes false for the legend name flag.
12141240
* @return a pixmap representing a legend image

src/core/raster/qgsrasterlayer.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -521,6 +521,10 @@ class CORE_EXPORT QgsRasterLayer : public QgsMapLayer
521521
/** \brief [ data provider interface ] If an operation returns 0 (e.g. draw()), this function returns the text of the error associated with the failure */
522522
QString lastErrorTitle();
523523

524+
/**Returns a list with classification items (Text and color)
525+
@note this method was added in version 1.8*/
526+
QList< QPair< QString, QColor > > legendSymbologyItems() const;
527+
524528
/** \brief Get a legend image for this layer */
525529
QPixmap legendAsPixmap();
526530

0 commit comments

Comments
 (0)