QGIS API Documentation 3.40.0-Bratislava (b56115d8743)
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#include "qgssymbol.h"
20#include "qgssymbollayerutils.h"
21#include "qgsmarkersymbol.h"
24
27 , mPoint( point )
28 , mSymbol( std::make_unique< QgsMarkerSymbol >() )
29{
30
31}
32
34
36{
37 return QStringLiteral( "marker" );
38}
39
41{
42 QPointF pt;
43 if ( context.coordinateTransform().isValid() )
44 {
45 double x = mPoint.x();
46 double y = mPoint.y();
47 double z = 0.0;
48 context.coordinateTransform().transformInPlace( x, y, z );
49 pt = QPointF( x, y );
50 }
51 else
52 pt = mPoint.toQPointF();
53
54 context.mapToPixel().transformInPlace( pt.rx(), pt.ry() );
55
56 mSymbol->startRender( context );
57 mSymbol->renderPoint( pt, nullptr, context );
58 mSymbol->stopRender( context );
59}
60
61bool QgsAnnotationMarkerItem::writeXml( QDomElement &element, QDomDocument &document, const QgsReadWriteContext &context ) const
62{
63 element.setAttribute( QStringLiteral( "x" ), qgsDoubleToString( mPoint.x() ) );
64 element.setAttribute( QStringLiteral( "y" ), qgsDoubleToString( mPoint.y() ) );
65 element.appendChild( QgsSymbolLayerUtils::saveSymbol( QStringLiteral( "markerSymbol" ), mSymbol.get(), document, context ) );
66
67 writeCommonProperties( element, document, context );
68
69 return true;
70}
71
73{
74 // in truth this should depend on whether the marker symbol is scale dependent or not!
77}
78
79QList<QgsAnnotationItemNode> QgsAnnotationMarkerItem::nodesV2( const QgsAnnotationItemEditContext & ) const
80{
82}
83
85{
86 switch ( operation->type() )
87 {
89 {
90 QgsAnnotationItemEditOperationMoveNode *moveOperation = qgis::down_cast< QgsAnnotationItemEditOperationMoveNode * >( operation );
91 mPoint = QgsPoint( moveOperation->after() );
93 }
94
96 {
98 }
99
101 {
102 QgsAnnotationItemEditOperationTranslateItem *moveOperation = qgis::down_cast< QgsAnnotationItemEditOperationTranslateItem * >( operation );
103 mPoint.setX( mPoint.x() + moveOperation->translationX() );
104 mPoint.setY( mPoint.y() + moveOperation->translationY() );
106 }
107
109 break;
110 }
111
113}
114
116{
117 switch ( operation->type() )
118 {
120 {
121 QgsAnnotationItemEditOperationMoveNode *moveOperation = dynamic_cast< QgsAnnotationItemEditOperationMoveNode * >( operation );
122 return new QgsAnnotationItemEditOperationTransientResults( QgsGeometry( moveOperation->after().clone() ) );
123 }
124
126 {
127 QgsAnnotationItemEditOperationTranslateItem *moveOperation = qgis::down_cast< QgsAnnotationItemEditOperationTranslateItem * >( operation );
128 return new QgsAnnotationItemEditOperationTransientResults( QgsGeometry( new QgsPoint( mPoint.x() + moveOperation->translationX(), mPoint.y() + moveOperation->translationY() ) ) );
129 }
130
133 break;
134 }
135 return nullptr;
136}
137
142
143bool QgsAnnotationMarkerItem::readXml( const QDomElement &element, const QgsReadWriteContext &context )
144{
145 const double x = element.attribute( QStringLiteral( "x" ) ).toDouble();
146 const double y = element.attribute( QStringLiteral( "y" ) ).toDouble();
147 mPoint = QgsPoint( x, y );
148
149 const QDomElement symbolElem = element.firstChildElement( QStringLiteral( "symbol" ) );
150 if ( !symbolElem.isNull() )
151 setSymbol( QgsSymbolLayerUtils::loadSymbol< QgsMarkerSymbol >( symbolElem, context ) );
152
153 readCommonProperties( element, context );
154 return true;
155}
156
158{
159 std::unique_ptr< QgsAnnotationMarkerItem > item = std::make_unique< QgsAnnotationMarkerItem >( mPoint );
160 item->setSymbol( mSymbol->clone() );
161 item->copyCommonProperties( this );
162 return item.release();
163}
164
166{
167 return QgsRectangle( mPoint.x(), mPoint.y(), mPoint.x(), mPoint.y() );
168}
169
171{
172 QPointF pt;
173 if ( context.coordinateTransform().isValid() )
174 {
175 double x = mPoint.x();
176 double y = mPoint.y();
177 double z = 0.0;
178 context.coordinateTransform().transformInPlace( x, y, z );
179 pt = QPointF( x, y );
180 }
181 else
182 pt = mPoint.toQPointF();
183
184 context.mapToPixel().transformInPlace( pt.rx(), pt.ry() );
185
186 mSymbol->startRender( context );
187 const QRectF boundsInPixels = mSymbol->bounds( pt, context );
188 mSymbol->stopRender( context );
189
190 const QgsPointXY topLeft = context.mapToPixel().toMapCoordinates( boundsInPixels.left(), boundsInPixels.top() );
191 const QgsPointXY topRight = context.mapToPixel().toMapCoordinates( boundsInPixels.right(), boundsInPixels.top() );
192 const QgsPointXY bottomLeft = context.mapToPixel().toMapCoordinates( boundsInPixels.left(), boundsInPixels.bottom() );
193 const QgsPointXY bottomRight = context.mapToPixel().toMapCoordinates( boundsInPixels.right(), boundsInPixels.bottom() );
194
195 const QgsRectangle boundsMapUnits = QgsRectangle( topLeft.x(), bottomLeft.y(), bottomRight.x(), topRight.y() );
197}
198
200{
201 return mSymbol.get();
202}
203
205{
206 mSymbol.reset( symbol );
207}
@ VertexHandle
Node is a handle for manipulating vertices.
@ ScaleDependentBoundingBox
Item's bounding box will vary depending on map scale.
@ SupportsReferenceScale
Item supports reference scale based rendering.
AnnotationItemEditOperationResult
Results from an edit operation on an annotation item.
Definition qgis.h:2351
@ Invalid
Operation has invalid parameters for the item, no change occurred.
@ Success
Item was modified successfully.
@ ItemCleared
The operation results in the item being cleared, and the item should be removed from the layer as a r...
QFlags< AnnotationItemFlag > AnnotationItemFlags
Annotation item flags.
Definition qgis.h:2302
@ Reverse
Reverse/inverse transform (from destination to source)
Abstract base class for annotation item edit operations.
@ TranslateItem
Translate (move) an item.
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).
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.
Abstract base class for annotation items which are drawn with QgsAnnotationLayers.
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.
An annotation item which renders a marker symbol at a point location.
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.
A class to represent a 2D point.
Definition qgspointxy.h:60
double y
Definition qgspointxy.h:64
double x
Definition qgspointxy.h:63
Point geometry type, with support for z-dimension and m-values.
Definition qgspoint.h:49
void setY(double y)
Sets the point's y-coordinate.
Definition qgspoint.h:343
void setX(double x)
Sets the point's x-coordinate.
Definition qgspoint.h:332
QPointF toQPointF() const
Returns the point as a QPointF.
Definition qgspoint.h:382
QgsPoint * clone() const override
Clones the geometry by performing a deep copy.
Definition qgspoint.cpp:104
double x
Definition qgspoint.h:52
double y
Definition qgspoint.h:53
The class is used as a container of context for various read/write operations on other 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 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:5834
Utility class for identifying a unique vertex within a geometry.
Definition qgsvertexid.h:30