Skip to content

Commit 160a65e

Browse files
author
mhugent
committed
Fix raster item in composer legend
git-svn-id: http://svn.osgeo.org/qgis/trunk/qgis@13526 c8812cc2-4d05-0410-92ff-de0c093fc19c
1 parent 768acbc commit 160a65e

File tree

3 files changed

+82
-4
lines changed

3 files changed

+82
-4
lines changed

src/core/composer/qgscomposerlegenditem.cpp

+55
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717

1818
#include "qgscomposerlegenditem.h"
1919
#include "qgsmaplayerregistry.h"
20+
#include "qgsrasterlayer.h"
2021
#include "qgssymbol.h"
2122
#include "qgssymbolv2.h"
2223
#include "qgssymbollayerv2utils.h"
@@ -220,6 +221,56 @@ void QgsComposerSymbolV2Item::setSymbolV2( QgsSymbolV2* s )
220221
mSymbolV2 = s;
221222
}
222223

224+
////////////////////QgsComposerRasterSymbolItem
225+
226+
QgsComposerRasterSymbolItem::QgsComposerRasterSymbolItem(): QgsComposerLegendItem()
227+
{
228+
}
229+
230+
QgsComposerRasterSymbolItem::QgsComposerRasterSymbolItem( const QString& text ): QgsComposerLegendItem( text )
231+
{
232+
}
233+
234+
QgsComposerRasterSymbolItem::QgsComposerRasterSymbolItem( const QIcon& icon, const QString& text ): QgsComposerLegendItem( icon, text )
235+
{
236+
}
237+
238+
QgsComposerRasterSymbolItem::~QgsComposerRasterSymbolItem()
239+
{
240+
}
241+
242+
QStandardItem* QgsComposerRasterSymbolItem::clone() const
243+
{
244+
QgsComposerRasterSymbolItem* cloneItem = new QgsComposerRasterSymbolItem();
245+
*cloneItem = *this;
246+
cloneItem->setLayerID( mLayerID );
247+
return cloneItem;
248+
}
249+
250+
void QgsComposerRasterSymbolItem::writeXML( QDomElement& elem, QDomDocument& doc ) const
251+
{
252+
QDomElement rasterClassElem = doc.createElement( "RasterClassificationItem" );
253+
rasterClassElem.setAttribute( "layerId", mLayerID );
254+
rasterClassElem.setAttribute( "text", text() );
255+
elem.appendChild( rasterClassElem );
256+
}
257+
258+
void QgsComposerRasterSymbolItem::readXML( const QDomElement& itemElem )
259+
{
260+
if ( itemElem.isNull() )
261+
{
262+
return;
263+
}
264+
setText( itemElem.attribute( "text", "" ) );
265+
setLayerID( itemElem.attribute( "layerId", "" ) );
266+
267+
QgsRasterLayer* rLayer = qobject_cast<QgsRasterLayer*>( QgsMapLayerRegistry::instance()->mapLayer( mLayerID ) );
268+
if ( rLayer )
269+
{
270+
setIcon( QIcon( rLayer->legendAsPixmap( true ) ) );
271+
}
272+
}
273+
223274
////////////////////QgsComposerLayerItem
224275

225276
QgsComposerLayerItem::QgsComposerLayerItem(): QgsComposerLegendItem()
@@ -285,6 +336,10 @@ void QgsComposerLayerItem::readXML( const QDomElement& itemElem )
285336
{
286337
currentChildItem = new QgsComposerSymbolV2Item();
287338
}
339+
else if ( elemTag == "RasterClassificationItem" )
340+
{
341+
currentChildItem = new QgsComposerRasterSymbolItem();
342+
}
288343
else
289344
{
290345
continue; //unsupported child type

src/core/composer/qgscomposerlegenditem.h

+23-1
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,8 @@ class CORE_EXPORT QgsComposerLegendItem: public QStandardItem
3737
GroupItem = QStandardItem::UserType,
3838
LayerItem,
3939
SymbologyItem,
40-
SymbologyV2Item
40+
SymbologyV2Item,
41+
RasterSymbolItem
4142
};
4243

4344
virtual void writeXML( QDomElement& elem, QDomDocument& doc ) const = 0;
@@ -104,6 +105,27 @@ class CORE_EXPORT QgsComposerSymbolV2Item: public QgsComposerLegendItem
104105
QgsSymbolV2* mSymbolV2;
105106
};
106107

108+
class CORE_EXPORT QgsComposerRasterSymbolItem: public QgsComposerLegendItem
109+
{
110+
public:
111+
QgsComposerRasterSymbolItem();
112+
QgsComposerRasterSymbolItem( const QString& text );
113+
QgsComposerRasterSymbolItem( const QIcon& icon, const QString& text );
114+
virtual ~QgsComposerRasterSymbolItem();
115+
116+
virtual QStandardItem* clone() const;
117+
118+
virtual void writeXML( QDomElement& elem, QDomDocument& doc ) const;
119+
virtual void readXML( const QDomElement& itemElem );
120+
121+
void setLayerID( const QString& id ) { mLayerID = id; }
122+
QString layerID() const { return mLayerID; }
123+
ItemType itemType() const { return RasterSymbolItem; }
124+
125+
private:
126+
QString mLayerID;
127+
};
128+
107129
class CORE_EXPORT QgsComposerLayerItem: public QgsComposerLegendItem
108130
{
109131
public:

src/core/composer/qgslegendmodel.cpp

+4-3
Original file line numberDiff line numberDiff line change
@@ -260,9 +260,9 @@ int QgsLegendModel::addRasterLayerItem( QStandardItem* layerItem, QgsMapLayer* r
260260
return 2;
261261
}
262262

263-
QStandardItem* currentSymbolItem = new QStandardItem( QIcon( rasterLayer->legendAsPixmap( true ) ), "" );
264-
currentSymbolItem->setData( QgsLegendModel::ClassificationItem, Qt::UserRole + 1 ); //first user data stores the item type
265-
263+
//use a vector symbol item without symbol
264+
QgsComposerRasterSymbolItem* currentSymbolItem = new QgsComposerRasterSymbolItem( QIcon( rasterLayer->legendAsPixmap( true ) ), "" );
265+
currentSymbolItem->setLayerID( rasterLayer->getLayerID() );
266266
int currentRowCount = layerItem->rowCount();
267267
layerItem->setChild( currentRowCount, 0, currentSymbolItem );
268268

@@ -478,6 +478,7 @@ void QgsLegendModel::updateRasterClassificationItem( QStandardItem* classificati
478478
}
479479

480480
QStandardItem* currentSymbolItem = new QStandardItem( QIcon( rl->legendAsPixmap( true ) ), "" );
481+
481482
currentSymbolItem->setData( QgsLegendModel::ClassificationItem, Qt::UserRole + 1 ); //first user data stores the item type
482483
parentItem->insertRow( 0, currentSymbolItem );
483484
parentItem->removeRow( 1 );

0 commit comments

Comments
 (0)