QGIS API Documentation  3.17.0-Master (a84647cf30)
qgsannotationpointtextitem.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  qgsannotationpointtextitem.cpp
3  ----------------
4  begin : August 2020
5  copyright : (C) 2020 by Nyall Dawson
6  email : nyall dot dawson at gmail dot com
7  ***************************************************************************/
8 
9 /***************************************************************************
10  * *
11  * This program is free software; you can redistribute it and/or modify *
12  * it under the terms of the GNU General Public License as published by *
13  * the Free Software Foundation; either version 2 of the License, or *
14  * (at your option) any later version. *
15  * *
16  ***************************************************************************/
17 
19 #include "qgstextrenderer.h"
20 
23  , mText( text )
24  , mPoint( point )
25 {
26 
27 }
28 
30 
32 {
33  return QStringLiteral( "pointtext" );
34 }
35 
37 {
38  QPointF pt;
39  if ( context.coordinateTransform().isValid() )
40  {
41  double x = mPoint.x();
42  double y = mPoint.y();
43  double z = 0.0;
44  context.coordinateTransform().transformInPlace( x, y, z );
45  pt = QPointF( x, y );
46  }
47  else
48  pt = mPoint.toQPointF();
49 
50  context.mapToPixel().transformInPlace( pt.rx(), pt.ry() );
51 
52  QgsTextRenderer::drawText( pt, mAngle * M_PI / 180.0,
54  mText.split( '\n' ), context, mTextFormat );
55 }
56 
57 bool QgsAnnotationPointTextItem::writeXml( QDomElement &element, QDomDocument &document, const QgsReadWriteContext &context ) const
58 {
59  element.setAttribute( QStringLiteral( "x" ), qgsDoubleToString( mPoint.x() ) );
60  element.setAttribute( QStringLiteral( "y" ), qgsDoubleToString( mPoint.y() ) );
61  element.setAttribute( QStringLiteral( "text" ), mText );
62  element.setAttribute( QStringLiteral( "zIndex" ), zIndex() );
63  element.setAttribute( QStringLiteral( "angle" ), qgsDoubleToString( mAngle ) );
64  element.setAttribute( QStringLiteral( "alignment" ), QString::number( mAlignment ) );
65 
66  QDomElement textFormatElem = document.createElement( QStringLiteral( "pointTextFormat" ) );
67  textFormatElem.appendChild( mTextFormat.writeXml( document, context ) );
68  element.appendChild( textFormatElem );
69 
70  return true;
71 }
72 
74 {
75  return new QgsAnnotationPointTextItem( QString(), QgsPointXY() );
76 }
77 
78 bool QgsAnnotationPointTextItem::readXml( const QDomElement &element, const QgsReadWriteContext &context )
79 {
80  const double x = element.attribute( QStringLiteral( "x" ) ).toDouble();
81  const double y = element.attribute( QStringLiteral( "y" ) ).toDouble();
82  mPoint = QgsPointXY( x, y );
83  mText = element.attribute( QStringLiteral( "text" ) );
84  mAngle = element.attribute( QStringLiteral( "angle" ) ).toDouble();
85  mAlignment = static_cast< Qt::Alignment >( element.attribute( QStringLiteral( "alignment" ) ).toInt() );
86  setZIndex( element.attribute( QStringLiteral( "zIndex" ) ).toInt() );
87 
88  const QDomElement textFormatElem = element.firstChildElement( QStringLiteral( "pointTextFormat" ) );
89  if ( !textFormatElem.isNull() )
90  {
91  QDomNodeList textFormatNodeList = textFormatElem.elementsByTagName( QStringLiteral( "text-style" ) );
92  QDomElement textFormatElem = textFormatNodeList.at( 0 ).toElement();
93  mTextFormat.readXml( textFormatElem, context );
94  }
95 
96  return true;
97 }
98 
100 {
101  std::unique_ptr< QgsAnnotationPointTextItem > item = qgis::make_unique< QgsAnnotationPointTextItem >( mText, mPoint );
102  item->setFormat( mTextFormat );
103  item->setAngle( mAngle );
104  item->setAlignment( mAlignment );
105  item->setZIndex( zIndex() );
106  return item.release();
107 }
108 
110 {
111  return QgsRectangle( mPoint.x(), mPoint.y(), mPoint.x(), mPoint.y() );
112 }
113 
115 {
116  return mTextFormat;
117 }
118 
120 {
121  mTextFormat = format;
122 }
123 
125 {
126  return mAlignment;
127 }
128 
130 {
131  mAlignment = alignment;
132 }
The class is used as a container of context for various read/write operations on other objects...
static void drawText(const QRectF &rect, double rotation, HAlignment alignment, const QStringList &textLines, QgsRenderContext &context, const QgsTextFormat &format, bool drawAsOutlines=true, VAlignment vAlignment=AlignTop)
Draws text within a rectangle using the specified settings.
A rectangle specified with double values.
Definition: qgsrectangle.h:41
~QgsAnnotationPointTextItem() override
void render(QgsRenderContext &context, QgsFeedback *feedback) override
Renders the item to the specified render context.
bool readXml(const QDomElement &element, const QgsReadWriteContext &context) override
Reads the item&#39;s state from the given DOM element.
void readXml(const QDomElement &elem, const QgsReadWriteContext &context)
Read settings from a DOM element.
void setZIndex(int index)
Sets the item&#39;s z index, which controls the order in which annotation items are rendered in the layer...
bool writeXml(QDomElement &element, QDomDocument &document, const QgsReadWriteContext &context) const override
Writes the item&#39;s state into an XML element.
QgsTextFormat format() const
Returns the text format used to render the text.
double y
Definition: qgspointxy.h:48
A class to represent a 2D point.
Definition: qgspointxy.h:43
QString type() const override
Returns a unique (untranslated) string identifying the type of item.
QgsRectangle boundingBox() const override
Returns the bounding box of the item&#39;s geographic location, in the parent layer&#39;s coordinate referenc...
bool isValid() const
Returns true if the coordinate transform is valid, ie both the source and destination CRS have been s...
Base class for feedback objects to be used for cancellation of something running in a worker thread...
Definition: qgsfeedback.h:43
void transformInPlace(double &x, double &y) const
Transform device coordinates to map coordinates.
QPointF toQPointF() const
Converts a point to a QPointF.
Definition: qgspointxy.h:154
QDomElement writeXml(QDomDocument &doc, const QgsReadWriteContext &context) const
Write settings into a DOM element.
QString qgsDoubleToString(double a, int precision=17)
Returns a string representation of a double.
Definition: qgis.h:276
An annotation item which renders a text string at a point location.
void setAlignment(Qt::Alignment alignment)
Sets the text&#39;s alignment relative to the reference point().
QgsCoordinateTransform coordinateTransform() const
Returns the current coordinate transform for the context.
double x
Definition: qgspointxy.h:47
int zIndex() const
Returns the item&#39;s z index, which controls the order in which annotation items are rendered in the la...
void transformInPlace(double &x, double &y, double &z, TransformDirection direction=ForwardTransform) const SIP_THROW(QgsCsException)
Transforms an array of x, y and z double coordinates in place, from the source CRS to the destination...
Abstract base class for annotation items which are drawn with QgsAnnotationLayers.
QgsAnnotationPointTextItem(const QString &text, QgsPointXY point)
Constructor for QgsAnnotationPointTextItem, containing the specified text at the specified point...
static HAlignment convertQtHAlignment(Qt::Alignment alignment)
Converts a Qt horizontal alignment flag to a QgsTextRenderer::HAlignment value.
Contains information about the context of a rendering operation.
const QgsMapToPixel & mapToPixel() const
Returns the context&#39;s map to pixel transform, which transforms between map coordinates and device coo...
Container for all settings relating to text rendering.
Definition: qgstextformat.h:39
void setFormat(const QgsTextFormat &format)
Sets the text format used to render the text.
QgsAnnotationPointTextItem * clone() override
Returns a clone of the item.
Qt::Alignment alignment() const
Returns the text&#39;s alignment relative to the reference point().
static QgsAnnotationPointTextItem * create()
Creates a new text at point annotation item.