QGIS API Documentation  3.18.1-Zürich (202f1bf7e5)
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 
129 void QgsAnnotationPointTextItem::setAlignment( Qt::Alignment alignment )
130 {
131  mAlignment = alignment;
132 }
Abstract base class for annotation items which are drawn with QgsAnnotationLayers.
void setZIndex(int index)
Sets the item's z index, which controls the order in which annotation items are rendered in the layer...
int zIndex() const
Returns the item's z index, which controls the order in which annotation items are rendered in the la...
An annotation item which renders a text string at a point location.
QString type() const override
Returns a unique (untranslated) string identifying the type of item.
QgsTextFormat format() const
Returns the text format used to render the text.
void setFormat(const QgsTextFormat &format)
Sets the text format used to render the text.
~QgsAnnotationPointTextItem() override
Qt::Alignment alignment() const
Returns the text's alignment relative to the reference point().
void setAlignment(Qt::Alignment alignment)
Sets the text's alignment relative to the reference point().
QgsRectangle boundingBox() const override
Returns the bounding box of the item's geographic location, in the parent layer's coordinate referenc...
QgsAnnotationPointTextItem * clone() override
Returns a clone of the item.
static QgsAnnotationPointTextItem * create()
Creates a new text at point annotation item.
bool readXml(const QDomElement &element, const QgsReadWriteContext &context) override
Reads the item's state from the given DOM element.
bool writeXml(QDomElement &element, QDomDocument &document, const QgsReadWriteContext &context) const override
Writes the item's state into an XML element.
void render(QgsRenderContext &context, QgsFeedback *feedback) override
Renders the item to the specified render context.
QgsAnnotationPointTextItem(const QString &text, QgsPointXY point)
Constructor for QgsAnnotationPointTextItem, containing the specified text at the specified point.
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...
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:45
void transformInPlace(double &x, double &y) const
Transforms device coordinates to map coordinates.
A class to represent a 2D point.
Definition: qgspointxy.h:44
double y
Definition: qgspointxy.h:48
Q_GADGET double x
Definition: qgspointxy.h:47
QPointF toQPointF() const
Converts a point to a QPointF.
Definition: qgspointxy.h:154
The class is used as a container of context for various read/write operations on other objects.
A rectangle specified with double values.
Definition: qgsrectangle.h:42
Contains information about the context of a rendering operation.
const QgsMapToPixel & mapToPixel() const
Returns the context's map to pixel transform, which transforms between map coordinates and device coo...
QgsCoordinateTransform coordinateTransform() const
Returns the current coordinate transform for the context.
Container for all settings relating to text rendering.
Definition: qgstextformat.h:41
void readXml(const QDomElement &elem, const QgsReadWriteContext &context)
Read settings from a DOM element.
QDomElement writeXml(QDomDocument &doc, const QgsReadWriteContext &context) const
Write settings into a DOM element.
static HAlignment convertQtHAlignment(Qt::Alignment alignment)
Converts a Qt horizontal alignment flag to a QgsTextRenderer::HAlignment value.
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.
QString qgsDoubleToString(double a, int precision=17)
Returns a string representation of a double.
Definition: qgis.h:276