Skip to content

Commit

Permalink
[api] Add QgsAnnotationPointTextItem annotation item class for
Browse files Browse the repository at this point in the history
rendering text at a specified map point
  • Loading branch information
nyalldawson committed Aug 11, 2020
1 parent e7cfe66 commit 25a78e1
Show file tree
Hide file tree
Showing 12 changed files with 633 additions and 0 deletions.
Expand Up @@ -34,6 +34,10 @@ Abstract base class for annotation items which are drawn with QgsAnnotationLayer
{
sipType = sipType_QgsAnnotationPolygonItem;
}
else if ( sipCpp->type() == QLatin1String( "pointtext" ) )
{
sipType = sipType_QgsAnnotationPointTextItem;
}
else
{
sipType = 0;
Expand Down
@@ -0,0 +1,135 @@
/************************************************************************
* This file has been generated automatically from *
* *
* src/core/annotations/qgsannotationpointtextitem.h *
* *
* Do not edit manually ! Edit header and run scripts/sipify.pl again *
************************************************************************/





class QgsAnnotationPointTextItem : QgsAnnotationItem
{
%Docstring
An annotation item which renders a text string at a point location.

.. versionadded:: 3.16
%End

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

QgsAnnotationPointTextItem( const QString &text, QgsPointXY point );
%Docstring
Constructor for QgsAnnotationPointTextItem, containing the specified ``text`` at the specified ``point``.
%End
~QgsAnnotationPointTextItem();

virtual QString type() const;

virtual void render( QgsRenderContext &context, QgsFeedback *feedback );

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


static QgsAnnotationPointTextItem *create() /Factory/;
%Docstring
Creates a new text at point annotation item.
%End

virtual bool readXml( const QDomElement &element, const QgsReadWriteContext &context );

virtual QgsAnnotationPointTextItem *clone() /Factory/;

virtual QgsRectangle boundingBox() const;


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

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

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

void setPoint( QgsPointXY point );
%Docstring
Sets the ``point`` location of the text.

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

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

QString text() const;
%Docstring
Returns the text rendered by the item.

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

void setText( const QString &text );
%Docstring
Sets the ``text`` rendered by the item.

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

QgsTextFormat format() const;
%Docstring
Returns the text format used to render the text.

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

void setFormat( const QgsTextFormat &format );
%Docstring
Sets the text ``format`` used to render the text.

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

double angle() const;
%Docstring
Returns the text's rotation angle, in degrees clockwise.

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

void setAngle( double angle );
%Docstring
Sets the text's rotation ``angle``, in degrees clockwise.

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

Qt::Alignment alignment() const;
%Docstring
Returns the text's alignment relative to the reference :py:func:`~QgsAnnotationPointTextItem.point`.

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

void setAlignment( Qt::Alignment alignment );
%Docstring
Sets the text's ``alignment`` relative to the reference :py:func:`~QgsAnnotationPointTextItem.point`.

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

private:
QgsAnnotationPointTextItem( const QgsAnnotationPointTextItem &other );
};

/************************************************************************
* This file has been generated automatically from *
* *
* src/core/annotations/qgsannotationpointtextitem.h *
* *
* Do not edit manually ! Edit header and run scripts/sipify.pl again *
************************************************************************/
1 change: 1 addition & 0 deletions python/core/core_auto.sip
Expand Up @@ -261,6 +261,7 @@
%Include auto_generated/annotations/qgsannotationlinestringitem.sip
%Include auto_generated/annotations/qgsannotationmarkeritem.sip
%Include auto_generated/annotations/qgsannotationmanager.sip
%Include auto_generated/annotations/qgsannotationpointtextitem.sip
%Include auto_generated/annotations/qgsannotationpolygonitem.sip
%Include auto_generated/annotations/qgshtmlannotation.sip
%Include auto_generated/annotations/qgssvgannotation.sip
Expand Down
2 changes: 2 additions & 0 deletions src/core/CMakeLists.txt
Expand Up @@ -132,6 +132,7 @@ SET(QGIS_CORE_SRCS
annotations/qgsannotationlinestringitem.cpp
annotations/qgsannotationmarkeritem.cpp
annotations/qgsannotationmanager.cpp
annotations/qgsannotationpointtextitem.cpp
annotations/qgsannotationpolygonitem.cpp
annotations/qgshtmlannotation.cpp
annotations/qgssvgannotation.cpp
Expand Down Expand Up @@ -1059,6 +1060,7 @@ SET(QGIS_CORE_HDRS
annotations/qgsannotationlinestringitem.h
annotations/qgsannotationmarkeritem.h
annotations/qgsannotationmanager.h
annotations/qgsannotationpointtextitem.h
annotations/qgsannotationpolygonitem.h
annotations/qgsannotationregistry.h
annotations/qgshtmlannotation.h
Expand Down
4 changes: 4 additions & 0 deletions src/core/annotations/qgsannotationitem.h
Expand Up @@ -52,6 +52,10 @@ class CORE_EXPORT QgsAnnotationItem
{
sipType = sipType_QgsAnnotationPolygonItem;
}
else if ( sipCpp->type() == QLatin1String( "pointtext" ) )
{
sipType = sipType_QgsAnnotationPointTextItem;
}
else
{
sipType = 0;
Expand Down
3 changes: 3 additions & 0 deletions src/core/annotations/qgsannotationitemregistry.cpp
Expand Up @@ -19,6 +19,7 @@
#include "qgsannotationmarkeritem.h"
#include "qgsannotationlinestringitem.h"
#include "qgsannotationpolygonitem.h"
#include "qgsannotationpointtextitem.h"
#include <QDomElement>

QgsAnnotationItemRegistry::QgsAnnotationItemRegistry( QObject *parent )
Expand All @@ -42,6 +43,8 @@ bool QgsAnnotationItemRegistry::populate()
QgsAnnotationLineStringItem::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" ),
QgsAnnotationPointTextItem::create ) );
return true;
}

Expand Down
132 changes: 132 additions & 0 deletions src/core/annotations/qgsannotationpointtextitem.cpp
@@ -0,0 +1,132 @@
/***************************************************************************
qgsannotationpointtextitem.cpp
----------------
begin : July 2020
copyright : (C) 2020 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. *
* *
***************************************************************************/

#include "qgsannotationpointtextitem.h"
#include "qgstextrenderer.h"

QgsAnnotationPointTextItem::QgsAnnotationPointTextItem( const QString &text, QgsPointXY point )
: QgsAnnotationItem()
, mText( text )
, mPoint( point )
{

}

QgsAnnotationPointTextItem::~QgsAnnotationPointTextItem() = default;

QString QgsAnnotationPointTextItem::type() const
{
return QStringLiteral( "pointtext" );
}

void QgsAnnotationPointTextItem::render( QgsRenderContext &context, QgsFeedback * )
{
QPointF pt;
if ( context.coordinateTransform().isValid() )
{
double x = mPoint.x();
double y = mPoint.y();
double z = 0.0;
context.coordinateTransform().transformInPlace( x, y, z );
pt = QPointF( x, y );
}
else
pt = mPoint.toQPointF();

context.mapToPixel().transformInPlace( pt.rx(), pt.ry() );

QgsTextRenderer::drawText( pt, mAngle * M_PI / 180.0,
QgsTextRenderer::convertQtHAlignment( mAlignment ),
mText.split( '\n' ), context, mTextFormat );
}

bool QgsAnnotationPointTextItem::writeXml( QDomElement &element, QDomDocument &document, const QgsReadWriteContext &context ) const
{
element.setAttribute( QStringLiteral( "x" ), qgsDoubleToString( mPoint.x() ) );
element.setAttribute( QStringLiteral( "y" ), qgsDoubleToString( mPoint.y() ) );
element.setAttribute( QStringLiteral( "text" ), mText );
element.setAttribute( QStringLiteral( "zIndex" ), zIndex() );
element.setAttribute( QStringLiteral( "angle" ), qgsDoubleToString( mAngle ) );
element.setAttribute( QStringLiteral( "alignment" ), QString::number( mAlignment ) );

QDomElement textFormatElem = document.createElement( QStringLiteral( "pointTextFormat" ) );
textFormatElem.appendChild( mTextFormat.writeXml( document, context ) );
element.appendChild( textFormatElem );

return true;
}

QgsAnnotationPointTextItem *QgsAnnotationPointTextItem::create()
{
return new QgsAnnotationPointTextItem( QString(), QgsPointXY() );
}

bool QgsAnnotationPointTextItem::readXml( const QDomElement &element, const QgsReadWriteContext &context )
{
const double x = element.attribute( QStringLiteral( "x" ) ).toDouble();
const double y = element.attribute( QStringLiteral( "y" ) ).toDouble();
mPoint = QgsPointXY( x, y );
mText = element.attribute( QStringLiteral( "text" ) );
mAngle = element.attribute( QStringLiteral( "angle" ) ).toDouble();
mAlignment = static_cast< Qt::Alignment >( element.attribute( QStringLiteral( "alignment" ) ).toInt() );
setZIndex( element.attribute( QStringLiteral( "zIndex" ) ).toInt() );

const QDomElement textFormatElem = element.firstChildElement( QStringLiteral( "pointTextFormat" ) );
if ( !textFormatElem.isNull() )
{
QDomNodeList textFormatNodeList = textFormatElem.elementsByTagName( QStringLiteral( "text-style" ) );
QDomElement textFormatElem = textFormatNodeList.at( 0 ).toElement();
mTextFormat.readXml( textFormatElem, context );
}

return true;
}

QgsAnnotationPointTextItem *QgsAnnotationPointTextItem::clone()
{
std::unique_ptr< QgsAnnotationPointTextItem > item = qgis::make_unique< QgsAnnotationPointTextItem >( mText, mPoint );
item->setFormat( mTextFormat );
item->setAngle( mAngle );
item->setAlignment( mAlignment );
item->setZIndex( zIndex() );
return item.release();
}

QgsRectangle QgsAnnotationPointTextItem::boundingBox() const
{
return QgsRectangle( mPoint.x(), mPoint.y(), mPoint.x(), mPoint.y() );
}

QgsTextFormat QgsAnnotationPointTextItem::format() const
{
return mTextFormat;
}

void QgsAnnotationPointTextItem::setFormat( const QgsTextFormat &format )
{
mTextFormat = format;
}

Qt::Alignment QgsAnnotationPointTextItem::alignment() const
{
return mAlignment;
}

void QgsAnnotationPointTextItem::setAlignment( Qt::Alignment alignment )
{
mAlignment = alignment;
}

0 comments on commit 25a78e1

Please sign in to comment.