Skip to content
Permalink
Browse files
Add a method to transform QgsAnnotationItems using a QTransform
transform
  • Loading branch information
nyalldawson committed Sep 7, 2021
1 parent 4f1fc3b commit 8d1f2bf7a6648ef14620138535b5c4f8ee7d313d
@@ -82,6 +82,15 @@ Returns the bounding box of the item's geographic location, in the parent layer'
virtual QgsRectangle boundingBox( QgsRenderContext &context ) const;
%Docstring
Returns the bounding box of the item's geographic location, in the parent layer's coordinate reference system.
%End

virtual bool transform( const QTransform &transform ) = 0;
%Docstring
Transforms the item's geometry using the specified ``transform``.

Returns ``True`` if the transformation was successful.

.. versionadded:: 3.22
%End

virtual void render( QgsRenderContext &context, QgsFeedback *feedback ) = 0;
@@ -37,6 +37,8 @@ Constructor for QgsAnnotationLineItem, with the specified ``linestring``.

virtual QList< QgsAnnotationItemNode > nodes() const;

virtual bool transform( const QTransform &transform );


static QgsAnnotationLineItem *create() /Factory/;
%Docstring
@@ -53,6 +53,8 @@ Creates a new marker annotation item.

virtual QgsRectangle boundingBox( QgsRenderContext &context ) const;

virtual bool transform( const QTransform &transform );


QgsPointXY geometry() const;
%Docstring
@@ -53,6 +53,8 @@ Creates a new text at point annotation item.

virtual QList< QgsAnnotationItemNode > nodes() const;

virtual bool transform( const QTransform &transform );


QgsPointXY point() const;
%Docstring
@@ -37,6 +37,8 @@ Constructor for QgsAnnotationPolygonItem, with the specified ``polygon`` geometr

virtual QList< QgsAnnotationItemNode > nodes() const;

virtual bool transform( const QTransform &transform );


static QgsAnnotationPolygonItem *create() /Factory/;
%Docstring
@@ -111,6 +111,15 @@ class CORE_EXPORT QgsAnnotationItem
*/
virtual QgsRectangle boundingBox( QgsRenderContext &context ) const { Q_UNUSED( context ) return boundingBox();}

/**
* Transforms the item's geometry using the specified \a transform.
*
* Returns TRUE if the transformation was successful.
*
* \since QGIS 3.22
*/
virtual bool transform( const QTransform &transform ) = 0;

