QGIS API Documentation  3.20.0-Odense (decaadbb31)
qgsannotationlineitem.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  qgsannotationlineitem.cpp
3  ----------------
4  begin : July 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 
18 #include "qgsannotationlineitem.h"
19 #include "qgssymbol.h"
20 #include "qgssymbollayerutils.h"
21 #include "qgslinesymbol.h"
22 
25  , mCurve( curve )
26  , mSymbol( std::make_unique< QgsLineSymbol >() )
27 {
28 
29 }
30 
32 
34 {
35  return QStringLiteral( "linestring" );
36 }
37 
39 {
40  QPolygonF pts = mCurve->asQPolygonF();
41 
42  //transform the QPolygonF to screen coordinates
43  if ( context.coordinateTransform().isValid() )
44  {
45  try
46  {
47  context.coordinateTransform().transformPolygon( pts );
48  }
49  catch ( QgsCsException & )
50  {
51  // we don't abort the rendering here, instead we remove any invalid points and just plot those which ARE valid
52  }
53  }
54 
55  // remove non-finite points, e.g. infinite or NaN points caused by reprojecting errors
56  pts.erase( std::remove_if( pts.begin(), pts.end(),
57  []( const QPointF point )
58  {
59  return !std::isfinite( point.x() ) || !std::isfinite( point.y() );
60  } ), pts.end() );
61 
62  QPointF *ptr = pts.data();
63  for ( int i = 0; i < pts.size(); ++i, ++ptr )
64  {
65  context.mapToPixel().transformInPlace( ptr->rx(), ptr->ry() );
66  }
67 
68  mSymbol->startRender( context );
69  mSymbol->renderPolyline( pts, nullptr, context );
70  mSymbol->stopRender( context );
71 }
72 
73 bool QgsAnnotationLineItem::writeXml( QDomElement &element, QDomDocument &document, const QgsReadWriteContext &context ) const
74 {
75  element.setAttribute( QStringLiteral( "wkt" ), mCurve->asWkt() );
76  element.setAttribute( QStringLiteral( "zIndex" ), zIndex() );
77 
78  element.appendChild( QgsSymbolLayerUtils::saveSymbol( QStringLiteral( "lineSymbol" ), mSymbol.get(), document, context ) );
79 
80  return true;
81 }
82 
84 {
85  return new QgsAnnotationLineItem( new QgsLineString() );
86 }
87 
88 bool QgsAnnotationLineItem::readXml( const QDomElement &element, const QgsReadWriteContext &context )
89 {
90  const QString wkt = element.attribute( QStringLiteral( "wkt" ) );
92  if ( const QgsCurve *curve = qgsgeometry_cast< const QgsCurve * >( geometry.constGet() ) )
93  mCurve.reset( curve->clone() );
94 
95  setZIndex( element.attribute( QStringLiteral( "zIndex" ) ).toInt() );
96 
97  const QDomElement symbolElem = element.firstChildElement( QStringLiteral( "symbol" ) );
98  if ( !symbolElem.isNull() )
99  setSymbol( QgsSymbolLayerUtils::loadSymbol< QgsLineSymbol >( symbolElem, context ) );
100 
101  return true;
102 }
103 
105 {
106  return mCurve->boundingBox();
107 }
108 
110 {
111  std::unique_ptr< QgsAnnotationLineItem > item = std::make_unique< QgsAnnotationLineItem >( mCurve->clone() );
112  item->setSymbol( mSymbol->clone() );
113  item->setZIndex( zIndex() );
114  return item.release();
115 }
116 
118 {
119  return mSymbol.get();
120 }
121 
123 {
124  mSymbol.reset( symbol );
125 }
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 line symbol along a line geometry.
QgsRectangle boundingBox() const override
Returns the bounding box of the item's geographic location, in the parent layer's coordinate referenc...
~QgsAnnotationLineItem() override
QgsAnnotationLineItem(QgsCurve *curve)
Constructor for QgsAnnotationLineItem, with the specified linestring.
void setSymbol(QgsLineSymbol *symbol)
Sets the symbol used to render the marker item.
QString type() const override
Returns a unique (untranslated) string identifying the type of 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.
const QgsCurve * geometry() const
Returns the geometry of the item.
void render(QgsRenderContext &context, QgsFeedback *feedback) override
Renders the item to the specified render context.
const QgsLineSymbol * symbol() const
Returns the symbol used to render the item.
QgsAnnotationLineItem * clone() override
Returns a clone of the item.
static QgsAnnotationLineItem * create()
Creates a new linestring annotation item.
void transformPolygon(QPolygonF &polygon, TransformDirection direction=ForwardTransform) const SIP_THROW(QgsCsException)
Transforms a polygon to the destination coordinate system.
bool isValid() const
Returns true if the coordinate transform is valid, ie both the source and destination CRS have been s...
Custom exception class for Coordinate Reference System related exceptions.
Definition: qgsexception.h:66
Abstract base class for curved geometry type.
Definition: qgscurve.h:36
Base class for feedback objects to be used for cancellation of something running in a worker thread.
Definition: qgsfeedback.h:45
A geometry is the spatial representation of a feature.
Definition: qgsgeometry.h:124
static QgsGeometry fromWkt(const QString &wkt)
Creates a new geometry from a WKT string.
Line string geometry type, with support for z-dimension and m-values.
Definition: qgslinestring.h:44
A line symbol type, for rendering LineString and MultiLineString geometries.
Definition: qgslinesymbol.h:30
void transformInPlace(double &x, double &y) const
Transforms device coordinates to map coordinates.
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.
static QDomElement saveSymbol(const QString &symbolName, const QgsSymbol *symbol, QDomDocument &doc, const QgsReadWriteContext &context)
Writes a symbol definition to XML.