Skip to content
Permalink
Browse files

Add api to determine if a clip region applies to a particular layer

  • Loading branch information
nyalldawson committed Jul 2, 2020
1 parent e38bb54 commit b5ae07869e193f45b4dd0e182442e468a5d0e825
@@ -79,6 +79,11 @@ Returns the list of layers to restrict the clipping region effects to.
If the list is empty then the clipping will be applied to all layers.

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

bool appliesToLayer( const QgsMapLayer *layer ) const;
%Docstring
Returns ``True`` if the clipping region should be applied to the specified map ``layer``.
%End

};
@@ -15,6 +15,7 @@

#include "qgsmapclippingregion.h"
#include "qgsmaplayerlistutils.h"
#include <algorithm>

QgsGeometry QgsMapClippingRegion::geometry() const
{
@@ -36,4 +37,16 @@ QList<QgsMapLayer *> QgsMapClippingRegion::restrictedLayers() const
return _qgis_listQPointerToRaw( mRestrictToLayersList );
}

bool QgsMapClippingRegion::appliesToLayer( const QgsMapLayer *layer ) const
{
if ( mRestrictToLayersList.empty() )
return true;

auto it = std::find_if( mRestrictToLayersList.begin(), mRestrictToLayersList.end(), [layer]( const QgsWeakMapLayerPointer & item ) -> bool
{
return item == layer;
} );
return it != mRestrictToLayersList.end();
}


@@ -102,6 +102,11 @@ class CORE_EXPORT QgsMapClippingRegion
*/
QList< QgsMapLayer * > restrictedLayers() const;

/**
* Returns TRUE if the clipping region should be applied to the specified map \a layer.
*/
bool appliesToLayer( const QgsMapLayer *layer ) const;

private:

//! Geometry of clipping region (in destination map coordinates and CRS)
@@ -10,13 +10,13 @@
__date__ = '2020-06'
__copyright__ = 'Copyright 2020, The QGIS Project'


import qgis # NOQA

from qgis.testing import unittest
from qgis.core import (
QgsMapClippingRegion,
QgsGeometry
QgsGeometry,
QgsVectorLayer
)


@@ -33,6 +33,33 @@ def testGetSet(self):
region.setFeatureClip(QgsMapClippingRegion.FeatureClippingType.PainterClip)
self.assertEqual(region.featureClip(), QgsMapClippingRegion.FeatureClippingType.PainterClip)

layer = QgsVectorLayer("Point?field=fldtxt:string&field=fldint:integer",
"addfeat", "memory")
layer2 = QgsVectorLayer("Point?field=fldtxt:string&field=fldint:integer",
"addfeat", "memory")
self.assertEqual(len(region.restrictedLayers()), 0)
region.setRestrictedLayers([layer, layer2])
self.assertCountEqual(region.restrictedLayers(), [layer, layer2])

def testAppliesToLayer(self):
layer = QgsVectorLayer("Point?field=fldtxt:string&field=fldint:integer",
"addfeat", "memory")
layer2 = QgsVectorLayer("Point?field=fldtxt:string&field=fldint:integer",
"addfeat", "memory")
layer3 = QgsVectorLayer("Point?field=fldtxt:string&field=fldint:integer",
"addfeat", "memory")

region = QgsMapClippingRegion(QgsGeometry.fromWkt('Polygon((0 0, 1 0, 1 1, 0 1, 0 0))'))
# should apply to all layers by default
self.assertTrue(region.appliesToLayer(layer))
self.assertTrue(region.appliesToLayer(layer2))
self.assertTrue(region.appliesToLayer(layer3))

region.setRestrictedLayers([layer, layer2])
self.assertTrue(region.appliesToLayer(layer))
self.assertTrue(region.appliesToLayer(layer2))
self.assertFalse(region.appliesToLayer(layer3))


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

0 comments on commit b5ae078

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