QGIS API Documentation 3.99.0-Master (2fe06baccd8)
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
30 , mCurve( curve )
31 , mSymbol( std::make_unique< QgsLineSymbol >() )
32{
33
34}
35
37
39{
40 return QStringLiteral( "linestring" );
41}
42
44{
45 QPolygonF pts = mCurve->asQPolygonF();
46
47 //transform the QPolygonF to screen coordinates
48 if ( context.coordinateTransform().isValid() )
49 {
50 try
51 {
53 }
54 catch ( QgsCsException & )
55 {
56 // we don't abort the rendering here, instead we remove any invalid points and just plot those which ARE valid
57 }
58 }
59
60 // remove non-finite points, e.g. infinite or NaN points caused by reprojecting errors
61 pts.erase( std::remove_if( pts.begin(), pts.end(),
62 []( const QPointF point )
63 {
64 return !std::isfinite( point.x() ) || !std::isfinite( point.y() );
65 } ), pts.end() );
66
67 QPointF *ptr = pts.data();
68 for ( int i = 0; i < pts.size(); ++i, ++ptr )
69 {
70 context.mapToPixel().transformInPlace( ptr->rx(), ptr->ry() );
71 }
72
73 mSymbol->startRender( context );
74 mSymbol->renderPolyline( pts, nullptr, context );
75 mSymbol->stopRender( context );
76}
77
78bool QgsAnnotationLineItem::writeXml( QDomElement &element, QDomDocument &document, const QgsReadWriteContext &context ) const
79{
80 element.setAttribute( QStringLiteral( "wkt" ), mCurve->asWkt() );
81 element.appendChild( QgsSymbolLayerUtils::saveSymbol( QStringLiteral( "lineSymbol" ), mSymbol.get(), document, context ) );
82 writeCommonProperties( element, document, context );
83
84 return true;
85}
86
87QList<QgsAnnotationItemNode> QgsAnnotationLineItem::nodesV2( const QgsAnnotationItemEditContext & ) const
88{
89 QList< QgsAnnotationItemNode > res;
90 int i = 0;
91 for ( auto it = mCurve->vertices_begin(); it != mCurve->vertices_end(); ++it, ++i )
92 {
93 res.append( QgsAnnotationItemNode( it.vertexId(), QgsPointXY( ( *it ).x(), ( *it ).y() ), Qgis::AnnotationItemNodeType::VertexHandle ) );
94 }
95 return res;
96}
97
99{
100 switch ( operation->type() )
101 {
103 {
104 QgsAnnotationItemEditOperationMoveNode *moveOperation = qgis::down_cast< QgsAnnotationItemEditOperationMoveNode * >( operation );
105 if ( mCurve->moveVertex( moveOperation->nodeId(), QgsPoint( moveOperation->after() ) ) )
107 break;
108 }
109
111 {
112 QgsAnnotationItemEditOperationDeleteNode *deleteOperation = qgis::down_cast< QgsAnnotationItemEditOperationDeleteNode * >( operation );
113 if ( mCurve->deleteVertex( deleteOperation->nodeId() ) )
115 break;
116 }
117
119 {
120 QgsAnnotationItemEditOperationAddNode *addOperation = qgis::down_cast< QgsAnnotationItemEditOperationAddNode * >( operation );
121
122 QgsPoint segmentPoint;
123 QgsVertexId endOfSegmentVertex;
124 mCurve->closestSegment( addOperation->point(), segmentPoint, endOfSegmentVertex );
125 if ( mCurve->insertVertex( endOfSegmentVertex, segmentPoint ) )
127 break;
128 }
129
131 {
132 QgsAnnotationItemEditOperationTranslateItem *moveOperation = qgis::down_cast< QgsAnnotationItemEditOperationTranslateItem * >( operation );
133 const QTransform transform = QTransform::fromTranslate( moveOperation->translationX(), moveOperation->translationY() );
134 mCurve->transform( transform );
136 }
137 }
138
140}
141
143{
144 switch ( operation->type() )
145 {
147 {
148 QgsAnnotationItemEditOperationMoveNode *moveOperation = qgis::down_cast< QgsAnnotationItemEditOperationMoveNode * >( operation );
149 std::unique_ptr< QgsCurve > modifiedCurve( mCurve->clone() );
150 if ( modifiedCurve->moveVertex( moveOperation->nodeId(), QgsPoint( moveOperation->after() ) ) )
151 {
152 return new QgsAnnotationItemEditOperationTransientResults( QgsGeometry( std::move( modifiedCurve ) ) );
153 }
154 break;
155 }
156
158 {
159 QgsAnnotationItemEditOperationTranslateItem *moveOperation = qgis::down_cast< QgsAnnotationItemEditOperationTranslateItem * >( operation );
160 const QTransform transform = QTransform::fromTranslate( moveOperation->translationX(), moveOperation->translationY() );
161 std::unique_ptr< QgsCurve > modifiedCurve( mCurve->clone() );
162 modifiedCurve->transform( transform );
163 return new QgsAnnotationItemEditOperationTransientResults( QgsGeometry( std::move( modifiedCurve ) ) );
164 }
165
168 break;
169 }
170 return nullptr;
171}
172
177
182
183bool QgsAnnotationLineItem::readXml( const QDomElement &element, const QgsReadWriteContext &context )
184{
185 const QString wkt = element.attribute( QStringLiteral( "wkt" ) );
187 if ( const QgsCurve *curve = qgsgeometry_cast< const QgsCurve * >( geometry.constGet() ) )
188 mCurve.reset( curve->clone() );
189
190 const QDomElement symbolElem = element.firstChildElement( QStringLiteral( "symbol" ) );
191 if ( !symbolElem.isNull() )
192 setSymbol( QgsSymbolLayerUtils::loadSymbol< QgsLineSymbol >( symbolElem, context ).release() );
193
194 readCommonProperties( element, context );
195
196 return true;
197}
198
200{
201 return mCurve->boundingBox();
202}
203
205{
206 auto item = std::make_unique< QgsAnnotationLineItem >( mCurve->clone() );
207 item->setSymbol( mSymbol->clone() );
208 item->copyCommonProperties( this );
209 return item.release();
210}
211
213
215{
216 return mSymbol.get();
217}
218
220{
221 mSymbol.reset( symbol );
222}
@ VertexHandle
Node is a handle for manipulating vertices.
Definition qgis.h:2508
@ SupportsReferenceScale
Item supports reference scale based rendering.
Definition qgis.h:2466
AnnotationItemEditOperationResult
Results from an edit operation on an annotation item.
Definition qgis.h:2519
@ Invalid
Operation has invalid parameters for the item, no change occurred.
Definition qgis.h:2521
@ Success
Item was modified successfully.
Definition qgis.h:2520
@ ItemCleared
The operation results in the item being cleared, and the item should be removed from the layer as a r...
Definition qgis.h:2522
QFlags< AnnotationItemFlag > AnnotationItemFlags
Annotation item flags.
Definition qgis.h:2470
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.
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:60
Point geometry type, with support for z-dimension and m-values.
Definition qgspoint.h:49
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:30