QGIS API Documentation 4.1.0-Master (60fea48833c)
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
39
41{
42 return u"linestring"_s;
43}
44
46{
47 QPolygonF pts = mCurve->asQPolygonF();
48
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 mSymbol->startRender( context );
72 mSymbol->renderPolyline( pts, nullptr, context );
73 mSymbol->stopRender( context );
74}
75
76bool QgsAnnotationLineItem::writeXml( QDomElement &element, QDomDocument &document, const QgsReadWriteContext &context ) const
77{
78 element.setAttribute( u"wkt"_s, mCurve->asWkt() );
79 element.appendChild( QgsSymbolLayerUtils::saveSymbol( u"lineSymbol"_s, mSymbol.get(), document, context ) );
80 writeCommonProperties( element, document, context );
81
82 return true;
83}
84
85QList<QgsAnnotationItemNode> QgsAnnotationLineItem::nodesV2( const QgsAnnotationItemEditContext & ) const
86{
87 QList< QgsAnnotationItemNode > res;
88 int i = 0;
89 for ( auto it = mCurve->vertices_begin(); it != mCurve->vertices_end(); ++it, ++i )
90 {
91 res.append( QgsAnnotationItemNode( it.vertexId(), QgsPointXY( ( *it ).x(), ( *it ).y() ), Qgis::AnnotationItemNodeType::VertexHandle ) );
92 }
93 return res;
94}
95
97{
98 switch ( operation->type() )
99 {
101 {
102 QgsAnnotationItemEditOperationMoveNode *moveOperation = qgis::down_cast< QgsAnnotationItemEditOperationMoveNode * >( operation );
103 if ( mCurve->moveVertex( moveOperation->nodeId(), QgsPoint( moveOperation->after() ) ) )
105 break;
106 }
107
109 {
110 QgsAnnotationItemEditOperationDeleteNode *deleteOperation = qgis::down_cast< QgsAnnotationItemEditOperationDeleteNode * >( operation );
111 if ( mCurve->deleteVertex( deleteOperation->nodeId() ) )
113 break;
114 }
115
117 {
118 QgsAnnotationItemEditOperationAddNode *addOperation = qgis::down_cast< QgsAnnotationItemEditOperationAddNode * >( operation );
119
120 QgsPoint segmentPoint;
121 QgsVertexId endOfSegmentVertex;
122 mCurve->closestSegment( addOperation->point(), segmentPoint, endOfSegmentVertex );
123 if ( mCurve->insertVertex( endOfSegmentVertex, segmentPoint ) )
125 break;
126 }
127
129 {
130 QgsAnnotationItemEditOperationTranslateItem *moveOperation = qgis::down_cast< QgsAnnotationItemEditOperationTranslateItem * >( operation );
131 const QTransform transform = QTransform::fromTranslate( moveOperation->translationX(), moveOperation->translationY() );
132 mCurve->transform( transform );
134 }
135
137 {
138 QgsAnnotationItemEditOperationRotateItem *rotateOperation = qgis::down_cast< QgsAnnotationItemEditOperationRotateItem * >( operation );
139 QgsPointXY center = mCurve->boundingBox().center();
140 QTransform transform = QTransform::fromTranslate( center.x(), center.y() );
141 transform.rotate( -rotateOperation->angle() );
142 transform.translate( -center.x(), -center.y() );
143 mCurve->transform( transform );
145 }
146 }
147
149}
150
152{
153 switch ( operation->type() )
154 {
156 {
157 QgsAnnotationItemEditOperationMoveNode *moveOperation = qgis::down_cast< QgsAnnotationItemEditOperationMoveNode * >( operation );
158 std::unique_ptr< QgsCurve > modifiedCurve( mCurve->clone() );
159 if ( modifiedCurve->moveVertex( moveOperation->nodeId(), QgsPoint( moveOperation->after() ) ) )
160 {
161 return new QgsAnnotationItemEditOperationTransientResults( QgsGeometry( std::move( modifiedCurve ) ) );
162 }
163 break;
164 }
165
167 {
168 QgsAnnotationItemEditOperationTranslateItem *moveOperation = qgis::down_cast< QgsAnnotationItemEditOperationTranslateItem * >( operation );
169 const QTransform transform = QTransform::fromTranslate( moveOperation->translationX(), moveOperation->translationY() );
170 std::unique_ptr< QgsCurve > modifiedCurve( mCurve->clone() );
171 modifiedCurve->transform( transform );
172 return new QgsAnnotationItemEditOperationTransientResults( QgsGeometry( std::move( modifiedCurve ) ) );
173 }
174
176 {
177 QgsAnnotationItemEditOperationRotateItem *rotateOperation = qgis::down_cast< QgsAnnotationItemEditOperationRotateItem * >( operation );
178 std::unique_ptr< QgsCurve > modifiedCurve( mCurve->clone() );
179 QgsPointXY center = modifiedCurve->boundingBox().center();
180 QTransform transform = QTransform::fromTranslate( center.x(), center.y() );
181 transform.rotate( -rotateOperation->angle() );
182 transform.translate( -center.x(), -center.y() );
183 modifiedCurve->transform( transform );
184 return new QgsAnnotationItemEditOperationTransientResults( QgsGeometry( std::move( modifiedCurve ) ) );
185 }
186
189 break;
190 }
191 return nullptr;
192}
193
198
203
204bool QgsAnnotationLineItem::readXml( const QDomElement &element, const QgsReadWriteContext &context )
205{
206 const QString wkt = element.attribute( u"wkt"_s );
208 if ( const QgsCurve *curve = qgsgeometry_cast< const QgsCurve * >( geometry.constGet() ) )
209 mCurve.reset( curve->clone() );
210
211 const QDomElement symbolElem = element.firstChildElement( u"symbol"_s );
212 if ( !symbolElem.isNull() )
213 setSymbol( QgsSymbolLayerUtils::loadSymbol< QgsLineSymbol >( symbolElem, context ).release() );
214
215 readCommonProperties( element, context );
216
217 return true;
218}
219
221{
222 return mCurve->boundingBox();
223}
224
226{
227 auto item = std::make_unique< QgsAnnotationLineItem >( mCurve->clone() );
228 item->setSymbol( mSymbol->clone() );
229 item->copyCommonProperties( this );
230 return item.release();
231}
232
234{
235 mCurve.reset( geometry );
236}
237
239{
240 return mSymbol.get();
241}
242
244{
245 mSymbol.reset( symbol );
246}
@ 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.
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