Skip to content
Permalink
Browse files
[api] Add method to retrieve the nodes for an annotation item
For now all nodes just directly represent vertices in the item,
but the intention here is that they can represent any form of handle
which can be used to manipulate an item (e.g. a bezier curve point,
some "smart shape" handle for resizing or rotating an item, etc)
  • Loading branch information
nyalldawson committed Sep 2, 2021
1 parent 0904dcf commit 4ff1e55a21d6d090dc0b64d35d50874807637131
Showing with 404 additions and 4 deletions.
  1. +5 −0 python/core/auto_additions/qgis.py
  2. +7 −0 python/core/auto_generated/annotations/qgsannotationitem.sip.in
  3. +87 −0 python/core/auto_generated/annotations/qgsannotationitemnode.sip.in
  4. +2 −0 python/core/auto_generated/annotations/qgsannotationlineitem.sip.in
  5. +2 −0 python/core/auto_generated/annotations/qgsannotationmarkeritem.sip.in
  6. +2 −0 python/core/auto_generated/annotations/qgsannotationpointtextitem.sip.in
  7. +2 −0 python/core/auto_generated/annotations/qgsannotationpolygonitem.sip.in
  8. +5 −0 python/core/auto_generated/qgis.sip.in
  9. +1 −0 python/core/core_auto.sip
  10. +1 −0 src/core/CMakeLists.txt
  11. +6 −0 src/core/annotations/qgsannotationitem.cpp
  12. +8 −0 src/core/annotations/qgsannotationitem.h
  13. +107 −0 src/core/annotations/qgsannotationitemnode.h
  14. +11 −0 src/core/annotations/qgsannotationlineitem.cpp
  15. +1 −0 src/core/annotations/qgsannotationlineitem.h
  16. +6 −0 src/core/annotations/qgsannotationmarkeritem.cpp
  17. +1 −0 src/core/annotations/qgsannotationmarkeritem.h
  18. +6 −0 src/core/annotations/qgsannotationpointtextitem.cpp
  19. +1 −0 src/core/annotations/qgsannotationpointtextitem.h
  20. +28 −0 src/core/annotations/qgsannotationpolygonitem.cpp
  21. +1 −0 src/core/annotations/qgsannotationpolygonitem.h
  22. +11 −0 src/core/qgis.h
  23. +1 −0 tests/src/python/CMakeLists.txt
  24. +54 −0 tests/src/python/test_qgsannotationitemnode.py
  25. +13 −1 tests/src/python/test_qgsannotationlineitem.py
  26. +11 −1 tests/src/python/test_qgsannotationmarkeritem.py
  27. +10 −1 tests/src/python/test_qgsannotationpointtextitem.py
  28. +14 −1 tests/src/python/test_qgsannotationpolygonitem.py
@@ -656,3 +656,8 @@
Qgis.AnnotationItemFlag.__doc__ = 'Flags for annotation items.\n\n.. versionadded:: 3.22\n\n' + '* ``ScaleDependentBoundingBox``: ' + Qgis.AnnotationItemFlag.ScaleDependentBoundingBox.__doc__
# --
Qgis.AnnotationItemFlag.baseClass = Qgis
# monkey patching scoped based enum
Qgis.AnnotationItemNodeType.VertexHandle.__doc__ = "Node is a handle for manipulating vertices"
Qgis.AnnotationItemNodeType.__doc__ = 'Annotation item node types.\n\n.. versionadded:: 3.22\n\n' + '* ``VertexHandle``: ' + Qgis.AnnotationItemNodeType.VertexHandle.__doc__
# --
Qgis.AnnotationItemNodeType.baseClass = Qgis
@@ -112,6 +112,13 @@ Sets the item's z ``index``, which controls the order in which annotation items
are rendered in the layer.

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

virtual QList< QgsAnnotationItemNode > nodes() const;
%Docstring
Returns the nodes for the item, used for editing the item.

.. versionadded:: 3.22
%End

