Skip to content

Commit

Permalink
[api] Allow annotation layers to be included in filtered map layer pr…
Browse files Browse the repository at this point in the history
…oxy models
  • Loading branch information
nyalldawson committed Sep 1, 2021
1 parent 26b536b commit 2169979
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 3 deletions.
1 change: 1 addition & 0 deletions python/core/auto_generated/qgsmaplayerproxymodel.sip.in
Expand Up @@ -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;
Expand Down
3 changes: 2 additions & 1 deletion src/app/qgslayerstylingwidget.cpp
Expand Up @@ -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 );
}
Expand Down
1 change: 1 addition & 0 deletions src/core/qgsmaplayerproxymodel.cpp
Expand Up @@ -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;

Expand Down
3 changes: 2 additions & 1 deletion src/core/qgsmaplayerproxymodel.h
Expand Up @@ -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 )
Expand Down
46 changes: 45 additions & 1 deletion tests/src/python/test_qgsmaplayerproxymodel.py
Expand Up @@ -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
Expand Down Expand Up @@ -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()
Expand Down

0 comments on commit 2169979

Please sign in to comment.