QGIS API Documentation 3.38.0-Grenoble (exported)
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#include "qgssymbol.h"
20#include "qgssymbollayerutils.h"
21#include "qgslinesymbol.h"
24#include "qgscurve.h"
25#include "qgslinestring.h"
26
29 , mCurve( curve )
30 , mSymbol( std::make_unique< QgsLineSymbol >() )
31{
32
33}
34
36
38{
39 return QStringLiteral( "linestring" );
40}
41
43{
44 QPolygonF pts = mCurve->asQPolygonF();
45
46 //transform the QPolygonF to screen coordinates
47 if ( context.coordinateTransform().isValid() )
48 {
49 try
50 {
52 }
53 catch ( QgsCsException & )
54 {
55 // we don't abort the rendering here, instead we remove any invalid points and just plot those which ARE valid
56 }
57 }
58
59 // remove non-finite points, e.g. infinite or NaN points caused by reprojecting errors
60 pts.erase( std::remove_if( pts.begin(), pts.end(),
61 []( const QPointF point )
62 {
63 return !std::isfinite( point.x() ) || !std::isfinite( point.y() );
64 } ), pts.end() );
65
66 QPointF *ptr = pts.data();
67 for ( int i = 0; i < pts.size(); ++i, ++ptr )
68 {
69 context.mapToPixel().transformInPlace( ptr->rx(), ptr->ry() );
70 }
71
72 mSymbol->startRender( context );
73 mSymbol->renderPolyline( pts, nullptr, context );
74 mSymbol->stopRender( context );
75}
76
77bool QgsAnnotationLineItem::writeXml( QDomElement &element, QDomDocument &document, const QgsReadWriteContext &context ) const
78{
79 element.setAttribute( QStringLiteral( "wkt" ), mCurve->asWkt() );
80 element.appendChild( QgsSymbolLayerUtils::saveSymbol( QStringLiteral( "lineSymbol" ), mSymbol.get(), document, context ) );
81 writeCommonProperties( element, document, context );
82
83 return true;
84}
85
86QList<QgsAnnotationItemNode> QgsAnnotationLineItem::nodes() const
87{
88 QList< QgsAnnotationItemNode > res;
89 int i = 0;
90 for ( auto it = mCurve->vertices_begin(); it != mCurve->vertices_end(); ++it, ++i )
91 {
92 res.append( QgsAnnotationItemNode( it.vertexId(), QgsPointXY( ( *it ).x(), ( *it ).y() ), Qgis::AnnotationItemNodeType::VertexHandle ) );
93 }
94 return res;
95}
96
98{
99 switch ( operation->type() )
100 {
102 {
103 QgsAnnotationItemEditOperationMoveNode *moveOperation = qgis::down_cast< QgsAnnotationItemEditOperationMoveNode * >( operation );
104 if ( mCurve->moveVertex( moveOperation->nodeId(), QgsPoint( moveOperation->after() ) ) )
106 break;
107 }
108
110 {
111 QgsAnnotationItemEditOperationDeleteNode *deleteOperation = qgis::down_cast< QgsAnnotationItemEditOperationDeleteNode * >( operation );
112 if ( mCurve->deleteVertex( deleteOperation->nodeId() ) )
114 break;
115 }
116
118 {
119 QgsAnnotationItemEditOperationAddNode *addOperation = qgis::down_cast< QgsAnnotationItemEditOperationAddNode * >( operation );
120
121 QgsPoint segmentPoint;
122 QgsVertexId endOfSegmentVertex;
123 mCurve->closestSegment( addOperation->point(), segmentPoint, endOfSegmentVertex );
124 if ( mCurve->insertVertex( endOfSegmentVertex, segmentPoint ) )
126 break;
127 }
128
130 {
131 QgsAnnotationItemEditOperationTranslateItem *moveOperation = qgis::down_cast< QgsAnnotationItemEditOperationTranslateItem * >( operation );
132 const QTransform transform = QTransform::fromTranslate( moveOperation->translationX(), moveOperation->translationY() );
133 mCurve->transform( transform );
135 }
136 }
137
139}
140
142{
143 switch ( operation->type() )
144 {
146 {
147 QgsAnnotationItemEditOperationMoveNode *moveOperation = dynamic_cast< QgsAnnotationItemEditOperationMoveNode * >( operation );
148 std::unique_ptr< QgsCurve > modifiedCurve( mCurve->clone() );
149 if ( modifiedCurve->moveVertex( moveOperation->nodeId(), QgsPoint( moveOperation->after() ) ) )
150 {
151 return new QgsAnnotationItemEditOperationTransientResults( QgsGeometry( std::move( modifiedCurve ) ) );
152 }
153 break;
154 }
155
157 {
158 QgsAnnotationItemEditOperationTranslateItem *moveOperation = qgis::down_cast< QgsAnnotationItemEditOperationTranslateItem * >( operation );
159 const QTransform transform = QTransform::fromTranslate( moveOperation->translationX(), moveOperation->translationY() );
160 std::unique_ptr< QgsCurve > modifiedCurve( mCurve->clone() );
161 modifiedCurve->transform( transform );
162 return new QgsAnnotationItemEditOperationTransientResults( QgsGeometry( std::move( modifiedCurve ) ) );
163 }
164
167 break;
168 }
169 return nullptr;
170}
171
176
177bool QgsAnnotationLineItem::readXml( const QDomElement &element, const QgsReadWriteContext &context )
178{
179 const QString wkt = element.attribute( QStringLiteral( "wkt" ) );
181 if ( const QgsCurve *curve = qgsgeometry_cast< const QgsCurve * >( geometry.constGet() ) )
182 mCurve.reset( curve->clone() );
183
184 const QDomElement symbolElem = element.firstChildElement( QStringLiteral( "symbol" ) );
185 if ( !symbolElem.isNull() )
186 setSymbol( QgsSymbolLayerUtils::loadSymbol< QgsLineSymbol >( symbolElem, context ) );
187
188 readCommonProperties( element, context );
189
190 return true;
191}
192
194{
195 return mCurve->boundingBox();
196}
197
199{
200 std::unique_ptr< QgsAnnotationLineItem > item = std::make_unique< QgsAnnotationLineItem >( mCurve->clone() );
201 item->setSymbol( mSymbol->clone() );
202 item->copyCommonProperties( this );
203 return item.release();
204}
205
206void QgsAnnotationLineItem::setGeometry( QgsCurve *geometry ) { mCurve.reset( geometry ); }
207
209{
210 return mSymbol.get();
211}
212
214{
215 mSymbol.reset( symbol );
216}
@ VertexHandle
Node is a handle for manipulating vertices.
AnnotationItemEditOperationResult
Results from an edit operation on an annotation item.
Definition qgis.h:2135
@ 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...
Abstract base class for annotation item edit operations.
@ TranslateItem
Translate (move) an item.
virtual Type type() const =0
Returns the operation type.
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.
bool writeCommonProperties(QDomElement &element, QDomDocument &document, const QgsReadWriteContext &context) const
Writes common properties from the base class into an XML element.
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 line symbol along a line geometry.
QgsRectangle boundingBox() const override
Returns the bounding box of the item's geographic location, in the parent layer's coordinate referenc...
~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.
QgsAnnotationItemEditOperationTransientResults * transientEditResults(QgsAbstractAnnotationItemEditOperation *operation) override
Retrieves the results of a transient (in progress) edit operation on the item.
QList< QgsAnnotationItemNode > nodes() const override
Returns the nodes for the item, used for editing the item.
bool readXml(const QDomElement &element, const QgsReadWriteContext &context) override
Reads the item's state from the given DOM element.
Qgis::AnnotationItemEditOperationResult applyEdit(QgsAbstractAnnotationItemEditOperation *operation) override
Applies an edit operation to the item.
QgsAnnotationLineItem * clone() const override
Returns a clone of the item.
const QgsCurve * geometry() const
Returns the geometry of 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:35
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 device coordinates to map 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
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