private:
@@ -0,0 +1,87 @@
/************************************************************************
* This file has been generated automatically from *
* *
* src/core/annotations/qgsannotationitemnode.h *
* *
* Do not edit manually ! Edit header and run scripts/sipify.pl again *
************************************************************************/




class QgsAnnotationItemNode
{
%Docstring(signature="appended")
Contains information about a node used for editing an annotation item.

.. versionadded:: 3.22
%End

%TypeHeaderCode
#include "qgsannotationitemnode.h"
%End
public:

QgsAnnotationItemNode();
%Docstring
Default constructor
%End

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

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

QgsPointXY point() const;
%Docstring
Returns the node's position, in geographic coordinates.

The coordinates will match the annotation item's CRS.

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

void setPoint( QgsPointXY point );
%Docstring
Sets the node's position, in geographic coordinates.

The coordinates will match the annotation item's CRS.

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

Qgis::AnnotationItemNodeType type() const;
%Docstring
Returns the node type.

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

void setType( Qgis::AnnotationItemNodeType type );
%Docstring
Sets the node type.

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

bool operator==( const QgsAnnotationItemNode &other ) const;

bool operator!=( const QgsAnnotationItemNode &other ) const;

};

/************************************************************************
* This file has been generated automatically from *
* *
* src/core/annotations/qgsannotationitemnode.h *
* *
* Do not edit manually ! Edit header and run scripts/sipify.pl again *
************************************************************************/
@@ -35,6 +35,8 @@ Constructor for QgsAnnotationLineItem, with the specified ``linestring``.

virtual bool writeXml( QDomElement &element, QDomDocument &document, const QgsReadWriteContext &context ) const;

virtual QList< QgsAnnotationItemNode > nodes() const;


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

virtual Qgis::AnnotationItemFlags flags() const;

virtual QList< QgsAnnotationItemNode > nodes() const;


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

virtual QgsRectangle boundingBox( QgsRenderContext &context ) const;

virtual QList< QgsAnnotationItemNode > nodes() const;


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

virtual bool writeXml( QDomElement &element, QDomDocument &document, const QgsReadWriteContext &context ) const;

virtual QList< QgsAnnotationItemNode > nodes() const;


static QgsAnnotationPolygonItem *create() /Factory/;
%Docstring
@@ -474,6 +474,11 @@ The development version
typedef QFlags<Qgis::AnnotationItemFlag> AnnotationItemFlags;


enum class AnnotationItemNodeType
{
VertexHandle,
};

static const double DEFAULT_SEARCH_RADIUS_MM;

