Skip to content
Permalink
Browse files

Added a validOnly flag to mapLayers to filter for valid layers only

  • Loading branch information
elpaso committed Nov 1, 2018
1 parent 455b660 commit 2bd90da9c1e17ca05293b63a26ed9bd4ea2db829
@@ -733,28 +733,17 @@ Retrieve a list of matching registered layers by layer name.
.. seealso:: :py:func:`mapLayers`
%End

QMap<QString, QgsMapLayer *> mapLayers() const;
QMap<QString, QgsMapLayer *> mapLayers( const bool validOnly = false ) const;
%Docstring
Returns a map of all registered layers by layer ID.

.. seealso:: :py:func:`mapLayer`

.. seealso:: :py:func:`mapLayersByName`

.. seealso:: :py:func:`layers`
%End

QMap<QString, QgsMapLayer *> validMapLayers() const;
%Docstring
Returns a map of all registered valid layers by layer ID.
:param validOnly: if set only valid layers will be returned

.. seealso:: :py:func:`mapLayer`

.. seealso:: :py:func:`mapLayersByName`

.. seealso:: :py:func:`layers`

.. versionadded:: 3.6
%End

bool isZipped() const;
@@ -781,7 +770,7 @@ The legendLayersAdded() signal is emitted only if addToLegend is true.
the layers yourself. Not available in Python.

:return: a list of the map layers that were added
successfully. If a layer is invalid, or already exists in the registry,
successfully. If a layer or already exists in the registry,
it will not be part of the returned QList.


@@ -2745,14 +2745,9 @@ void QgsProject::reloadAllLayers()
}
}

QMap<QString, QgsMapLayer *> QgsProject::mapLayers() const
QMap<QString, QgsMapLayer *> QgsProject::mapLayers( const bool validOnly ) const
{
return mLayerStore->mapLayers();
}

QMap<QString, QgsMapLayer *> QgsProject::validMapLayers() const
{
return mLayerStore->validMapLayers();
return validOnly ? mLayerStore->validMapLayers() : mLayerStore->mapLayers();
}

QgsTransactionGroup *QgsProject::transactionGroup( const QString &providerKey, const QString &connString )
@@ -713,20 +713,13 @@ class CORE_EXPORT QgsProject : public QObject, public QgsExpressionContextGenera

/**
* Returns a map of all registered layers by layer ID.
*
* \param validOnly if set only valid layers will be returned
* \see mapLayer()
* \see mapLayersByName()
* \see layers()
*/
QMap<QString, QgsMapLayer *> mapLayers() const;

/**
* Returns a map of all registered valid layers by layer ID.
* \see mapLayer()
* \see mapLayersByName()
* \see layers()
* \since QGIS 3.6
*/
QMap<QString, QgsMapLayer *> validMapLayers() const;
QMap<QString, QgsMapLayer *> mapLayers( const bool validOnly = false ) const;

/**
* Returns true if the project comes from a zip archive, false otherwise.
@@ -769,7 +762,7 @@ class CORE_EXPORT QgsProject : public QObject, public QgsExpressionContextGenera
* the layers yourself. Not available in Python.
*
* \returns a list of the map layers that were added
* successfully. If a layer is invalid, or already exists in the registry,
* successfully. If a layer or already exists in the registry,
* it will not be part of the returned QList.
*
* \note As a side-effect QgsProject is made dirty.
@@ -254,12 +254,12 @@ def test_addMapLayerInvalid(self):

vl = QgsVectorLayer("Point?field=x:string", 'test', "xxx")
self.assertEqual(QgsProject.instance().addMapLayer(vl), vl)
self.assertFalse(vl in QgsProject.instance().validMapLayers().values())
self.assertFalse(vl in QgsProject.instance().mapLayers(True).values())
self.assertEqual(len(QgsProject.instance().mapLayersByName('test')), 1)
self.assertEqual(QgsProject.instance().count(), 1)
self.assertEqual(QgsProject.instance().validCount(), 0)

self.assertEqual(len(QgsProject.instance().validMapLayers()), 0)
self.assertEqual(len(QgsProject.instance().mapLayers(True)), 0)

QgsProject.instance().removeAllMapLayers()

@@ -323,7 +323,7 @@ def test_addMapLayersInvalid(self):

vl = QgsVectorLayer("Point?field=x:string", 'test', "xxx")
self.assertEqual(QgsProject.instance().addMapLayers([vl]), [vl])
self.assertFalse(vl in QgsProject.instance().validMapLayers().values())
self.assertFalse(vl in QgsProject.instance().mapLayers(True).values())
self.assertEqual(len(QgsProject.instance().mapLayersByName('test')), 1)
self.assertEqual(QgsProject.instance().count(), 1)
self.assertEqual(QgsProject.instance().validCount(), 0)

0 comments on commit 2bd90da

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