Skip to content
Permalink
Browse files

Support for custom labels in symbol-v2 legend node

  • Loading branch information
wonder-sk committed Sep 1, 2014
1 parent 89941f6 commit 8dba8af60d2cfd37bb206e99f3f3310c4bf129bb
@@ -312,13 +312,13 @@ void QgsSymbolV2LegendNode::updateLabel()

if ( mEmbeddedInParent )
{
mLabel = mParent->layerName();
mLabel = mUserLabel.isEmpty() ? mParent->layerName() : mUserLabel;
if ( showFeatureCount && vl && vl->pendingFeatureCount() >= 0 )
mLabel += QString( " [%1]" ).arg( vl->pendingFeatureCount() );
}
else
{
mLabel = mItem.label();
mLabel = mUserLabel.isEmpty() ? mItem.label() : mUserLabel;
if ( showFeatureCount && vl && mItem.legacyRuleKey() )
mLabel += QString( " [%1]" ).arg( vl->featureCount( mItem.legacyRuleKey() ) );
}
@@ -329,9 +329,10 @@ void QgsSymbolV2LegendNode::updateLabel()
// -------------------------------------------------------------------------


QgsSimpleLegendNode::QgsSimpleLegendNode( QgsLayerTreeLayer* nodeLayer, const QString& label, const QIcon& icon )
QgsSimpleLegendNode::QgsSimpleLegendNode( QgsLayerTreeLayer* nodeLayer, const QString& label, const QString& id, const QIcon& icon )
: QgsLayerTreeModelLegendNode( nodeLayer )
, mLabel( label )
, mId( id )
, mIcon( icon )
{
}
@@ -41,6 +41,8 @@ class CORE_EXPORT QgsLayerTreeModelLegendNode : public QObject
/** Return pointer to the parent layer node */
QgsLayerTreeLayer* parent() const { return mParent; }

virtual QString id() const { return QString(); }

/** Return item flags associated with the item. Default implementation returns Qt::ItemIsEnabled. */
virtual Qt::ItemFlags flags() const;

@@ -113,6 +115,8 @@ class CORE_EXPORT QgsSymbolV2LegendNode : public QgsLayerTreeModelLegendNode
QgsSymbolV2LegendNode( QgsLayerTreeLayer* nodeLayer, const QgsLegendSymbolItemV2& item );
~QgsSymbolV2LegendNode();

virtual QString id() const { return mItem.ruleKey(); }

virtual Qt::ItemFlags flags() const;
virtual QVariant data( int role ) const;
virtual bool setData( const QVariant& value, int role );
@@ -123,12 +127,16 @@ class CORE_EXPORT QgsSymbolV2LegendNode : public QgsLayerTreeModelLegendNode

virtual void setEmbeddedInParent( bool embedded );

void setUserLabel( const QString& userLabel ) { mUserLabel = userLabel; }
QString userLabel() const { return mUserLabel; }

private:
void updateLabel();

private:
QgsLegendSymbolItemV2 mItem;
mutable QIcon mIcon; // cached symbol preview
QString mUserLabel;
QString mLabel;
};

@@ -141,12 +149,13 @@ class CORE_EXPORT QgsSymbolV2LegendNode : public QgsLayerTreeModelLegendNode
class CORE_EXPORT QgsSimpleLegendNode : public QgsLayerTreeModelLegendNode
{
public:
QgsSimpleLegendNode( QgsLayerTreeLayer* nodeLayer, const QString& label, const QIcon& icon = QIcon() );
QgsSimpleLegendNode( QgsLayerTreeLayer* nodeLayer, const QString& label, const QString& id, const QIcon& icon = QIcon() );

virtual QVariant data( int role ) const;

private:
QString mLabel;
QString mId;
QIcon mIcon;
};

@@ -69,12 +69,15 @@ QList<QgsLayerTreeModelLegendNode*> QgsDefaultVectorLayerLegend::createLayerTree
QSettings settings;
if ( settings.value( "/qgis/showLegendClassifiers", false ).toBool() && !r->legendClassificationAttribute().isEmpty() )
{
nodes.append( new QgsSimpleLegendNode( nodeLayer, r->legendClassificationAttribute() ) );
nodes.append( new QgsSimpleLegendNode( nodeLayer, r->legendClassificationAttribute(), "_class_attribute_" ) );
}

