QGIS API Documentation 4.0.0-Norrköping (1ddcee3d0e4)
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
122 break;
123 }
124
126}
127
129{
130 switch ( operation->type() )
131 {
133 {
134 QgsAnnotationItemEditOperationMoveNode *moveOperation = qgis::down_cast< QgsAnnotationItemEditOperationMoveNode * >( operation );
135 return new QgsAnnotationItemEditOperationTransientResults( QgsGeometry( moveOperation->after().clone() ) );
136 }
137
139 {
140 QgsAnnotationItemEditOperationTranslateItem *moveOperation = qgis::down_cast< QgsAnnotationItemEditOperationTranslateItem * >( operation );
141 return new QgsAnnotationItemEditOperationTransientResults( QgsGeometry( new QgsPoint( mPoint.x() + moveOperation->translationX(), mPoint.y() + moveOperation->translationY() ) ) );
142 }
143
147 break;
148 }
149 return nullptr;
150}
151
156
157bool QgsAnnotationMarkerItem::readXml( const QDomElement &element, const QgsReadWriteContext &context )
158{
159 const double x = element.attribute( u"x"_s ).toDouble();
160 const double y = element.attribute( u"y"_s ).toDouble();
161 mPoint = QgsPoint( x, y );
162
163 const QDomElement symbolElem = element.firstChildElement( u"symbol"_s );
164 if ( !symbolElem.isNull() )
165 setSymbol( QgsSymbolLayerUtils::loadSymbol< QgsMarkerSymbol >( symbolElem, context ).release() );
166
167 readCommonProperties( element, context );
168 return true;
169}
170
172{
173 auto item = std::make_unique< QgsAnnotationMarkerItem >( mPoint );
174 item->setSymbol( mSymbol->clone() );
175 item->copyCommonProperties( this );
176 return item.release();
177}
178
180{
181 return QgsRectangle( mPoint.x(), mPoint.y(), mPoint.x(), mPoint.y() );
182}
183
185{
186 QPointF pt;
187 if ( context.coordinateTransform().isValid() )
188 {
189 double x = mPoint.x();
190 double y = mPoint.y();
191 double z = 0.0;
192 context.coordinateTransform().transformInPlace( x, y, z );
193 pt = QPointF( x, y );
194 }
195 else
196 pt = mPoint.toQPointF();
197
198 context.mapToPixel().transformInPlace( pt.rx(), pt.ry() );
199
200 mSymbol->startRender( context );
201 const QRectF boundsInPixels = mSymbol->bounds( pt, context );
202 mSymbol->stopRender( context );
203
204 const QgsPointXY topLeft = context.mapToPixel().toMapCoordinates( boundsInPixels.left(), boundsInPixels.top() );
205 const QgsPointXY topRight = context.mapToPixel().toMapCoordinates( boundsInPixels.right(), boundsInPixels.top() );
206 const QgsPointXY bottomLeft = context.mapToPixel().toMapCoordinates( boundsInPixels.left(), boundsInPixels.bottom() );
207 const QgsPointXY bottomRight = context.mapToPixel().toMapCoordinates( boundsInPixels.right(), boundsInPixels.bottom() );
208
209 const QgsRectangle boundsMapUnits = QgsRectangle( topLeft.x(), bottomLeft.y(), bottomRight.x(), topRight.y() );
211}
212
214{
215 return mSymbol.get();
216}
217
219{
220 mSymbol.reset( symbol );
221}
@ VertexHandle
Node is a handle for manipulating vertices.
Definition qgis.h:2597
@ ScaleDependentBoundingBox
Item's bounding box will vary depending on map scale.
Definition qgis.h:2554
@ 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
@ Reverse
Reverse/inverse transform (from destination to source).
Definition qgis.h:2766
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:6893
Utility class for identifying a unique vertex within a geometry.
Definition qgsvertexid.h:34