QGIS API Documentation 4.3.0-Master (35968c35f7e)
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"
19
21#include "qgsapplication.h"
22#include "qgscalloutsregistry.h"
23#include "qgspainting.h"
24#include "qgsrendercontext.h"
25#include "qgssymbollayerutils.h"
26#include "qgsunittypes.h"
27
28#include <QPainter>
29#include <QString>
30#include <QTransform>
31
32using namespace Qt::StringLiterals;
33
35
37
42
47
54
59
66
67QList<QgsAnnotationItemNode> QgsAnnotationItem::nodes() const
68{
69 return {};
70}
71
72QList<QgsAnnotationItemNode> QgsAnnotationItem::nodesV2( const QgsAnnotationItemEditContext & ) const
73{
75 return nodes();
77}
78
80{
81 return mCallout.get();
82}
83
85{
86 mCallout.reset( callout );
87}
88
90{
91 return mCalloutAnchor;
92}
93
95{
96 mCalloutAnchor = anchor;
97}
98
113
114bool QgsAnnotationItem::writeCommonProperties( QDomElement &element, QDomDocument &doc, const QgsReadWriteContext &context ) const
115{
116 element.setAttribute( u"enabled"_s, static_cast<int>( enabled() ) );
117 element.setAttribute( u"zIndex"_s, zIndex() );
118 element.setAttribute( u"useReferenceScale"_s, useSymbologyReferenceScale() ? u"1"_s : u"0"_s );
119 element.setAttribute( u"referenceScale"_s, qgsDoubleToString( symbologyReferenceScale() ) );
120
121 if ( mCallout )
122 {
123 element.setAttribute( u"calloutType"_s, mCallout->type() );
124 mCallout->saveProperties( doc, element, context );
125 }
126 if ( !mCalloutAnchor.isEmpty() )
127 {
128 element.setAttribute( u"calloutAnchor"_s, mCalloutAnchor.asWkt() );
129 }
130 if ( mOffsetFromCallout.isValid() )
131 {
132 element.setAttribute( u"offsetFromCallout"_s, QgsSymbolLayerUtils::encodeSize( mOffsetFromCallout ) );
133 element.setAttribute( u"offsetFromCalloutUnit"_s, QgsUnitTypes::encodeUnit( mOffsetFromCalloutUnit ) );
134 }
135 return true;
136}
137
138bool QgsAnnotationItem::readCommonProperties( const QDomElement &element, const QgsReadWriteContext &context )
139{
140 setEnabled( element.attribute( u"enabled"_s, u"1"_s ).toInt() );
141 setZIndex( element.attribute( u"zIndex"_s ).toInt() );
142 setUseSymbologyReferenceScale( element.attribute( u"useReferenceScale"_s, u"0"_s ).toInt() );
143 setSymbologyReferenceScale( element.attribute( u"referenceScale"_s ).toDouble() );
144
145 const QString calloutType = element.attribute( u"calloutType"_s );
146 if ( calloutType.isEmpty() )
147 {
148 mCallout.reset();
149 }
150 else
151 {
152 mCallout = QgsApplication::calloutRegistry()->createCallout( calloutType, element.firstChildElement( u"callout"_s ), context );
153 if ( !mCallout )
155 }
156
157 const QString calloutAnchorWkt = element.attribute( u"calloutAnchor"_s );
158 setCalloutAnchor( calloutAnchorWkt.isEmpty() ? QgsGeometry() : QgsGeometry::fromWkt( calloutAnchorWkt ) );
159
160 mOffsetFromCallout = element.attribute( u"offsetFromCallout"_s ).isEmpty() ? QSizeF() : QgsSymbolLayerUtils::decodeSize( element.attribute( u"offsetFromCallout"_s ) );
161 bool ok = false;
162 mOffsetFromCalloutUnit = QgsUnitTypes::decodeRenderUnit( element.attribute( u"offsetFromCalloutUnit"_s ), &ok );
163 if ( !ok )
164 mOffsetFromCalloutUnit = Qgis::RenderUnit::Millimeters;
165
166 return true;
167}
168
169void QgsAnnotationItem::renderCallout( QgsRenderContext &context, const QRectF &rect, double angle, QgsCallout::QgsCalloutContext &calloutContext, QgsFeedback * )
170{
171 if ( !mCallout || mCalloutAnchor.isEmpty() )
172 return;
173
174 // anchor must be in painter coordinates
175 QgsGeometry anchor = mCalloutAnchor;
176 if ( context.coordinateTransform().isValid() )
177 {
178 try
179 {
180 anchor.transform( context.coordinateTransform() );
181 }
182 catch ( QgsCsException & )
183 {
184 return;
185 }
186 }
187 anchor.transform( context.mapToPixel().transform() );
188
189 // Rotate the painter with the item, but counter-rotate the anchor so the
190 // callout still points to its anchor on screen.
191 QPainter *painter = context.painter();
192 const bool rotated = !qgsDoubleNear( angle, 0 );
193
195 if ( rotated )
196 {
197 const QPointF center = rect.center();
198 QTransform anchorTransform;
199 anchorTransform.translate( center.x(), center.y() );
200 anchorTransform.rotate( -angle );
201 anchorTransform.translate( -center.x(), -center.y() );
202 anchor.transform( anchorTransform );
203
204 painterState.save();
205 QgsPainting::rotatePainterAroundPoint( painter, center, angle );
206 }
207
208 mCallout->startRender( context );
209 mCallout->render( context, rect, 0, anchor, calloutContext );
210 mCallout->stopRender( context );
211}
212
214{
215 return mOffsetFromCalloutUnit;
216}
217
219{
220 mOffsetFromCalloutUnit = unit;
221}
222
224{
225 return mOffsetFromCallout;
226}
227
228void QgsAnnotationItem::setOffsetFromCallout( const QSizeF &offset )
229{
230 mOffsetFromCallout = offset;
231}
AnnotationItemEditOperationResult
Results from an edit operation on an annotation item.
Definition qgis.h:2726
@ Invalid
Operation has invalid parameters for the item, no change occurred.
Definition qgis.h:2728
QFlags< AnnotationItemFlag > AnnotationItemFlags
Annotation item flags.
Definition qgis.h:2677
RenderUnit
Rendering size units.
Definition qgis.h:5758
@ Millimeters
Millimeters.
Definition qgis.h:5759
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.
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 std::unique_ptr< QgsCallout > defaultCallout()
Create a new instance of a callout with default settings.
std::unique_ptr< QgsCallout > createCallout(const QString &type, const QVariantMap &properties=QVariantMap(), const QgsReadWriteContext &context=QgsReadWriteContext()) const
Creates a new instance of a callout, given the callout type and properties.
Contains additional contextual information about the context in which a callout is being rendered.
Definition qgscallout.h:248
Abstract base class for callout renderers.
Definition qgscallout.h:55
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.
QgsPointXY transform(const QgsPointXY &p) const
Transforms a point p from map (world) coordinates to device coordinates.
static void rotatePainterAroundPoint(QPainter *painter, const QPointF &point, double angle)
Rotates a painter by angle degrees clockwise around point.
A container for the context for various read/write operations on objects.
Contains information about the context of a rendering operation.
QPainter * painter()
Returns the destination QPainter for the render 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.
Scoped object for saving and restoring a QPainter object's state.
@ NoSave
Do not save the current painter state.
void save()
Performs a manual save on the painter state, pushing another saved state to the painter.
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:8361
QString qgsDoubleToString(double a, int precision=17)
Returns a string representation of a double.
Definition qgis.h:7632
#define Q_NOWARN_DEPRECATED_PUSH
Definition qgis.h:8360
bool qgsDoubleNear(double a, double b, double epsilon=4 *std::numeric_limits< double >::epsilon())
Compare two doubles (but allow some difference).
Definition qgis.h:7725