Skip to content
Permalink
Browse files
[api] Allow annotation layers to be included in filtered map layer pr…
…oxy models
  • Loading branch information
nyalldawson committed Sep 1, 2021
1 parent 26b536b commit 21699794bdaef8166a617371b63dd2e3f1062fe2
@@ -36,6 +36,7 @@ The :py:class:`QgsMapLayerProxyModel` class provides an easy to use model to dis
MeshLayer,
VectorTileLayer,
PointCloudLayer,
AnnotationLayer,
All
};
typedef QFlags<QgsMapLayerProxyModel::Filter> Filters;
@@ -107,7 +107,8 @@ QgsLayerStylingWidget::QgsLayerStylingWidget( QgsMapCanvas *canvas, QgsMessageBa
| QgsMapLayerProxyModel::Filter::PluginLayer
| QgsMapLayerProxyModel::Filter::MeshLayer
| QgsMapLayerProxyModel::Filter::VectorTileLayer
| QgsMapLayerProxyModel::Filter::PointCloudLayer );
| QgsMapLayerProxyModel::Filter::PointCloudLayer
| QgsMapLayerProxyModel::Filter::AnnotationLayer );

mStackedWidget->setCurrentIndex( 0 );
}
@@ -54,6 +54,7 @@ bool QgsMapLayerProxyModel::layerMatchesFilters( const QgsMapLayer *layer, const
( filters.testFlag( MeshLayer ) && layer->type() == QgsMapLayerType::MeshLayer ) ||
( filters.testFlag( VectorTileLayer ) && layer->type() == QgsMapLayerType::VectorTileLayer ) ||
( filters.testFlag( PointCloudLayer ) && layer->type() == QgsMapLayerType::PointCloudLayer ) ||
( filters.testFlag( AnnotationLayer ) && layer->type() == QgsMapLayerType::AnnotationLayer ) ||
( filters.testFlag( PluginLayer ) && layer->type() == QgsMapLayerType::PluginLayer ) )
return true;

@@ -53,7 +53,8 @@ class CORE_EXPORT QgsMapLayerProxyModel : public QSortFilterProxyModel
MeshLayer = 128, //!< QgsMeshLayer \since QGIS 3.6
VectorTileLayer = 256, //!< QgsVectorTileLayer \since QGIS 3.14
PointCloudLayer = 512, //!< QgsPointCloudLayer \since QGIS 3.18
All = RasterLayer | VectorLayer | PluginLayer | MeshLayer | VectorTileLayer | PointCloudLayer
AnnotationLayer = 1024, //!< QgsAnnotationLayer \since QGIS 3.22
All = RasterLayer | VectorLayer | PluginLayer | MeshLayer | VectorTileLayer | PointCloudLayer | AnnotationLayer
};
Q_DECLARE_FLAGS( Filters, Filter )
Q_FLAG( Filters )
@@ -12,7 +12,14 @@

import qgis # NOQA

from qgis.core import QgsVectorLayer, QgsMeshLayer, QgsProject, QgsMapLayerModel, QgsMapLayerProxyModel
from qgis.core import (
QgsVectorLayer,
QgsMeshLayer,
QgsAnnotationLayer,
QgsProject,
QgsMapLayerModel,
QgsMapLayerProxyModel
)
from qgis.PyQt.QtCore import Qt, QModelIndex

from qgis.testing import start_app, unittest
@@ -75,6 +82,43 @@ def testMeshLayer(self):
self.assertTrue(m.acceptsLayer(l1))
self.assertFalse(m.acceptsLayer(l2))

def testAnnotationLayer(self):
"""
Test filtering annotation layers
"""
QgsProject.instance().clear()

m = QgsMapLayerProxyModel()
options = QgsAnnotationLayer.LayerOptions(QgsProject.instance().transformContext())
l1 = QgsAnnotationLayer('annotation 1', options)
QgsProject.instance().addMapLayer(l1)
l2 = create_layer('l2')
QgsProject.instance().addMapLayer(l2)

m.setFilters(QgsMapLayerProxyModel.AnnotationLayer)
self.assertEqual(m.filters(), QgsMapLayerProxyModel.AnnotationLayer)

self.assertEqual(m.rowCount(), 1)
self.assertEqual(m.data(m.index(0, 0)), 'annotation 1')

self.assertTrue(m.acceptsLayer(l1))
self.assertFalse(m.acceptsLayer(l2))

m.setFilters(QgsMapLayerProxyModel.VectorLayer)
self.assertEqual(m.rowCount(), 1)
self.assertEqual(m.data(m.index(0, 0)), 'l2')

self.assertFalse(m.acceptsLayer(l1))
self.assertTrue(m.acceptsLayer(l2))

m.setFilters(QgsMapLayerProxyModel.All)
self.assertEqual(m.rowCount(), 2)
self.assertEqual(m.data(m.index(0, 0)), 'annotation 1')
self.assertEqual(m.data(m.index(1, 0)), 'l2')

self.assertTrue(m.acceptsLayer(l1))
self.assertTrue(m.acceptsLayer(l2))

def testFilterGeometryType(self):
""" test filtering by geometry type """
QgsProject.instance().clear()

0 comments on commit 2169979

Please sign in to comment.