Skip to content
Permalink
Browse files
Add method to erase results from specific layers to
QgsRenderedItemResults
  • Loading branch information
nyalldawson committed Sep 7, 2021
1 parent d64b7c6 commit ae17baa7374687e661220458558fe22e9cc9885d
@@ -72,6 +72,11 @@ Items are removed from ``other`` and transferred to this object.
After calling this method the ``other`` results will be left in an undefined state.
%End

void eraseResultsFromLayers( const QStringList &layerIds );
%Docstring
Erases results from layers matching those in the specified list of layers IDs.
%End

private:
QgsRenderedItemResults( const QgsRenderedItemResults & );
};
@@ -157,4 +157,19 @@ void QgsRenderedItemResults::transferResults( QgsRenderedItemResults *other )
other->mDetails.clear();
}

void QgsRenderedItemResults::eraseResultsFromLayers( const QStringList &layerIds )
{
for ( auto it = mDetails.begin(); it != mDetails.end(); )
{
if ( layerIds.contains( ( *it )->layerId() ) )
{
mDetails.erase( it );
}
else
{
it++;
}
}
}


@@ -90,6 +90,11 @@ class CORE_EXPORT QgsRenderedItemResults
*/
void transferResults( QgsRenderedItemResults *other );

/**
* Erases results from layers matching those in the specified list of layers IDs.
*/
void eraseResultsFromLayers( const QStringList &layerIds );

private:
#ifdef SIP_RUN
QgsRenderedItemResults( const QgsRenderedItemResults & );
@@ -142,6 +142,38 @@ def test_transfer_results(self):
('layer_id2', 'item_id_2'),
('layer_id3', 'item_id_3')])

def test_erase_results(self):
results = QgsRenderedItemResults()

rc = QgsRenderContext()
details1 = QgsRenderedAnnotationItemDetails('layer_id', 'item_id_1')
details1.setBoundingBox(QgsRectangle(1, 1, 10, 10))

details2 = QgsRenderedAnnotationItemDetails('layer_id2', 'item_id_2')
details2.setBoundingBox(QgsRectangle(1, 1, 5, 5))

details3 = QgsRenderedAnnotationItemDetails('layer_id3', 'item_id_3')
details3.setBoundingBox(QgsRectangle(4, 4, 7, 7))

results.appendResults([details1, details2, details3], rc)

self.assertCountEqual([(i.layerId(), i.itemId()) for i in results.renderedItems()],
[('layer_id', 'item_id_1'),
('layer_id2', 'item_id_2'),
('layer_id3', 'item_id_3')])

results.eraseResultsFromLayers([])
self.assertCountEqual([(i.layerId(), i.itemId()) for i in results.renderedItems()],
[('layer_id', 'item_id_1'),
('layer_id2', 'item_id_2'),
('layer_id3', 'item_id_3')])

results.eraseResultsFromLayers(['layer_id2', 'layer_id3'])
self.assertCountEqual([(i.layerId(), i.itemId()) for i in results.renderedItems()],
[('layer_id', 'item_id_1')])
results.eraseResultsFromLayers(['layer_id2', 'layer_id'])
self.assertFalse(results.renderedItems())


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

0 comments on commit ae17baa

Please sign in to comment.