QGIS API Documentation 3.41.0-Master (cea29feecf2)
Loading...
Searching...
No Matches
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 "qgscurvepolygon.h"
22#include "qgscurve.h"
23#include "qgspolygon.h"
24#include "qgsfillsymbol.h"
27
30 , mPolygon( polygon )
31 , mSymbol( std::make_unique< QgsFillSymbol >() )
32{
33
34}
35
37
39{
40 return QStringLiteral( "polygon" );
41}
42
44{
45
46 auto transformRing = [&context]( QPolygonF & pts )
47 {
48 //transform the QPolygonF to screen coordinates
49 if ( context.coordinateTransform().isValid() )
50 {
51 try
52 {
54 }
55 catch ( QgsCsException & )
56 {
57 // we don't abort the rendering here, instead we remove any invalid points and just plot those which ARE valid
58 }
59 }
60
61 // remove non-finite points, e.g. infinite or NaN points caused by reprojecting errors
62 pts.erase( std::remove_if( pts.begin(), pts.end(),
63 []( const QPointF point )
64 {
65 return !std::isfinite( point.x() ) || !std::isfinite( point.y() );
66 } ), pts.end() );
67
68 QPointF *ptr = pts.data();
69 for ( int i = 0; i < pts.size(); ++i, ++ptr )
70 {
71 context.mapToPixel().transformInPlace( ptr->rx(), ptr->ry() );
72 }
73 };
74
75 QPolygonF exterior = mPolygon->exteriorRing()->asQPolygonF();
76 transformRing( exterior );
77 QVector<QPolygonF> rings;
78 rings.reserve( mPolygon->numInteriorRings() );
79 for ( int i = 0; i < mPolygon->numInteriorRings(); ++i )
80 {
81 QPolygonF ring = mPolygon->interiorRing( i )->asQPolygonF();
82 transformRing( ring );
83 rings.append( ring );
84 }
85
86 mSymbol->startRender( context );
87 mSymbol->renderPolygon( exterior, rings.empty() ? nullptr : &rings, nullptr, context );
88 mSymbol->stopRender( context );
89}
90
91bool QgsAnnotationPolygonItem::writeXml( QDomElement &element, QDomDocument &document, const QgsReadWriteContext &context ) const
92{
93 element.setAttribute( QStringLiteral( "wkt" ), mPolygon->asWkt() );
94 element.appendChild( QgsSymbolLayerUtils::saveSymbol( QStringLiteral( "lineSymbol" ), mSymbol.get(), document, context ) );
95
96 writeCommonProperties( element, document, context );
97 return true;
98}
99
100QList<QgsAnnotationItemNode> QgsAnnotationPolygonItem::nodesV2( const QgsAnnotationItemEditContext & ) const
101{
102 QList< QgsAnnotationItemNode > res;
103
104 auto processRing = [&res]( const QgsCurve * ring, int ringId )
105 {
106 // we don't want a duplicate node for the closed ring vertex
107 const int count = ring->isClosed() ? ring->numPoints() - 1 : ring->numPoints();
108 res.reserve( res.size() + count );
109 for ( int i = 0; i < count; ++i )
110 {
111 res << QgsAnnotationItemNode( QgsVertexId( 0, ringId, i ), QgsPointXY( ring->xAt( i ), ring->yAt( i ) ), Qgis::AnnotationItemNodeType::VertexHandle );
112 }
113 };
114
115 if ( const QgsCurve *ring = mPolygon->exteriorRing() )
116 {
117 processRing( ring, 0 );
118 }
119 for ( int i = 0; i < mPolygon->numInteriorRings(); ++i )
120 {
121 processRing( mPolygon->interiorRing( i ), i + 1 );
122 }
123
124 return res;
125}
126
128{
129 switch ( operation->type() )
130 {
132 {
133 QgsAnnotationItemEditOperationMoveNode *moveOperation = dynamic_cast< QgsAnnotationItemEditOperationMoveNode * >( operation );
134 if ( mPolygon->moveVertex( moveOperation->nodeId(), QgsPoint( moveOperation->after() ) ) )
136 break;
137 }
138
140 {
141 QgsAnnotationItemEditOperationDeleteNode *deleteOperation = qgis::down_cast< QgsAnnotationItemEditOperationDeleteNode * >( operation );
142 if ( mPolygon->deleteVertex( deleteOperation->nodeId() ) )
144 break;
145 }
146
148 {
149 QgsAnnotationItemEditOperationAddNode *addOperation = qgis::down_cast< QgsAnnotationItemEditOperationAddNode * >( operation );
150
151 QgsPoint segmentPoint;
152 QgsVertexId endOfSegmentVertex;
153 mPolygon->closestSegment( addOperation->point(), segmentPoint, endOfSegmentVertex );
154 if ( mPolygon->insertVertex( endOfSegmentVertex, segmentPoint ) )
156 break;
157 }
158
160 {
161 QgsAnnotationItemEditOperationTranslateItem *moveOperation = qgis::down_cast< QgsAnnotationItemEditOperationTranslateItem * >( operation );
162 const QTransform transform = QTransform::fromTranslate( moveOperation->translationX(), moveOperation->translationY() );
163 mPolygon->transform( transform );
165 }
166 }
167
169}
170
172{
173 switch ( operation->type() )
174 {
176 {
177 QgsAnnotationItemEditOperationMoveNode *moveOperation = dynamic_cast< QgsAnnotationItemEditOperationMoveNode * >( operation );
178 std::unique_ptr< QgsCurvePolygon > modifiedPolygon( mPolygon->clone() );
179 if ( modifiedPolygon->moveVertex( moveOperation->nodeId(), QgsPoint( moveOperation->after() ) ) )
180 {
181 return new QgsAnnotationItemEditOperationTransientResults( QgsGeometry( std::move( modifiedPolygon ) ) );
182 }
183 break;
184 }
185
187 {
188 QgsAnnotationItemEditOperationTranslateItem *moveOperation = qgis::down_cast< QgsAnnotationItemEditOperationTranslateItem * >( operation );
189 const QTransform transform = QTransform::fromTranslate( moveOperation->translationX(), moveOperation->translationY() );
190 std::unique_ptr< QgsCurvePolygon > modifiedPolygon( mPolygon->clone() );
191 modifiedPolygon->transform( transform );
192 return new QgsAnnotationItemEditOperationTransientResults( QgsGeometry( std::move( modifiedPolygon ) ) );
193 }
194
197 break;
198 }
199 return nullptr;
200}
201
206
211
212bool QgsAnnotationPolygonItem::readXml( const QDomElement &element, const QgsReadWriteContext &context )
213{
214 const QString wkt = element.attribute( QStringLiteral( "wkt" ) );
216 if ( const QgsCurvePolygon *polygon = qgsgeometry_cast< const QgsCurvePolygon * >( geometry.constGet() ) )
217 mPolygon.reset( polygon->clone() );
218
219 const QDomElement symbolElem = element.firstChildElement( QStringLiteral( "symbol" ) );
220 if ( !symbolElem.isNull() )
221 setSymbol( QgsSymbolLayerUtils::loadSymbol< QgsFillSymbol >( symbolElem, context ) );
222
223 readCommonProperties( element, context );
224 return true;
225}
226
228{
229 std::unique_ptr< QgsAnnotationPolygonItem > item = std::make_unique< QgsAnnotationPolygonItem >( mPolygon->clone() );
230 item->setSymbol( mSymbol->clone() );
231 item->copyCommonProperties( this );;
232 return item.release();
233}
234
236{
237 return mPolygon->boundingBox();
238}
239
241{
242 mPolygon.reset( geometry );
243}
244
246{
247 return mSymbol.get();
248}
249
251{
252 mSymbol.reset( symbol );
253}
@ VertexHandle
Node is a handle for manipulating vertices.
@ SupportsReferenceScale
Item supports reference scale based rendering.
AnnotationItemEditOperationResult
Results from an edit operation on an annotation item.
Definition qgis.h:2396
@ Invalid
Operation has invalid parameters for the item, no change occurred.
@ Success
Item was modified successfully.
@ ItemCleared
The operation results in the item being cleared, and the item should be removed from the layer as a r...
QFlags< AnnotationItemFlag > AnnotationItemFlags
Annotation item flags.
Definition qgis.h:2347
Abstract base class for annotation item edit operations.
@ TranslateItem
Translate (move) an item.
virtual Type type() const =0
Returns the operation type.
Encapsulates the context for an annotation item edit operation.
Annotation item edit operation consisting of adding a node.
QgsPoint point() const
Returns the node position (in layer coordinates).
Annotation item edit operation consisting of deleting a node.
QgsVertexId nodeId() const
Returns the deleted node ID.
Annotation item edit operation consisting of moving a node.
QgsPoint after() const
Returns the node position after the move occurred (in layer coordinates).
QgsVertexId nodeId() const
Returns the associated node ID.
Encapsulates the transient results of an in-progress annotation edit operation.
Annotation item edit operation consisting of translating (moving) an item.
double translationY() const
Returns the y-axis translation, in layer units.
double translationX() const
Returns the x-axis translation, in layer units.
Contains information about a node used for editing an annotation item.
Abstract base class for annotation items which are drawn with QgsAnnotationLayers.
virtual bool writeCommonProperties(QDomElement &element, QDomDocument &document, const QgsReadWriteContext &context) const
Writes common properties from the base class into an XML element.
virtual bool readCommonProperties(const QDomElement &element, const QgsReadWriteContext &context)
Reads common properties from the base class from the given DOM element.
An annotation item which renders a fill symbol for a polygon geometry.
Qgis::AnnotationItemEditOperationResult applyEditV2(QgsAbstractAnnotationItemEditOperation *operation, const QgsAnnotationItemEditContext &context) override
Applies an edit operation to the item.
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.
const QgsCurvePolygon * geometry() const
Returns the geometry of the item.
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...
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.
void setGeometry(QgsCurvePolygon *geometry)
Sets the geometry of the item.
Qgis::AnnotationItemFlags flags() const override
Returns item flags.
QgsAnnotationPolygonItem * clone() const override
Returns a clone of the item.
QList< QgsAnnotationItemNode > nodesV2(const QgsAnnotationItemEditContext &context) const override
Returns the nodes for the item, used for editing the item.
~QgsAnnotationPolygonItem() override
static QgsAnnotationPolygonItem * create()
Creates a new polygon annotation item.
QgsAnnotationPolygonItem(QgsCurvePolygon *polygon)
Constructor for QgsAnnotationPolygonItem, with the specified polygon geometry.
QgsAnnotationItemEditOperationTransientResults * transientEditResultsV2(QgsAbstractAnnotationItemEditOperation *operation, const QgsAnnotationItemEditContext &context) override
Retrieves the results of a transient (in progress) edit operation on the item.
void transformPolygon(QPolygonF &polygon, Qgis::TransformDirection direction=Qgis::TransformDirection::Forward) const
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.
Curve polygon geometry type.
Abstract base class for curved geometry type.
Definition qgscurve.h:35
virtual int numPoints() const =0
Returns the number of points in the curve.
virtual bool isClosed() const
Returns true if the curve is closed.
Definition qgscurve.cpp:53
virtual double xAt(int index) const =0
Returns the x-coordinate of the specified node in the line string.
virtual double yAt(int index) const =0
Returns the y-coordinate of the specified node in the line string.
Base class for feedback objects to be used for cancellation of something running in a worker thread.
Definition qgsfeedback.h:44
A fill symbol type, for rendering Polygon and MultiPolygon geometries.
A geometry is the spatial representation of a feature.
static Q_INVOKABLE QgsGeometry fromWkt(const QString &wkt)
Creates a new geometry from a WKT string.
void transformInPlace(double &x, double &y) const
Transforms map coordinates to device coordinates.
A class to represent a 2D point.
Definition qgspointxy.h:60
Point geometry type, with support for z-dimension and m-values.
Definition qgspoint.h:49
Polygon geometry type.
Definition qgspolygon.h:33
The class is used as a container of context for various read/write operations on other objects.
A rectangle specified with double values.
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.
Utility class for identifying a unique vertex within a geometry.
Definition qgsvertexid.h:30