Skip to content

Commit

Permalink
Add method to determine whether an annotation layer is empty
Browse files Browse the repository at this point in the history
  • Loading branch information
nyalldawson committed Sep 1, 2020
1 parent 636653d commit b9cd453
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 0 deletions.
Expand Up @@ -72,6 +72,11 @@ Removes (and deletes) the item with matching ``id``.
void clear();
%Docstring
Removes all items from the layer.
%End

bool isEmpty() const;
%Docstring
Returns ``True`` if the annotation layer is empty and contains no annotations.
%End

QMap<QString, QgsAnnotationItem *> items() const;
Expand Down
5 changes: 5 additions & 0 deletions src/core/annotations/qgsannotationlayer.cpp
Expand Up @@ -73,6 +73,11 @@ void QgsAnnotationLayer::clear()
triggerRepaint();
}

bool QgsAnnotationLayer::isEmpty() const
{
return mItems.empty();
}

void QgsAnnotationLayer::setOpacity( double opacity )
{
mOpacity = opacity;
Expand Down
5 changes: 5 additions & 0 deletions src/core/annotations/qgsannotationlayer.h
Expand Up @@ -96,6 +96,11 @@ class CORE_EXPORT QgsAnnotationLayer : public QgsMapLayer
*/
void clear();

/**
* Returns TRUE if the annotation layer is empty and contains no annotations.
*/
bool isEmpty() const;

/**
* Returns a map of items contained in the layer, by unique item ID.
*
Expand Down
4 changes: 4 additions & 0 deletions tests/src/python/test_qgsannotationlayer.py
Expand Up @@ -62,11 +62,14 @@ def testItems(self):
layer = QgsAnnotationLayer('test', QgsAnnotationLayer.LayerOptions(QgsProject.instance().transformContext()))
self.assertTrue(layer.isValid())

self.assertTrue(layer.isEmpty())

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)])))
marker_item_id = layer.addItem(QgsAnnotationMarkerItem(QgsPoint(12, 13)))

self.assertEqual(len(layer.items()), 3)
self.assertFalse(layer.isEmpty())

self.assertIsInstance(layer.items()[polygon_item_id], QgsAnnotationPolygonItem)
self.assertIsInstance(layer.items()[linestring_item_id], QgsAnnotationLineItem)
Expand All @@ -86,6 +89,7 @@ def testItems(self):

self.assertTrue(layer.removeItem(marker_item_id))
self.assertEqual(len(layer.items()), 0)
self.assertTrue(layer.isEmpty())

layer.addItem(QgsAnnotationPolygonItem(QgsPolygon(QgsLineString([QgsPoint(12, 13), QgsPoint(14, 13), QgsPoint(14, 15), QgsPoint(12, 13)]))))
layer.addItem(QgsAnnotationLineItem(QgsLineString([QgsPoint(11, 13), QgsPoint(12, 13), QgsPoint(12, 15)])))
Expand Down

0 comments on commit b9cd453

Please sign in to comment.