QGIS API Documentation  3.16.0-Hannover (43b64b13f3)
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 }
QgsPointXY::y
double y
Definition: qgspointxy.h:48
QgsRenderContext::mapToPixel
const QgsMapToPixel & mapToPixel() const
Returns the context's map to pixel transform, which transforms between map coordinates and device coo...
Definition: qgsrendercontext.h:325
QgsTextRenderer::drawText
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.
Definition: qgstextrenderer.cpp:75
QgsReadWriteContext
The class is used as a container of context for various read/write operations on other objects.
Definition: qgsreadwritecontext.h:35
qgstextrenderer.h
QgsAnnotationPointTextItem::boundingBox
QgsRectangle boundingBox() const override
Returns the bounding box of the item's geographic location, in the parent layer's coordinate referenc...
Definition: qgsannotationpointtextitem.cpp:109
QgsPointXY::x
Q_GADGET double x
Definition: qgspointxy.h:47
QgsRenderContext
Contains information about the context of a rendering operation.
Definition: qgsrendercontext.h:58
QgsPointXY::toQPointF
QPointF toQPointF() const
Converts a point to a QPointF.
Definition: qgspointxy.h:154
QgsCoordinateTransform::isValid
bool isValid() const
Returns true if the coordinate transform is valid, ie both the source and destination CRS have been s...
Definition: qgscoordinatetransform.cpp:892
QgsRectangle
A rectangle specified with double values.
Definition: qgsrectangle.h:42
qgsDoubleToString
QString qgsDoubleToString(double a, int precision=17)
Returns a string representation of a double.
Definition: qgis.h:275
QgsAnnotationPointTextItem::clone
QgsAnnotationPointTextItem * clone() override
Returns a clone of the item.
Definition: qgsannotationpointtextitem.cpp:99
QgsAnnotationPointTextItem::~QgsAnnotationPointTextItem
~QgsAnnotationPointTextItem() override
QgsRenderContext::coordinateTransform
QgsCoordinateTransform coordinateTransform() const
Returns the current coordinate transform for the context.
Definition: qgsrendercontext.h:245
QgsTextFormat
Container for all settings relating to text rendering.
Definition: qgstextformat.h:40
QgsAnnotationPointTextItem::writeXml
bool writeXml(QDomElement &element, QDomDocument &document, const QgsReadWriteContext &context) const override
Writes the item's state into an XML element.
Definition: qgsannotationpointtextitem.cpp:57
QgsFeedback
Base class for feedback objects to be used for cancellation of something running in a worker thread.
Definition: qgsfeedback.h:44
QgsAnnotationPointTextItem::type
QString type() const override
Returns a unique (untranslated) string identifying the type of item.
Definition: qgsannotationpointtextitem.cpp:31
QgsTextFormat::readXml
void readXml(const QDomElement &elem, const QgsReadWriteContext &context)
Read settings from a DOM element.
Definition: qgstextformat.cpp:406
QgsAnnotationPointTextItem::QgsAnnotationPointTextItem
QgsAnnotationPointTextItem(const QString &text, QgsPointXY point)
Constructor for QgsAnnotationPointTextItem, containing the specified text at the specified point.
Definition: qgsannotationpointtextitem.cpp:21
QgsAnnotationPointTextItem::readXml
bool readXml(const QDomElement &element, const QgsReadWriteContext &context) override
Reads the item's state from the given DOM element.
Definition: qgsannotationpointtextitem.cpp:78
QgsPointXY
A class to represent a 2D point.
Definition: qgspointxy.h:44
QgsAnnotationPointTextItem::setAlignment
void setAlignment(Qt::Alignment alignment)
Sets the text's alignment relative to the reference point().
Definition: qgsannotationpointtextitem.cpp:129
QgsAnnotationPointTextItem::create
static QgsAnnotationPointTextItem * create()
Creates a new text at point annotation item.
Definition: qgsannotationpointtextitem.cpp:73
QgsAnnotationItem::zIndex
int zIndex() const
Returns the item's z index, which controls the order in which annotation items are rendered in the la...
Definition: qgsannotationitem.h:121
QgsMapToPixel::transformInPlace
void transformInPlace(double &x, double &y) const
Transform device coordinates to map coordinates.
Definition: qgsmaptopixel.cpp:233
qgsannotationpointtextitem.h
QgsAnnotationPointTextItem::setFormat
void setFormat(const QgsTextFormat &format)
Sets the text format used to render the text.
Definition: qgsannotationpointtextitem.cpp:119
QgsAnnotationPointTextItem::format
QgsTextFormat format() const
Returns the text format used to render the text.
Definition: qgsannotationpointtextitem.cpp:114
QgsTextRenderer::convertQtHAlignment
static HAlignment convertQtHAlignment(Qt::Alignment alignment)
Converts a Qt horizontal alignment flag to a QgsTextRenderer::HAlignment value.
Definition: qgstextrenderer.cpp:40
QgsAnnotationPointTextItem::render
void render(QgsRenderContext &context, QgsFeedback *feedback) override
Renders the item to the specified render context.
Definition: qgsannotationpointtextitem.cpp:36
QgsTextFormat::writeXml
QDomElement writeXml(QDomDocument &doc, const QgsReadWriteContext &context) const
Write settings into a DOM element.
Definition: qgstextformat.cpp:555
QgsAnnotationItem::setZIndex
void setZIndex(int index)
Sets the item's z index, which controls the order in which annotation items are rendered in the layer...
Definition: qgsannotationitem.h:129
QgsAnnotationItem
Abstract base class for annotation items which are drawn with QgsAnnotationLayers.
Definition: qgsannotationitem.h:39
QgsAnnotationPointTextItem::alignment
Qt::Alignment alignment() const
Returns the text's alignment relative to the reference point().
Definition: qgsannotationpointtextitem.cpp:124
QgsAnnotationPointTextItem
An annotation item which renders a text string at a point location.
Definition: qgsannotationpointtextitem.h:34
QgsCoordinateTransform::transformInPlace
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...
Definition: qgscoordinatetransform.cpp:313