Skip to content
Permalink
Browse files

Add method for retrieving legend nodes by layer ID and legend rule key

  • Loading branch information
nyalldawson committed Dec 7, 2015
1 parent 197ee6c commit 83e139a550c4b022d2d24c0fe23514645f4e4ab6
@@ -97,6 +97,15 @@ class QgsLayerTreeModel : QAbstractItemModel
//! @note added in 2.6
QList<QgsLayerTreeModelLegendNode*> layerLegendNodes( QgsLayerTreeLayer* nodeLayer );

/** Searches through the layer tree to find a legend node with a matching layer ID
* and rule key.
* @param layerId map layer ID
* @param ruleKey legend node rule key
* @returns QgsLayerTreeModelLegendNode if found
* @note added in QGIS 2.14
*/
QgsLayerTreeModelLegendNode* findLegendNode( const QString& layerId, const QString& ruleKey ) const;

//! Return pointer to the root node of the layer tree. Always a non-null pointer.
QgsLayerTreeGroup* rootGroup() const;
//! Reset the model and use a new root group node
@@ -1333,6 +1333,26 @@ QList<QgsLayerTreeModelLegendNode*> QgsLayerTreeModel::layerLegendNodes( QgsLaye
return mLegend.value( nodeLayer ).activeNodes;
}

QgsLayerTreeModelLegendNode* QgsLayerTreeModel::findLegendNode( const QString& layerId, const QString& ruleKey ) const
{
Q_FOREACH ( QgsLayerTreeLayer* layer, mLegend.keys() )
{
if ( layer->layerId() == layerId )
{
Q_FOREACH ( QgsLayerTreeModelLegendNode* legendNode, mLegend.value( layer ).activeNodes )
{
if ( legendNode->data( QgsLayerTreeModelLegendNode::RuleKeyRole ).toString() == ruleKey )
{
//found it!
return legendNode;
}
}
}
}

return 0;
}

void QgsLayerTreeModel::legendInvalidateMapBasedData()
{
if ( !testFlag( DeferredLegendInvalidation ) )
@@ -121,6 +121,15 @@ class CORE_EXPORT QgsLayerTreeModel : public QAbstractItemModel
//! @note added in 2.6
QList<QgsLayerTreeModelLegendNode*> layerLegendNodes( QgsLayerTreeLayer* nodeLayer );

/** Searches through the layer tree to find a legend node with a matching layer ID
* and rule key.
* @param layerId map layer ID
* @param ruleKey legend node rule key
* @returns QgsLayerTreeModelLegendNode if found
* @note added in QGIS 2.14
*/
QgsLayerTreeModelLegendNode* findLegendNode( const QString& layerId, const QString& ruleKey ) const;

//! Return pointer to the root node of the layer tree. Always a non-null pointer.
QgsLayerTreeGroup* rootGroup() const;
//! Reset the model and use a new root group node
@@ -38,6 +38,7 @@ class TestQgsLayerTree : public QObject
void testCheckStateMutuallyExclusive();
void testCheckStateMutuallyExclusiveEdgeCases();
void testShowHideAllSymbolNodes();
void testFindLegendNode();

private:

@@ -268,6 +269,48 @@ void TestQgsLayerTree::testShowHideAllSymbolNodes()
QgsMapLayerRegistry::instance()->removeMapLayers( QList<QgsMapLayer*>() << vl );
}

void TestQgsLayerTree::testFindLegendNode()
{
//new memory layer
QgsVectorLayer* vl = new QgsVectorLayer( "Point?field=col1:integer", "vl", "memory" );
QVERIFY( vl->isValid() );

QgsMapLayerRegistry::instance()->addMapLayers( QList<QgsMapLayer*>() << vl );

//create a categorized renderer for layer
QgsCategorizedSymbolRendererV2* renderer = new QgsCategorizedSymbolRendererV2();
renderer->setClassAttribute( "col1" );
renderer->setSourceSymbol( QgsSymbolV2::defaultSymbol( QGis::Point ) );
renderer->addCategory( QgsRendererCategoryV2( "a", QgsSymbolV2::defaultSymbol( QGis::Point ), "a" ) );
renderer->addCategory( QgsRendererCategoryV2( "b", QgsSymbolV2::defaultSymbol( QGis::Point ), "b" ) );
renderer->addCategory( QgsRendererCategoryV2( "c", QgsSymbolV2::defaultSymbol( QGis::Point ), "c" ) );
vl->setRendererV2( renderer );

//create legend with symbology nodes for categorized renderer
QgsLayerTreeGroup* root = new QgsLayerTreeGroup();
QgsLayerTreeModel* m = new QgsLayerTreeModel( root, 0 );
QVERIFY( !m->findLegendNode( QString( "id" ), QString( "rule" ) ) );
QgsLayerTreeLayer* n = new QgsLayerTreeLayer( vl );
root->addChildNode( n );
m->refreshLayerLegend( n );
QVERIFY( !m->findLegendNode( QString( "id" ), QString( "rule" ) ) );
QVERIFY( !m->findLegendNode( QString( "vl" ), QString( "rule" ) ) );

QgsLegendSymbolListV2 symbolList = renderer->legendSymbolItemsV2();
Q_FOREACH ( const QgsLegendSymbolItemV2& symbol, symbolList )
{
QgsLayerTreeModelLegendNode* found = m->findLegendNode( vl->id(), symbol.ruleKey() );
QVERIFY( found );
QCOMPARE( found->layerNode()->layerId(), vl->id() );
QCOMPARE( found->data( Qt::DisplayRole ).toString(), symbol.label() );
}

//cleanup
delete m;
delete root;
QgsMapLayerRegistry::instance()->removeMapLayers( QList<QgsMapLayer*>() << vl );
}


QTEST_MAIN( TestQgsLayerTree )
#include "testqgslayertree.moc"

0 comments on commit 83e139a

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