QGIS API Documentation  3.20.0-Odense (decaadbb31)
qgsannotationpolygonitem.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  qgsannotationpolygonitem.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 
19 #include "qgssymbol.h"
20 #include "qgssymbollayerutils.h"
21 #include "qgssurface.h"
22 #include "qgsfillsymbol.h"
23 
26  , mPolygon( polygon )
27  , mSymbol( std::make_unique< QgsFillSymbol >() )
28 {
29 
30 }
31 
33 
35 {
36  return QStringLiteral( "polygon" );
37 }
38 
40 {
41 
42  auto transformRing = [&context]( QPolygonF & pts )
43  {
44  //transform the QPolygonF to screen coordinates
45  if ( context.coordinateTransform().isValid() )
46  {
47  try
48  {
49  context.coordinateTransform().transformPolygon( pts );
50  }
51  catch ( QgsCsException & )
52  {
53  // we don't abort the rendering here, instead we remove any invalid points and just plot those which ARE valid
54  }
55  }
56 
57  // remove non-finite points, e.g. infinite or NaN points caused by reprojecting errors
58  pts.erase( std::remove_if( pts.begin(), pts.end(),
59  []( const QPointF point )
60  {
61  return !std::isfinite( point.x() ) || !std::isfinite( point.y() );
62  } ), pts.end() );
63 
64  QPointF *ptr = pts.data();
65  for ( int i = 0; i < pts.size(); ++i, ++ptr )
66  {
67  context.mapToPixel().transformInPlace( ptr->rx(), ptr->ry() );
68  }
69  };
70 
71  QPolygonF exterior = mPolygon->exteriorRing()->asQPolygonF();
72  transformRing( exterior );
73  QVector<QPolygonF> rings;
74  rings.reserve( mPolygon->numInteriorRings() );
75  for ( int i = 0; i < mPolygon->numInteriorRings(); ++i )
76  {
77  QPolygonF ring = mPolygon->interiorRing( i )->asQPolygonF();
78  transformRing( ring );
79  rings.append( ring );
80  }
81 
82  mSymbol->startRender( context );
83  mSymbol->renderPolygon( exterior, rings.empty() ? nullptr : &rings, nullptr, context );
84  mSymbol->stopRender( context );
85 }
86 
87 bool QgsAnnotationPolygonItem::writeXml( QDomElement &element, QDomDocument &document, const QgsReadWriteContext &context ) const
88 {
89  element.setAttribute( QStringLiteral( "wkt" ), mPolygon->asWkt() );
90 
91  element.setAttribute( QStringLiteral( "zIndex" ), zIndex() );
92  element.appendChild( QgsSymbolLayerUtils::saveSymbol( QStringLiteral( "lineSymbol" ), mSymbol.get(), document, context ) );
93 
94  return true;
95 }
96 
98 {
99  return new QgsAnnotationPolygonItem( new QgsPolygon() );
100 }
101 
102 bool QgsAnnotationPolygonItem::readXml( const QDomElement &element, const QgsReadWriteContext &context )
103 {
104  const QString wkt = element.attribute( QStringLiteral( "wkt" ) );
106  if ( const QgsCurvePolygon *polygon = qgsgeometry_cast< const QgsCurvePolygon * >( geometry.constGet() ) )
107  mPolygon.reset( polygon->clone() );
108 
109  setZIndex( element.attribute( QStringLiteral( "zIndex" ) ).toInt() );
110 
111  const QDomElement symbolElem = element.firstChildElement( QStringLiteral( "symbol" ) );
112  if ( !symbolElem.isNull() )
113  setSymbol( QgsSymbolLayerUtils::loadSymbol< QgsFillSymbol >( symbolElem, context ) );
114 
115  return true;
116 }
117 
119 {
120  std::unique_ptr< QgsAnnotationPolygonItem > item = std::make_unique< QgsAnnotationPolygonItem >( mPolygon->clone() );
121  item->setSymbol( mSymbol->clone() );
122  item->setZIndex( zIndex() );
123  return item.release();
124 }
125 
127 {
128  return mPolygon->boundingBox();
129 }
130 
132 {
133  return mSymbol.get();
134 }
135 
137 {
138  mSymbol.reset( symbol );
139 }
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 fill symbol for a polygon geometry.
bool readXml(const QDomElement &element, const QgsReadWriteContext &context) override
Reads the item's state from the given DOM element.
void render(QgsRenderContext &context, QgsFeedback *feedback) override
Renders the item to the specified render context.
void setSymbol(QgsFillSymbol *symbol)
Sets the symbol used to render the polygon item.
const QgsFillSymbol * symbol() const
Returns the symbol used to render the item.
QgsRectangle boundingBox() const override
Returns the bounding box of the item's geographic location, in the parent layer's coordinate referenc...
const QgsCurvePolygon * geometry() const
Returns the geometry of the item.
QString type() const override
Returns a unique (untranslated) string identifying the type of item.
bool writeXml(QDomElement &element, QDomDocument &document, const QgsReadWriteContext &context) const override
Writes the item's state into an XML element.
QgsAnnotationPolygonItem * clone() override
Returns a clone of the item.
~QgsAnnotationPolygonItem() override
static QgsAnnotationPolygonItem * create()
Creates a new polygon annotation item.
QgsAnnotationPolygonItem(QgsCurvePolygon *polygon)
Constructor for QgsAnnotationPolygonItem, with the specified polygon geometry.
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
Curve polygon geometry type.
Base class for feedback objects to be used for cancellation of something running in a worker thread.
Definition: qgsfeedback.h:45
A fill symbol type, for rendering Polygon and MultiPolygon geometries.
Definition: qgsfillsymbol.h:30
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.
void transformInPlace(double &x, double &y) const
Transforms device coordinates to map coordinates.
Polygon geometry type.
Definition: qgspolygon.h:34
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.