Skip to content
Permalink
Browse files

add findAllGroups to recursively get all groups

  • Loading branch information
jgrocha authored and nyalldawson committed Mar 23, 2021
1 parent fed1ef2 commit 2eb7781f5160ca8d61132239f73fb8ccb84ac1e8
@@ -131,7 +131,12 @@ Find group node with specified name. Searches recursively the whole sub-tree.

QList<QgsLayerTreeGroup *> findGroups() const;
%Docstring
Find all group layer nodes
Find child group layer nodes. Does not search recursively the whole sub-tree.
%End

QList<QgsLayerTreeGroup *> findAllGroups() const;
%Docstring
Find all group layer nodes. Searches recursively the whole sub-tree.
%End

static QgsLayerTreeGroup *readXml( QDomElement &element, const QgsReadWriteContext &context ) /Factory/;
@@ -265,6 +265,22 @@ QList<QgsLayerTreeGroup *> QgsLayerTreeGroup::findGroups() const
return list;
}

QList<QgsLayerTreeGroup *> QgsLayerTreeGroup::findAllGroups() const
{
QList<QgsLayerTreeGroup *> list;

for ( QgsLayerTreeNode *child : std::as_const( mChildren ) )
{
if ( QgsLayerTree::isGroup( child ) )
{
QgsLayerTreeGroup *childGroup = QgsLayerTree::toGroup( child );
list << childGroup;
list << childGroup->findAllGroups( );
}
}
return list;
}

QgsLayerTreeGroup *QgsLayerTreeGroup::readXml( QDomElement &element, const QgsReadWriteContext &context )
{
if ( element.tagName() != QLatin1String( "layer-tree-group" ) )
@@ -142,10 +142,15 @@ class CORE_EXPORT QgsLayerTreeGroup : public QgsLayerTreeNode
QgsLayerTreeGroup *findGroup( const QString &name );

/**
* Find all group layer nodes
* Find child group layer nodes. Does not search recursively the whole sub-tree.
*/
QList<QgsLayerTreeGroup *> findGroups() const;

/**
* Find all group layer nodes. Searches recursively the whole sub-tree.
*/
QList<QgsLayerTreeGroup *> findAllGroups() const;

/**
* Read group (tree) from XML element <layer-tree-group> and return the newly created group (or NULLPTR on error).
* Does not resolve textual references to layers. Call resolveReferences() afterwards to do it.
@@ -55,6 +55,7 @@ class TestQgsLayerTree : public QObject
void testFindLayer();
void testLayerDeleted();
void testFindGroups();
void testFindNestedGroups();
void testUtilsCollectMapLayers();
void testUtilsCountMapLayers();
void testSymbolText();
@@ -715,6 +716,29 @@ void TestQgsLayerTree::testFindGroups()
QVERIFY( groups.contains( group3 ) );
}

void TestQgsLayerTree::testFindNestedGroups()
{
QgsProject project;
QgsLayerTreeGroup *group1 = project.layerTreeRoot()->addGroup( QStringLiteral( "Group_One" ) );
QVERIFY( group1 );
QgsLayerTreeGroup *group2 = group1->addGroup( QStringLiteral( "Group_Two" ) );
QVERIFY( group2 );
QgsLayerTreeGroup *group3 = group2->addGroup( QStringLiteral( "Group_Three" ) );
QVERIFY( group3 );

QList<QgsLayerTreeGroup *> groups = project.layerTreeRoot()->findGroups();

QVERIFY( groups.contains( group1 ) );
QVERIFY( groups.contains( group2 ) == 0 );
QVERIFY( groups.contains( group3 ) == 0 );

QList<QgsLayerTreeGroup *> all = project.layerTreeRoot()->findAllGroups();

QVERIFY( all.contains( group1 ) );
QVERIFY( all.contains( group2 ) );
QVERIFY( all.contains( group3 ) );
}

void TestQgsLayerTree::testUtilsCollectMapLayers()
{
QgsVectorLayer *vl1 = new QgsVectorLayer( QStringLiteral( "Point?field=col1:integer" ), QStringLiteral( "vl1" ), QStringLiteral( "memory" ) );

0 comments on commit 2eb7781

Please sign in to comment.