QGIS API Documentation  3.16.0-Hannover (43b64b13f3)
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 }
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:325
QgsReadWriteContext
The class is used as a container of context for various read/write operations on other objects.
Definition: qgsreadwritecontext.h:35
QgsPoint
Point geometry type, with support for z-dimension and m-values.
Definition: qgspoint.h:38
QgsAnnotationMarkerItem::QgsAnnotationMarkerItem
QgsAnnotationMarkerItem(const QgsPoint &point)
Constructor for QgsAnnotationMarkerItem, at the specified point.
Definition: qgsannotationmarkeritem.cpp:22
qgssymbollayerutils.h
QgsAnnotationMarkerItem::type
QString type() const override
Returns a unique (untranslated) string identifying the type of item.
Definition: qgsannotationmarkeritem.cpp:32
QgsRenderContext
Contains information about the context of a rendering operation.
Definition: qgsrendercontext.h:58
QgsAnnotationMarkerItem::render
void render(QgsRenderContext &context, QgsFeedback *feedback) override
Renders the item to the specified render context.
Definition: qgsannotationmarkeritem.cpp:37
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:892
QgsRectangle
A rectangle specified with double values.
Definition: qgsrectangle.h:42
qgsDoubleToString
QString qgsDoubleToString(double a, int precision=17)
Returns a string representation of a double.
Definition: qgis.h:275
QgsAnnotationMarkerItem
An annotation item which renders a marker symbol at a point location.
Definition: qgsannotationmarkeritem.h:33
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:97
QgsRenderContext::coordinateTransform
QgsCoordinateTransform coordinateTransform() const
Returns the current coordinate transform for the context.
Definition: qgsrendercontext.h:245
QgsPoint::y
double y
Definition: qgspoint.h:42
QgsAnnotationMarkerItem::clone
QgsAnnotationMarkerItem * clone() override
Returns a clone of the item.
Definition: qgsannotationmarkeritem.cpp:89
QgsPoint::toQPointF
QPointF toQPointF() const SIP_HOLDGIL
Returns the point as a QPointF.
Definition: qgspoint.h:320
QgsMarkerSymbol
A marker symbol type, for rendering Point and MultiPoint geometries.
Definition: qgssymbol.h:931
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:102
QgsPoint::x
Q_GADGET double x
Definition: qgspoint.h:41
QgsAnnotationMarkerItem::~QgsAnnotationMarkerItem
~QgsAnnotationMarkerItem() override
QgsAnnotationItem::zIndex
int zIndex() const
Returns the item's z index, which controls the order in which annotation items are rendered in the la...
Definition: qgsannotationitem.h:121
QgsMapToPixel::transformInPlace
void transformInPlace(double &x, double &y) const
Transform device coordinates to map coordinates.
Definition: qgsmaptopixel.cpp:233
QgsAnnotationMarkerItem::create
static QgsAnnotationMarkerItem * create()
Creates a new marker annotation item.
Definition: qgsannotationmarkeritem.cpp:69
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:58
QgsAnnotationMarkerItem::readXml
bool readXml(const QDomElement &element, const QgsReadWriteContext &context) override
Reads the item's state from the given DOM element.
Definition: qgsannotationmarkeritem.cpp:74
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:1182
QgsAnnotationItem::setZIndex
void setZIndex(int index)
Sets the item's z index, which controls the order in which annotation items are rendered in the layer...
Definition: qgsannotationitem.h:129
QgsAnnotationItem
Abstract base class for annotation items which are drawn with QgsAnnotationLayers.
Definition: qgsannotationitem.h:39
QgsAnnotationMarkerItem::setSymbol
void setSymbol(QgsMarkerSymbol *symbol)
Sets the symbol used to render the marker item.
Definition: qgsannotationmarkeritem.cpp:107
QgsCoordinateTransform::transformInPlace
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...
Definition: qgscoordinatetransform.cpp:313