static const float DEFAULT_MAPTOPIXEL_THRESHOLD;
@@ -205,6 +205,7 @@
%Include auto_generated/./3d/qgsabstract3drenderer.sip
%Include auto_generated/annotations/qgsannotation.sip
%Include auto_generated/annotations/qgsannotationitem.sip
%Include auto_generated/annotations/qgsannotationitemnode.sip
%Include auto_generated/annotations/qgsannotationitemregistry.sip
%Include auto_generated/annotations/qgsannotationlayer.sip
%Include auto_generated/annotations/qgsannotationlineitem.sip
@@ -1153,6 +1153,7 @@ set(QGIS_CORE_HDRS

annotations/qgsannotation.h
annotations/qgsannotationitem.h
annotations/qgsannotationitemnode.h
annotations/qgsannotationitemregistry.h
annotations/qgsannotationlayer.h
annotations/qgsannotationlayerrenderer.h
@@ -16,8 +16,14 @@
***************************************************************************/

#include "qgsannotationitem.h"
#include "qgsannotationitemnode.h"

Qgis::AnnotationItemFlags QgsAnnotationItem::flags() const
{
return Qgis::AnnotationItemFlags();
}

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

/**
* \ingroup core
@@ -140,6 +141,13 @@ class CORE_EXPORT QgsAnnotationItem
*/
void setZIndex( int index ) { mZIndex = index; }

/**
* Returns the nodes for the item, used for editing the item.
*
* \since QGIS 3.22
*/
virtual QList< QgsAnnotationItemNode > nodes() const;

private:

int mZIndex = 0;
@@ -0,0 +1,107 @@
/***************************************************************************
qgsannotationitemnode.h
----------------
copyright : (C) 2021 by Nyall Dawson
email : nyall dot dawson at gmail dot com
***************************************************************************/

/***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/

#ifndef QGSANNOTATIONITEMEDITNODE_H
#define QGSANNOTATIONITEMEDITNODE_H

#include "qgis_core.h"
#include "qgis_sip.h"
#include "qgspointxy.h"
#include "qgis.h"

/**
* \ingroup core
* \brief Contains information about a node used for editing an annotation item.
* \since QGIS 3.22
*/
class CORE_EXPORT QgsAnnotationItemNode
{
public:

/**
* Default constructor
*/
QgsAnnotationItemNode() = default;

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

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

/**
* Returns the node's position, in geographic coordinates.
*
* The coordinates will match the annotation item's CRS.
*
* \see setPoint()
*/
QgsPointXY point() const { return mPoint; }

/**
* Sets the node's position, in geographic coordinates.
*
* The coordinates will match the annotation item's CRS.
*
* \see point()
*/
void setPoint( QgsPointXY point ) { mPoint = point; }

/**
* Returns the node type.
*
* \see setType()
*/
Qgis::AnnotationItemNodeType type() const { return mType; }

/**
* Sets the node type.
*
* \see type()
*/
void setType( Qgis::AnnotationItemNodeType type ) { mType = type; }

// TODO c++20 - replace with = default
bool operator==( const QgsAnnotationItemNode &other ) const
{
return mType == other.mType && mPoint == other.mPoint;
}

bool operator!=( const QgsAnnotationItemNode &other ) const
{
return !( *this == other );
}

private:

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

};

#endif // QGSANNOTATIONITEMEDITNODE_H
@@ -19,6 +19,7 @@
#include "qgssymbol.h"
#include "qgssymbollayerutils.h"
#include "qgslinesymbol.h"
#include "qgsannotationitemnode.h"

QgsAnnotationLineItem::QgsAnnotationLineItem( QgsCurve *curve )
: QgsAnnotationItem()
@@ -80,6 +81,16 @@ bool QgsAnnotationLineItem::writeXml( QDomElement &element, QDomDocument &docume
return true;
}

QList<QgsAnnotationItemNode> QgsAnnotationLineItem::nodes() const
{
QList< QgsAnnotationItemNode > res;
for ( auto it = mCurve->vertices_begin(); it != mCurve->vertices_end(); ++it )
{
res.append( QgsAnnotationItemNode( QgsPointXY( ( *it ).x(), ( *it ).y() ), Qgis::AnnotationItemNodeType::VertexHandle ) );
}
return res;
}

QgsAnnotationLineItem *QgsAnnotationLineItem::create()
{
return new QgsAnnotationLineItem( new QgsLineString() );
@@ -43,6 +43,7 @@ class CORE_EXPORT QgsAnnotationLineItem : public QgsAnnotationItem
QString type() const override;
void render( QgsRenderContext &context, QgsFeedback *feedback ) override;
bool writeXml( QDomElement &element, QDomDocument &document, const QgsReadWriteContext &context ) const override;
QList< QgsAnnotationItemNode > nodes() const override;

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

QgsAnnotationMarkerItem::QgsAnnotationMarkerItem( const QgsPoint &point )
: QgsAnnotationItem()
@@ -73,6 +74,11 @@ Qgis::AnnotationItemFlags QgsAnnotationMarkerItem::flags() const
return Qgis::AnnotationItemFlag::ScaleDependentBoundingBox;
}

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

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

/**
* Creates a new marker annotation item.

0 comments on commit 4ff1e55

Please sign in to comment.