Skip to content
Permalink
Browse files
Add method to retrieve a specific item from an annotation layer by id
  • Loading branch information
nyalldawson committed Aug 19, 2021
1 parent 0cb6ff5 commit e3b10ff5b15b452c83f7652cad5f5baffdb5ddd2
@@ -91,6 +91,13 @@ Returns a map of items contained in the layer, by unique item ID.

This map contains references to items owned by the layer, and ownership of these remains
with the layer.
%End

QgsAnnotationItem *item( const QString &id );
%Docstring
Returns the item with the specified ``id``, or ``None`` if no matching item was found.

.. versionadded:: 3.22
%End

virtual Qgis::MapLayerProperties properties() const;
@@ -81,6 +81,11 @@ bool QgsAnnotationLayer::isEmpty() const
return mItems.empty();
}

QgsAnnotationItem *QgsAnnotationLayer::item( const QString &id )
{
return mItems.value( id );
}

Qgis::MapLayerProperties QgsAnnotationLayer::properties() const
{
// annotation layers are always editable
@@ -117,6 +117,13 @@ class CORE_EXPORT QgsAnnotationLayer : public QgsMapLayer
*/
QMap<QString, QgsAnnotationItem *> items() const { return mItems; }

/**
* Returns the item with the specified \a id, or NULLPTR if no matching item was found.
*
* \since QGIS 3.22
*/
QgsAnnotationItem *item( const QString &id );

Qgis::MapLayerProperties properties() const override;
QgsAnnotationLayer *clone() const override SIP_FACTORY;
QgsMapLayerRenderer *createMapRenderer( QgsRenderContext &rendererContext ) override SIP_FACTORY;
@@ -65,6 +65,8 @@ def testItems(self):
self.assertTrue(layer.isValid())

self.assertTrue(layer.isEmpty())
self.assertIsNone(layer.item('xxxx'))
self.assertIsNone(layer.item(''))

polygon_item_id = layer.addItem(QgsAnnotationPolygonItem(QgsPolygon(QgsLineString([QgsPoint(12, 13), QgsPoint(14, 13), QgsPoint(14, 15), QgsPoint(12, 13)]))))
linestring_item_id = layer.addItem(QgsAnnotationLineItem(QgsLineString([QgsPoint(11, 13), QgsPoint(12, 13), QgsPoint(12, 15)])))
@@ -77,6 +79,12 @@ def testItems(self):
self.assertIsInstance(layer.items()[linestring_item_id], QgsAnnotationLineItem)
self.assertIsInstance(layer.items()[marker_item_id], QgsAnnotationMarkerItem)

self.assertIsInstance(layer.item(polygon_item_id), QgsAnnotationPolygonItem)
self.assertIsInstance(layer.item(linestring_item_id), QgsAnnotationLineItem)
self.assertIsInstance(layer.item(marker_item_id), QgsAnnotationMarkerItem)
self.assertIsNone(layer.item('xxxx'))
self.assertIsNone(layer.item(''))

self.assertFalse(layer.removeItem('xxxx'))
self.assertEqual(len(layer.items()), 3)
self.assertTrue(layer.removeItem(linestring_item_id))

0 comments on commit e3b10ff

Please sign in to comment.