Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[api] Add QgsAnnotationPointTextItem annotation item class for
rendering text at a specified map point
- Loading branch information
1 parent
e7cfe66
commit 25a78e1
Showing
12 changed files
with
633 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
135 changes: 135 additions & 0 deletions
135
python/core/auto_generated/annotations/qgsannotationpointtextitem.sip.in
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 * | ||
************************************************************************/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
} |
Oops, something went wrong.