foreach ( const QgsLegendSymbolItemV2& i, r->legendSymbolItemsV2() )
{
nodes.append( new QgsSymbolV2LegendNode( nodeLayer, i ) );
QgsSymbolV2LegendNode* legendNode = new QgsSymbolV2LegendNode( nodeLayer, i );
if ( mUserLabels.contains( i.ruleKey() ) )
legendNode->setUserLabel( mUserLabels[i.ruleKey()] );
nodes.append( legendNode );
}

if ( nodes.count() == 1 && nodes[0]->data( Qt::EditRole ).toString().isEmpty() )
@@ -83,6 +86,25 @@ QList<QgsLayerTreeModelLegendNode*> QgsDefaultVectorLayerLegend::createLayerTree
return nodes;
}

void QgsDefaultVectorLayerLegend::setRuleUserLabel( const QString& ruleKey, const QString& label )
{
if ( label.isEmpty() )
mUserLabels.remove( ruleKey );
else
mUserLabels[ruleKey] = label;
emit itemsChanged();
}

QString QgsDefaultVectorLayerLegend::ruleUserLabel( const QString& ruleKey ) const
{
return mUserLabels.value( ruleKey );
}

QStringList QgsDefaultVectorLayerLegend::rulesWithUserLabel() const
{
return mUserLabels.keys();
}


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

@@ -125,7 +147,7 @@ QList<QgsLayerTreeModelLegendNode*> QgsDefaultRasterLayerLegend::createLayerTree
if ( count == max_count )
{
QString label = tr( "following %1 items\nnot displayed" ).arg( rasterItemList.size() - max_count );
nodes << new QgsSimpleLegendNode( nodeLayer, label );
nodes << new QgsSimpleLegendNode( nodeLayer, label, "_more_items_" );
break;
}
}
@@ -152,10 +174,11 @@ QList<QgsLayerTreeModelLegendNode*> QgsDefaultPluginLayerLegend::createLayerTree
if ( symbologyList.count() == 0 )
return nodes;

int i = 0;
typedef QPair<QString, QPixmap> XY;
foreach ( XY item, symbologyList )
{
nodes << new QgsSimpleLegendNode( nodeLayer, item.first, QIcon( item.second ) );
nodes << new QgsSimpleLegendNode( nodeLayer, item.first, QString::number( i++ ), QIcon( item.second ) );
}

return nodes;
@@ -61,6 +61,7 @@ class CORE_EXPORT QgsMapLayerLegend : public QObject
void itemsChanged();
};

#include <QHash>

/** Default legend implementation for vector layers
* @note added in 2.6
@@ -72,8 +73,15 @@ class CORE_EXPORT QgsDefaultVectorLayerLegend : public QgsMapLayerLegend

virtual QList<QgsLayerTreeModelLegendNode*> createLayerTreeModelLegendNodes( QgsLayerTreeLayer* nodeLayer );

void setRuleUserLabel( const QString& ruleKey, const QString& label );

QString ruleUserLabel( const QString& ruleKey ) const;

QStringList rulesWithUserLabel() const;

private:
QgsVectorLayer* mLayer;
QHash<QString, QString> mUserLabels;
};


@@ -177,16 +177,17 @@ void TestQgsLegendRenderer::testModel()
QVERIFY( lstNodes.count() == 1 );
QCOMPARE( lstNodes[0]->data( Qt::DisplayRole ).toString(), QString( "Line Layer" ) );

#if 0 // TODO: implement custom legend entry labels
QgsDefaultVectorLayerLegend* legendVL1 = dynamic_cast<QgsDefaultVectorLayerLegend*>( mVL1->legend() );
QVERIFY( legendVL1 );

// set user text
mVL1->legend()->setNodeUserText( lstNodes[0].id(), "Hurray" );
legendVL1->setRuleUserLabel( lstNodes[0]->id(), "Hurray" );

QList<QgsLayerTreeModelLegendNode*> lstNodes2 = legendModel.layerLegendNodes( nodeVL1 );
QCOMPARE( lstNodes2[0]->data( Qt::DisplayRole ).toString(), QString( "Hurray" ) );

// reset user text
mVL1->legend()->setNodeUserText( lstNodes2[0].id(), QString() );
#endif
legendVL1->setRuleUserLabel( lstNodes2[0]->id(), QString() );
}


0 comments on commit 8dba8af

Please sign in to comment.
You can’t perform that action at this time.