Skip to content

Commit

Permalink
Add method to retrieve a specific item from an annotation layer by id
Browse files Browse the repository at this point in the history
  • Loading branch information
nyalldawson committed Aug 19, 2021
1 parent 0cb6ff5 commit e3b10ff
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 0 deletions.
Expand Up @@ -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;
Expand Down
5 changes: 5 additions & 0 deletions src/core/annotations/qgsannotationlayer.cpp
Expand Up @@ -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
Expand Down
7 changes: 7 additions & 0 deletions src/core/annotations/qgsannotationlayer.h
Expand Up @@ -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;
Expand Down
8 changes: 8 additions & 0 deletions tests/src/python/test_qgsannotationlayer.py
Expand Up @@ -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)])))
Expand All @@ -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))
Expand Down

0 comments on commit e3b10ff

Please sign in to comment.