Skip to content

Commit

Permalink
Add optional recursive = false to findGroups
Browse files Browse the repository at this point in the history
  • Loading branch information
jgrocha authored and nyalldawson committed Mar 23, 2021
1 parent 2eb7781 commit 01806b1
Show file tree
Hide file tree
Showing 4 changed files with 8 additions and 29 deletions.
9 changes: 2 additions & 7 deletions python/core/auto_generated/layertree/qgslayertreegroup.sip.in
Expand Up @@ -129,14 +129,9 @@ Find layer IDs used in all layer nodes. Searches recursively the whole sub-tree.
Find group node with specified name. Searches recursively the whole sub-tree.
%End

QList<QgsLayerTreeGroup *> findGroups() const;
QList<QgsLayerTreeGroup *> findGroups( bool recursive = false ) const;
%Docstring
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.
Find group layer nodes. Searches recursively the whole sub-tree, is recursive is set.
%End

static QgsLayerTreeGroup *readXml( QDomElement &element, const QgsReadWriteContext &context ) /Factory/;
Expand Down
17 changes: 3 additions & 14 deletions src/core/layertree/qgslayertreegroup.cpp
Expand Up @@ -253,29 +253,18 @@ QgsLayerTreeGroup *QgsLayerTreeGroup::findGroup( const QString &name )
return nullptr;
}

QList<QgsLayerTreeGroup *> QgsLayerTreeGroup::findGroups() const
QList<QgsLayerTreeGroup *> QgsLayerTreeGroup::findGroups( bool recursive ) const
{
QList<QgsLayerTreeGroup *> list;

for ( QgsLayerTreeNode *child : mChildren )
{
if ( QgsLayerTree::isGroup( child ) )
list << QgsLayerTree::toGroup( child );
}
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( );
if ( recursive )
list << childGroup->findGroups( recursive );
}
}
return list;
Expand Down
9 changes: 2 additions & 7 deletions src/core/layertree/qgslayertreegroup.h
Expand Up @@ -142,14 +142,9 @@ class CORE_EXPORT QgsLayerTreeGroup : public QgsLayerTreeNode
QgsLayerTreeGroup *findGroup( const QString &name );

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

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

/**
* Read group (tree) from XML element <layer-tree-group> and return the newly created group (or NULLPTR on error).
Expand Down
2 changes: 1 addition & 1 deletion tests/src/core/testqgslayertree.cpp
Expand Up @@ -732,7 +732,7 @@ void TestQgsLayerTree::testFindNestedGroups()
QVERIFY( groups.contains( group2 ) == 0 );
QVERIFY( groups.contains( group3 ) == 0 );

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

QVERIFY( all.contains( group1 ) );
QVERIFY( all.contains( group2 ) );
Expand Down

0 comments on commit 01806b1

Please sign in to comment.