Skip to content
Permalink
Browse files

[annotations] Refactor annotation item subclasses so that they

work correctly with curved geometry types, and tweak some API naming
accordingly

There's no need to be restrictive and only require linear geometries
for these items!
  • Loading branch information
nyalldawson committed Aug 19, 2020
1 parent 2b6be63 commit 5a7cb166d00bec91d4ade6451fbbe65c32a910d7
@@ -28,7 +28,7 @@ Abstract base class for annotation items which are drawn with QgsAnnotationLayer
}
else if ( sipCpp->type() == QLatin1String( "linestring" ) )
{
sipType = sipType_QgsAnnotationLineStringItem;
sipType = sipType_QgsAnnotationLineItem;
}
else if ( sipCpp->type() == QLatin1String( "polygon" ) )
{
@@ -1,7 +1,7 @@
/************************************************************************
* This file has been generated automatically from *
* *
* src/core/annotations/qgsannotationlinestringitem.h *
* src/core/annotations/qgsannotationlineitem.h *
* *
* Do not edit manually ! Edit header and run scripts/sipify.pl again *
************************************************************************/
@@ -10,24 +10,24 @@



class QgsAnnotationLineStringItem : QgsAnnotationItem
class QgsAnnotationLineItem : QgsAnnotationItem
{
%Docstring
An annotation item which renders a line symbol along a linestring geometry.
An annotation item which renders a line symbol along a line geometry.

.. versionadded:: 3.16
%End

%TypeHeaderCode
#include "qgsannotationlinestringitem.h"
#include "qgsannotationlineitem.h"
%End
public:

QgsAnnotationLineStringItem( const QgsLineString &linestring );
QgsAnnotationLineItem( QgsCurve *curve /Transfer/ );
%Docstring
Constructor for QgsAnnotationLineStringItem, with the specified ``linestring``.
Constructor for QgsAnnotationLineItem, with the specified ``linestring``.
%End
~QgsAnnotationLineStringItem();
~QgsAnnotationLineItem();

virtual QString type() const;

@@ -36,7 +36,7 @@ Constructor for QgsAnnotationLineStringItem, with the specified ``linestring``.
virtual bool writeXml( QDomElement &element, QDomDocument &document, const QgsReadWriteContext &context ) const;


static QgsAnnotationLineStringItem *create() /Factory/;
static QgsAnnotationLineItem *create() /Factory/;
%Docstring
Creates a new linestring annotation item.
%End
@@ -46,25 +46,25 @@ Creates a new linestring annotation item.
virtual QgsRectangle boundingBox() const;


virtual QgsAnnotationLineStringItem *clone() /Factory/;
virtual QgsAnnotationLineItem *clone() /Factory/;


QgsLineString lineString() const;
const QgsCurve *geometry() const;
%Docstring
Returns the line string geometry of the item.
Returns the geometry of the item.

The coordinate reference system for the line will be the parent layer's :py:func:`QgsAnnotationLayer.crs()`.

.. seealso:: :py:func:`setLineString`
.. seealso:: :py:func:`setGeometry`
%End

void setLineString( const QgsLineString &lineString );
void setGeometry( QgsCurve *geometry /Transfer/ );
%Docstring
Sets the ``lineString`` geometry of the item.
Sets the ``geometry`` of the item. Ownership of ``geometry`` is transferred.

The coordinate reference system for the line will be the parent layer's :py:func:`QgsAnnotationLayer.crs()`.

.. seealso:: :py:func:`lineString`
.. seealso:: :py:func:`geometry`
%End

const QgsLineSymbol *symbol() const;
@@ -84,13 +84,13 @@ The item takes ownership of the symbol.
%End

private:
QgsAnnotationLineStringItem( const QgsAnnotationLineStringItem &other );
QgsAnnotationLineItem( const QgsAnnotationLineItem &other );
};

/************************************************************************
* This file has been generated automatically from *
* *
* src/core/annotations/qgsannotationlinestringitem.h *
* src/core/annotations/qgsannotationlineitem.h *
* *
* Do not edit manually ! Edit header and run scripts/sipify.pl again *
************************************************************************/
@@ -23,7 +23,7 @@ An annotation item which renders a marker symbol at a point location.
%End
public:

QgsAnnotationMarkerItem( QgsPointXY point );
QgsAnnotationMarkerItem( const QgsPoint &point );
%Docstring
Constructor for QgsAnnotationMarkerItem, at the specified ``point``.
%End
@@ -48,22 +48,22 @@ Creates a new marker annotation item.
virtual QgsRectangle boundingBox() const;


QgsPointXY point() const;
QgsPointXY geometry() const;
%Docstring
Returns the point location of the marker.
Returns the point geometry of the marker.

The coordinate reference system for the point will be the parent layer's :py:func:`QgsAnnotationLayer.crs()`.

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

void setPoint( QgsPointXY point );
void setGeometry( const QgsPoint &geometry );
%Docstring
Sets the ``point`` location of the marker.
Sets the point ``geometry`` location of the marker.

The coordinate reference system for the point will be the parent layer's :py:func:`QgsAnnotationLayer.crs()`.

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

const QgsMarkerSymbol *symbol() const;
@@ -23,7 +23,7 @@ An annotation item which renders a fill symbol for a polygon geometry.
%End
public:

QgsAnnotationPolygonItem( const QgsPolygon &polygon );
QgsAnnotationPolygonItem( QgsCurvePolygon *polygon /Transfer/ );
%Docstring
Constructor for QgsAnnotationPolygonItem, with the specified ``polygon`` geometry.
%End
@@ -48,22 +48,22 @@ Creates a new polygon annotation item.
virtual QgsRectangle boundingBox() const;


QgsPolygon polygon() const;
const QgsCurvePolygon *geometry() const;
%Docstring
Returns the polygon geometry of the item.
Returns the geometry of the item.

The coordinate reference system for the polygon will be the parent layer's :py:func:`QgsAnnotationLayer.crs()`.

.. seealso:: :py:func:`setPolygon`
.. seealso:: :py:func:`setGeometry`
%End

void setPolygon( const QgsPolygon &polygon );
void setGeometry( QgsCurvePolygon *geometry /Transfer/ );
%Docstring
Sets the ``polygon`` geometry of the item.
Sets the ``geometry`` of the item.

The coordinate reference system for the polygon will be the parent layer's :py:func:`QgsAnnotationLayer.crs()`.

.. seealso:: :py:func:`polygon`
.. seealso:: :py:func:`geometry`
%End

const QgsFillSymbol *symbol() const;
@@ -258,7 +258,7 @@
%Include auto_generated/annotations/qgsannotationitem.sip
%Include auto_generated/annotations/qgsannotationitemregistry.sip
%Include auto_generated/annotations/qgsannotationlayer.sip
%Include auto_generated/annotations/qgsannotationlinestringitem.sip
%Include auto_generated/annotations/qgsannotationlineitem.sip
%Include auto_generated/annotations/qgsannotationmarkeritem.sip
%Include auto_generated/annotations/qgsannotationmanager.sip
%Include auto_generated/annotations/qgsannotationpointtextitem.sip
@@ -128,7 +128,7 @@ SET(QGIS_CORE_SRCS
annotations/qgsannotationitemregistry.cpp
annotations/qgsannotationlayer.cpp
annotations/qgsannotationlayerrenderer.cpp
annotations/qgsannotationlinestringitem.cpp
annotations/qgsannotationlineitem.cpp
annotations/qgsannotationmarkeritem.cpp
annotations/qgsannotationmanager.cpp
annotations/qgsannotationpointtextitem.cpp
@@ -1057,7 +1057,7 @@ SET(QGIS_CORE_HDRS
annotations/qgsannotationitemregistry.h
annotations/qgsannotationlayer.h
annotations/qgsannotationlayerrenderer.h
annotations/qgsannotationlinestringitem.h
annotations/qgsannotationlineitem.h
annotations/qgsannotationmarkeritem.h
annotations/qgsannotationmanager.h
annotations/qgsannotationpointtextitem.h
@@ -46,7 +46,7 @@ class CORE_EXPORT QgsAnnotationItem
}
else if ( sipCpp->type() == QLatin1String( "linestring" ) )
{
sipType = sipType_QgsAnnotationLineStringItem;
sipType = sipType_QgsAnnotationLineItem;
}
else if ( sipCpp->type() == QLatin1String( "polygon" ) )
{
@@ -17,7 +17,7 @@
#include "qgsannotationitemregistry.h"
#include "qgsannotationitem.h"
#include "qgsannotationmarkeritem.h"
#include "qgsannotationlinestringitem.h"
#include "qgsannotationlineitem.h"
#include "qgsannotationpolygonitem.h"
#include "qgsannotationpointtextitem.h"
#include <QDomElement>
@@ -40,7 +40,7 @@ bool QgsAnnotationItemRegistry::populate()
mMetadata.insert( QStringLiteral( "marker" ), new QgsAnnotationItemMetadata( QStringLiteral( "marker" ), QObject::tr( "Marker" ), QObject::tr( "Markers" ),
QgsAnnotationMarkerItem::create ) );
mMetadata.insert( QStringLiteral( "linestring" ), new QgsAnnotationItemMetadata( QStringLiteral( "linestring" ), QObject::tr( "Polyline" ), QObject::tr( "Polylines" ),
QgsAnnotationLineStringItem::create ) );
QgsAnnotationLineItem::create ) );
mMetadata.insert( QStringLiteral( "polygon" ), new QgsAnnotationItemMetadata( QStringLiteral( "polygon" ), QObject::tr( "Polygon" ), QObject::tr( "Polygons" ),
QgsAnnotationPolygonItem::create ) );
mMetadata.insert( QStringLiteral( "pointtext" ), new QgsAnnotationItemMetadata( QStringLiteral( "pointtext" ), QObject::tr( "Text at point" ), QObject::tr( "Text at points" ),
@@ -1,5 +1,5 @@
/***************************************************************************
qgsannotationlinestringitem.cpp
qgsannotationlineitem.cpp
----------------
begin : July 2020
copyright : (C) 2020 by Nyall Dawson
@@ -15,28 +15,28 @@
* *
***************************************************************************/

#include "qgsannotationlinestringitem.h"
#include "qgsannotationlineitem.h"
#include "qgssymbol.h"
#include "qgssymbollayerutils.h"

QgsAnnotationLineStringItem::QgsAnnotationLineStringItem( const QgsLineString &linestring )
QgsAnnotationLineItem::QgsAnnotationLineItem( QgsCurve *curve )
: QgsAnnotationItem()
, mLineString( linestring )
, mCurve( curve )
, mSymbol( qgis::make_unique< QgsLineSymbol >() )
{

}

QgsAnnotationLineStringItem::~QgsAnnotationLineStringItem() = default;
QgsAnnotationLineItem::~QgsAnnotationLineItem() = default;

QString QgsAnnotationLineStringItem::type() const
QString QgsAnnotationLineItem::type() const
{
return QStringLiteral( "linestring" );
}

void QgsAnnotationLineStringItem::render( QgsRenderContext &context, QgsFeedback * )
void QgsAnnotationLineItem::render( QgsRenderContext &context, QgsFeedback * )
{
QPolygonF pts = mLineString.asQPolygonF();
QPolygonF pts = mCurve->asQPolygonF();

//transform the QPolygonF to screen coordinates
if ( context.coordinateTransform().isValid() )
@@ -69,26 +69,27 @@ void QgsAnnotationLineStringItem::render( QgsRenderContext &context, QgsFeedback
mSymbol->stopRender( context );
}

bool QgsAnnotationLineStringItem::writeXml( QDomElement &element, QDomDocument &document, const QgsReadWriteContext &context ) const
bool QgsAnnotationLineItem::writeXml( QDomElement &element, QDomDocument &document, const QgsReadWriteContext &context ) const
{
element.setAttribute( QStringLiteral( "wkt" ), mLineString.asWkt() );
element.setAttribute( QStringLiteral( "wkt" ), mCurve->asWkt() );
element.setAttribute( QStringLiteral( "zIndex" ), zIndex() );

element.appendChild( QgsSymbolLayerUtils::saveSymbol( QStringLiteral( "lineSymbol" ), mSymbol.get(), document, context ) );

return true;
}

QgsAnnotationLineStringItem *QgsAnnotationLineStringItem::create()
QgsAnnotationLineItem *QgsAnnotationLineItem::create()
{
return new QgsAnnotationLineStringItem( QgsLineString() );
return new QgsAnnotationLineItem( new QgsLineString() );
}

bool QgsAnnotationLineStringItem::readXml( const QDomElement &element, const QgsReadWriteContext &context )
bool QgsAnnotationLineItem::readXml( const QDomElement &element, const QgsReadWriteContext &context )
{
const QString wkt = element.attribute( QStringLiteral( "wkt" ) );
QgsLineString ls;
mLineString.fromWkt( wkt );
const QgsGeometry geometry = QgsGeometry::fromWkt( wkt );
if ( const QgsCurve *curve = qgsgeometry_cast< const QgsCurve * >( geometry.constGet() ) )
mCurve.reset( curve->clone() );

setZIndex( element.attribute( QStringLiteral( "zIndex" ) ).toInt() );

@@ -99,25 +100,25 @@ bool QgsAnnotationLineStringItem::readXml( const QDomElement &element, const Qgs
return true;
}

QgsRectangle QgsAnnotationLineStringItem::boundingBox() const
QgsRectangle QgsAnnotationLineItem::boundingBox() const
{
return mLineString.boundingBox();
return mCurve->boundingBox();
}

QgsAnnotationLineStringItem *QgsAnnotationLineStringItem::clone()
QgsAnnotationLineItem *QgsAnnotationLineItem::clone()
{
std::unique_ptr< QgsAnnotationLineStringItem > item = qgis::make_unique< QgsAnnotationLineStringItem >( mLineString );
std::unique_ptr< QgsAnnotationLineItem > item = qgis::make_unique< QgsAnnotationLineItem >( mCurve->clone() );
item->setSymbol( mSymbol->clone() );
item->setZIndex( zIndex() );
return item.release();
}

const QgsLineSymbol *QgsAnnotationLineStringItem::symbol() const
const QgsLineSymbol *QgsAnnotationLineItem::symbol() const
{
return mSymbol.get();
}

void QgsAnnotationLineStringItem::setSymbol( QgsLineSymbol *symbol )
void QgsAnnotationLineItem::setSymbol( QgsLineSymbol *symbol )
{
mSymbol.reset( symbol );
}

0 comments on commit 5a7cb16

Please sign in to comment.
You can’t perform that action at this time.