QGIS API Documentation  3.23.0-Master (4fd2f04bd0)
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"
22 #include "qgsannotationitemnode.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 
61 bool 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 
72 Qgis::AnnotationItemFlags QgsAnnotationMarkerItem::flags() const
73 {
74  // in truth this should depend on whether the marker symbol is scale dependent or not!
76 }
77 
78 QList<QgsAnnotationItemNode> QgsAnnotationMarkerItem::nodes() const
79 {
81 }
82 
84 {
85  switch ( operation->type() )
86  {
88  {
89  QgsAnnotationItemEditOperationMoveNode *moveOperation = qgis::down_cast< QgsAnnotationItemEditOperationMoveNode * >( operation );
90  mPoint = QgsPoint( moveOperation->after() );
92  }
93 
95  {
97  }
98 
100  {
101  QgsAnnotationItemEditOperationTranslateItem *moveOperation = qgis::down_cast< QgsAnnotationItemEditOperationTranslateItem * >( operation );
102  mPoint.setX( mPoint.x() + moveOperation->translationX() );
103  mPoint.setY( mPoint.y() + moveOperation->translationY() );
105  }
106 
108  break;
109  }
110 
112 }
113 
115 {
116  switch ( operation->type() )
117  {
119  {
120  QgsAnnotationItemEditOperationMoveNode *moveOperation = dynamic_cast< QgsAnnotationItemEditOperationMoveNode * >( operation );
121  return new QgsAnnotationItemEditOperationTransientResults( QgsGeometry( moveOperation->after().clone() ) );
122  }
123 
125  {
126  QgsAnnotationItemEditOperationTranslateItem *moveOperation = qgis::down_cast< QgsAnnotationItemEditOperationTranslateItem * >( operation );
127  return new QgsAnnotationItemEditOperationTransientResults( QgsGeometry( new QgsPoint( mPoint.x() + moveOperation->translationX(), mPoint.y() + moveOperation->translationY() ) ) );
128  }
129 
132  break;
133  }
134  return nullptr;
135 }
136 
138 {
139  return new QgsAnnotationMarkerItem( QgsPoint() );
140 }
141 
142 bool QgsAnnotationMarkerItem::readXml( const QDomElement &element, const QgsReadWriteContext &context )
143 {
144  const double x = element.attribute( QStringLiteral( "x" ) ).toDouble();
145  const double y = element.attribute( QStringLiteral( "y" ) ).toDouble();
146  mPoint = QgsPoint( x, y );
147 
148  const QDomElement symbolElem = element.firstChildElement( QStringLiteral( "symbol" ) );
149  if ( !symbolElem.isNull() )
150  setSymbol( QgsSymbolLayerUtils::loadSymbol< QgsMarkerSymbol >( symbolElem, context ) );
151 
152  readCommonProperties( element, context );
153  return true;
154 }
155 
157 {
158  std::unique_ptr< QgsAnnotationMarkerItem > item = std::make_unique< QgsAnnotationMarkerItem >( mPoint );
159  item->setSymbol( mSymbol->clone() );
160  item->copyCommonProperties( this );
161  return item.release();
162 }
163 
165 {
166  return QgsRectangle( mPoint.x(), mPoint.y(), mPoint.x(), mPoint.y() );
167 }
168 
170 {
171  QPointF pt;
172  if ( context.coordinateTransform().isValid() )
173  {
174  double x = mPoint.x();
175  double y = mPoint.y();
176  double z = 0.0;
177  context.coordinateTransform().transformInPlace( x, y, z );
178  pt = QPointF( x, y );
179  }
180  else
181  pt = mPoint.toQPointF();
182 
183  context.mapToPixel().transformInPlace( pt.rx(), pt.ry() );
184 
185  mSymbol->startRender( context );
186  const QRectF boundsInPixels = mSymbol->bounds( pt, context );
187  mSymbol->stopRender( context );
188 
189  const QgsPointXY topLeft = context.mapToPixel().toMapCoordinates( boundsInPixels.left(), boundsInPixels.top() );
190  const QgsPointXY topRight = context.mapToPixel().toMapCoordinates( boundsInPixels.right(), boundsInPixels.top() );
191  const QgsPointXY bottomLeft = context.mapToPixel().toMapCoordinates( boundsInPixels.left(), boundsInPixels.bottom() );
192  const QgsPointXY bottomRight = context.mapToPixel().toMapCoordinates( boundsInPixels.right(), boundsInPixels.bottom() );
193 
194  const QgsRectangle boundsMapUnits = QgsRectangle( topLeft.x(), bottomLeft.y(), bottomRight.x(), topRight.y() );
195  return context.coordinateTransform().transformBoundingBox( boundsMapUnits, Qgis::TransformDirection::Reverse );
196 }
197 
199 {
200  return mSymbol.get();
201 }
202 
204 {
205  mSymbol.reset( symbol );
206 }
@ VertexHandle
Node is a handle for manipulating vertices.
@ ScaleDependentBoundingBox
Item's bounding box will vary depending on map scale.
AnnotationItemEditOperationResult
Results from an edit operation on an annotation item.
Definition: qgis.h:828
@ 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...
Abstract base class for annotation item edit operations.
@ TranslateItem
Translate (move) an item.
virtual Type type() const =0
Returns the operation type.
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.
bool writeCommonProperties(QDomElement &element, QDomDocument &document, const QgsReadWriteContext &context) const
Writes common properties from the base class into an XML element.
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.
QgsAnnotationMarkerItem * clone() override
Returns a clone of the item.
Qgis::AnnotationItemFlags flags() const override
Returns item flags.
Qgis::AnnotationItemEditOperationResult applyEdit(QgsAbstractAnnotationItemEditOperation *operation) override
Applies an edit operation to the item.
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
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.
QgsAnnotationItemEditOperationTransientResults * transientEditResults(QgsAbstractAnnotationItemEditOperation *operation) override
Retrieves the results of a transient (in progress) edit operation on the item.
QList< QgsAnnotationItemNode > nodes() const override
Returns the nodes for the item, used for editing the item.
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.
bool isValid() const
Returns true if the coordinate transform is valid, ie both the source and destination CRS have been s...
void transformInPlace(double &x, double &y, double &z, Qgis::TransformDirection direction=Qgis::TransformDirection::Forward) const SIP_THROW(QgsCsException)
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 SIP_THROW(QgsCsException)
Transforms a rectangle from the source CRS to the destination CRS.
Base class for feedback objects to be used for cancellation of something running in a worker thread.
Definition: qgsfeedback.h:45
A geometry is the spatial representation of a feature.
Definition: qgsgeometry.h:125
QgsPointXY toMapCoordinates(int x, int y) const
Transforms device coordinates to map (world) coordinates.
void transformInPlace(double &x, double &y) const
Transforms device coordinates to map coordinates.
A marker symbol type, for rendering Point and MultiPoint geometries.
A class to represent a 2D point.
Definition: qgspointxy.h:59
double y
Definition: qgspointxy.h:63
Q_GADGET double x
Definition: qgspointxy.h:62
Point geometry type, with support for z-dimension and m-values.
Definition: qgspoint.h:49
QPointF toQPointF() const SIP_HOLDGIL
Returns the point as a QPointF.
Definition: qgspoint.h:331
void setX(double x) SIP_HOLDGIL
Sets the point's x-coordinate.
Definition: qgspoint.h:280
Q_GADGET double x
Definition: qgspoint.h:52
void setY(double y) SIP_HOLDGIL
Sets the point's y-coordinate.
Definition: qgspoint.h:291
QgsPoint * clone() const override
Clones the geometry by performing a deep copy.
Definition: qgspoint.cpp:104
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.
Definition: qgsrectangle.h:42
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:1498
Utility class for identifying a unique vertex within a geometry.
Definition: qgsvertexid.h:31