Skip to content

Commit

Permalink
Fix raster item in composer legend
Browse files Browse the repository at this point in the history
git-svn-id: http://svn.osgeo.org/qgis/trunk/qgis@13526 c8812cc2-4d05-0410-92ff-de0c093fc19c
  • Loading branch information
mhugent committed May 18, 2010
1 parent 768acbc commit 160a65e
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 4 deletions.
55 changes: 55 additions & 0 deletions src/core/composer/qgscomposerlegenditem.cpp
Expand Up @@ -17,6 +17,7 @@

#include "qgscomposerlegenditem.h"
#include "qgsmaplayerregistry.h"
#include "qgsrasterlayer.h"
#include "qgssymbol.h"
#include "qgssymbolv2.h"
#include "qgssymbollayerv2utils.h"
Expand Down Expand Up @@ -220,6 +221,56 @@ void QgsComposerSymbolV2Item::setSymbolV2( QgsSymbolV2* s )
mSymbolV2 = s;
}

////////////////////QgsComposerRasterSymbolItem

QgsComposerRasterSymbolItem::QgsComposerRasterSymbolItem(): QgsComposerLegendItem()
{
}

QgsComposerRasterSymbolItem::QgsComposerRasterSymbolItem( const QString& text ): QgsComposerLegendItem( text )
{
}

QgsComposerRasterSymbolItem::QgsComposerRasterSymbolItem( const QIcon& icon, const QString& text ): QgsComposerLegendItem( icon, text )
{
}

QgsComposerRasterSymbolItem::~QgsComposerRasterSymbolItem()
{
}

QStandardItem* QgsComposerRasterSymbolItem::clone() const
{
QgsComposerRasterSymbolItem* cloneItem = new QgsComposerRasterSymbolItem();
*cloneItem = *this;
cloneItem->setLayerID( mLayerID );
return cloneItem;
}

void QgsComposerRasterSymbolItem::writeXML( QDomElement& elem, QDomDocument& doc ) const
{
QDomElement rasterClassElem = doc.createElement( "RasterClassificationItem" );
rasterClassElem.setAttribute( "layerId", mLayerID );
rasterClassElem.setAttribute( "text", text() );
elem.appendChild( rasterClassElem );
}

void QgsComposerRasterSymbolItem::readXML( const QDomElement& itemElem )
{
if ( itemElem.isNull() )
{
return;
}
setText( itemElem.attribute( "text", "" ) );
setLayerID( itemElem.attribute( "layerId", "" ) );

QgsRasterLayer* rLayer = qobject_cast<QgsRasterLayer*>( QgsMapLayerRegistry::instance()->mapLayer( mLayerID ) );
if ( rLayer )
{
setIcon( QIcon( rLayer->legendAsPixmap( true ) ) );
}
}

////////////////////QgsComposerLayerItem

QgsComposerLayerItem::QgsComposerLayerItem(): QgsComposerLegendItem()
Expand Down Expand Up @@ -285,6 +336,10 @@ void QgsComposerLayerItem::readXML( const QDomElement& itemElem )
{
currentChildItem = new QgsComposerSymbolV2Item();
}
else if ( elemTag == "RasterClassificationItem" )
{
currentChildItem = new QgsComposerRasterSymbolItem();
}
else
{
continue; //unsupported child type
Expand Down
24 changes: 23 additions & 1 deletion src/core/composer/qgscomposerlegenditem.h
Expand Up @@ -37,7 +37,8 @@ class CORE_EXPORT QgsComposerLegendItem: public QStandardItem
GroupItem = QStandardItem::UserType,
LayerItem,
SymbologyItem,
SymbologyV2Item
SymbologyV2Item,
RasterSymbolItem
};

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

class CORE_EXPORT QgsComposerRasterSymbolItem: public QgsComposerLegendItem
{
public:
QgsComposerRasterSymbolItem();
QgsComposerRasterSymbolItem( const QString& text );
QgsComposerRasterSymbolItem( const QIcon& icon, const QString& text );
virtual ~QgsComposerRasterSymbolItem();

virtual QStandardItem* clone() const;

virtual void writeXML( QDomElement& elem, QDomDocument& doc ) const;
virtual void readXML( const QDomElement& itemElem );

void setLayerID( const QString& id ) { mLayerID = id; }
QString layerID() const { return mLayerID; }
ItemType itemType() const { return RasterSymbolItem; }

private:
QString mLayerID;
};

class CORE_EXPORT QgsComposerLayerItem: public QgsComposerLegendItem
{
public:
Expand Down
7 changes: 4 additions & 3 deletions src/core/composer/qgslegendmodel.cpp
Expand Up @@ -260,9 +260,9 @@ int QgsLegendModel::addRasterLayerItem( QStandardItem* layerItem, QgsMapLayer* r
return 2;
}

QStandardItem* currentSymbolItem = new QStandardItem( QIcon( rasterLayer->legendAsPixmap( true ) ), "" );
currentSymbolItem->setData( QgsLegendModel::ClassificationItem, Qt::UserRole + 1 ); //first user data stores the item type

//use a vector symbol item without symbol
QgsComposerRasterSymbolItem* currentSymbolItem = new QgsComposerRasterSymbolItem( QIcon( rasterLayer->legendAsPixmap( true ) ), "" );
currentSymbolItem->setLayerID( rasterLayer->getLayerID() );
int currentRowCount = layerItem->rowCount();
layerItem->setChild( currentRowCount, 0, currentSymbolItem );

Expand Down Expand Up @@ -478,6 +478,7 @@ void QgsLegendModel::updateRasterClassificationItem( QStandardItem* classificati
}

QStandardItem* currentSymbolItem = new QStandardItem( QIcon( rl->legendAsPixmap( true ) ), "" );

currentSymbolItem->setData( QgsLegendModel::ClassificationItem, Qt::UserRole + 1 ); //first user data stores the item type
parentItem->insertRow( 0, currentSymbolItem );
parentItem->removeRow( 1 );
Expand Down

0 comments on commit 160a65e

Please sign in to comment.