Skip to content
Permalink
Browse files
Add method to create rubber band geometry for an annotation item
  • Loading branch information
nyalldawson committed Sep 7, 2021
1 parent 8d1f2bf commit 6d926d4ffe4e471fbe3c542d5a48d988e58ba4d2
@@ -198,6 +198,16 @@ exactly 2mm thick when a map is rendered at 1:1000, or 1mm thick when rendered a
.. seealso:: :py:func:`symbologyReferenceScale`

.. seealso:: :py:func:`setUseSymbologyReferenceScale`
%End

virtual QgsGeometry rubberBandGeometry() const;
%Docstring
Returns the geometry to use as a rubber band for map tools which manipulate the item.

The default implementation returns a null geometry, which indicates that the item bounds should
be used as the rubber band.

.. versionadded:: 3.22
%End

protected:
@@ -37,6 +37,8 @@ Constructor for QgsAnnotationLineItem, with the specified ``linestring``.

virtual QList< QgsAnnotationItemNode > nodes() const;

virtual QgsGeometry rubberBandGeometry() const;

virtual bool transform( const QTransform &transform );


@@ -37,6 +37,8 @@ Constructor for QgsAnnotationPolygonItem, with the specified ``polygon`` geometr

virtual QList< QgsAnnotationItemNode > nodes() const;

virtual QgsGeometry rubberBandGeometry() const;

virtual bool transform( const QTransform &transform );


@@ -50,3 +50,8 @@ bool QgsAnnotationItem::readCommonProperties( const QDomElement &element, const
setSymbologyReferenceScale( element.attribute( QStringLiteral( "referenceScale" ) ).toDouble() );
return true;
}

QgsGeometry QgsAnnotationItem::rubberBandGeometry() const
{
return QgsGeometry();
}
@@ -221,6 +221,16 @@ class CORE_EXPORT QgsAnnotationItem
*/
void setSymbologyReferenceScale( double scale ) { mReferenceScale = scale; }

/**
* Returns the geometry to use as a rubber band for map tools which manipulate the item.
*
* The default implementation returns a null geometry, which indicates that the item bounds should
* be used as the rubber band.
*
* \since QGIS 3.22
*/
virtual QgsGeometry rubberBandGeometry() const;

protected:

/**
@@ -90,6 +90,11 @@ QList<QgsAnnotationItemNode> QgsAnnotationLineItem::nodes() const
return res;
}

QgsGeometry QgsAnnotationLineItem::rubberBandGeometry() const
{
return QgsGeometry( mCurve->clone() );
}

bool QgsAnnotationLineItem::transform( const QTransform &transform )
{
mCurve->transform( transform );
@@ -44,6 +44,7 @@ class CORE_EXPORT QgsAnnotationLineItem : public QgsAnnotationItem
void render( QgsRenderContext &context, QgsFeedback *feedback ) override;
bool writeXml( QDomElement &element, QDomDocument &document, const QgsReadWriteContext &context ) const override;
QList< QgsAnnotationItemNode > nodes() const override;
QgsGeometry rubberBandGeometry() const override;
bool transform( const QTransform &transform ) override;

/**
@@ -121,6 +121,11 @@ QList<QgsAnnotationItemNode> QgsAnnotationPolygonItem::nodes() const
return res;
}

QgsGeometry QgsAnnotationPolygonItem::rubberBandGeometry() const
{
return QgsGeometry( mPolygon->clone() );
}

bool QgsAnnotationPolygonItem::transform( const QTransform &transform )
{
mPolygon->transform( transform );
@@ -43,6 +43,7 @@ class CORE_EXPORT QgsAnnotationPolygonItem : public QgsAnnotationItem
void render( QgsRenderContext &context, QgsFeedback *feedback ) override;
bool writeXml( QDomElement &element, QDomDocument &document, const QgsReadWriteContext &context ) const override;
QList< QgsAnnotationItemNode > nodes() const override;
QgsGeometry rubberBandGeometry() const override;
bool transform( const QTransform &transform ) override;

/**
@@ -88,6 +88,14 @@ def test_transform(self):
item.transform(transform)
self.assertEqual(item.geometry().asWkt(), 'LineString (112 213, 114 213, 114 215)')

def test_rubberbandgeometry(self):
"""
Test creating rubber band geometry
"""
item = QgsAnnotationLineItem(QgsLineString([QgsPoint(12, 13), QgsPoint(14, 13), QgsPoint(14, 15)]))
band = item.rubberBandGeometry()
self.assertEqual(band.asWkt(), 'LineString (12 13, 14 13, 14 15)')

def testReadWriteXml(self):
doc = QDomDocument("testdoc")
elem = doc.createElement('test')
@@ -83,6 +83,14 @@ def test_nodes(self):
QgsAnnotationItemNode(QgsPointXY(14, 13), Qgis.AnnotationItemNodeType.VertexHandle),
QgsAnnotationItemNode(QgsPointXY(14, 15), Qgis.AnnotationItemNodeType.VertexHandle)])

def test_rubberbandgeometry(self):
"""
Test creating rubber band geometry
"""
item = QgsAnnotationPolygonItem(QgsPolygon(QgsLineString([QgsPoint(12, 13), QgsPoint(14, 13), QgsPoint(14, 15), QgsPoint(12, 13)])))
band = item.rubberBandGeometry()
self.assertEqual(band.asWkt(), 'Polygon ((12 13, 14 13, 14 15, 12 13))')

def test_transform(self):
item = QgsAnnotationPolygonItem(QgsPolygon(QgsLineString([QgsPoint(12, 13), QgsPoint(14, 13), QgsPoint(14, 15), QgsPoint(12, 13)])))
self.assertEqual(item.geometry().asWkt(), 'Polygon ((12 13, 14 13, 14 15, 12 13))')

0 comments on commit 6d926d4

Please sign in to comment.