Automatic creation of composer legend symbol item icon

This commit is contained in:
Martin Dobias 2014-08-04 19:22:03 +07:00
parent fda50ede01
commit 9dd7c73cd0
5 changed files with 90 additions and 10 deletions

View File

@ -166,6 +166,18 @@ QgsComposerSymbolV2Item::~QgsComposerSymbolV2Item()
delete mSymbolV2;
}
QVariant QgsComposerSymbolV2Item::data( int role ) const
{
if ( role == Qt::DecorationRole )
{
if ( mIcon.isNull() )
mIcon = QgsSymbolLayerV2Utils::symbolPreviewIcon( mSymbolV2, QSize( 30, 30 ) );
return mIcon;
}
else
return QgsComposerBaseSymbolItem::data( role );
}
QStandardItem* QgsComposerSymbolV2Item::clone() const
{
QgsComposerSymbolV2Item* cloneItem = new QgsComposerSymbolV2Item();

View File

@ -129,9 +129,13 @@ class CORE_EXPORT QgsComposerSymbolV2Item : public QgsComposerBaseSymbolItem
public:
QgsComposerSymbolV2Item();
QgsComposerSymbolV2Item( const QString& text );
//! @deprecated
QgsComposerSymbolV2Item( const QIcon& icon, const QString& text );
virtual ~QgsComposerSymbolV2Item();
//! lazy creation of icon
virtual QVariant data( int role ) const;
virtual QStandardItem* clone() const;
virtual void writeXML( QDomElement& elem, QDomDocument& doc ) const;
@ -149,6 +153,7 @@ class CORE_EXPORT QgsComposerSymbolV2Item : public QgsComposerBaseSymbolItem
private:
QgsSymbolV2* mSymbolV2;
mutable QIcon mIcon;
};
class CORE_EXPORT QgsComposerRasterSymbolItem : public QgsComposerBaseSymbolItem
@ -228,6 +233,10 @@ class CORE_EXPORT QgsComposerGroupItem: public QgsComposerLegendItem
};
/**
* Item used for 2nd column of the legend model for layers and groups to indicate
* style of the item (e.g. hidden, group, sub-group)
*/
class CORE_EXPORT QgsComposerStyleItem: public QStandardItem
{
public:

View File

@ -206,10 +206,6 @@ int QgsLegendModel::addVectorLayerItemsV2( QStandardItem* layerItem, QgsVectorLa
currentSymbolItem->setFlags( Qt::ItemIsEnabled | Qt::ItemIsSelectable );
if ( symbolIt->second )
{
if ( mHasTopLevelWindow ) //only use QIcon / QPixmap if we have a running x-server
{
currentSymbolItem->setIcon( QgsSymbolLayerV2Utils::symbolPreviewIcon( symbolIt->second, QSize( 30, 30 ) ) );
}
currentSymbolItem->setSymbolV2( symbolIt->second->clone() );
}
layerItem->setChild( row, 0, currentSymbolItem );
@ -220,10 +216,6 @@ int QgsLegendModel::addVectorLayerItemsV2( QStandardItem* layerItem, QgsVectorLa
else
{
QgsComposerSymbolV2Item* currentSymbolItem = new QgsComposerSymbolV2Item( "" );
if ( mHasTopLevelWindow ) //only use QIcon / QPixmap if we have a running x-server
{
currentSymbolItem->setIcon( QgsSymbolLayerV2Utils::symbolPreviewIcon( symbolIt->second, QSize( 30, 30 ) ) );
}
currentSymbolItem->setSymbolV2( symbolIt->second->clone() );
layerItem->setChild( row, 0, currentSymbolItem );
currentSymbolItem->setText( symbolIt->first );

View File

@ -194,6 +194,66 @@ QList<QgsLayerTreeModelLegendNode*> QgsDefaultVectorLayerLegend::createLayerTree
return nodes;
}
/*
#include "qgscomposerlegenditem.h"
QList<QgsComposerBaseSymbolItem*> QgsDefaultVectorLayerLegend::createLegendModelItems( QgsComposerLayerItem* layerItem )
{
QList<QgsComposerBaseSymbolItem*> items;
QgsFeatureRendererV2* renderer = mLayer->rendererV2();
if ( !renderer )
return items;
if ( layerItem->showFeatureCount() )
{
if ( !mLayer->countSymbolFeatures() )
{
QgsDebugMsg( "Cannot get feature counts" );
}
}
double scaleDenominator = -1;
QString rule;
// TODO: new method for legend symbol items (symbol + label + scale + rule)
QgsLegendSymbolList lst = renderer->legendSymbolItems( scaleDenominator, rule );
QgsLegendSymbolList::const_iterator symbolIt = lst.constBegin();
int row = 0;
for ( ; symbolIt != lst.constEnd(); ++symbolIt )
{
if ( scaleDenominator == -1 && rule.isEmpty() )
{
QgsComposerSymbolV2Item* currentSymbolItem = new QgsComposerSymbolV2Item( QString() );
// Get userText from old item if exists
QgsComposerSymbolV2Item* oldSymbolItem = dynamic_cast<QgsComposerSymbolV2Item*>( layerItem->child( row, 0 ) );
if ( oldSymbolItem )
{
currentSymbolItem->setUserText( oldSymbolItem->userText() );
}
currentSymbolItem->setFlags( Qt::ItemIsEnabled | Qt::ItemIsSelectable );
if ( symbolIt->second )
{
currentSymbolItem->setSymbolV2( symbolIt->second->clone() );
}
layerItem->setChild( row, 0, currentSymbolItem );
// updateSymbolV2ItemText needs layer set
updateSymbolV2ItemText( currentSymbolItem );
}
else
{
QgsComposerSymbolV2Item* currentSymbolItem = new QgsComposerSymbolV2Item( QString() );
currentSymbolItem->setSymbolV2( symbolIt->second->clone() );
layerItem->setChild( row, 0, currentSymbolItem );
currentSymbolItem->setText( symbolIt->first );
}
row++;
}
}*/
// -------------------------------------------------------------------------

View File

@ -102,6 +102,8 @@ class CORE_EXPORT QgsSimpleLegendNode : public QgsLayerTreeModelLegendNode
QIcon mIcon;
};
class QgsComposerLayerItem;
class QgsComposerBaseSymbolItem;
/**
* The QgsMapLayerLegend class is abstract interface for implementations
@ -125,8 +127,11 @@ class CORE_EXPORT QgsMapLayerLegend : public QObject
// TODO: support for layer tree view delegates
// TODO: support for legend renderer
/**
* Return list of legend model items to be used in QgsLegendRenderer.
* Ownership is transferred to the caller.
*/
virtual QList<QgsComposerBaseSymbolItem*> createLegendModelItems( QgsComposerLayerItem* layerItem ) { Q_UNUSED( layerItem ); return QList<QgsComposerBaseSymbolItem*>(); }
//! Create new legend implementation for vector layer
static QgsMapLayerLegend* defaultVectorLegend( QgsVectorLayer* vl );
@ -153,6 +158,8 @@ class CORE_EXPORT QgsDefaultVectorLayerLegend : public QgsMapLayerLegend
virtual QList<QgsLayerTreeModelLegendNode*> createLayerTreeModelLegendNodes( QgsLayerTreeLayer* nodeLayer );
//virtual QList<QgsComposerBaseSymbolItem*> createLegendModelItems( QgsComposerLayerItem* layerItem );
private:
QgsVectorLayer* mLayer;
};