QGIS API Documentation 3.99.0-Master (8e76e220402)
Loading...
Searching...
No Matches
qgsannotationmarkeritem.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgsannotationmarkeritem.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 "qgsmarkersymbol.h"
23#include "qgssymbol.h"
24#include "qgssymbollayerutils.h"
25
26#include <QString>
27
28using namespace Qt::StringLiterals;
29
32 , mPoint( point )
33 , mSymbol( std::make_unique< QgsMarkerSymbol >() )
34{
35
36}
37
39
41{
42 return u"marker"_s;
43}
44
46{
47 QPointF pt;
48 if ( context.coordinateTransform().isValid() )
49 {
50 double x = mPoint.x();
51 double y = mPoint.y();
52 double z = 0.0;
53 context.coordinateTransform().transformInPlace( x, y, z );
54 pt = QPointF( x, y );
55 }
56 else
57 pt = mPoint.toQPointF();
58
59 context.mapToPixel().transformInPlace( pt.rx(), pt.ry() );
60
61 mSymbol->startRender( context );
62 mSymbol->renderPoint( pt, nullptr, context );
63 mSymbol->stopRender( context );
64}
65
66bool QgsAnnotationMarkerItem::writeXml( QDomElement &element, QDomDocument &document, const QgsReadWriteContext &context ) const
67{
68 element.setAttribute( u"x"_s, qgsDoubleToString( mPoint.x() ) );
69 element.setAttribute( u"y"_s, qgsDoubleToString( mPoint.y() ) );
70 element.appendChild( QgsSymbolLayerUtils::saveSymbol( u"markerSymbol"_s, mSymbol.get(), document, context ) );
71
72 writeCommonProperties( element, document, context );
73
74 return true;
75}
76
78{
79 // in truth this should depend on whether the marker symbol is scale dependent or not!
82}
83
84QList<QgsAnnotationItemNode> QgsAnnotationMarkerItem::nodesV2( const QgsAnnotationItemEditContext & ) const
85{
87}
88
90{
91 switch ( operation->type() )
92 {
94 {
95 QgsAnnotationItemEditOperationMoveNode *moveOperation = qgis::down_cast< QgsAnnotationItemEditOperationMoveNode * >( operation );
96 mPoint = QgsPoint( moveOperation->after() );
98 }
99
101 {
103 }
104
106 {
107 QgsAnnotationItemEditOperationTranslateItem *moveOperation = qgis::down_cast< QgsAnnotationItemEditOperationTranslateItem * >( operation );
108 mPoint.setX( mPoint.x() + moveOperation->translationX() );
109 mPoint.setY( mPoint.y() + moveOperation->translationY() );
111 }
112
114 {
115 QgsAnnotationItemEditOperationRotateItem *rotateOperation = qgis::down_cast< QgsAnnotationItemEditOperationRotateItem * >( operation );
116 if ( mSymbol )
117 {
118 mSymbol->setAngle( std::fmod( mSymbol->angle() + rotateOperation->angle(), 360.0 ) );
120 }
121 break;
122 }
123
125 break;
126 }
127
129}
130
132{
133 switch ( operation->type() )
134 {
136 {
137 QgsAnnotationItemEditOperationMoveNode *moveOperation = qgis::down_cast< QgsAnnotationItemEditOperationMoveNode * >( operation );
138 return new QgsAnnotationItemEditOperationTransientResults( QgsGeometry( moveOperation->after().clone() ) );
139 }
140
142 {
143 QgsAnnotationItemEditOperationTranslateItem *moveOperation = qgis::down_cast< QgsAnnotationItemEditOperationTranslateItem * >( operation );
144 return new QgsAnnotationItemEditOperationTransientResults( QgsGeometry( new QgsPoint( mPoint.x() + moveOperation->translationX(), mPoint.y() + moveOperation->translationY() ) ) );
145 }
146
150 break;
151 }
152 return nullptr;
153}
154
159
160bool QgsAnnotationMarkerItem::readXml( const QDomElement &element, const QgsReadWriteContext &context )
161{
162 const double x = element.attribute( u"x"_s ).toDouble();
163 const double y = element.attribute( u"y"_s ).toDouble();
164 mPoint = QgsPoint( x, y );
165
166 const QDomElement symbolElem = element.firstChildElement( u"symbol"_s );
167 if ( !symbolElem.isNull() )
168 setSymbol( QgsSymbolLayerUtils::loadSymbol< QgsMarkerSymbol >( symbolElem, context ).release() );
169
170 readCommonProperties( element, context );
171 return true;
172}
173
175{
176 auto item = std::make_unique< QgsAnnotationMarkerItem >( mPoint );
177 item->setSymbol( mSymbol->clone() );
178 item->copyCommonProperties( this );
179 return item.release();
180}
181
183{
184 return QgsRectangle( mPoint.x(), mPoint.y(), mPoint.x(), mPoint.y() );
185}
186
188{
189 QPointF pt;
190 if ( context.coordinateTransform().isValid() )
191 {
192 double x = mPoint.x();
193 double y = mPoint.y();
194 double z = 0.0;
195 context.coordinateTransform().transformInPlace( x, y, z );
196 pt = QPointF( x, y );
197 }
198 else
199 pt = mPoint.toQPointF();
200
201 context.mapToPixel().transformInPlace( pt.rx(), pt.ry() );
202
203 mSymbol->startRender( context );
204 const QRectF boundsInPixels = mSymbol->bounds( pt, context );
205 mSymbol->stopRender( context );
206
207 const QgsPointXY topLeft = context.mapToPixel().toMapCoordinates( boundsInPixels.left(), boundsInPixels.top() );
208 const QgsPointXY topRight = context.mapToPixel().toMapCoordinates( boundsInPixels.right(), boundsInPixels.top() );
209 const QgsPointXY bottomLeft = context.mapToPixel().toMapCoordinates( boundsInPixels.left(), boundsInPixels.bottom() );
210 const QgsPointXY bottomRight = context.mapToPixel().toMapCoordinates( boundsInPixels.right(), boundsInPixels.bottom() );
211
212 const QgsRectangle boundsMapUnits = QgsRectangle( topLeft.x(), bottomLeft.y(), bottomRight.x(), topRight.y() );
214}
215
217{
218 return mSymbol.get();
219}
220
222{
223 mSymbol.reset( symbol );
224}
@ VertexHandle
Node is a handle for manipulating vertices.
Definition qgis.h:2566
@ ScaleDependentBoundingBox
Item's bounding box will vary depending on map scale.
Definition qgis.h:2523
@ 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
@ Reverse
Reverse/inverse transform (from destination to source).
Definition qgis.h:2731
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 moving a node.
QgsPoint after() const
Returns the node position after the move occurred (in layer coordinates).
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.
QgsAnnotationItemEditOperationTransientResults * transientEditResultsV2(QgsAbstractAnnotationItemEditOperation *operation, const QgsAnnotationItemEditContext &context) override
Retrieves the results of a transient (in progress) edit operation on the item.
Qgis::AnnotationItemFlags flags() const override
Returns item flags.
const QgsMarkerSymbol * symbol() const
Returns the symbol used to render the marker item.
QgsAnnotationMarkerItem(const QgsPoint &point)
Constructor for QgsAnnotationMarkerItem, at the specified point.
void render(QgsRenderContext &context, QgsFeedback *feedback) override
Renders the item to the specified render context.
~QgsAnnotationMarkerItem() override
QList< QgsAnnotationItemNode > nodesV2(const QgsAnnotationItemEditContext &context) const override
Returns the nodes for the item, used for editing the item.
QgsRectangle boundingBox() const override
Returns the bounding box of the item's geographic location, in the parent layer's coordinate referenc...
bool writeXml(QDomElement &element, QDomDocument &document, const QgsReadWriteContext &context) const override
Writes the item's state into an XML element.
static QgsAnnotationMarkerItem * create()
Creates a new marker annotation item.
QString type() const override
Returns a unique (untranslated) string identifying the type of item.
void setSymbol(QgsMarkerSymbol *symbol)
Sets the symbol used to render the marker item.
bool readXml(const QDomElement &element, const QgsReadWriteContext &context) override
Reads the item's state from the given DOM element.
Qgis::AnnotationItemEditOperationResult applyEditV2(QgsAbstractAnnotationItemEditOperation *operation, const QgsAnnotationItemEditContext &context) override
Applies an edit operation to the item.
QgsAnnotationMarkerItem * clone() const override
Returns a clone of the item.
void transformInPlace(double &x, double &y, double &z, Qgis::TransformDirection direction=Qgis::TransformDirection::Forward) const
Transforms an array of x, y and z double coordinates in place, from the source CRS to the destination...
QgsRectangle transformBoundingBox(const QgsRectangle &rectangle, Qgis::TransformDirection direction=Qgis::TransformDirection::Forward, bool handle180Crossover=false) const
Transforms a rectangle from the source CRS to the destination CRS.
bool isValid() const
Returns true if the coordinate transform is valid, ie both the source and destination CRS have been s...
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.
QgsPointXY toMapCoordinates(int x, int y) const
Transforms device coordinates to map (world) coordinates.
void transformInPlace(double &x, double &y) const
Transforms map coordinates to device coordinates.
A marker symbol type, for rendering Point and MultiPoint geometries.
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
QgsPoint * clone() const override
Clones the geometry by performing a deep copy.
Definition qgspoint.cpp:129
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.
QString qgsDoubleToString(double a, int precision=17)
Returns a string representation of a double.
Definition qgis.h:6852
Utility class for identifying a unique vertex within a geometry.
Definition qgsvertexid.h:34