QGIS API Documentation 4.1.0-Master (376402f9aeb)
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 for ( auto it = mCurve->vertices_begin(); it != mCurve->vertices_end(); ++it )
89 {
90 res.append( QgsAnnotationItemNode( it.vertexId(), QgsPointXY( ( *it ).x(), ( *it ).y() ), Qgis::AnnotationItemNodeType::VertexHandle ) );
91 }
92 return res;
93}
94
96{
97 switch ( operation->type() )
98 {
100 {
101 QgsAnnotationItemEditOperationMoveNode *moveOperation = qgis::down_cast< QgsAnnotationItemEditOperationMoveNode * >( operation );
102 if ( mCurve->moveVertex( moveOperation->nodeId(), QgsPoint( moveOperation->after() ) ) )
104 break;
105 }
106
108 {
109 QgsAnnotationItemEditOperationDeleteNode *deleteOperation = qgis::down_cast< QgsAnnotationItemEditOperationDeleteNode * >( operation );
110 if ( mCurve->deleteVertex( deleteOperation->nodeId() ) )
112 break;
113 }
114
116 {
117 QgsAnnotationItemEditOperationAddNode *addOperation = qgis::down_cast< QgsAnnotationItemEditOperationAddNode * >( operation );
118
119 QgsPoint segmentPoint;
120 QgsVertexId endOfSegmentVertex;
121 mCurve->closestSegment( addOperation->point(), segmentPoint, endOfSegmentVertex );
122 if ( mCurve->insertVertex( endOfSegmentVertex, segmentPoint ) )
124 break;
125 }
126
128 {
129 QgsAnnotationItemEditOperationTranslateItem *moveOperation = qgis::down_cast< QgsAnnotationItemEditOperationTranslateItem * >( operation );
130 const QTransform transform = QTransform::fromTranslate( moveOperation->translationX(), moveOperation->translationY() );
131 mCurve->transform( transform );
133 }
134
136 {
137 QgsAnnotationItemEditOperationRotateItem *rotateOperation = qgis::down_cast< QgsAnnotationItemEditOperationRotateItem * >( operation );
138 QgsPointXY center = mCurve->boundingBox().center();
139 QTransform transform = QTransform::fromTranslate( center.x(), center.y() );
140 transform.rotate( -rotateOperation->angle() );
141 transform.translate( -center.x(), -center.y() );
142 mCurve->transform( transform );
144 }
145 }
146
148}
149
151{
152 switch ( operation->type() )
153 {
155 {
156 QgsAnnotationItemEditOperationMoveNode *moveOperation = qgis::down_cast< QgsAnnotationItemEditOperationMoveNode * >( operation );
157 std::unique_ptr< QgsCurve > modifiedCurve( mCurve->clone() );
158 if ( modifiedCurve->moveVertex( moveOperation->nodeId(), QgsPoint( moveOperation->after() ) ) )
159 {
160 return new QgsAnnotationItemEditOperationTransientResults( QgsGeometry( std::move( modifiedCurve ) ) );
161 }
162 break;
163 }
164
166 {
167 QgsAnnotationItemEditOperationTranslateItem *moveOperation = qgis::down_cast< QgsAnnotationItemEditOperationTranslateItem * >( operation );
168 const QTransform transform = QTransform::fromTranslate( moveOperation->translationX(), moveOperation->translationY() );
169 std::unique_ptr< QgsCurve > modifiedCurve( mCurve->clone() );
170 modifiedCurve->transform( transform );
171 return new QgsAnnotationItemEditOperationTransientResults( QgsGeometry( std::move( modifiedCurve ) ) );
172 }
173
175 {
176 QgsAnnotationItemEditOperationRotateItem *rotateOperation = qgis::down_cast< QgsAnnotationItemEditOperationRotateItem * >( operation );
177 std::unique_ptr< QgsCurve > modifiedCurve( mCurve->clone() );
178 QgsPointXY center = modifiedCurve->boundingBox().center();
179 QTransform transform = QTransform::fromTranslate( center.x(), center.y() );
180 transform.rotate( -rotateOperation->angle() );
181 transform.translate( -center.x(), -center.y() );
182 modifiedCurve->transform( transform );
183 return new QgsAnnotationItemEditOperationTransientResults( QgsGeometry( std::move( modifiedCurve ) ) );
184 }
185
188 break;
189 }
190 return nullptr;
191}
192
197
202
203bool QgsAnnotationLineItem::readXml( const QDomElement &element, const QgsReadWriteContext &context )
204{
205 const QString wkt = element.attribute( u"wkt"_s );
207 if ( const QgsCurve *curve = qgsgeometry_cast< const QgsCurve * >( geometry.constGet() ) )
208 mCurve.reset( curve->clone() );
209
210 const QDomElement symbolElem = element.firstChildElement( u"symbol"_s );
211 if ( !symbolElem.isNull() )
212 setSymbol( QgsSymbolLayerUtils::loadSymbol< QgsLineSymbol >( symbolElem, context ).release() );
213
214 readCommonProperties( element, context );
215
216 return true;
217}
218
220{
221 return mCurve->boundingBox();
222}
223
225{
226 auto item = std::make_unique< QgsAnnotationLineItem >( mCurve->clone() );
227 item->setSymbol( mSymbol->clone() );
228 item->copyCommonProperties( this );
229 return item.release();
230}
231
233{
234 mCurve.reset( geometry );
235}
236
238{
239 return mSymbol.get();
240}
241
243{
244 mSymbol.reset( symbol );
245}
@ VertexHandle
Node is a handle for manipulating vertices.
Definition qgis.h:2664
@ SupportsReferenceScale
Item supports reference scale based rendering.
Definition qgis.h:2622
AnnotationItemEditOperationResult
Results from an edit operation on an annotation item.
Definition qgis.h:2675
@ Invalid
Operation has invalid parameters for the item, no change occurred.
Definition qgis.h:2677
@ Success
Item was modified successfully.
Definition qgis.h:2676
@ ItemCleared
The operation results in the item being cleared, and the item should be removed from the layer as a r...
Definition qgis.h:2678
QFlags< AnnotationItemFlag > AnnotationItemFlags
Annotation item flags.
Definition qgis.h:2626
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