Skip to content
Permalink
Browse files
Add an id number to QgsAnnotationItemNode
  • Loading branch information
nyalldawson committed Sep 10, 2021
1 parent d9348db commit 5ed05fadca4880362236dcb3a4d5f4ed8a97cc3e
@@ -27,19 +27,25 @@ Contains information about a node used for editing an annotation item.
Default constructor
%End

QgsAnnotationItemNode( const QgsPointXY &point, Qgis::AnnotationItemNodeType type );
QgsAnnotationItemNode( const QgsVertexId &id, const QgsPointXY &point, Qgis::AnnotationItemNodeType type );
%Docstring
Constructor for QgsAnnotationItemNode, with the specified ``point`` and ``type``.
Constructor for QgsAnnotationItemNode, with the specified ``id``, ``point`` and ``type``.
%End

SIP_PYOBJECT __repr__();
%MethodCode
QString str = QStringLiteral( "<QgsAnnotationItemNode: %1 (%2, %3)>" ).arg( qgsEnumValueToKey( sipCpp->type() ) )
QString str = QStringLiteral( "<QgsAnnotationItemNode: %1 - %2 (%3, %4)>" ).arg( sipCpp->id().vertex )
.arg( qgsEnumValueToKey( sipCpp->type() ) )
.arg( sipCpp->point().x() )
.arg( sipCpp->point().y() );
sipRes = PyUnicode_FromString( str.toUtf8().constData() );
%End

QgsVertexId id() const;
%Docstring
Returns the ID number of the node, used for uniquely identifying the node in the item.
%End

QgsPointXY point() const;
%Docstring
Returns the node's position, in geographic coordinates.
@@ -20,6 +20,7 @@
#include "qgis_core.h"
#include "qgis_sip.h"
#include "qgspointxy.h"
#include "qgsabstractgeometry.h"
#include "qgis.h"

/**
@@ -37,23 +38,30 @@ class CORE_EXPORT QgsAnnotationItemNode
QgsAnnotationItemNode() = default;

/**
* Constructor for QgsAnnotationItemNode, with the specified \a point and \a type.
* Constructor for QgsAnnotationItemNode, with the specified \a id, \a point and \a type.
*/
QgsAnnotationItemNode( const QgsPointXY &point, Qgis::AnnotationItemNodeType type )
: mPoint( point )
QgsAnnotationItemNode( const QgsVertexId &id, const QgsPointXY &point, Qgis::AnnotationItemNodeType type )
: mId( id )
, mPoint( point )
, mType( type )
{}

#ifdef SIP_RUN
SIP_PYOBJECT __repr__();
% MethodCode
QString str = QStringLiteral( "<QgsAnnotationItemNode: %1 (%2, %3)>" ).arg( qgsEnumValueToKey( sipCpp->type() ) )
QString str = QStringLiteral( "<QgsAnnotationItemNode: %1 - %2 (%3, %4)>" ).arg( sipCpp->id().vertex )
.arg( qgsEnumValueToKey( sipCpp->type() ) )
.arg( sipCpp->point().x() )
.arg( sipCpp->point().y() );
sipRes = PyUnicode_FromString( str.toUtf8().constData() );
% End
#endif

/**
* Returns the ID number of the node, used for uniquely identifying the node in the item.
*/
QgsVertexId id() const { return mId; }

