QGIS API Documentation  3.20.0-Odense (decaadbb31)
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 
25  , mPoint( point )
26  , mSymbol( std::make_unique< QgsMarkerSymbol >() )
27 {
28 
29 }
30 
32 
34 {
35  return QStringLiteral( "marker" );
36 }
37 
39 {
40  QPointF pt;
41  if ( context.coordinateTransform().isValid() )
42  {
43  double x = mPoint.x();
44  double y = mPoint.y();
45  double z = 0.0;
46  context.coordinateTransform().transformInPlace( x, y, z );
47  pt = QPointF( x, y );
48  }
49  else
50  pt = mPoint.toQPointF();
51 
52  context.mapToPixel().transformInPlace( pt.rx(), pt.ry() );
53 
54  mSymbol->startRender( context );
55  mSymbol->renderPoint( pt, nullptr, context );
56  mSymbol->stopRender( context );
57 }
58 
59 bool QgsAnnotationMarkerItem::writeXml( QDomElement &element, QDomDocument &document, const QgsReadWriteContext &context ) const
60 {
61  element.setAttribute( QStringLiteral( "x" ), qgsDoubleToString( mPoint.x() ) );
62  element.setAttribute( QStringLiteral( "y" ), qgsDoubleToString( mPoint.y() ) );
63  element.setAttribute( QStringLiteral( "zIndex" ), zIndex() );
64 
65  element.appendChild( QgsSymbolLayerUtils::saveSymbol( QStringLiteral( "markerSymbol" ), mSymbol.get(), document, context ) );
66 
67  return true;
68 }
69 
71 {
72  return new QgsAnnotationMarkerItem( QgsPoint() );
73 }
74 
75 bool QgsAnnotationMarkerItem::readXml( const QDomElement &element, const QgsReadWriteContext &context )
76 {
77  const double x = element.attribute( QStringLiteral( "x" ) ).toDouble();
78  const double y = element.attribute( QStringLiteral( "y" ) ).toDouble();
79  mPoint = QgsPoint( x, y );
80 
81  setZIndex( element.attribute( QStringLiteral( "zIndex" ) ).toInt() );
82 
83  const QDomElement symbolElem = element.firstChildElement( QStringLiteral( "symbol" ) );
84  if ( !symbolElem.isNull() )
85  setSymbol( QgsSymbolLayerUtils::loadSymbol< QgsMarkerSymbol >( symbolElem, context ) );
86 
87  return true;
88 }
89 
91 {
92  std::unique_ptr< QgsAnnotationMarkerItem > item = std::make_unique< QgsAnnotationMarkerItem >( mPoint );
93  item->setSymbol( mSymbol->clone() );
94  item->setZIndex( zIndex() );
95  return item.release();
96 }
97 
99 {
100  return QgsRectangle( mPoint.x(), mPoint.y(), mPoint.x(), mPoint.y() );
101 }
102 
104 {
105  return mSymbol.get();
106 }
107 
109 {
110  mSymbol.reset( symbol );
111 }
Abstract base class for annotation items which are drawn with QgsAnnotationLayers.
void setZIndex(int index)
Sets the item's z index, which controls the order in which annotation items are rendered in the layer...
int zIndex() const
Returns the item's z index, which controls the order in which annotation items are rendered in the la...
An annotation item which renders a marker symbol at a point location.
QgsAnnotationMarkerItem * clone() override
Returns a clone of 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.
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.
void transformInPlace(double &x, double &y, double &z, TransformDirection direction=ForwardTransform) const SIP_THROW(QgsCsException)
Transforms an array of x, y and z double coordinates in place, from the source CRS to the destination...
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:45
void transformInPlace(double &x, double &y) const
Transforms device coordinates to map coordinates.
A marker symbol type, for rendering Point and MultiPoint geometries.
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
Q_GADGET 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.
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:550