Skip to content

Commit

Permalink
Automatic creation of composer legend symbol item icon
Browse files Browse the repository at this point in the history
  • Loading branch information
wonder-sk committed Sep 1, 2014
1 parent fda50ed commit 9dd7c73
Show file tree
Hide file tree
Showing 5 changed files with 90 additions and 10 deletions.
12 changes: 12 additions & 0 deletions src/core/composer/qgscomposerlegenditem.cpp
Expand Up @@ -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();
Expand Down
9 changes: 9 additions & 0 deletions src/core/composer/qgscomposerlegenditem.h
Expand Up @@ -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;
Expand All @@ -149,6 +153,7 @@ class CORE_EXPORT QgsComposerSymbolV2Item : public QgsComposerBaseSymbolItem

private:
QgsSymbolV2* mSymbolV2;
mutable QIcon mIcon;
};

class CORE_EXPORT QgsComposerRasterSymbolItem : public QgsComposerBaseSymbolItem
Expand Down Expand Up @@ -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:
Expand Down
8 changes: 0 additions & 8 deletions src/core/composer/qgslegendmodel.cpp
Expand Up @@ -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 );
Expand All @@ -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 );
Expand Down
60 changes: 60 additions & 0 deletions src/core/qgsmaplayerlegend.cpp
Expand Up @@ -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++;
}
}*/


// -------------------------------------------------------------------------

Expand Down
11 changes: 9 additions & 2 deletions src/core/qgsmaplayerlegend.h
Expand Up @@ -102,6 +102,8 @@ class CORE_EXPORT QgsSimpleLegendNode : public QgsLayerTreeModelLegendNode
QIcon mIcon;
};

class QgsComposerLayerItem;
class QgsComposerBaseSymbolItem;

/**
* The QgsMapLayerLegend class is abstract interface for implementations
Expand All @@ -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 );
Expand All @@ -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;
};
Expand Down

0 comments on commit 9dd7c73

Please sign in to comment.