/**
* Returns the node's position, in geographic coordinates.
*
@@ -89,7 +97,7 @@ class CORE_EXPORT QgsAnnotationItemNode
// TODO c++20 - replace with = default
bool operator==( const QgsAnnotationItemNode &other ) const
{
return mType == other.mType && mPoint == other.mPoint;
return mId == other.mId && mType == other.mType && mPoint == other.mPoint;
}

bool operator!=( const QgsAnnotationItemNode &other ) const
@@ -99,6 +107,7 @@ class CORE_EXPORT QgsAnnotationItemNode

private:

QgsVertexId mId;
QgsPointXY mPoint;
Qgis::AnnotationItemNodeType mType = Qgis::AnnotationItemNodeType::VertexHandle;

@@ -83,9 +83,10 @@ bool QgsAnnotationLineItem::writeXml( QDomElement &element, QDomDocument &docume
QList<QgsAnnotationItemNode> QgsAnnotationLineItem::nodes() const
{
QList< QgsAnnotationItemNode > res;
for ( auto it = mCurve->vertices_begin(); it != mCurve->vertices_end(); ++it )
int i = 0;
for ( auto it = mCurve->vertices_begin(); it != mCurve->vertices_end(); ++it, ++i )
{
res.append( QgsAnnotationItemNode( QgsPointXY( ( *it ).x(), ( *it ).y() ), Qgis::AnnotationItemNodeType::VertexHandle ) );
res.append( QgsAnnotationItemNode( it.vertexId(), QgsPointXY( ( *it ).x(), ( *it ).y() ), Qgis::AnnotationItemNodeType::VertexHandle ) );
}
return res;
}
@@ -76,7 +76,7 @@ Qgis::AnnotationItemFlags QgsAnnotationMarkerItem::flags() const

QList<QgsAnnotationItemNode> QgsAnnotationMarkerItem::nodes() const
{
return { QgsAnnotationItemNode( mPoint, Qgis::AnnotationItemNodeType::VertexHandle )};
return { QgsAnnotationItemNode( QgsVertexId( 0, 0, 0 ), mPoint, Qgis::AnnotationItemNodeType::VertexHandle )};
}

QgsAnnotationMarkerItem *QgsAnnotationMarkerItem::create()
@@ -134,7 +134,7 @@ QgsRectangle QgsAnnotationPointTextItem::boundingBox( QgsRenderContext &context

QList<QgsAnnotationItemNode> QgsAnnotationPointTextItem::nodes() const
{
return { QgsAnnotationItemNode( mPoint, Qgis::AnnotationItemNodeType::VertexHandle )};
return { QgsAnnotationItemNode( QgsVertexId( 0, 0, 0 ), mPoint, Qgis::AnnotationItemNodeType::VertexHandle )};
}

bool QgsAnnotationPointTextItem::transform( const QTransform &transform )
@@ -98,24 +98,24 @@ QList<QgsAnnotationItemNode> QgsAnnotationPolygonItem::nodes() const
{
QList< QgsAnnotationItemNode > res;

auto processRing = [&res]( const QgsCurve * ring )
auto processRing = [&res]( const QgsCurve * ring, int ringId )
{
// we don't want a duplicate node for the closed ring vertex
const int count = ring->isClosed() ? ring->numPoints() - 1 : ring->numPoints();
res.reserve( res.size() + count );
for ( int i = 0; i < count; ++i )
{
res << QgsAnnotationItemNode( QgsPointXY( ring->xAt( i ), ring->yAt( i ) ), Qgis::AnnotationItemNodeType::VertexHandle );
res << QgsAnnotationItemNode( QgsVertexId( 0, ringId, i ), QgsPointXY( ring->xAt( i ), ring->yAt( i ) ), Qgis::AnnotationItemNodeType::VertexHandle );
}
};

if ( const QgsCurve *ring = mPolygon->exteriorRing() )
{
processRing( ring );
processRing( ring, 0 );
}
for ( int i = 0; i < mPolygon->numInteriorRings(); ++i )
{
processRing( mPolygon->interiorRing( i ) );
processRing( mPolygon->interiorRing( i ), i + 1 );
}

return res;
@@ -16,7 +16,8 @@
from qgis.core import (
QgsAnnotationItemNode,
QgsPointXY,
Qgis
Qgis,
QgsVertexId
)
from qgis.testing import start_app, unittest

@@ -29,26 +30,31 @@
class TestQgsAnnotationItemNode(unittest.TestCase):

def test_basic(self):
node = QgsAnnotationItemNode(QgsPointXY(1, 2), Qgis.AnnotationItemNodeType.VertexHandle)
node = QgsAnnotationItemNode(QgsVertexId(0, 0, 1), QgsPointXY(1, 2), Qgis.AnnotationItemNodeType.VertexHandle)
self.assertEqual(node.point(), QgsPointXY(1, 2))
self.assertEqual(node.id(), QgsVertexId(0, 0, 1))

node.setPoint(QgsPointXY(3, 4))
self.assertEqual(node.point(), QgsPointXY(3, 4))

self.assertEqual(node.type(), Qgis.AnnotationItemNodeType.VertexHandle)

def test_repr(self):
node = QgsAnnotationItemNode(QgsPointXY(1, 2), Qgis.AnnotationItemNodeType.VertexHandle)
self.assertEqual(str(node), '<QgsAnnotationItemNode: VertexHandle (1, 2)>')
node = QgsAnnotationItemNode(QgsVertexId(0, 0, 1), QgsPointXY(1, 2), Qgis.AnnotationItemNodeType.VertexHandle)
self.assertEqual(str(node), '<QgsAnnotationItemNode: 1 - VertexHandle (1, 2)>')

def test_equality(self):
node = QgsAnnotationItemNode(QgsPointXY(1, 2), Qgis.AnnotationItemNodeType.VertexHandle)
node2 = QgsAnnotationItemNode(QgsPointXY(1, 2), Qgis.AnnotationItemNodeType.VertexHandle)
node = QgsAnnotationItemNode(QgsVertexId(0, 0, 1), QgsPointXY(1, 2), Qgis.AnnotationItemNodeType.VertexHandle)
node2 = QgsAnnotationItemNode(QgsVertexId(0, 0, 1), QgsPointXY(1, 2), Qgis.AnnotationItemNodeType.VertexHandle)
self.assertEqual(node, node2)

node2.setPoint(QgsPointXY(3, 4))
self.assertNotEqual(node, node2)

node = QgsAnnotationItemNode(QgsVertexId(0, 0, 1), QgsPointXY(1, 2), Qgis.AnnotationItemNodeType.VertexHandle)
node2 = QgsAnnotationItemNode(QgsVertexId(0, 0, 2), QgsPointXY(1, 2), Qgis.AnnotationItemNodeType.VertexHandle)
self.assertNotEqual(node, node2)


if __name__ == '__main__':
unittest.main()
@@ -35,7 +35,8 @@
QgsCircularString,
QgsAnnotationItemNode,
QgsPointXY,
Qgis
Qgis,
QgsVertexId
)
from qgis.PyQt.QtXml import QDomDocument

@@ -76,9 +77,9 @@ def test_nodes(self):
Test nodes for item
"""
item = QgsAnnotationLineItem(QgsLineString([QgsPoint(12, 13), QgsPoint(14, 13), QgsPoint(14, 15)]))
self.assertEqual(item.nodes(), [QgsAnnotationItemNode(QgsPointXY(12, 13), Qgis.AnnotationItemNodeType.VertexHandle),
QgsAnnotationItemNode(QgsPointXY(14, 13), Qgis.AnnotationItemNodeType.VertexHandle),
QgsAnnotationItemNode(QgsPointXY(14, 15), Qgis.AnnotationItemNodeType.VertexHandle)])
self.assertEqual(item.nodes(), [QgsAnnotationItemNode(QgsVertexId(0, 0, 0), QgsPointXY(12, 13), Qgis.AnnotationItemNodeType.VertexHandle),
QgsAnnotationItemNode(QgsVertexId(0, 0, 1), QgsPointXY(14, 13), Qgis.AnnotationItemNodeType.VertexHandle),
QgsAnnotationItemNode(QgsVertexId(0, 0, 2), QgsPointXY(14, 15), Qgis.AnnotationItemNodeType.VertexHandle)])

def test_transform(self):
item = QgsAnnotationLineItem(QgsLineString([QgsPoint(12, 13), QgsPoint(14, 13), QgsPoint(14, 15)]))
@@ -33,7 +33,8 @@
QgsRectangle,
QgsAnnotationItemNode,
Qgis,
QgsPointXY
QgsPointXY,
QgsVertexId
)
from qgis.PyQt.QtXml import QDomDocument

@@ -76,7 +77,7 @@ def test_nodes(self):
Test nodes for item
"""
item = QgsAnnotationMarkerItem(QgsPoint(12, 13))
self.assertEqual(item.nodes(), [QgsAnnotationItemNode(QgsPointXY(12, 13), Qgis.AnnotationItemNodeType.VertexHandle)])
self.assertEqual(item.nodes(), [QgsAnnotationItemNode(QgsVertexId(0, 0, 0), QgsPointXY(12, 13), Qgis.AnnotationItemNodeType.VertexHandle)])

def test_transform(self):
item = QgsAnnotationMarkerItem(QgsPoint(12, 13))
@@ -34,7 +34,8 @@
QgsRectangle,
QgsTextFormat,
QgsAnnotationItemNode,
Qgis
Qgis,
QgsVertexId
)
from qgis.PyQt.QtXml import QDomDocument

@@ -87,7 +88,7 @@ def test_nodes(self):
Test nodes for item
"""
item = QgsAnnotationPointTextItem('my text', QgsPointXY(12, 13))
self.assertEqual(item.nodes(), [QgsAnnotationItemNode(QgsPointXY(12, 13), Qgis.AnnotationItemNodeType.VertexHandle)])
self.assertEqual(item.nodes(), [QgsAnnotationItemNode(QgsVertexId(0, 0, 0), QgsPointXY(12, 13), Qgis.AnnotationItemNodeType.VertexHandle)])

def test_transform(self):
item = QgsAnnotationPointTextItem('my text', QgsPointXY(12, 13))
@@ -37,7 +37,8 @@
QgsCircularString,
QgsAnnotationItemNode,
Qgis,
QgsPointXY
QgsPointXY,
QgsVertexId
)
from qgis.PyQt.QtXml import QDomDocument

@@ -79,9 +80,9 @@ def test_nodes(self):
"""
item = QgsAnnotationPolygonItem(QgsPolygon(QgsLineString([QgsPoint(12, 13), QgsPoint(14, 13), QgsPoint(14, 15), QgsPoint(12, 13)])))
# nodes shouldn't form a closed ring
self.assertEqual(item.nodes(), [QgsAnnotationItemNode(QgsPointXY(12, 13), Qgis.AnnotationItemNodeType.VertexHandle),
QgsAnnotationItemNode(QgsPointXY(14, 13), Qgis.AnnotationItemNodeType.VertexHandle),
QgsAnnotationItemNode(QgsPointXY(14, 15), Qgis.AnnotationItemNodeType.VertexHandle)])
self.assertEqual(item.nodes(), [QgsAnnotationItemNode(QgsVertexId(0, 0, 0), QgsPointXY(12, 13), Qgis.AnnotationItemNodeType.VertexHandle),
QgsAnnotationItemNode(QgsVertexId(0, 0, 1), QgsPointXY(14, 13), Qgis.AnnotationItemNodeType.VertexHandle),
QgsAnnotationItemNode(QgsVertexId(0, 0, 2), QgsPointXY(14, 15), Qgis.AnnotationItemNodeType.VertexHandle)])

def test_rubberbandgeometry(self):
"""

0 comments on commit 5ed05fa

Please sign in to comment.