Skip to content
Permalink
Browse files

Move recursive "collect layers" algorithm to QgsLayerTreeUtils + tests

  • Loading branch information
wonder-sk committed Sep 26, 2018
1 parent ea17162 commit 5047571c13ae04c277a45729e8bc786920f57de1
@@ -96,6 +96,14 @@ Insert a QgsMapLayer just below another one
:param layerToInsert: the new layer to insert just below the reference layer

:return: the new tree layer
%End

static QSet<QgsMapLayer *> collectMapLayersRecursive( const QList<QgsLayerTreeNode *> &nodes );
%Docstring
Returns map layers from the given list of layer tree nodes. Also recursively visits
child nodes of groups.

.. versionadded:: 3.4
%End
};

@@ -413,3 +413,27 @@ QgsLayerTreeLayer *QgsLayerTreeUtils::insertLayerBelow( QgsLayerTreeGroup *group
QgsLayerTreeGroup *parent = static_cast<QgsLayerTreeGroup *>( inTree->parent() ) ? static_cast<QgsLayerTreeGroup *>( inTree->parent() ) : group;
return parent->insertLayer( idx, layerToInsert );
}

static void _collectMapLayers( const QList<QgsLayerTreeNode *> &nodes, QSet<QgsMapLayer *> &layersSet )
{
for ( QgsLayerTreeNode *node : nodes )
{
if ( QgsLayerTree::isLayer( node ) )
{
QgsLayerTreeLayer *nodeLayer = QgsLayerTree::toLayer( node );
if ( nodeLayer->layer() )
layersSet << nodeLayer->layer();
}
else if ( QgsLayerTree::isGroup( node ) )
{
_collectMapLayers( QgsLayerTree::toGroup( node )->children(), layersSet );
}
}
}

QSet<QgsMapLayer *> QgsLayerTreeUtils::collectMapLayersRecursive( const QList<QgsLayerTreeNode *> &nodes )
{
QSet<QgsMapLayer *> layersSet;
_collectMapLayers( nodes, layersSet );
return layersSet;
}
@@ -87,6 +87,13 @@ class CORE_EXPORT QgsLayerTreeUtils
* \returns the new tree layer
*/
static QgsLayerTreeLayer *insertLayerBelow( QgsLayerTreeGroup *group, const QgsMapLayer *refLayer, QgsMapLayer *layerToInsert );

/**
* Returns map layers from the given list of layer tree nodes. Also recursively visits
* child nodes of groups.
* \since QGIS 3.4
*/
static QSet<QgsMapLayer *> collectMapLayersRecursive( const QList<QgsLayerTreeNode *> &nodes );
};

#endif // QGSLAYERTREEUTILS_H
@@ -19,6 +19,7 @@
#include "qgslayertreeembeddedwidgetregistry.h"
#include "qgslayertreemodel.h"
#include "qgslayertreemodellegendnode.h"
#include "qgslayertreeutils.h"
#include "qgslayertreeviewdefaultactions.h"
#include "qgsmaplayer.h"
#include "qgsgui.h"
@@ -356,28 +357,10 @@ QList<QgsMapLayer *> QgsLayerTreeView::selectedLayers() const
return list;
}

static void _collectMapLayers( const QList<QgsLayerTreeNode *> &nodes, QSet<QgsMapLayer *> &layersSet )
{
for ( QgsLayerTreeNode *node : nodes )
{
if ( QgsLayerTree::isLayer( node ) )
{
QgsLayerTreeLayer *nodeLayer = QgsLayerTree::toLayer( node );
if ( nodeLayer->layer() )
layersSet << nodeLayer->layer();
}
else if ( QgsLayerTree::isGroup( node ) )
{
_collectMapLayers( QgsLayerTree::toGroup( node )->children(), layersSet );
}
}
}

QList<QgsMapLayer *> QgsLayerTreeView::selectedLayersRecursive() const
{
QSet<QgsMapLayer *> layersSet;
const QList<QgsLayerTreeNode *> nodes = layerTreeModel()->indexes2nodes( selectionModel()->selectedIndexes(), false );
_collectMapLayers( nodes, layersSet );
QSet<QgsMapLayer *> layersSet = QgsLayerTreeUtils::collectMapLayersRecursive( nodes );
return layersSet.toList();
}

@@ -26,6 +26,7 @@
#include <qgsrulebasedrenderer.h>
#include <qgslayertreemodel.h>
#include <qgslayertreemodellegendnode.h>
#include <qgslayertreeutils.h>

class TestQgsLayerTree : public QObject
{
@@ -50,6 +51,7 @@ class TestQgsLayerTree : public QObject
void testFindLayer();
void testLayerDeleted();
void testFindGroups();
void testUtilsCollectMapLayers();

private:

@@ -661,6 +663,30 @@ void TestQgsLayerTree::testFindGroups()
QVERIFY( groups.contains( group3 ) );
}

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

QgsProject project;
project.addMapLayer( vl1 );
project.addMapLayer( vl2 );

QgsLayerTree root;
QgsLayerTreeLayer *nodeVl1 = root.addLayer( vl1 );
QgsLayerTreeGroup *nodeGrp = root.addGroup( "grp" );
QgsLayerTreeLayer *nodeVl2 = nodeGrp->addLayer( vl2 );
Q_UNUSED( nodeVl2 );

QSet<QgsMapLayer *> set1 = QgsLayerTreeUtils::collectMapLayersRecursive( QList<QgsLayerTreeNode *>() << &root );
QSet<QgsMapLayer *> set2 = QgsLayerTreeUtils::collectMapLayersRecursive( QList<QgsLayerTreeNode *>() << nodeVl1 );
QSet<QgsMapLayer *> set3 = QgsLayerTreeUtils::collectMapLayersRecursive( QList<QgsLayerTreeNode *>() << nodeGrp );

QCOMPARE( set1, QSet<QgsMapLayer *>() << vl1 << vl2 );
QCOMPARE( set2, QSet<QgsMapLayer *>() << vl1 );
QCOMPARE( set3, QSet<QgsMapLayer *>() << vl2 );
}


QGSTEST_MAIN( TestQgsLayerTree )
#include "testqgslayertree.moc"

0 comments on commit 5047571

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