QGIS API Documentation 3.99.0-Master (d270888f95f)
Loading...
Searching...
No Matches
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
19
22#include "qgscurve.h"
23#include "qgslinestring.h"
24#include "qgslinesymbol.h"
25#include "qgssymbol.h"
26#include "qgssymbollayerutils.h"
27
28#include <QString>
29
30using namespace Qt::StringLiterals;
31
34 , mCurve( curve )
35 , mSymbol( std::make_unique< QgsLineSymbol >() )
36{
37
38}
39
41
43{
44 return u"linestring"_s;
45}
46
48{
49 QPolygonF pts = mCurve->asQPolygonF();
50
51 //transform the QPolygonF to screen coordinates
52 if ( context.coordinateTransform().isValid() )
53 {
54 try
55 {
57 }
58 catch ( QgsCsException & )
59 {
60 // we don't abort the rendering here, instead we remove any invalid points and just plot those which ARE valid
61 }
62 }
63
64 // remove non-finite points, e.g. infinite or NaN points caused by reprojecting errors
65 pts.erase( std::remove_if( pts.begin(), pts.end(),
66 []( const QPointF point )
67 {
68 return !std::isfinite( point.x() ) || !std::isfinite( point.y() );
69 } ), pts.end() );
70
71 QPointF *ptr = pts.data();
72 for ( int i = 0; i < pts.size(); ++i, ++ptr )
73 {
74 context.mapToPixel().transformInPlace( ptr->rx(), ptr->ry() );
75 }
76
77 mSymbol->startRender( context );
78 mSymbol->renderPolyline( pts, nullptr, context );
79 mSymbol->stopRender( context );
80}
81
82bool QgsAnnotationLineItem::writeXml( QDomElement &element, QDomDocument &document, const QgsReadWriteContext &context ) const
83{
84 element.setAttribute( u"wkt"_s, mCurve->asWkt() );
85 element.appendChild( QgsSymbolLayerUtils::saveSymbol( u"lineSymbol"_s, mSymbol.get(), document, context ) );
86 writeCommonProperties( element, document, context );
87
88 return true;
89}
90
91QList<QgsAnnotationItemNode> QgsAnnotationLineItem::nodesV2( const QgsAnnotationItemEditContext & ) const
92{
93 QList< QgsAnnotationItemNode > res;
94 int i = 0;
95 for ( auto it = mCurve->vertices_begin(); it != mCurve->vertices_end(); ++it, ++i )
96 {
97 res.append( QgsAnnotationItemNode( it.vertexId(), QgsPointXY( ( *it ).x(), ( *it ).y() ), Qgis::AnnotationItemNodeType::VertexHandle ) );
98 }
99 return res;
100}
101
103{
104 switch ( operation->type() )
105 {
107 {
108 QgsAnnotationItemEditOperationMoveNode *moveOperation = qgis::down_cast< QgsAnnotationItemEditOperationMoveNode * >( operation );
109 if ( mCurve->moveVertex( moveOperation->nodeId(), QgsPoint( moveOperation->after() ) ) )
111 break;
112 }
113
115 {
116 QgsAnnotationItemEditOperationDeleteNode *deleteOperation = qgis::down_cast< QgsAnnotationItemEditOperationDeleteNode * >( operation );
117 if ( mCurve->deleteVertex( deleteOperation->nodeId() ) )
119 break;
120 }
121
123 {
124 QgsAnnotationItemEditOperationAddNode *addOperation = qgis::down_cast< QgsAnnotationItemEditOperationAddNode * >( operation );
125
126 QgsPoint segmentPoint;
127 QgsVertexId endOfSegmentVertex;
128 mCurve->closestSegment( addOperation->point(), segmentPoint, endOfSegmentVertex );
129 if ( mCurve->insertVertex( endOfSegmentVertex, segmentPoint ) )
131 break;
132 }
133
135 {
136 QgsAnnotationItemEditOperationTranslateItem *moveOperation = qgis::down_cast< QgsAnnotationItemEditOperationTranslateItem * >( operation );
137 const QTransform transform = QTransform::fromTranslate( moveOperation->translationX(), moveOperation->translationY() );
138 mCurve->transform( transform );
140 }
141
143 {
144 QgsAnnotationItemEditOperationRotateItem *rotateOperation = qgis::down_cast< QgsAnnotationItemEditOperationRotateItem * >( operation );
145 QgsPointXY center = mCurve->boundingBox().center();
146 QTransform transform = QTransform::fromTranslate( center.x(), center.y() );
147 transform.rotate( -rotateOperation->angle() );
148 transform.translate( -center.x(), -center.y() );
149 mCurve->transform( transform );
151 }
152 }
153
155}
156
158{
159 switch ( operation->type() )
160 {
162 {
163 QgsAnnotationItemEditOperationMoveNode *moveOperation = qgis::down_cast< QgsAnnotationItemEditOperationMoveNode * >( operation );
164 std::unique_ptr< QgsCurve > modifiedCurve( mCurve->clone() );
165 if ( modifiedCurve->moveVertex( moveOperation->nodeId(), QgsPoint( moveOperation->after() ) ) )
166 {
167 return new QgsAnnotationItemEditOperationTransientResults( QgsGeometry( std::move( modifiedCurve ) ) );
168 }
169 break;
170 }
171
173 {
174 QgsAnnotationItemEditOperationTranslateItem *moveOperation = qgis::down_cast< QgsAnnotationItemEditOperationTranslateItem * >( operation );
175 const QTransform transform = QTransform::fromTranslate( moveOperation->translationX(), moveOperation->translationY() );
176 std::unique_ptr< QgsCurve > modifiedCurve( mCurve->clone() );
177 modifiedCurve->transform( transform );
178 return new QgsAnnotationItemEditOperationTransientResults( QgsGeometry( std::move( modifiedCurve ) ) );
179 }
180
182 {
183 QgsAnnotationItemEditOperationRotateItem *rotateOperation = qgis::down_cast< QgsAnnotationItemEditOperationRotateItem * >( operation );
184 std::unique_ptr< QgsCurve > modifiedCurve( mCurve->clone() );
185 QgsPointXY center = modifiedCurve->boundingBox().center();
186 QTransform transform = QTransform::fromTranslate( center.x(), center.y() );
187 transform.rotate( -rotateOperation->angle() );
188 transform.translate( -center.x(), -center.y() );
189 modifiedCurve->transform( transform );
190 return new QgsAnnotationItemEditOperationTransientResults( QgsGeometry( std::move( modifiedCurve ) ) );
191 }
192
195 break;
196 }
197 return nullptr;
198}
199
204
209
210bool QgsAnnotationLineItem::readXml( const QDomElement &element, const QgsReadWriteContext &context )
211{
212 const QString wkt = element.attribute( u"wkt"_s );
214 if ( const QgsCurve *curve = qgsgeometry_cast< const QgsCurve * >( geometry.constGet() ) )
215 mCurve.reset( curve->clone() );
216
217 const QDomElement symbolElem = element.firstChildElement( u"symbol"_s );
218 if ( !symbolElem.isNull() )
219 setSymbol( QgsSymbolLayerUtils::loadSymbol< QgsLineSymbol >( symbolElem, context ).release() );
220
221 readCommonProperties( element, context );
222
223 return true;
224}
225
227{
228 return mCurve->boundingBox();
229}
230
232{
233 auto item = std::make_unique< QgsAnnotationLineItem >( mCurve->clone() );
234 item->setSymbol( mSymbol->clone() );
235 item->copyCommonProperties( this );
236 return item.release();
237}
238
240
242{
243 return mSymbol.get();
244}
245
247{
248 mSymbol.reset( symbol );
249}
@ 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.
QgsRectangle boundingBox() const override
Returns the bounding box of the item's geographic location, in the parent layer's coordinate referenc...
Qgis::AnnotationItemFlags flags() const override
Returns item flags.
~QgsAnnotationLineItem() override
QgsAnnotationLineItem(QgsCurve *curve)
Constructor for QgsAnnotationLineItem, with the specified curve.
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.
void setGeometry(QgsCurve *geometry)
Sets the geometry of the item.
bool readXml(const QDomElement &element, const QgsReadWriteContext &context) override
Reads the item's state from the given DOM element.
QList< QgsAnnotationItemNode > nodesV2(const QgsAnnotationItemEditContext &context) const override
Returns the nodes for the item, used for editing the item.
QgsAnnotationItemEditOperationTransientResults * transientEditResultsV2(QgsAbstractAnnotationItemEditOperation *operation, const QgsAnnotationItemEditContext &context) override
Retrieves the results of a transient (in progress) edit operation on the item.
QgsAnnotationLineItem * clone() const override
Returns a clone of the item.
const QgsCurve * geometry() const
Returns the geometry of the item.
Qgis::AnnotationItemEditOperationResult applyEditV2(QgsAbstractAnnotationItemEditOperation *operation, const QgsAnnotationItemEditContext &context) override
Applies an edit operation to the item.
bool writeXml(QDomElement &element, QDomDocument &document, const QgsReadWriteContext &context) const override
Writes the item's state into an XML element.
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.
static QgsAnnotationLineItem * create()
Creates a new linestring annotation 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.
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:44
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.
Line string geometry type, with support for z-dimension and m-values.
A line symbol type, for rendering LineString and MultiLineString geometries.
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
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