Skip to content
Permalink
Browse files
Allow QgsAnnotationItemEditOperationMoveNode operations to be
applied to annotation items to move nodes in the item
  • Loading branch information
nyalldawson committed Sep 10, 2021
1 parent a87206d commit 631343265d04507597a8022db3ddbe3bdddd8fec
@@ -121,6 +121,15 @@ Implementations should include a call to :py:func:`~QgsAnnotationItem.readCommon
.. seealso:: :py:func:`writeXml`

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

virtual bool applyEdit( QgsAbstractAnnotationItemEditOperation *operation );
%Docstring
Applies an edit ``operation`` to the item.

Returns ``True`` if the operation was successfully applied.

.. versionadded:: 3.22
%End

int zIndex() const;
@@ -41,6 +41,8 @@ Constructor for QgsAnnotationLineItem, with the specified ``linestring``.

virtual bool transform( const QTransform &transform );

virtual bool applyEdit( QgsAbstractAnnotationItemEditOperation *operation );


static QgsAnnotationLineItem *create() /Factory/;
%Docstring
@@ -39,6 +39,8 @@ Constructor for QgsAnnotationMarkerItem, at the specified ``point``.

virtual QList< QgsAnnotationItemNode > nodes() const;

virtual bool applyEdit( QgsAbstractAnnotationItemEditOperation *operation );


static QgsAnnotationMarkerItem *create() /Factory/;
%Docstring
@@ -55,6 +55,8 @@ Creates a new text at point annotation item.

virtual bool transform( const QTransform &transform );

virtual bool applyEdit( QgsAbstractAnnotationItemEditOperation *operation );


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

virtual bool transform( const QTransform &transform );

virtual bool applyEdit( QgsAbstractAnnotationItemEditOperation *operation );


static QgsAnnotationPolygonItem *create() /Factory/;
%Docstring
@@ -23,6 +23,11 @@ Qgis::AnnotationItemFlags QgsAnnotationItem::flags() const
return Qgis::AnnotationItemFlags();
}

bool QgsAnnotationItem::applyEdit( QgsAbstractAnnotationItemEditOperation * )
{
return false;
}

QList<QgsAnnotationItemNode> QgsAnnotationItem::nodes() const
{
return {};
@@ -29,6 +29,7 @@ class QgsMarkerSymbol;
class QgsLineSymbol;
class QgsFillSymbol;
class QgsAnnotationItemNode;
class QgsAbstractAnnotationItemEditOperation;

/**
* \ingroup core
@@ -148,6 +149,15 @@ class CORE_EXPORT QgsAnnotationItem
*/
virtual bool readXml( const QDomElement &element, const QgsReadWriteContext &context ) = 0;

/**
* Applies an edit \a operation to the item.
*
* Returns TRUE if the operation was successfully applied.
*
* \since QGIS 3.22
*/
virtual bool applyEdit( QgsAbstractAnnotationItemEditOperation *operation );

/**
* Returns the item's z index, which controls the order in which annotation items
* are rendered in the layer.
@@ -20,6 +20,7 @@
#include "qgssymbollayerutils.h"
#include "qgslinesymbol.h"
#include "qgsannotationitemnode.h"
#include "qgsannotationitemeditoperation.h"

QgsAnnotationLineItem::QgsAnnotationLineItem( QgsCurve *curve )
: QgsAnnotationItem()
@@ -102,6 +103,18 @@ bool QgsAnnotationLineItem::transform( const QTransform &transform )
return true;
}

bool QgsAnnotationLineItem::applyEdit( QgsAbstractAnnotationItemEditOperation *operation )
{
if ( QgsAnnotationItemEditOperationMoveNode *moveOperation = dynamic_cast< QgsAnnotationItemEditOperationMoveNode * >( operation ) )
{
return mCurve->moveVertex( moveOperation->nodeId(), QgsPoint( moveOperation->after() ) );
}
else
{
return false;
}
}

QgsAnnotationLineItem *QgsAnnotationLineItem::create()
{
return new QgsAnnotationLineItem( new QgsLineString() );
@@ -46,6 +46,7 @@ class CORE_EXPORT QgsAnnotationLineItem : public QgsAnnotationItem
QList< QgsAnnotationItemNode > nodes() const override;
QgsGeometry rubberBandGeometry() const override;
bool transform( const QTransform &transform ) override;
bool applyEdit( QgsAbstractAnnotationItemEditOperation *operation ) override;

/**
* Creates a new linestring annotation item.
@@ -20,6 +20,7 @@
#include "qgssymbollayerutils.h"
#include "qgsmarkersymbol.h"
#include "qgsannotationitemnode.h"
#include "qgsannotationitemeditoperation.h"

QgsAnnotationMarkerItem::QgsAnnotationMarkerItem( const QgsPoint &point )
: QgsAnnotationItem()
@@ -79,6 +80,19 @@ QList<QgsAnnotationItemNode> QgsAnnotationMarkerItem::nodes() const
return { QgsAnnotationItemNode( QgsVertexId( 0, 0, 0 ), mPoint, Qgis::AnnotationItemNodeType::VertexHandle )};
}

bool QgsAnnotationMarkerItem::applyEdit( QgsAbstractAnnotationItemEditOperation *operation )
{
if ( QgsAnnotationItemEditOperationMoveNode *moveOperation = dynamic_cast< QgsAnnotationItemEditOperationMoveNode * >( operation ) )
{
mPoint = QgsPoint( moveOperation->after() );
return true;
}
else
{
return false;
}
}

QgsAnnotationMarkerItem *QgsAnnotationMarkerItem::create()
{
return new QgsAnnotationMarkerItem( QgsPoint() );
@@ -44,6 +44,7 @@ class CORE_EXPORT QgsAnnotationMarkerItem : public QgsAnnotationItem
bool writeXml( QDomElement &element, QDomDocument &document, const QgsReadWriteContext &context ) const override;
Qgis::AnnotationItemFlags flags() const override;
QList< QgsAnnotationItemNode > nodes() const override;
bool applyEdit( QgsAbstractAnnotationItemEditOperation *operation ) override;

/**
* Creates a new marker annotation item.
@@ -18,6 +18,7 @@
#include "qgsannotationpointtextitem.h"
#include "qgstextrenderer.h"
#include "qgsannotationitemnode.h"
#include "qgsannotationitemeditoperation.h"

QgsAnnotationPointTextItem::QgsAnnotationPointTextItem( const QString &text, QgsPointXY point )
: QgsAnnotationItem()
@@ -146,6 +147,19 @@ bool QgsAnnotationPointTextItem::transform( const QTransform &transform )
return true;
}

bool QgsAnnotationPointTextItem::applyEdit( QgsAbstractAnnotationItemEditOperation *operation )
{
if ( QgsAnnotationItemEditOperationMoveNode *moveOperation = dynamic_cast< QgsAnnotationItemEditOperationMoveNode * >( operation ) )
{
mPoint = moveOperation->after();
return true;
}
else
{
return false;
}
}

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

/**
* Returns the point location of the text.
@@ -21,6 +21,7 @@
#include "qgssurface.h"
#include "qgsfillsymbol.h"
#include "qgsannotationitemnode.h"
#include "qgsannotationitemeditoperation.h"

QgsAnnotationPolygonItem::QgsAnnotationPolygonItem( QgsCurvePolygon *polygon )
: QgsAnnotationItem()
@@ -132,6 +133,18 @@ bool QgsAnnotationPolygonItem::transform( const QTransform &transform )
return true;
}

bool QgsAnnotationPolygonItem::applyEdit( QgsAbstractAnnotationItemEditOperation *operation )
{
if ( QgsAnnotationItemEditOperationMoveNode *moveOperation = dynamic_cast< QgsAnnotationItemEditOperationMoveNode * >( operation ) )
{
return mPolygon->moveVertex( moveOperation->nodeId(), QgsPoint( moveOperation->after() ) );
}
else
{
return false;
}
}

QgsAnnotationPolygonItem *QgsAnnotationPolygonItem::create()
{
return new QgsAnnotationPolygonItem( new QgsPolygon() );
@@ -45,6 +45,7 @@ class CORE_EXPORT QgsAnnotationPolygonItem : public QgsAnnotationItem
QList< QgsAnnotationItemNode > nodes() const override;
QgsGeometry rubberBandGeometry() const override;
bool transform( const QTransform &transform ) override;
bool applyEdit( QgsAbstractAnnotationItemEditOperation *operation ) override;

/**
* Creates a new polygon annotation item.
@@ -36,7 +36,8 @@
QgsAnnotationItemNode,
QgsPointXY,
Qgis,
QgsVertexId
QgsVertexId,
QgsAnnotationItemEditOperationMoveNode
)
from qgis.PyQt.QtXml import QDomDocument

@@ -89,6 +90,17 @@ def test_transform(self):
item.transform(transform)
self.assertEqual(item.geometry().asWkt(), 'LineString (112 213, 114 213, 114 215)')

def test_apply_move_node_edit(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)')

self.assertTrue(item.applyEdit(QgsAnnotationItemEditOperationMoveNode('', QgsVertexId(0, 0, 1), QgsPointXY(14, 13), QgsPointXY(17, 18))))
self.assertEqual(item.geometry().asWkt(), 'LineString (12 13, 17 18, 14 15)')
self.assertTrue(item.applyEdit(QgsAnnotationItemEditOperationMoveNode('', QgsVertexId(0, 0, 2), QgsPointXY(14, 15), QgsPointXY(19, 20))))
self.assertEqual(item.geometry().asWkt(), 'LineString (12 13, 17 18, 19 20)')
self.assertFalse(item.applyEdit(QgsAnnotationItemEditOperationMoveNode('', QgsVertexId(0, 0, 3), QgsPointXY(14, 15), QgsPointXY(19, 20))))
self.assertEqual(item.geometry().asWkt(), 'LineString (12 13, 17 18, 19 20)')

def test_rubberbandgeometry(self):
"""
Test creating rubber band geometry
@@ -34,7 +34,8 @@
QgsAnnotationItemNode,
Qgis,
QgsPointXY,
QgsVertexId
QgsVertexId,
QgsAnnotationItemEditOperationMoveNode
)
from qgis.PyQt.QtXml import QDomDocument

@@ -87,6 +88,13 @@ def test_transform(self):
item.transform(transform)
self.assertEqual(item.geometry().asWkt(), 'POINT(112 213)')

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

self.assertTrue(item.applyEdit(QgsAnnotationItemEditOperationMoveNode('', QgsVertexId(0, 0, 1), QgsPointXY(14, 13), QgsPointXY(17, 18))))
self.assertEqual(item.geometry().asWkt(), 'POINT(17 18)')

def testReadWriteXml(self):
doc = QDomDocument("testdoc")
elem = doc.createElement('test')
@@ -35,7 +35,8 @@
QgsTextFormat,
QgsAnnotationItemNode,
Qgis,
QgsVertexId
QgsVertexId,
QgsAnnotationItemEditOperationMoveNode
)
from qgis.PyQt.QtXml import QDomDocument

@@ -98,6 +99,13 @@ def test_transform(self):
item.transform(transform)
self.assertEqual(item.point().asWkt(), 'POINT(112 213)')

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

self.assertTrue(item.applyEdit(QgsAnnotationItemEditOperationMoveNode('', QgsVertexId(0, 0, 1), QgsPointXY(14, 13), QgsPointXY(17, 18))))
self.assertEqual(item.point().asWkt(), 'POINT(17 18)')

def testReadWriteXml(self):
doc = QDomDocument("testdoc")
elem = doc.createElement('test')
@@ -38,7 +38,8 @@
QgsAnnotationItemNode,
Qgis,
QgsPointXY,
QgsVertexId
QgsVertexId,
QgsAnnotationItemEditOperationMoveNode
)
from qgis.PyQt.QtXml import QDomDocument

@@ -100,6 +101,18 @@ def test_transform(self):
item.transform(transform)
self.assertEqual(item.geometry().asWkt(), 'Polygon ((112 213, 114 213, 114 215, 112 213))')

def test_apply_move_node_edit(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))')

self.assertTrue(item.applyEdit(QgsAnnotationItemEditOperationMoveNode('', QgsVertexId(0, 0, 1), QgsPointXY(14, 13), QgsPointXY(17, 18))))
self.assertEqual(item.geometry().asWkt(), 'Polygon ((12 13, 17 18, 14 15, 12 13))')
self.assertTrue(item.applyEdit(QgsAnnotationItemEditOperationMoveNode('', QgsVertexId(0, 0, 3), QgsPointXY(12, 13), QgsPointXY(19, 20))))
self.assertEqual(item.geometry().asWkt(), 'Polygon ((19 20, 17 18, 14 15, 19 20))')
self.assertFalse(item.applyEdit(QgsAnnotationItemEditOperationMoveNode('', QgsVertexId(0, 0, 4), QgsPointXY(14, 15), QgsPointXY(19, 20))))
self.assertEqual(item.geometry().asWkt(), 'Polygon ((19 20, 17 18, 14 15, 19 20))')

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

0 comments on commit 6313432

Please sign in to comment.