QGIS API Documentation 3.99.0-Master (752b475928d)
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
28 , mPoint( point )
29 , mSymbol( std::make_unique< QgsMarkerSymbol >() )
30{
31
32}
33
35
37{
38 return QStringLiteral( "marker" );
39}
40
42{
43 QPointF pt;
44 if ( context.coordinateTransform().isValid() )
45 {
46 double x = mPoint.x();
47 double y = mPoint.y();
48 double z = 0.0;
49 context.coordinateTransform().transformInPlace( x, y, z );
50 pt = QPointF( x, y );
51 }
52 else
53 pt = mPoint.toQPointF();
54
55 context.mapToPixel().transformInPlace( pt.rx(), pt.ry() );
56
57 mSymbol->startRender( context );
58 mSymbol->renderPoint( pt, nullptr, context );
59 mSymbol->stopRender( context );
60}
61
62bool QgsAnnotationMarkerItem::writeXml( QDomElement &element, QDomDocument &document, const QgsReadWriteContext &context ) const
63{
64 element.setAttribute( QStringLiteral( "x" ), qgsDoubleToString( mPoint.x() ) );
65 element.setAttribute( QStringLiteral( "y" ), qgsDoubleToString( mPoint.y() ) );
66 element.appendChild( QgsSymbolLayerUtils::saveSymbol( QStringLiteral( "markerSymbol" ), mSymbol.get(), document, context ) );
67
68 writeCommonProperties( element, document, context );
69
70 return true;
71}
72
74{
75 // in truth this should depend on whether the marker symbol is scale dependent or not!
78}
79
80QList<QgsAnnotationItemNode> QgsAnnotationMarkerItem::nodesV2( const QgsAnnotationItemEditContext & ) const
81{
83}
84
86{
87 switch ( operation->type() )
88 {
90 {
91 QgsAnnotationItemEditOperationMoveNode *moveOperation = qgis::down_cast< QgsAnnotationItemEditOperationMoveNode * >( operation );
92 mPoint = QgsPoint( moveOperation->after() );
94 }
95
97 {
99 }
100
102 {
103 QgsAnnotationItemEditOperationTranslateItem *moveOperation = qgis::down_cast< QgsAnnotationItemEditOperationTranslateItem * >( operation );
104 mPoint.setX( mPoint.x() + moveOperation->translationX() );
105 mPoint.setY( mPoint.y() + moveOperation->translationY() );
107 }
108
110 break;
111 }
112
114}
115
117{
118 switch ( operation->type() )
119 {
121 {
122 QgsAnnotationItemEditOperationMoveNode *moveOperation = qgis::down_cast< QgsAnnotationItemEditOperationMoveNode * >( operation );
123 return new QgsAnnotationItemEditOperationTransientResults( QgsGeometry( moveOperation->after().clone() ) );
124 }
125
127 {
128 QgsAnnotationItemEditOperationTranslateItem *moveOperation = qgis::down_cast< QgsAnnotationItemEditOperationTranslateItem * >( operation );
129 return new QgsAnnotationItemEditOperationTransientResults( QgsGeometry( new QgsPoint( mPoint.x() + moveOperation->translationX(), mPoint.y() + moveOperation->translationY() ) ) );
130 }
131
134 break;
135 }
136 return nullptr;
137}
138
143
144bool QgsAnnotationMarkerItem::readXml( const QDomElement &element, const QgsReadWriteContext &context )
145{
146 const double x = element.attribute( QStringLiteral( "x" ) ).toDouble();
147 const double y = element.attribute( QStringLiteral( "y" ) ).toDouble();
148 mPoint = QgsPoint( x, y );
149
150 const QDomElement symbolElem = element.firstChildElement( QStringLiteral( "symbol" ) );
151 if ( !symbolElem.isNull() )
152 setSymbol( QgsSymbolLayerUtils::loadSymbol< QgsMarkerSymbol >( symbolElem, context ).release() );
153
154 readCommonProperties( element, context );
155 return true;
156}
157
159{
160 auto item = std::make_unique< QgsAnnotationMarkerItem >( mPoint );
161 item->setSymbol( mSymbol->clone() );
162 item->copyCommonProperties( this );
163 return item.release();
164}
165
167{
168 return QgsRectangle( mPoint.x(), mPoint.y(), mPoint.x(), mPoint.y() );
169}
170
172{
173 QPointF pt;
174 if ( context.coordinateTransform().isValid() )
175 {
176 double x = mPoint.x();
177 double y = mPoint.y();
178 double z = 0.0;
179 context.coordinateTransform().transformInPlace( x, y, z );
180 pt = QPointF( x, y );
181 }
182 else
183 pt = mPoint.toQPointF();
184
185 context.mapToPixel().transformInPlace( pt.rx(), pt.ry() );
186
187 mSymbol->startRender( context );
188 const QRectF boundsInPixels = mSymbol->bounds( pt, context );
189 mSymbol->stopRender( context );
190
191 const QgsPointXY topLeft = context.mapToPixel().toMapCoordinates( boundsInPixels.left(), boundsInPixels.top() );
192 const QgsPointXY topRight = context.mapToPixel().toMapCoordinates( boundsInPixels.right(), boundsInPixels.top() );
193 const QgsPointXY bottomLeft = context.mapToPixel().toMapCoordinates( boundsInPixels.left(), boundsInPixels.bottom() );
194 const QgsPointXY bottomRight = context.mapToPixel().toMapCoordinates( boundsInPixels.right(), boundsInPixels.bottom() );
195
196 const QgsRectangle boundsMapUnits = QgsRectangle( topLeft.x(), bottomLeft.y(), bottomRight.x(), topRight.y() );
198}
199
201{
202 return mSymbol.get();
203}
204
206{
207 mSymbol.reset( symbol );
208}
@ VertexHandle
Node is a handle for manipulating vertices.
Definition qgis.h:2508
@ ScaleDependentBoundingBox
Item's bounding box will vary depending on map scale.
Definition qgis.h:2465
@ SupportsReferenceScale
Item supports reference scale based rendering.
Definition qgis.h:2466
AnnotationItemEditOperationResult
Results from an edit operation on an annotation item.
Definition qgis.h:2519
@ Invalid
Operation has invalid parameters for the item, no change occurred.
Definition qgis.h:2521
@ Success
Item was modified successfully.
Definition qgis.h:2520
@ ItemCleared
The operation results in the item being cleared, and the item should be removed from the layer as a r...
Definition qgis.h:2522
QFlags< AnnotationItemFlag > AnnotationItemFlags
Annotation item flags.
Definition qgis.h:2470
@ Reverse
Reverse/inverse transform (from destination to source).
Definition qgis.h:2673
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).
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: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
QgsPoint * clone() const override
Clones the geometry by performing a deep copy.
Definition qgspoint.cpp:108
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:6524
Utility class for identifying a unique vertex within a geometry.
Definition qgsvertexid.h:30