QGIS API Documentation  3.18.1-Zürich (202f1bf7e5)
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 
24  , mPoint( point )
25  , mSymbol( qgis::make_unique< QgsMarkerSymbol >() )
26 {
27 
28 }
29 
31 
33 {
34  return QStringLiteral( "marker" );
35 }
36 
38 {
39  QPointF pt;
40  if ( context.coordinateTransform().isValid() )
41  {
42  double x = mPoint.x();
43  double y = mPoint.y();
44  double z = 0.0;
45  context.coordinateTransform().transformInPlace( x, y, z );
46  pt = QPointF( x, y );
47  }
48  else
49  pt = mPoint.toQPointF();
50 
51  context.mapToPixel().transformInPlace( pt.rx(), pt.ry() );
52 
53  mSymbol->startRender( context );
54  mSymbol->renderPoint( pt, nullptr, context );
55  mSymbol->stopRender( context );
56 }
57 
58 bool QgsAnnotationMarkerItem::writeXml( QDomElement &element, QDomDocument &document, const QgsReadWriteContext &context ) const
59 {
60  element.setAttribute( QStringLiteral( "x" ), qgsDoubleToString( mPoint.x() ) );
61  element.setAttribute( QStringLiteral( "y" ), qgsDoubleToString( mPoint.y() ) );
62  element.setAttribute( QStringLiteral( "zIndex" ), zIndex() );
63 
64  element.appendChild( QgsSymbolLayerUtils::saveSymbol( QStringLiteral( "markerSymbol" ), mSymbol.get(), document, context ) );
65 
66  return true;
67 }
68 
70 {
71  return new QgsAnnotationMarkerItem( QgsPoint() );
72 }
73 
74 bool QgsAnnotationMarkerItem::readXml( const QDomElement &element, const QgsReadWriteContext &context )
75 {
76  const double x = element.attribute( QStringLiteral( "x" ) ).toDouble();
77  const double y = element.attribute( QStringLiteral( "y" ) ).toDouble();
78  mPoint = QgsPoint( x, y );
79 
80  setZIndex( element.attribute( QStringLiteral( "zIndex" ) ).toInt() );
81 
82  const QDomElement symbolElem = element.firstChildElement( QStringLiteral( "symbol" ) );
83  if ( !symbolElem.isNull() )
84  setSymbol( QgsSymbolLayerUtils::loadSymbol< QgsMarkerSymbol >( symbolElem, context ) );
85 
86  return true;
87 }
88 
90 {
91  std::unique_ptr< QgsAnnotationMarkerItem > item = qgis::make_unique< QgsAnnotationMarkerItem >( mPoint );
92  item->setSymbol( mSymbol->clone() );
93  item->setZIndex( zIndex() );
94  return item.release();
95 }
96 
98 {
99  return QgsRectangle( mPoint.x(), mPoint.y(), mPoint.x(), mPoint.y() );
100 }
101 
103 {
104  return mSymbol.get();
105 }
106 
108 {
109  mSymbol.reset( symbol );
110 }
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.
Definition: qgssymbol.h:1004
Point geometry type, with support for z-dimension and m-values.
Definition: qgspoint.h:38
QPointF toQPointF() const SIP_HOLDGIL
Returns the point as a QPointF.
Definition: qgspoint.h:320
Q_GADGET double x
Definition: qgspoint.h:41
double y
Definition: qgspoint.h:42
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:276