QGIS API Documentation 4.1.0-Master (60fea48833c)
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
40
42{
43 return u"polygon"_s;
44}
45
47{
48 auto transformRing = [&context]( QPolygonF &pts ) {
49 //transform the QPolygonF to screen coordinates
50 if ( context.coordinateTransform().isValid() )
51 {
52 try
53 {
55 }
56 catch ( QgsCsException & )
57 {
58 // we don't abort the rendering here, instead we remove any invalid points and just plot those which ARE valid
59 }
60 }
61
62 // remove non-finite points, e.g. infinite or NaN points caused by reprojecting errors
63 pts.erase( std::remove_if( pts.begin(), pts.end(), []( const QPointF point ) { return !std::isfinite( point.x() ) || !std::isfinite( point.y() ); } ), pts.end() );
64
65 QPointF *ptr = pts.data();
66 for ( int i = 0; i < pts.size(); ++i, ++ptr )
67 {
68 context.mapToPixel().transformInPlace( ptr->rx(), ptr->ry() );
69 }
70 };
71
72 QPolygonF exterior = mPolygon->exteriorRing()->asQPolygonF();
73 transformRing( exterior );
74 QVector<QPolygonF> rings;
75 rings.reserve( mPolygon->numInteriorRings() );
76 for ( int i = 0; i < mPolygon->numInteriorRings(); ++i )
77 {
78 QPolygonF ring = mPolygon->interiorRing( i )->asQPolygonF();
79 transformRing( ring );
80 rings.append( ring );
81 }
82
83 mSymbol->startRender( context );
84 mSymbol->renderPolygon( exterior, rings.empty() ? nullptr : &rings, nullptr, context );
85 mSymbol->stopRender( context );
86}
87
88bool QgsAnnotationPolygonItem::writeXml( QDomElement &element, QDomDocument &document, const QgsReadWriteContext &context ) const
89{
90 element.setAttribute( u"wkt"_s, mPolygon->asWkt() );
91 element.appendChild( QgsSymbolLayerUtils::saveSymbol( u"lineSymbol"_s, mSymbol.get(), document, context ) );
92
93 writeCommonProperties( element, document, context );
94 return true;
95}
96
97QList<QgsAnnotationItemNode> QgsAnnotationPolygonItem::nodesV2( const QgsAnnotationItemEditContext & ) const
98{
99 QList< QgsAnnotationItemNode > res;
100
101 auto processRing = [&res]( const QgsCurve *ring, int ringId ) {
102 // we don't want a duplicate node for the closed ring vertex
103 const int count = ring->isClosed() ? ring->numPoints() - 1 : ring->numPoints();
104 res.reserve( res.size() + count );
105 for ( int i = 0; i < count; ++i )
106 {
107 res << QgsAnnotationItemNode( QgsVertexId( 0, ringId, i ), QgsPointXY( ring->xAt( i ), ring->yAt( i ) ), Qgis::AnnotationItemNodeType::VertexHandle );
108 }
109 };
110
111 if ( const QgsCurve *ring = mPolygon->exteriorRing() )
112 {
113 processRing( ring, 0 );
114 }
115 for ( int i = 0; i < mPolygon->numInteriorRings(); ++i )
116 {
117 processRing( mPolygon->interiorRing( i ), i + 1 );
118 }
119
120 return res;
121}
122
124{
125 switch ( operation->type() )
126 {
128 {
129 QgsAnnotationItemEditOperationMoveNode *moveOperation = qgis::down_cast< QgsAnnotationItemEditOperationMoveNode * >( operation );
130 if ( mPolygon->moveVertex( moveOperation->nodeId(), QgsPoint( moveOperation->after() ) ) )
132 break;
133 }
134
136 {
137 QgsAnnotationItemEditOperationDeleteNode *deleteOperation = qgis::down_cast< QgsAnnotationItemEditOperationDeleteNode * >( operation );
138 if ( mPolygon->deleteVertex( deleteOperation->nodeId() ) )
140 break;
141 }
142
144 {
145 QgsAnnotationItemEditOperationAddNode *addOperation = qgis::down_cast< QgsAnnotationItemEditOperationAddNode * >( operation );
146
147 QgsPoint segmentPoint;
148 QgsVertexId endOfSegmentVertex;
149 mPolygon->closestSegment( addOperation->point(), segmentPoint, endOfSegmentVertex );
150 if ( mPolygon->insertVertex( endOfSegmentVertex, segmentPoint ) )
152 break;
153 }
154
156 {
157 QgsAnnotationItemEditOperationTranslateItem *moveOperation = qgis::down_cast< QgsAnnotationItemEditOperationTranslateItem * >( operation );
158 const QTransform transform = QTransform::fromTranslate( moveOperation->translationX(), moveOperation->translationY() );
159 mPolygon->transform( transform );
161 }
162
164 {
165 QgsAnnotationItemEditOperationRotateItem *rotateOperation = qgis::down_cast< QgsAnnotationItemEditOperationRotateItem * >( operation );
166 QgsPointXY center = mPolygon->boundingBox().center();
167 QTransform transform = QTransform::fromTranslate( center.x(), center.y() );
168 transform.rotate( -rotateOperation->angle() );
169 transform.translate( -center.x(), -center.y() );
170 mPolygon->transform( transform );
172 }
173 }
174
176}
177
179{
180 switch ( operation->type() )
181 {
183 {
184 QgsAnnotationItemEditOperationMoveNode *moveOperation = qgis::down_cast< QgsAnnotationItemEditOperationMoveNode * >( operation );
185 std::unique_ptr< QgsCurvePolygon > modifiedPolygon( mPolygon->clone() );
186 if ( modifiedPolygon->moveVertex( moveOperation->nodeId(), QgsPoint( moveOperation->after() ) ) )
187 {
188 return new QgsAnnotationItemEditOperationTransientResults( QgsGeometry( std::move( modifiedPolygon ) ) );
189 }
190 break;
191 }
192
194 {
195 QgsAnnotationItemEditOperationTranslateItem *moveOperation = qgis::down_cast< QgsAnnotationItemEditOperationTranslateItem * >( operation );
196 const QTransform transform = QTransform::fromTranslate( moveOperation->translationX(), moveOperation->translationY() );
197 std::unique_ptr< QgsCurvePolygon > modifiedPolygon( mPolygon->clone() );
198 modifiedPolygon->transform( transform );
199 return new QgsAnnotationItemEditOperationTransientResults( QgsGeometry( std::move( modifiedPolygon ) ) );
200 }
201
203 {
204 QgsAnnotationItemEditOperationRotateItem *rotateOperation = qgis::down_cast< QgsAnnotationItemEditOperationRotateItem * >( operation );
205 std::unique_ptr< QgsCurvePolygon > modifiedPolygon( mPolygon->clone() );
206 QgsPointXY center = modifiedPolygon->boundingBox().center();
207 QTransform transform = QTransform::fromTranslate( center.x(), center.y() );
208 transform.rotate( -rotateOperation->angle() );
209 transform.translate( -center.x(), -center.y() );
210 modifiedPolygon->transform( transform );
211 return new QgsAnnotationItemEditOperationTransientResults( QgsGeometry( std::move( modifiedPolygon ) ) );
212 }
213
216 break;
217 }
218 return nullptr;
219}
220
225
230
231bool QgsAnnotationPolygonItem::readXml( const QDomElement &element, const QgsReadWriteContext &context )
232{
233 const QString wkt = element.attribute( u"wkt"_s );
235 if ( const QgsCurvePolygon *polygon = qgsgeometry_cast< const QgsCurvePolygon * >( geometry.constGet() ) )
236 mPolygon.reset( polygon->clone() );
237
238 const QDomElement symbolElem = element.firstChildElement( u"symbol"_s );
239 if ( !symbolElem.isNull() )
240 setSymbol( QgsSymbolLayerUtils::loadSymbol< QgsFillSymbol >( symbolElem, context ).release() );
241
242 readCommonProperties( element, context );
243 return true;
244}
245
247{
248 auto item = std::make_unique< QgsAnnotationPolygonItem >( mPolygon->clone() );
249 item->setSymbol( mSymbol->clone() );
250 item->copyCommonProperties( this );
251 ;
252 return item.release();
253}
254
256{
257 return mPolygon->boundingBox();
258}
259
264
266{
267 return mSymbol.get();
268}
269
271{
272 mSymbol.reset( symbol );
273}
@ VertexHandle
Node is a handle for manipulating vertices.
Definition qgis.h:2597
@ SupportsReferenceScale
Item supports reference scale based rendering.
Definition qgis.h:2555
AnnotationItemEditOperationResult
Results from an edit operation on an annotation item.
Definition qgis.h:2608
@ Invalid
Operation has invalid parameters for the item, no change occurred.
Definition qgis.h:2610
@ Success
Item was modified successfully.
Definition qgis.h:2609
@ ItemCleared
The operation results in the item being cleared, and the item should be removed from the layer as a r...
Definition qgis.h:2611
QFlags< AnnotationItemFlag > AnnotationItemFlags
Annotation item flags.
Definition qgis.h:2559
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: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.
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