QGIS API Documentation 3.99.0-Master (d270888f95f)
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
22#include "qgscurve.h"
23#include "qgscurvepolygon.h"
24#include "qgsfillsymbol.h"
25#include "qgspolygon.h"
26#include "qgssymbol.h"
27#include "qgssymbollayerutils.h"
28
29#include <QString>
30
31using namespace Qt::StringLiterals;
32
35 , mPolygon( polygon )
36 , mSymbol( std::make_unique< QgsFillSymbol >() )
37{
38
39}
40
42
44{
45 return u"polygon"_s;
46}
47
49{
50
51 auto transformRing = [&context]( QPolygonF & pts )
52 {
53 //transform the QPolygonF to screen coordinates
54 if ( context.coordinateTransform().isValid() )
55 {
56 try
57 {
59 }
60 catch ( QgsCsException & )
61 {
62 // we don't abort the rendering here, instead we remove any invalid points and just plot those which ARE valid
63 }
64 }
65
66 // remove non-finite points, e.g. infinite or NaN points caused by reprojecting errors
67 pts.erase( std::remove_if( pts.begin(), pts.end(),
68 []( const QPointF point )
69 {
70 return !std::isfinite( point.x() ) || !std::isfinite( point.y() );
71 } ), pts.end() );
72
73 QPointF *ptr = pts.data();
74 for ( int i = 0; i < pts.size(); ++i, ++ptr )
75 {
76 context.mapToPixel().transformInPlace( ptr->rx(), ptr->ry() );
77 }
78 };
79
80 QPolygonF exterior = mPolygon->exteriorRing()->asQPolygonF();
81 transformRing( exterior );
82 QVector<QPolygonF> rings;
83 rings.reserve( mPolygon->numInteriorRings() );
84 for ( int i = 0; i < mPolygon->numInteriorRings(); ++i )
85 {
86 QPolygonF ring = mPolygon->interiorRing( i )->asQPolygonF();
87 transformRing( ring );
88 rings.append( ring );
89 }
90
91 mSymbol->startRender( context );
92 mSymbol->renderPolygon( exterior, rings.empty() ? nullptr : &rings, nullptr, context );
93 mSymbol->stopRender( context );
94}
95
96bool QgsAnnotationPolygonItem::writeXml( QDomElement &element, QDomDocument &document, const QgsReadWriteContext &context ) const
97{
98 element.setAttribute( u"wkt"_s, mPolygon->asWkt() );
99 element.appendChild( QgsSymbolLayerUtils::saveSymbol( u"lineSymbol"_s, mSymbol.get(), document, context ) );
100
101 writeCommonProperties( element, document, context );
102 return true;
103}
104
105QList<QgsAnnotationItemNode> QgsAnnotationPolygonItem::nodesV2( const QgsAnnotationItemEditContext & ) const
106{
107 QList< QgsAnnotationItemNode > res;
108
109 auto processRing = [&res]( const QgsCurve * ring, int ringId )
110 {
111 // we don't want a duplicate node for the closed ring vertex
112 const int count = ring->isClosed() ? ring->numPoints() - 1 : ring->numPoints();
113 res.reserve( res.size() + count );
114 for ( int i = 0; i < count; ++i )
115 {
116 res << QgsAnnotationItemNode( QgsVertexId( 0, ringId, i ), QgsPointXY( ring->xAt( i ), ring->yAt( i ) ), Qgis::AnnotationItemNodeType::VertexHandle );
117 }
118 };
119
120 if ( const QgsCurve *ring = mPolygon->exteriorRing() )
121 {
122 processRing( ring, 0 );
123 }
124 for ( int i = 0; i < mPolygon->numInteriorRings(); ++i )
125 {
126 processRing( mPolygon->interiorRing( i ), i + 1 );
127 }
128
129 return res;
130}
131
133{
134 switch ( operation->type() )
135 {
137 {
138 QgsAnnotationItemEditOperationMoveNode *moveOperation = qgis::down_cast< QgsAnnotationItemEditOperationMoveNode * >( operation );
139 if ( mPolygon->moveVertex( moveOperation->nodeId(), QgsPoint( moveOperation->after() ) ) )
141 break;
142 }
143
145 {
146 QgsAnnotationItemEditOperationDeleteNode *deleteOperation = qgis::down_cast< QgsAnnotationItemEditOperationDeleteNode * >( operation );
147 if ( mPolygon->deleteVertex( deleteOperation->nodeId() ) )
149 break;
150 }
151
153 {
154 QgsAnnotationItemEditOperationAddNode *addOperation = qgis::down_cast< QgsAnnotationItemEditOperationAddNode * >( operation );
155
156 QgsPoint segmentPoint;
157 QgsVertexId endOfSegmentVertex;
158 mPolygon->closestSegment( addOperation->point(), segmentPoint, endOfSegmentVertex );
159 if ( mPolygon->insertVertex( endOfSegmentVertex, segmentPoint ) )
161 break;
162 }
163
165 {
166 QgsAnnotationItemEditOperationTranslateItem *moveOperation = qgis::down_cast< QgsAnnotationItemEditOperationTranslateItem * >( operation );
167 const QTransform transform = QTransform::fromTranslate( moveOperation->translationX(), moveOperation->translationY() );
168 mPolygon->transform( transform );
170 }
171
173 {
174 QgsAnnotationItemEditOperationRotateItem *rotateOperation = qgis::down_cast< QgsAnnotationItemEditOperationRotateItem * >( operation );
175 QgsPointXY center = mPolygon->boundingBox().center();
176 QTransform transform = QTransform::fromTranslate( center.x(), center.y() );
177 transform.rotate( -rotateOperation->angle() );
178 transform.translate( -center.x(), -center.y() );
179 mPolygon->transform( transform );
181 }
182 }
183
185}
186
188{
189 switch ( operation->type() )
190 {
192 {
193 QgsAnnotationItemEditOperationMoveNode *moveOperation = qgis::down_cast< QgsAnnotationItemEditOperationMoveNode * >( operation );
194 std::unique_ptr< QgsCurvePolygon > modifiedPolygon( mPolygon->clone() );
195 if ( modifiedPolygon->moveVertex( moveOperation->nodeId(), QgsPoint( moveOperation->after() ) ) )
196 {
197 return new QgsAnnotationItemEditOperationTransientResults( QgsGeometry( std::move( modifiedPolygon ) ) );
198 }
199 break;
200 }
201
203 {
204 QgsAnnotationItemEditOperationTranslateItem *moveOperation = qgis::down_cast< QgsAnnotationItemEditOperationTranslateItem * >( operation );
205 const QTransform transform = QTransform::fromTranslate( moveOperation->translationX(), moveOperation->translationY() );
206 std::unique_ptr< QgsCurvePolygon > modifiedPolygon( mPolygon->clone() );
207 modifiedPolygon->transform( transform );
208 return new QgsAnnotationItemEditOperationTransientResults( QgsGeometry( std::move( modifiedPolygon ) ) );
209 }
210
212 {
213 QgsAnnotationItemEditOperationRotateItem *rotateOperation = qgis::down_cast< QgsAnnotationItemEditOperationRotateItem * >( operation );
214 std::unique_ptr< QgsCurvePolygon > modifiedPolygon( mPolygon->clone() );
215 QgsPointXY center = modifiedPolygon->boundingBox().center();
216 QTransform transform = QTransform::fromTranslate( center.x(), center.y() );
217 transform.rotate( -rotateOperation->angle() );
218 transform.translate( -center.x(), -center.y() );
219 modifiedPolygon->transform( transform );
220 return new QgsAnnotationItemEditOperationTransientResults( QgsGeometry( std::move( modifiedPolygon ) ) );
221 }
222
225 break;
226 }
227 return nullptr;
228}
229
234
239
240bool QgsAnnotationPolygonItem::readXml( const QDomElement &element, const QgsReadWriteContext &context )
241{
242 const QString wkt = element.attribute( u"wkt"_s );
244 if ( const QgsCurvePolygon *polygon = qgsgeometry_cast< const QgsCurvePolygon * >( geometry.constGet() ) )
245 mPolygon.reset( polygon->clone() );
246
247 const QDomElement symbolElem = element.firstChildElement( u"symbol"_s );
248 if ( !symbolElem.isNull() )
249 setSymbol( QgsSymbolLayerUtils::loadSymbol< QgsFillSymbol >( symbolElem, context ).release() );
250
251 readCommonProperties( element, context );
252 return true;
253}
254
256{
257 auto item = std::make_unique< QgsAnnotationPolygonItem >( mPolygon->clone() );
258 item->setSymbol( mSymbol->clone() );
259 item->copyCommonProperties( this );;
260 return item.release();
261}
262
264{
265 return mPolygon->boundingBox();
266}
267
272
274{
275 return mSymbol.get();
276}
277
279{
280 mSymbol.reset( symbol );
281}
@ VertexHandle
Node is a handle for manipulating vertices.
Definition qgis.h:2566
@ SupportsReferenceScale
Item supports reference scale based rendering.
Definition qgis.h:2524
AnnotationItemEditOperationResult
Results from an edit operation on an annotation item.
Definition qgis.h:2577
@ Invalid
Operation has invalid parameters for the item, no change occurred.
Definition qgis.h:2579
@ Success
Item was modified successfully.
Definition qgis.h:2578
@ ItemCleared
The operation results in the item being cleared, and the item should be removed from the layer as a r...
Definition qgis.h:2580
QFlags< AnnotationItemFlag > AnnotationItemFlags
Annotation item flags.
Definition qgis.h:2528
Abstract base class for annotation item edit operations.
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.
Annotation item edit operation consisting of rotating an item.
double angle() const
Returns the rotation angle value (in degrees clockwise).
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.
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.
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:36
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:54
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.
Represents a 2D point.
Definition qgspointxy.h:62
double y
Definition qgspointxy.h:66
double x
Definition qgspointxy.h:65
Point geometry type, with support for z-dimension and m-values.
Definition qgspoint.h:53
Polygon geometry type.
Definition qgspolygon.h:37
A container for the context for various read/write operations on 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 std::unique_ptr< QgsSymbol > loadSymbol(const QDomElement &element, const QgsReadWriteContext &context)
Attempts to load a symbol from a DOM element.
static QDomElement saveSymbol(const QString &symbolName, const QgsSymbol *symbol, QDomDocument &doc, const QgsReadWriteContext &context)
Writes a symbol definition to XML.
T qgsgeometry_cast(QgsAbstractGeometry *geom)
Utility class for identifying a unique vertex within a geometry.
Definition qgsvertexid.h:34