QGIS API Documentation 4.3.0-Master (d57cc4a041c)
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
37
39{
40 return u"marker"_s;
41}
42
44{
45 QPointF pt;
46 if ( context.coordinateTransform().isValid() )
47 {
48 double x = mPoint.x();
49 double y = mPoint.y();
50 double z = 0.0;
51 context.coordinateTransform().transformInPlace( x, y, z );
52 pt = QPointF( x, y );
53 }
54 else
55 pt = mPoint.toQPointF();
56
57 context.mapToPixel().transformInPlace( pt.rx(), pt.ry() );
58
59 mSymbol->startRender( context );
60 mSymbol->renderPoint( pt, nullptr, context );
61 mSymbol->stopRender( context );
62}
63
64bool QgsAnnotationMarkerItem::writeXml( QDomElement &element, QDomDocument &document, const QgsReadWriteContext &context ) const
65{
66 element.setAttribute( u"x"_s, qgsDoubleToString( mPoint.x() ) );
67 element.setAttribute( u"y"_s, qgsDoubleToString( mPoint.y() ) );
68 element.appendChild( QgsSymbolLayerUtils::saveSymbol( u"markerSymbol"_s, mSymbol.get(), document, context ) );
69
70 writeCommonProperties( element, document, context );
71
72 return true;
73}
74
76{
77 // in truth this should depend on whether the marker symbol is scale dependent or not!
79}
80
81QList<QgsAnnotationItemNode> QgsAnnotationMarkerItem::nodesV2( const QgsAnnotationItemEditContext & ) const
82{
84}
85
87{
88 switch ( operation->type() )
89 {
91 {
92 QgsAnnotationItemEditOperationMoveNode *moveOperation = qgis::down_cast< QgsAnnotationItemEditOperationMoveNode * >( operation );
93 mPoint = QgsPoint( moveOperation->after() );
95 }
96
98 {
100 }
101
103 {
104 QgsAnnotationItemEditOperationTranslateItem *moveOperation = qgis::down_cast< QgsAnnotationItemEditOperationTranslateItem * >( operation );
105 mPoint.setX( mPoint.x() + moveOperation->translationX() );
106 mPoint.setY( mPoint.y() + moveOperation->translationY() );
108 }
109
111 {
112 QgsAnnotationItemEditOperationRotateItem *rotateOperation = qgis::down_cast< QgsAnnotationItemEditOperationRotateItem * >( operation );
113 if ( mSymbol )
114 {
115 mSymbol->setAngle( std::fmod( mSymbol->angle() + rotateOperation->angle(), 360.0 ) );
117 }
118 break;
119 }
120
123 break;
124 }
125
127}
128
130{
131 switch ( operation->type() )
132 {
134 {
135 QgsAnnotationItemEditOperationMoveNode *moveOperation = qgis::down_cast< QgsAnnotationItemEditOperationMoveNode * >( operation );
136 return new QgsAnnotationItemEditOperationTransientResults( QgsGeometry( moveOperation->after().clone() ) );
137 }
138
140 {
141 QgsAnnotationItemEditOperationTranslateItem *moveOperation = qgis::down_cast< QgsAnnotationItemEditOperationTranslateItem * >( operation );
142 return new QgsAnnotationItemEditOperationTransientResults( QgsGeometry( new QgsPoint( mPoint.x() + moveOperation->translationX(), mPoint.y() + moveOperation->translationY() ) ) );
143 }
144
149 break;
150 }
151 return nullptr;
152}
153
158
159bool QgsAnnotationMarkerItem::readXml( const QDomElement &element, const QgsReadWriteContext &context )
160{
161 const double x = element.attribute( u"x"_s ).toDouble();
162 const double y = element.attribute( u"y"_s ).toDouble();
163 mPoint = QgsPoint( x, y );
164
165 const QDomElement symbolElem = element.firstChildElement( u"symbol"_s );
166 if ( !symbolElem.isNull() )
167 setSymbol( QgsSymbolLayerUtils::loadSymbol< QgsMarkerSymbol >( symbolElem, context ).release() );
168
169 readCommonProperties( element, context );
170 return true;
171}
172
174{
175 auto item = std::make_unique< QgsAnnotationMarkerItem >( mPoint );
176 item->setSymbol( mSymbol->clone() );
177 item->copyCommonProperties( this );
178 return item.release();
179}
180
182{
183 return QgsRectangle( mPoint.x(), mPoint.y(), mPoint.x(), mPoint.y() );
184}
185
187{
188 QPointF pt;
189 if ( context.coordinateTransform().isValid() )
190 {
191 double x = mPoint.x();
192 double y = mPoint.y();
193 double z = 0.0;
194 context.coordinateTransform().transformInPlace( x, y, z );
195 pt = QPointF( x, y );
196 }
197 else
198 pt = mPoint.toQPointF();
199
200 context.mapToPixel().transformInPlace( pt.rx(), pt.ry() );
201
202 mSymbol->startRender( context );
203 const QRectF boundsInPixels = mSymbol->bounds( pt, context );
204 mSymbol->stopRender( context );
205
206 const QgsPointXY topLeft = context.mapToPixel().toMapCoordinates( boundsInPixels.left(), boundsInPixels.top() );
207 const QgsPointXY topRight = context.mapToPixel().toMapCoordinates( boundsInPixels.right(), boundsInPixels.top() );
208 const QgsPointXY bottomLeft = context.mapToPixel().toMapCoordinates( boundsInPixels.left(), boundsInPixels.bottom() );
209 const QgsPointXY bottomRight = context.mapToPixel().toMapCoordinates( boundsInPixels.right(), boundsInPixels.bottom() );
210
211 const QgsRectangle boundsMapUnits = QgsRectangle( topLeft.x(), bottomLeft.y(), bottomRight.x(), topRight.y() );
213}
214
216{
217 return mSymbol.get();
218}
219
221{
222 mSymbol.reset( symbol );
223}
@ VertexHandle
Node is a handle for manipulating vertices.
Definition qgis.h:2695
@ ScaleDependentBoundingBox
Item's bounding box will vary depending on map scale.
Definition qgis.h:2652
@ SupportsReferenceScale
Item supports reference scale based rendering.
Definition qgis.h:2653
AnnotationItemEditOperationResult
Results from an edit operation on an annotation item.
Definition qgis.h:2706
@ Invalid
Operation has invalid parameters for the item, no change occurred.
Definition qgis.h:2708
@ Success
Item was modified successfully.
Definition qgis.h:2707
@ ItemCleared
The operation results in the item being cleared, and the item should be removed from the layer as a r...
Definition qgis.h:2709
QFlags< AnnotationItemFlag > AnnotationItemFlags
Annotation item flags.
Definition qgis.h:2657
@ Reverse
Reverse/inverse transform (from destination to source).
Definition qgis.h:2864
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:138
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:7451
Utility class for identifying a unique vertex within a geometry.
Definition qgsvertexid.h:35