Skip to content
Permalink
Browse files
Add method to determine if a layer from a project is contained in
any group layers
  • Loading branch information
nyalldawson committed Nov 25, 2021
1 parent 7d4d251 commit ce408fc7295a24bb86e3511725b9e22ccc784b33
Showing with 55 additions and 1 deletion.
  1. +16 −0 src/core/project/qgsprojectutils.cpp
  2. +8 −0 src/core/project/qgsprojectutils.h
  3. +31 −1 tests/src/python/test_qgsprojectutils.py
@@ -18,6 +18,7 @@
#include "qgsprojectutils.h"
#include "qgsmaplayerutils.h"
#include "qgsproject.h"
#include "qgsgrouplayer.h"

QList<QgsMapLayer *> QgsProjectUtils::layersMatchingPath( const QgsProject *project, const QString &path )
{
@@ -52,3 +53,18 @@ bool QgsProjectUtils::updateLayerPath( QgsProject *project, const QString &oldPa
}
return res;
}

bool QgsProjectUtils::layerIsContainedInGroupLayer( QgsProject *project, QgsMapLayer *layer )
{
const QMap<QString, QgsMapLayer *> mapLayers( project->mapLayers() );
for ( QgsMapLayer *l : mapLayers )
{
if ( l->type() != QgsMapLayerType::GroupLayer )
continue;

QgsGroupLayer *groupLayer = qobject_cast< QgsGroupLayer * >( l );
if ( groupLayer->childLayers().contains( layer ) )
return true;
}
return false;
}
@@ -51,6 +51,14 @@ class CORE_EXPORT QgsProjectUtils
*/
static bool updateLayerPath( QgsProject *project, const QString &oldPath, const QString &newPath );

/**
* Returns TRUE if the specified \a layer is a child layer from any QgsGroupLayer in the given \a project.
*
* \since QGIS 3.24
*/
static bool layerIsContainedInGroupLayer( QgsProject *project, QgsMapLayer *layer );


};

#endif // QGSPROJECTUTILS_H
@@ -16,7 +16,7 @@
from qgis.testing import unittest
from qgis.core import (
QgsProjectUtils,
QgsCoordinateReferenceSystem,
QgsGroupLayer,
QgsCoordinateTransformContext,
QgsVectorLayer,
QgsRasterLayer,
@@ -119,6 +119,36 @@ def test_updateLayerPath(self):
self.assertFalse(QgsProjectUtils.updateLayerPath(p, unitTestDataPath() + '/mixed_layers.gpkg',
unitTestDataPath() + '/mixed_layers22.gpkg'))

def test_layer_is_contained_in_group_layer(self):
p = QgsProject()
layer = QgsVectorLayer("Point?field=fldtxt:string",
"layer1", "memory")
p.addMapLayer(layer)
layer2 = QgsVectorLayer("Point?field=fldtxt:string",
"layer2", "memory")
p.addMapLayer(layer2)
layer3 = QgsVectorLayer("Point?field=fldtxt:string",
"layer3", "memory")
p.addMapLayer(layer3)
layer4 = QgsVectorLayer("Point?field=fldtxt:string",
"layer4", "memory")
p.addMapLayer(layer4)

options = QgsGroupLayer.LayerOptions(QgsCoordinateTransformContext())
group_layer = QgsGroupLayer('group', options)
group_layer.setChildLayers([layer, layer4])
p.addMapLayer(group_layer)

options = QgsGroupLayer.LayerOptions(QgsCoordinateTransformContext())
group_layer2 = QgsGroupLayer('group2', options)
group_layer2.setChildLayers([group_layer, layer3])
p.addMapLayer(group_layer2)

self.assertTrue(QgsProjectUtils.layerIsContainedInGroupLayer(p, layer))
self.assertFalse(QgsProjectUtils.layerIsContainedInGroupLayer(p, layer2))
self.assertTrue(QgsProjectUtils.layerIsContainedInGroupLayer(p, layer3))
self.assertTrue(QgsProjectUtils.layerIsContainedInGroupLayer(p, layer4))


if __name__ == '__main__':
unittest.main()

0 comments on commit ce408fc

Please sign in to comment.