Skip to content

Commit 2bd90da

Browse files
committedNov 5, 2018
Added a validOnly flag to mapLayers to filter for valid layers only
1 parent 455b660 commit 2bd90da

File tree

4 files changed

+12
-35
lines changed

4 files changed

+12
-35
lines changed
 

‎python/core/auto_generated/qgsproject.sip.in

Lines changed: 3 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -733,28 +733,17 @@ Retrieve a list of matching registered layers by layer name.
733733
.. seealso:: :py:func:`mapLayers`
734734
%End
735735

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

740-
.. seealso:: :py:func:`mapLayer`
741-
742-
.. seealso:: :py:func:`mapLayersByName`
743-
744-
.. seealso:: :py:func:`layers`
745-
%End
746-
747-
QMap<QString, QgsMapLayer *> validMapLayers() const;
748-
%Docstring
749-
Returns a map of all registered valid layers by layer ID.
740+
:param validOnly: if set only valid layers will be returned
750741

751742
.. seealso:: :py:func:`mapLayer`
752743

753744
.. seealso:: :py:func:`mapLayersByName`
754745

755746
.. seealso:: :py:func:`layers`
756-
757-
.. versionadded:: 3.6
758747
%End
759748

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

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

787776

‎src/core/qgsproject.cpp

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2745,14 +2745,9 @@ void QgsProject::reloadAllLayers()
27452745
}
27462746
}
27472747

2748-
QMap<QString, QgsMapLayer *> QgsProject::mapLayers() const
2748+
QMap<QString, QgsMapLayer *> QgsProject::mapLayers( const bool validOnly ) const
27492749
{
2750-
return mLayerStore->mapLayers();
2751-
}
2752-
2753-
QMap<QString, QgsMapLayer *> QgsProject::validMapLayers() const
2754-
{
2755-
return mLayerStore->validMapLayers();
2750+
return validOnly ? mLayerStore->validMapLayers() : mLayerStore->mapLayers();
27562751
}
27572752

27582753
QgsTransactionGroup *QgsProject::transactionGroup( const QString &providerKey, const QString &connString )

‎src/core/qgsproject.h

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -713,20 +713,13 @@ class CORE_EXPORT QgsProject : public QObject, public QgsExpressionContextGenera
713713

714714
/**
715715
* Returns a map of all registered layers by layer ID.
716+
*
717+
* \param validOnly if set only valid layers will be returned
716718
* \see mapLayer()
717719
* \see mapLayersByName()
718720
* \see layers()
719721
*/
720-
QMap<QString, QgsMapLayer *> mapLayers() const;
721-
722-
/**
723-
* Returns a map of all registered valid layers by layer ID.
724-
* \see mapLayer()
725-
* \see mapLayersByName()
726-
* \see layers()
727-
* \since QGIS 3.6
728-
*/
729-
QMap<QString, QgsMapLayer *> validMapLayers() const;
722+
QMap<QString, QgsMapLayer *> mapLayers( const bool validOnly = false ) const;
730723

731724
/**
732725
* Returns true if the project comes from a zip archive, false otherwise.
@@ -769,7 +762,7 @@ class CORE_EXPORT QgsProject : public QObject, public QgsExpressionContextGenera
769762
* the layers yourself. Not available in Python.
770763
*
771764
* \returns a list of the map layers that were added
772-
* successfully. If a layer is invalid, or already exists in the registry,
765+
* successfully. If a layer or already exists in the registry,
773766
* it will not be part of the returned QList.
774767
*
775768
* \note As a side-effect QgsProject is made dirty.

‎tests/src/python/test_qgsproject.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -254,12 +254,12 @@ def test_addMapLayerInvalid(self):
254254

255255
vl = QgsVectorLayer("Point?field=x:string", 'test', "xxx")
256256
self.assertEqual(QgsProject.instance().addMapLayer(vl), vl)
257-
self.assertFalse(vl in QgsProject.instance().validMapLayers().values())
257+
self.assertFalse(vl in QgsProject.instance().mapLayers(True).values())
258258
self.assertEqual(len(QgsProject.instance().mapLayersByName('test')), 1)
259259
self.assertEqual(QgsProject.instance().count(), 1)
260260
self.assertEqual(QgsProject.instance().validCount(), 0)
261261

262-
self.assertEqual(len(QgsProject.instance().validMapLayers()), 0)
262+
self.assertEqual(len(QgsProject.instance().mapLayers(True)), 0)
263263

264264
QgsProject.instance().removeAllMapLayers()
265265

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

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

0 commit comments

Comments
 (0)
Please sign in to comment.