QGIS API Documentation 3.41.0-Master (af5edcb665c)
Loading...
Searching...
No Matches
qgsannotationitem.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgsannotationitem.cpp
3 -----------------
4 begin : August 2021
5 copyright : (C) 2021 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
18#include "qgsannotationitem.h"
20#include "qgscalloutsregistry.h"
21#include "qgsapplication.h"
22#include "qgsrendercontext.h"
23#include "qgssymbollayerutils.h"
24#include "qgsunittypes.h"
25
27
29
34
39
46
51
58
59QList<QgsAnnotationItemNode> QgsAnnotationItem::nodes() const
60{
61 return {};
62}
63
64QList<QgsAnnotationItemNode> QgsAnnotationItem::nodesV2( const QgsAnnotationItemEditContext & ) const
65{
67 return nodes();
69}
70
72{
73 return mCallout.get();
74}
75
77{
78 mCallout.reset( callout );
79}
80
82{
83 return mCalloutAnchor;
84}
85
87{
88 mCalloutAnchor = anchor;
89}
90
105
106bool QgsAnnotationItem::writeCommonProperties( QDomElement &element, QDomDocument &doc, const QgsReadWriteContext &context ) const
107{
108 element.setAttribute( QStringLiteral( "enabled" ), static_cast<int>( enabled() ) );
109 element.setAttribute( QStringLiteral( "zIndex" ), zIndex() );
110 element.setAttribute( QStringLiteral( "useReferenceScale" ), useSymbologyReferenceScale() ? QStringLiteral( "1" ) : QStringLiteral( "0" ) );
111 element.setAttribute( QStringLiteral( "referenceScale" ), qgsDoubleToString( symbologyReferenceScale() ) );
112
113 if ( mCallout )
114 {
115 element.setAttribute( QStringLiteral( "calloutType" ), mCallout->type() );
116 mCallout->saveProperties( doc, element, context );
117 }
118 if ( !mCalloutAnchor.isEmpty() )
119 {
120 element.setAttribute( QStringLiteral( "calloutAnchor" ), mCalloutAnchor.asWkt() );
121 }
122 if ( mOffsetFromCallout.isValid() )
123 {
124 element.setAttribute( QStringLiteral( "offsetFromCallout" ), QgsSymbolLayerUtils::encodeSize( mOffsetFromCallout ) );
125 element.setAttribute( QStringLiteral( "offsetFromCalloutUnit" ), QgsUnitTypes::encodeUnit( mOffsetFromCalloutUnit ) );
126 }
127 return true;
128}
129
130bool QgsAnnotationItem::readCommonProperties( const QDomElement &element, const QgsReadWriteContext &context )
131{
132 setEnabled( element.attribute( QStringLiteral( "enabled" ), QStringLiteral( "1" ) ).toInt() );
133 setZIndex( element.attribute( QStringLiteral( "zIndex" ) ).toInt() );
134 setUseSymbologyReferenceScale( element.attribute( QStringLiteral( "useReferenceScale" ), QStringLiteral( "0" ) ).toInt() );
135 setSymbologyReferenceScale( element.attribute( QStringLiteral( "referenceScale" ) ).toDouble() );
136
137 const QString calloutType = element.attribute( QStringLiteral( "calloutType" ) );
138 if ( calloutType.isEmpty() )
139 {
140 mCallout.reset();
141 }
142 else
143 {
144 mCallout.reset( QgsApplication::calloutRegistry()->createCallout( calloutType, element.firstChildElement( QStringLiteral( "callout" ) ), context ) );
145 if ( !mCallout )
146 mCallout.reset( QgsCalloutRegistry::defaultCallout() );
147 }
148
149 const QString calloutAnchorWkt = element.attribute( QStringLiteral( "calloutAnchor" ) );
150 setCalloutAnchor( calloutAnchorWkt.isEmpty() ? QgsGeometry() : QgsGeometry::fromWkt( calloutAnchorWkt ) );
151
152 mOffsetFromCallout = element.attribute( QStringLiteral( "offsetFromCallout" ) ).isEmpty() ? QSizeF() : QgsSymbolLayerUtils::decodeSize( element.attribute( QStringLiteral( "offsetFromCallout" ) ) );
153 bool ok = false;
154 mOffsetFromCalloutUnit = QgsUnitTypes::decodeRenderUnit( element.attribute( QStringLiteral( "offsetFromCalloutUnit" ) ), &ok );
155 if ( !ok )
156 mOffsetFromCalloutUnit = Qgis::RenderUnit::Millimeters;
157
158 return true;
159}
160
161void QgsAnnotationItem::renderCallout( QgsRenderContext &context, const QRectF &rect, double angle, QgsCallout::QgsCalloutContext &calloutContext, QgsFeedback * )
162{
163 if ( !mCallout || mCalloutAnchor.isEmpty() )
164 return;
165
166 // anchor must be in painter coordinates
167 QgsGeometry anchor = mCalloutAnchor;
168 if ( context.coordinateTransform().isValid() )
169 {
170 try
171 {
172 anchor.transform( context.coordinateTransform() );
173 }
174 catch ( QgsCsException & )
175 {
176 return;
177 }
178 }
179 anchor.transform( context.mapToPixel().transform() );
180
181 mCallout->startRender( context );
182 mCallout->render( context, rect, angle, anchor, calloutContext );
183 mCallout->stopRender( context );
184}
185
187{
188 return mOffsetFromCalloutUnit;
189}
190
192{
193 mOffsetFromCalloutUnit = unit;
194}
195
197{
198 return mOffsetFromCallout;
199}
200
201void QgsAnnotationItem::setOffsetFromCallout( const QSizeF &offset )
202{
203 mOffsetFromCallout = offset;
204}
AnnotationItemEditOperationResult
Results from an edit operation on an annotation item.
Definition qgis.h:2414
@ Invalid
Operation has invalid parameters for the item, no change occurred.
QFlags< AnnotationItemFlag > AnnotationItemFlags
Annotation item flags.
Definition qgis.h:2365
RenderUnit
Rendering size units.
Definition qgis.h:4910
@ Millimeters
Millimeters.
Abstract base class for annotation item edit operations.
Encapsulates the context for an annotation item edit operation.
Encapsulates the transient results of an in-progress annotation edit operation.
Abstract base class for annotation items which are drawn with QgsAnnotationLayers.
virtual bool writeCommonProperties(QDomElement &element, QDomDocument &document, const QgsReadWriteContext &context) const
Writes common properties from the base class into an XML element.
void setUseSymbologyReferenceScale(bool enabled)
Sets whether the annotation item uses a symbology reference scale.
void setZIndex(int index)
Sets the item's z index, which controls the order in which annotation items are rendered in the layer...
bool useSymbologyReferenceScale() const
Returns true if the annotation item uses a symbology reference scale.
virtual ~QgsAnnotationItem()
void setCallout(QgsCallout *callout)
Sets the item's callout renderer, responsible for drawing item callouts.
virtual QList< QgsAnnotationItemNode > nodesV2(const QgsAnnotationItemEditContext &context) const
Returns the nodes for the item, used for editing the item.
void setOffsetFromCallout(const QSizeF &offset)
Sets the offset of the annotation item from the calloutAnchor().
void setEnabled(bool enabled)
Sets if the item will be rendered or not in the layer.
QgsGeometry calloutAnchor() const
Returns the callout's anchor geometry.
bool enabled() const
Returns true if the item is enabled and will be rendered in the layer.
Qgis::RenderUnit offsetFromCalloutUnit() const
Returns the units for the offsetFromCallout().
virtual Q_DECL_DEPRECATED QgsAnnotationItemEditOperationTransientResults * transientEditResults(QgsAbstractAnnotationItemEditOperation *operation)
Retrieves the results of a transient (in progress) edit operation on the item.
virtual QgsAnnotationItemEditOperationTransientResults * transientEditResultsV2(QgsAbstractAnnotationItemEditOperation *operation, const QgsAnnotationItemEditContext &context)
Retrieves the results of a transient (in progress) edit operation on the item.
virtual void copyCommonProperties(const QgsAnnotationItem *other)
Copies common properties from the base class from an other item.
void setSymbologyReferenceScale(double scale)
Sets the annotation's symbology reference scale.
virtual Qgis::AnnotationItemFlags flags() const
Returns item flags.
void setOffsetFromCalloutUnit(Qgis::RenderUnit unit)
Sets the unit for the offsetFromCallout().
virtual Q_DECL_DEPRECATED QList< QgsAnnotationItemNode > nodes() const
Returns the nodes for the item, used for editing the item.
virtual Qgis::AnnotationItemEditOperationResult applyEditV2(QgsAbstractAnnotationItemEditOperation *operation, const QgsAnnotationItemEditContext &context)
Applies an edit operation to the item.
void setCalloutAnchor(const QgsGeometry &anchor)
Sets the callout's anchor geometry.
QSizeF offsetFromCallout() const
Returns the (optional) offset of the annotation item from the calloutAnchor().
int zIndex() const
Returns the item's z index, which controls the order in which annotation items are rendered in the la...
virtual Q_DECL_DEPRECATED Qgis::AnnotationItemEditOperationResult applyEdit(QgsAbstractAnnotationItemEditOperation *operation)
Applies an edit operation to the item.
QgsCallout * callout() const
Returns the item's callout renderer, responsible for drawing item callouts.
double symbologyReferenceScale() const
Returns the annotation's symbology reference scale.
virtual bool readCommonProperties(const QDomElement &element, const QgsReadWriteContext &context)
Reads common properties from the base class from the given DOM element.
void renderCallout(QgsRenderContext &context, const QRectF &rect, double angle, QgsCallout::QgsCalloutContext &calloutContext, QgsFeedback *feedback)
Renders the item's callout.
static QgsCalloutRegistry * calloutRegistry()
Returns the application's callout registry, used for managing callout types.
static QgsCallout * defaultCallout()
Create a new instance of a callout with default settings.
Contains additional contextual information about the context in which a callout is being rendered.
Definition qgscallout.h:249
Abstract base class for callout renderers.
Definition qgscallout.h:54
virtual QgsCallout * clone() const =0
Duplicates a callout by creating a deep copy of the callout.
bool isValid() const
Returns true if the coordinate transform is valid, ie both the source and destination CRS have been s...
Custom exception class for Coordinate Reference System related exceptions.
Base class for feedback objects to be used for cancellation of something running in a worker thread.
Definition qgsfeedback.h:44
A geometry is the spatial representation of a feature.
Qgis::GeometryOperationResult transform(const QgsCoordinateTransform &ct, Qgis::TransformDirection direction=Qgis::TransformDirection::Forward, bool transformZ=false)
Transforms this geometry as described by the coordinate transform ct.
static Q_INVOKABLE QgsGeometry fromWkt(const QString &wkt)
Creates a new geometry from a WKT string.
bool isEmpty() const
Returns true if the geometry is empty (eg a linestring with no vertices, or a collection with no geom...
Q_INVOKABLE QString asWkt(int precision=17) const
Exports the geometry to WKT.
QgsPointXY transform(const QgsPointXY &p) const
Transforms a point p from map (world) coordinates to device coordinates.
The class is used as a container of context for various read/write operations on other objects.
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 QString encodeSize(QSizeF size)
Encodes a QSizeF to a string.
static QSizeF decodeSize(const QString &string)
Decodes a QSizeF from a string.
static Q_INVOKABLE Qgis::RenderUnit decodeRenderUnit(const QString &string, bool *ok=nullptr)
Decodes a render unit from a string.
static Q_INVOKABLE QString encodeUnit(Qgis::DistanceUnit unit)
Encodes a distance unit to a string.
#define Q_NOWARN_DEPRECATED_POP
Definition qgis.h:6643
QString qgsDoubleToString(double a, int precision=17)
Returns a string representation of a double.
Definition qgis.h:5983
#define Q_NOWARN_DEPRECATED_PUSH
Definition qgis.h:6642