mirror of
https://github.com/qgis/QGIS.git
synced 2025-12-15 00:07:25 -05:00
Allow removing items from annotation layers
This commit is contained in:
parent
5e9264dfb7
commit
f0d2f3d21d
@ -59,6 +59,10 @@ Ownership of ``item`` is transferred to the layer.
|
||||
Returns the unique ID assigned to the item.
|
||||
%End
|
||||
|
||||
bool removeItem( const QString &id );
|
||||
%Docstring
|
||||
Removes (and deletes) the item with matching ``id``.
|
||||
%End
|
||||
|
||||
QMap<QString, QgsAnnotationItem *> items() const;
|
||||
%Docstring
|
||||
@ -84,15 +88,12 @@ and 1.0 (fully opaque).
|
||||
.. seealso:: :py:func:`setOpacity`
|
||||
%End
|
||||
|
||||
|
||||
virtual QgsAnnotationLayer *clone() const /Factory/;
|
||||
|
||||
virtual QgsMapLayerRenderer *createMapRenderer( QgsRenderContext &rendererContext ) /Factory/;
|
||||
|
||||
virtual QgsRectangle extent() const;
|
||||
|
||||
|
||||
|
||||
virtual void setTransformContext( const QgsCoordinateTransformContext &context );
|
||||
|
||||
virtual bool readXml( const QDomNode &layerNode, QgsReadWriteContext &context );
|
||||
|
||||
@ -42,12 +42,14 @@ QString QgsAnnotationLayer::addItem( QgsAnnotationItem *item )
|
||||
return uuid;
|
||||
}
|
||||
|
||||
#if 0
|
||||
QgsAnnotationItem *QgsAnnotationItem::takeItem( const QString &itemId )
|
||||
bool QgsAnnotationLayer::removeItem( const QString &id )
|
||||
{
|
||||
return mItems.take( itemId );
|
||||
if ( !mItems.contains( id ) )
|
||||
return false;
|
||||
|
||||
delete mItems.take( id );
|
||||
return true;
|
||||
}
|
||||
#endif
|
||||
|
||||
QgsAnnotationLayer *QgsAnnotationLayer::clone() const
|
||||
{
|
||||
@ -184,52 +186,3 @@ bool QgsAnnotationLayer::readSymbology( const QDomNode &node, QString &, QgsRead
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
#if 0
|
||||
QString QgsAnnotationLayer::pickItem( const QgsRectangle &pickRect, const QgsMapSettings &mapSettings ) const
|
||||
{
|
||||
for ( auto it = mItems.begin(), itEnd = mItems.end(); it != itEnd; ++it )
|
||||
{
|
||||
QgsCoordinateTransform crst( mapSettings.destinationCrs(), it.value()->crs(), transformContext() );
|
||||
if ( it.value()->intersects( crst.transform( pickRect ), mapSettings ) )
|
||||
{
|
||||
return it.key();
|
||||
}
|
||||
}
|
||||
return QString();
|
||||
}
|
||||
|
||||
QString QgsAnnotationLayer::pickItem( const QgsPointXY &mapPos, const QgsMapSettings &mapSettings ) const
|
||||
{
|
||||
QgsRenderContext renderContext = QgsRenderContext::fromMapSettings( mapSettings );
|
||||
double radiusmm = QgsSettings().value( "/Map/searchRadiusMM", Qgis::DEFAULT_SEARCH_RADIUS_MM ).toDouble();
|
||||
radiusmm = radiusmm > 0 ? radiusmm : Qgis::DEFAULT_SEARCH_RADIUS_MM;
|
||||
double radiusmu = radiusmm * renderContext.scaleFactor() * renderContext.mapToPixel().mapUnitsPerPixel();
|
||||
QgsRectangle filterRect;
|
||||
filterRect.setXMinimum( mapPos.x() - radiusmu );
|
||||
filterRect.setXMaximum( mapPos.x() + radiusmu );
|
||||
filterRect.setYMinimum( mapPos.y() - radiusmu );
|
||||
filterRect.setYMaximum( mapPos.y() + radiusmu );
|
||||
return pickItem( filterRect, mapSettings );
|
||||
}
|
||||
|
||||
QRectF QgsAnnotationLayer::margin() const
|
||||
{
|
||||
bool empty = true;
|
||||
QRectF rect;
|
||||
for ( const KadasMapItem *item : mItems )
|
||||
{
|
||||
if ( empty )
|
||||
{
|
||||
rect = item->margin();
|
||||
}
|
||||
else
|
||||
{
|
||||
rect = rect.united( item->margin() );
|
||||
}
|
||||
}
|
||||
return rect;
|
||||
}
|
||||
#endif
|
||||
|
||||
|
||||
@ -81,7 +81,10 @@ class CORE_EXPORT QgsAnnotationLayer : public QgsMapLayer
|
||||
*/
|
||||
QString addItem( QgsAnnotationItem *item SIP_TRANSFER );
|
||||
|
||||
//KadasMapItem *takeItem( const QString &itemId );
|
||||
/**
|
||||
* Removes (and deletes) the item with matching \a id.
|
||||
*/
|
||||
bool removeItem( const QString &id );
|
||||
|
||||
/**
|
||||
* Returns a map of items contained in the layer, by unique item ID.
|
||||
@ -105,17 +108,9 @@ class CORE_EXPORT QgsAnnotationLayer : public QgsMapLayer
|
||||
*/
|
||||
double opacity() const { return mOpacity; }
|
||||
|
||||
// QRectF margin() const;
|
||||
|
||||
QgsAnnotationLayer *clone() const override SIP_FACTORY;
|
||||
QgsMapLayerRenderer *createMapRenderer( QgsRenderContext &rendererContext ) override SIP_FACTORY;
|
||||
QgsRectangle extent() const override;
|
||||
|
||||
#if 0
|
||||
virtual QString pickItem( const QgsRectangle &pickRect, const QgsMapSettings &mapSettings ) const;
|
||||
QString pickItem( const QgsPointXY &mapPos, const QgsMapSettings &mapSettings ) const;
|
||||
#endif
|
||||
|
||||
void setTransformContext( const QgsCoordinateTransformContext &context ) override;
|
||||
bool readXml( const QDomNode &layerNode, QgsReadWriteContext &context ) override;
|
||||
bool writeXml( QDomNode &layer_node, QDomDocument &doc, const QgsReadWriteContext &context ) const override;
|
||||
|
||||
@ -74,6 +74,21 @@ class TestQgsAnnotationLayer(unittest.TestCase):
|
||||
self.assertIsInstance(layer.items()[linestring_item_id], QgsAnnotationLineStringItem)
|
||||
self.assertIsInstance(layer.items()[marker_item_id], QgsAnnotationMarkerItem)
|
||||
|
||||
self.assertFalse(layer.removeItem('xxxx'))
|
||||
self.assertEqual(len(layer.items()), 3)
|
||||
self.assertTrue(layer.removeItem(linestring_item_id))
|
||||
self.assertEqual(len(layer.items()), 2)
|
||||
self.assertIsInstance(layer.items()[polygon_item_id], QgsAnnotationPolygonItem)
|
||||
self.assertIsInstance(layer.items()[marker_item_id], QgsAnnotationMarkerItem)
|
||||
self.assertFalse(layer.removeItem(linestring_item_id))
|
||||
|
||||
self.assertTrue(layer.removeItem(polygon_item_id))
|
||||
self.assertEqual(len(layer.items()), 1)
|
||||
self.assertIsInstance(layer.items()[marker_item_id], QgsAnnotationMarkerItem)
|
||||
|
||||
self.assertTrue(layer.removeItem(marker_item_id))
|
||||
self.assertEqual(len(layer.items()), 0)
|
||||
|
||||
def testReadWriteXml(self):
|
||||
doc = QDomDocument("testdoc")
|
||||
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user