Skip to content

Commit

Permalink
add findAllGroups to recursively get all groups
Browse files Browse the repository at this point in the history
  • Loading branch information
jgrocha authored and nyalldawson committed Mar 23, 2021
1 parent fed1ef2 commit 2eb7781
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 2 deletions.
Expand Up @@ -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/;
Expand Down
16 changes: 16 additions & 0 deletions src/core/layertree/qgslayertreegroup.cpp
Expand Up @@ -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" ) )
Expand Down
7 changes: 6 additions & 1 deletion src/core/layertree/qgslayertreegroup.h
Expand Up @@ -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.
Expand Down
24 changes: 24 additions & 0 deletions tests/src/core/testqgslayertree.cpp
Expand Up @@ -55,6 +55,7 @@ class TestQgsLayerTree : public QObject
void testFindLayer();
void testLayerDeleted();
void testFindGroups();
void testFindNestedGroups();
void testUtilsCollectMapLayers();
void testUtilsCountMapLayers();
void testSymbolText();
Expand Down Expand Up @@ -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" ) );
Expand Down

0 comments on commit 2eb7781

Please sign in to comment.