/**
* Renders the item to the specified render \a context.
*
@@ -90,6 +90,12 @@ QList<QgsAnnotationItemNode> QgsAnnotationLineItem::nodes() const
return res;
}

bool QgsAnnotationLineItem::transform( const QTransform &transform )
{
mCurve->transform( transform );
return true;
}

QgsAnnotationLineItem *QgsAnnotationLineItem::create()
{
return new QgsAnnotationLineItem( new QgsLineString() );
@@ -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;
bool transform( const QTransform &transform ) override;

/**
* Creates a new linestring annotation item.
@@ -140,6 +140,12 @@ QgsRectangle QgsAnnotationMarkerItem::boundingBox( QgsRenderContext &context ) c
return context.coordinateTransform().transformBoundingBox( boundsMapUnits, QgsCoordinateTransform::ReverseTransform );
}

bool QgsAnnotationMarkerItem::transform( const QTransform &transform )
{
mPoint.transform( transform );
return true;
}

const QgsMarkerSymbol *QgsAnnotationMarkerItem::symbol() const
{
return mSymbol.get();
@@ -54,6 +54,7 @@ class CORE_EXPORT QgsAnnotationMarkerItem : public QgsAnnotationItem
QgsAnnotationMarkerItem *clone() override SIP_FACTORY;
QgsRectangle boundingBox() const override;
QgsRectangle boundingBox( QgsRenderContext &context ) const override;
bool transform( const QTransform &transform ) override;

/**
* Returns the point geometry of the marker.
@@ -137,6 +137,15 @@ QList<QgsAnnotationItemNode> QgsAnnotationPointTextItem::nodes() const
return { QgsAnnotationItemNode( mPoint, Qgis::AnnotationItemNodeType::VertexHandle )};
}

bool QgsAnnotationPointTextItem::transform( const QTransform &transform )
{
double x = mPoint.x();
double y = mPoint.y();
transform.map( mPoint.x(), mPoint.y(), &x, &y );
mPoint.set( x, y );
return true;
}

QgsTextFormat QgsAnnotationPointTextItem::format() const
{
return mTextFormat;
@@ -55,6 +55,7 @@ class CORE_EXPORT QgsAnnotationPointTextItem : public QgsAnnotationItem
QgsRectangle boundingBox() const override;
QgsRectangle boundingBox( QgsRenderContext &context ) const override;
QList< QgsAnnotationItemNode > nodes() const override;
bool transform( const QTransform &transform ) override;

/**
* Returns the point location of the text.
@@ -121,6 +121,12 @@ QList<QgsAnnotationItemNode> QgsAnnotationPolygonItem::nodes() const
return res;
}

bool QgsAnnotationPolygonItem::transform( const QTransform &transform )
{
mPolygon->transform( transform );
return true;
}

QgsAnnotationPolygonItem *QgsAnnotationPolygonItem::create()
{
return new QgsAnnotationPolygonItem( new QgsPolygon() );
@@ -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;
bool transform( const QTransform &transform ) override;

/**
* Creates a new polygon annotation item.
@@ -52,6 +52,11 @@ class TestItem : public QgsAnnotationItem
return new TestItem();
}

bool transform( const QTransform & ) override
{
return true;
}

bool writeXml( QDomElement &, QDomDocument &, const QgsReadWriteContext & ) const override
{
return true;
@@ -68,7 +68,7 @@ class TestItem : public QgsAnnotationItem // clazy:exclude=missing-qobject-macro
void render( QgsRenderContext &, QgsFeedback * ) override {}
bool writeXml( QDomElement &, QDomDocument &, const QgsReadWriteContext & ) const override { return true; }
bool readXml( const QDomElement &, const QgsReadWriteContext & ) override { return true; }

bool transform( const QTransform & ) override { return true; }
};

class TestItemWidget: public QgsAnnotationItemBaseWidget
@@ -18,7 +18,8 @@
QDir)
from qgis.PyQt.QtGui import (QImage,
QPainter,
QColor)
QColor,
QTransform)
from qgis.core import (QgsMapSettings,
QgsCoordinateTransform,
QgsProject,
@@ -79,6 +80,14 @@ def test_nodes(self):
QgsAnnotationItemNode(QgsPointXY(14, 13), Qgis.AnnotationItemNodeType.VertexHandle),
QgsAnnotationItemNode(QgsPointXY(14, 15), Qgis.AnnotationItemNodeType.VertexHandle)])

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

transform = QTransform.fromTranslate(100, 200)
item.transform(transform)
self.assertEqual(item.geometry().asWkt(), 'LineString (112 213, 114 213, 114 215)')

def testReadWriteXml(self):
doc = QDomDocument("testdoc")
elem = doc.createElement('test')
@@ -18,7 +18,8 @@
QDir)
from qgis.PyQt.QtGui import (QImage,
QPainter,
QColor)
QColor,
QTransform)
from qgis.core import (QgsMapSettings,
QgsCoordinateTransform,
QgsProject,
@@ -77,6 +78,14 @@ def test_nodes(self):
item = QgsAnnotationMarkerItem(QgsPoint(12, 13))
self.assertEqual(item.nodes(), [QgsAnnotationItemNode(QgsPointXY(12, 13), Qgis.AnnotationItemNodeType.VertexHandle)])

def test_transform(self):
item = QgsAnnotationMarkerItem(QgsPoint(12, 13))
self.assertEqual(item.geometry().asWkt(), 'POINT(12 13)')

transform = QTransform.fromTranslate(100, 200)
item.transform(transform)
self.assertEqual(item.geometry().asWkt(), 'POINT(112 213)')

def testReadWriteXml(self):
doc = QDomDocument("testdoc")
elem = doc.createElement('test')
@@ -19,7 +19,8 @@
Qt)
from qgis.PyQt.QtGui import (QImage,
QPainter,
QColor)
QColor,
QTransform)
from qgis.core import (QgsMapSettings,
QgsCoordinateTransform,
QgsProject,
@@ -88,6 +89,14 @@ def test_nodes(self):
item = QgsAnnotationPointTextItem('my text', QgsPointXY(12, 13))
self.assertEqual(item.nodes(), [QgsAnnotationItemNode(QgsPointXY(12, 13), Qgis.AnnotationItemNodeType.VertexHandle)])

def test_transform(self):
item = QgsAnnotationPointTextItem('my text', QgsPointXY(12, 13))
self.assertEqual(item.point().asWkt(), 'POINT(12 13)')

transform = QTransform.fromTranslate(100, 200)
item.transform(transform)
self.assertEqual(item.point().asWkt(), 'POINT(112 213)')

def testReadWriteXml(self):
doc = QDomDocument("testdoc")
elem = doc.createElement('test')
@@ -18,7 +18,8 @@
QDir)
from qgis.PyQt.QtGui import (QImage,
QPainter,
QColor)
QColor,
QTransform)
from qgis.core import (QgsMapSettings,
QgsCoordinateTransform,
QgsProject,
@@ -82,6 +83,14 @@ def test_nodes(self):
QgsAnnotationItemNode(QgsPointXY(14, 13), Qgis.AnnotationItemNodeType.VertexHandle),
QgsAnnotationItemNode(QgsPointXY(14, 15), Qgis.AnnotationItemNodeType.VertexHandle)])

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))')

transform = QTransform.fromTranslate(100, 200)
item.transform(transform)
self.assertEqual(item.geometry().asWkt(), 'Polygon ((112 213, 114 213, 114 215, 112 213))')

def testReadWriteXml(self):
doc = QDomDocument("testdoc")
elem = doc.createElement('test')

0 comments on commit 8d1f2bf

Please sign in to comment.