Skip to content
Permalink
Browse files

Add method to QgsMapThemeCollection to retrieve ordered list of

visible layers for the project
  • Loading branch information
nyalldawson committed Mar 13, 2017
1 parent a8816e5 commit 452abb2484323d2d11e680f96f2e0f1f7ab0630b
@@ -117,6 +117,7 @@ class QgsMapThemeCollection : QObject
QgsProject *project();
void setProject( QgsProject *project );
QList< QgsMapLayer * > masterLayerOrder() const;
QList< QgsMapLayer * > masterVisibleLayers() const;

signals:

@@ -183,6 +183,21 @@ QList<QgsMapLayer *> QgsMapThemeCollection::masterLayerOrder() const
return mProject->layerOrder();
}

QList<QgsMapLayer *> QgsMapThemeCollection::masterVisibleLayers() const
{
QList< QgsMapLayer * > visible;
Q_FOREACH ( QgsMapLayer *layer, masterLayerOrder() )
{
QgsLayerTreeLayer *nodeLayer = mProject->layerTreeRoot()->findLayer( layer );
if ( nodeLayer )
{
if ( nodeLayer->isVisible() )
visible << layer;
}
}
return visible;
}


bool QgsMapThemeCollection::hasMapTheme( const QString &name ) const
{
@@ -246,9 +246,19 @@ class CORE_EXPORT QgsMapThemeCollection : public QObject
* Returns the master layer order (this will always match the project's QgsProject::layerOrder() ).
* All map themes will maintain the same layer order as the master layer order.
* @note added in QGIS 3.0
* @see masterVisibleLayers()
*/
QList< QgsMapLayer * > masterLayerOrder() const;

/**
* Returns the master list of visible layers. The order of returned layers will always match those
* of masterLayerOrder(), but the returned layers are filtered to only include those visible
* in the project's layer tree.
* @note added in QGIS 3.0
* @see masterLayerOrder()
*/
QList< QgsMapLayer * > masterVisibleLayers() const;

signals:

/**
@@ -102,6 +102,7 @@ def testThemeChanged(self):
def testMasterLayerOrder(self):
""" test master layer order"""
prj = QgsProject.instance()
prj.clear()
layer = QgsVectorLayer("Point?field=fldtxt:string",
"layer1", "memory")
layer2 = QgsVectorLayer("Point?field=fldtxt:string",
@@ -174,6 +175,35 @@ def testMasterLayerOrder(self):
self.assertEqual(prj.mapThemeCollection().mapThemeVisibleLayers('theme2'), [layer3, layer2, layer])
self.assertEqual(prj.mapThemeCollection().mapThemeVisibleLayers('theme3'), [layer2, layer])

def testMasterVisibleLayers(self):
""" test master visible layers"""
prj = QgsProject.instance()
prj.clear()
layer = QgsVectorLayer("Point?field=fldtxt:string",
"layer1", "memory")
layer2 = QgsVectorLayer("Point?field=fldtxt:string",
"layer2", "memory")
layer3 = QgsVectorLayer("Point?field=fldtxt:string",
"layer3", "memory")
prj.addMapLayers([layer, layer2, layer3])

# general setup...
prj.setLayerOrder([layer2, layer])
self.assertEqual(prj.mapThemeCollection().masterVisibleLayers(), [layer2, layer])
prj.setLayerOrder([layer3, layer, layer2])
self.assertEqual(prj.mapThemeCollection().masterVisibleLayers(), [layer3, layer, layer2])

#hide some layers
root = prj.layerTreeRoot()
layer_node = root.findLayer(layer2)
layer_node.setItemVisibilityChecked(False)
self.assertEqual(prj.mapThemeCollection().masterVisibleLayers(), [layer3, layer])
layer_node.setItemVisibilityChecked(True)
self.assertEqual(prj.mapThemeCollection().masterVisibleLayers(), [layer3, layer, layer2])
layer_node.setItemVisibilityChecked(False)
prj.setLayerOrder([layer, layer2, layer3])
self.assertEqual(prj.mapThemeCollection().masterVisibleLayers(), [layer, layer3])


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

0 comments on commit 452abb2

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