QGIS API Documentation  3.26.3-Buenos Aires (65e4edfdad)
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 }
QgsAnnotationMarkerItem::transientEditResults
QgsAnnotationItemEditOperationTransientResults * transientEditResults(QgsAbstractAnnotationItemEditOperation *operation) override
Retrieves the results of a transient (in progress) edit operation on the item.
Definition: qgsannotationmarkeritem.cpp:114
QgsCoordinateTransform::transformInPlace
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...
Definition: qgscoordinatetransform.cpp:364
QgsPointXY::y
double y
Definition: qgspointxy.h:63
QgsAbstractAnnotationItemEditOperation::Type::DeleteNode
@ DeleteNode
Delete a node.
QgsRenderContext::mapToPixel
const QgsMapToPixel & mapToPixel() const
Returns the context's map to pixel transform, which transforms between map coordinates and device coo...
Definition: qgsrendercontext.h:258
QgsReadWriteContext
The class is used as a container of context for various read/write operations on other objects.
Definition: qgsreadwritecontext.h:34
QgsPoint
Point geometry type, with support for z-dimension and m-values.
Definition: qgspoint.h:48
QgsAnnotationMarkerItem::QgsAnnotationMarkerItem
QgsAnnotationMarkerItem(const QgsPoint &point)
Constructor for QgsAnnotationMarkerItem, at the specified point.
Definition: qgsannotationmarkeritem.cpp:25
QgsAnnotationItemEditOperationTranslateItem
Annotation item edit operation consisting of translating (moving) an item.
Definition: qgsannotationitemeditoperation.h:182
qgssymbollayerutils.h
QgsCoordinateTransform::transformBoundingBox
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.
Definition: qgscoordinatetransform.cpp:560
QgsAbstractAnnotationItemEditOperation::type
virtual Type type() const =0
Returns the operation type.
QgsAnnotationMarkerItem::type
QString type() const override
Returns a unique (untranslated) string identifying the type of item.
Definition: qgsannotationmarkeritem.cpp:35
QgsRenderContext
Contains information about the context of a rendering operation.
Definition: qgsrendercontext.h:59
QgsAnnotationMarkerItem::render
void render(QgsRenderContext &context, QgsFeedback *feedback) override
Renders the item to the specified render context.
Definition: qgsannotationmarkeritem.cpp:40
QgsMapToPixel::toMapCoordinates
QgsPointXY toMapCoordinates(int x, int y) const
Transforms device coordinates to map (world) coordinates.
Definition: qgsmaptopixel.h:173
QgsCoordinateTransform::isValid
bool isValid() const
Returns true if the coordinate transform is valid, ie both the source and destination CRS have been s...
Definition: qgscoordinatetransform.cpp:900
QgsRectangle
A rectangle specified with double values.
Definition: qgsrectangle.h:41
qgsDoubleToString
QString qgsDoubleToString(double a, int precision=17)
Returns a string representation of a double.
Definition: qgis.h:2204
QgsAnnotationMarkerItem
An annotation item which renders a marker symbol at a point location.
Definition: qgsannotationmarkeritem.h:32
QgsPoint::clone
QgsPoint * clone() const override
Clones the geometry by performing a deep copy.
Definition: qgspoint.cpp:104
Qgis::AnnotationItemEditOperationResult
AnnotationItemEditOperationResult
Results from an edit operation on an annotation item.
Definition: qgis.h:1153
Qgis::AnnotationItemEditOperationResult::ItemCleared
@ ItemCleared
The operation results in the item being cleared, and the item should be removed from the layer as a r...
qgsannotationmarkeritem.h
QgsAnnotationMarkerItem::boundingBox
QgsRectangle boundingBox() const override
Returns the bounding box of the item's geographic location, in the parent layer's coordinate referenc...
Definition: qgsannotationmarkeritem.cpp:164
QgsRenderContext::coordinateTransform
QgsCoordinateTransform coordinateTransform() const
Returns the current coordinate transform for the context.
Definition: qgsrendercontext.h:178
QgsPoint::y
double y
Definition: qgspoint.h:70
QgsAnnotationMarkerItem::flags
Qgis::AnnotationItemFlags flags() const override
Returns item flags.
Definition: qgsannotationmarkeritem.cpp:72
QgsAnnotationMarkerItem::clone
QgsAnnotationMarkerItem * clone() override
Returns a clone of the item.
Definition: qgsannotationmarkeritem.cpp:156
QgsAnnotationMarkerItem::applyEdit
Qgis::AnnotationItemEditOperationResult applyEdit(QgsAbstractAnnotationItemEditOperation *operation) override
Applies an edit operation to the item.
Definition: qgsannotationmarkeritem.cpp:83
QgsPoint::toQPointF
QPointF toQPointF() const SIP_HOLDGIL
Returns the point as a QPointF.
Definition: qgspoint.h:348
QgsAbstractAnnotationItemEditOperation::Type::TranslateItem
@ TranslateItem
Translate (move) an item.
QgsMarkerSymbol
A marker symbol type, for rendering Point and MultiPoint geometries.
Definition: qgsmarkersymbol.h:30
QgsFeedback
Base class for feedback objects to be used for cancellation of something running in a worker thread.
Definition: qgsfeedback.h:44
QgsAnnotationMarkerItem::symbol
const QgsMarkerSymbol * symbol() const
Returns the symbol used to render the marker item.
Definition: qgsannotationmarkeritem.cpp:198
QgsAnnotationItem::writeCommonProperties
bool writeCommonProperties(QDomElement &element, QDomDocument &document, const QgsReadWriteContext &context) const
Writes common properties from the base class into an XML element.
Definition: qgsannotationitem.cpp:48
QgsPoint::setX
void setX(double x) SIP_HOLDGIL
Sets the point's x-coordinate.
Definition: qgspoint.h:297
QgsAnnotationItem::readCommonProperties
bool readCommonProperties(const QDomElement &element, const QgsReadWriteContext &context)
Reads common properties from the base class from the given DOM element.
Definition: qgsannotationitem.cpp:56
QgsAnnotationItemEditOperationMoveNode
Annotation item edit operation consisting of moving a node.
Definition: qgsannotationitemeditoperation.h:75
QgsAbstractAnnotationItemEditOperation
Abstract base class for annotation item edit operations.
Definition: qgsannotationitemeditoperation.h:32
qgsannotationitemnode.h
QgsPointXY
A class to represent a 2D point.
Definition: qgspointxy.h:58
QgsAnnotationItemEditOperationMoveNode::after
QgsPoint after() const
Returns the node position after the move occurred (in layer coordinates).
Definition: qgsannotationitemeditoperation.h:104
QgsAnnotationMarkerItem::~QgsAnnotationMarkerItem
~QgsAnnotationMarkerItem() override
Qgis::AnnotationItemNodeType::VertexHandle
@ VertexHandle
Node is a handle for manipulating vertices.
QgsMapToPixel::transformInPlace
void transformInPlace(double &x, double &y) const
Transforms device coordinates to map coordinates.
Definition: qgsmaptopixel.h:128
Qgis::AnnotationItemEditOperationResult::Success
@ Success
Item was modified successfully.
QgsAnnotationMarkerItem::nodes
QList< QgsAnnotationItemNode > nodes() const override
Returns the nodes for the item, used for editing the item.
Definition: qgsannotationmarkeritem.cpp:78
QgsAnnotationItemNode
Contains information about a node used for editing an annotation item.
Definition: qgsannotationitemnode.h:31
QgsAbstractAnnotationItemEditOperation::Type::AddNode
@ AddNode
Add a node.
QgsAbstractAnnotationItemEditOperation::Type::MoveNode
@ MoveNode
Move a node.
QgsAnnotationMarkerItem::create
static QgsAnnotationMarkerItem * create()
Creates a new marker annotation item.
Definition: qgsannotationmarkeritem.cpp:137
QgsGeometry
A geometry is the spatial representation of a feature.
Definition: qgsgeometry.h:124
qgsmarkersymbol.h
QgsPointXY::x
double x
Definition: qgspointxy.h:62
QgsVertexId
Utility class for identifying a unique vertex within a geometry.
Definition: qgsvertexid.h:30
QgsPoint::setY
void setY(double y) SIP_HOLDGIL
Sets the point's y-coordinate.
Definition: qgspoint.h:308
Qgis::AnnotationItemFlag::ScaleDependentBoundingBox
@ ScaleDependentBoundingBox
Item's bounding box will vary depending on map scale.
QgsAnnotationMarkerItem::writeXml
bool writeXml(QDomElement &element, QDomDocument &document, const QgsReadWriteContext &context) const override
Writes the item's state into an XML element.
Definition: qgsannotationmarkeritem.cpp:61
QgsAnnotationItemEditOperationTranslateItem::translationY
double translationY() const
Returns the y-axis translation, in layer units.
Definition: qgsannotationitemeditoperation.h:211
qgsannotationitemeditoperation.h
QgsAnnotationItemEditOperationTranslateItem::translationX
double translationX() const
Returns the x-axis translation, in layer units.
Definition: qgsannotationitemeditoperation.h:204
QgsAnnotationMarkerItem::readXml
bool readXml(const QDomElement &element, const QgsReadWriteContext &context) override
Reads the item's state from the given DOM element.
Definition: qgsannotationmarkeritem.cpp:142
qgssymbol.h
QgsSymbolLayerUtils::saveSymbol
static QDomElement saveSymbol(const QString &symbolName, const QgsSymbol *symbol, QDomDocument &doc, const QgsReadWriteContext &context)
Writes a symbol definition to XML.
Definition: qgssymbollayerutils.cpp:1397
QgsPoint::x
double x
Definition: qgspoint.h:69
QgsAnnotationItem
Abstract base class for annotation items which are drawn with QgsAnnotationLayers.
Definition: qgsannotationitem.h:42
QgsAnnotationItemEditOperationTransientResults
Encapsulates the transient results of an in-progress annotation edit operation.
Definition: qgsannotationitemeditoperation.h:226
QgsAnnotationMarkerItem::setSymbol
void setSymbol(QgsMarkerSymbol *symbol)
Sets the symbol used to render the marker item.
Definition: qgsannotationmarkeritem.cpp:203
Qgis::AnnotationItemEditOperationResult::Invalid
@ Invalid
Operation has invalid parameters for the item, no change occurred.