QGIS API Documentation  3.18.1-Zürich (202f1bf7e5)
qgsannotation.h
Go to the documentation of this file.
1 /***************************************************************************
2  qgsannotation.h
3  ---------------
4  begin : January 2017
5  copyright : (C) 2017 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 #ifndef QGSANNOTATION_H
19 #define QGSANNOTATION_H
20 
21 #include "qgis_core.h"
22 #include "qgis.h"
23 #include "qgspointxy.h"
25 #include "qgssymbol.h"
26 #include "qgsmargins.h"
27 #include "qgsmaplayer.h"
28 
29 #include <QPointer>
30 
31 class QgsRenderContext;
32 
51 class CORE_EXPORT QgsAnnotation : public QObject
52 {
53 
54 #ifdef SIP_RUN
56  if ( dynamic_cast< QgsTextAnnotation * >( sipCpp ) )
57  sipType = sipType_QgsTextAnnotation;
58  else if ( dynamic_cast< QgsSvgAnnotation * >( sipCpp ) )
59  sipType = sipType_QgsSvgAnnotation;
60  else if ( dynamic_cast< QgsHtmlAnnotation * >( sipCpp ) )
61  sipType = sipType_QgsHtmlAnnotation;
62  else
63  sipType = NULL;
64  SIP_END
65 #endif
66 
67 
68  Q_OBJECT
69  Q_PROPERTY( bool visible READ isVisible WRITE setVisible )
70  Q_PROPERTY( bool hasFixedMapPosition READ hasFixedMapPosition WRITE setHasFixedMapPosition )
71  Q_PROPERTY( QgsPointXY mapPosition READ mapPosition WRITE setMapPosition )
72  Q_PROPERTY( QSizeF frameSize READ frameSize WRITE setFrameSize )
73 
74  public:
75 
79  QgsAnnotation( QObject *parent SIP_TRANSFERTHIS = nullptr );
80 
85  virtual QgsAnnotation *clone() const = 0 SIP_FACTORY;
86 
91  bool isVisible() const { return mVisible; }
92 
97  void setVisible( bool visible );
98 
107  bool hasFixedMapPosition() const { return mHasFixedMapPosition; }
108 
114  void setHasFixedMapPosition( bool fixed );
115 
123  QgsPointXY mapPosition() const { return mMapPosition; }
124 
130  void setMapPosition( const QgsPointXY &position );
131 
137  QgsCoordinateReferenceSystem mapPositionCrs() const { return mMapPositionCrs; }
138 
143  void setMapPositionCrs( const QgsCoordinateReferenceSystem &crs );
144 
151  QPointF relativePosition() const { return mRelativePosition; }
152 
159  void setRelativePosition( QPointF position );
160 
166  Q_DECL_DEPRECATED void setFrameOffsetFromReferencePoint( QPointF offset ) SIP_DEPRECATED;
167 
173  Q_DECL_DEPRECATED QPointF frameOffsetFromReferencePoint() const SIP_DEPRECATED;
174 
180  void setFrameOffsetFromReferencePointMm( QPointF offset );
181 
187  QPointF frameOffsetFromReferencePointMm() const { return mOffsetFromReferencePoint; }
188 
195  Q_DECL_DEPRECATED void setFrameSize( QSizeF size ) SIP_DEPRECATED;
196 
203  Q_DECL_DEPRECATED QSizeF frameSize() const SIP_DEPRECATED;
204 
211  void setFrameSizeMm( QSizeF size );
212 
219  QSizeF frameSizeMm() const { return mFrameSize; }
220 
226  void setContentsMargin( const QgsMargins &margins );
227 
233  QgsMargins contentsMargin() const { return mContentsMargins; }
234 
240  void setFillSymbol( QgsFillSymbol *symbol SIP_TRANSFER );
241 
246  QgsFillSymbol *fillSymbol() const { return mFillSymbol.get(); }
247 
251  void render( QgsRenderContext &context ) const;
252 
259  virtual void writeXml( QDomElement &elem, QDomDocument &doc, const QgsReadWriteContext &context ) const = 0;
260 
267  virtual void readXml( const QDomElement &itemElem, const QgsReadWriteContext &context ) = 0;
268 
274  void setMarkerSymbol( QgsMarkerSymbol *symbol SIP_TRANSFER );
275 
280  QgsMarkerSymbol *markerSymbol() const { return mMarkerSymbol.get(); }
281 
288  QgsMapLayer *mapLayer() const { return mMapLayer.data(); }
289 
296  void setMapLayer( QgsMapLayer *layer );
297 
303  QgsFeature associatedFeature() const { return mFeature; }
304 
309  virtual void setAssociatedFeature( const QgsFeature &feature );
310 
320  virtual bool accept( QgsStyleEntityVisitorInterface *visitor ) const;
321 
322  signals:
323 
326 
331  void moved();
332 
337 
338  protected:
339 
344  virtual void renderAnnotation( QgsRenderContext &context, QSizeF size ) const = 0;
345 
350  virtual QSizeF minimumFrameSize() const;
351 
358  void _writeXml( QDomElement &itemElem, QDomDocument &doc, const QgsReadWriteContext &context ) const;
359 
366  void _readXml( const QDomElement &annotationElem, const QgsReadWriteContext &context );
367 
374  void copyCommonProperties( QgsAnnotation *target ) const;
375 
376  private:
377 
379  void updateBalloon();
380 
382  QLineF segment( int index, QgsRenderContext *context ) const;
383 
385  void drawFrame( QgsRenderContext &context ) const;
386 
388  void drawMarkerSymbol( QgsRenderContext &context ) const;
389 
390  bool mVisible = true;
391 
393  bool mHasFixedMapPosition = true;
394 
396  QgsPointXY mMapPosition;
397 
399  QgsCoordinateReferenceSystem mMapPositionCrs;
400 
402  QPointF mRelativePosition;
403 
405  QPointF mOffsetFromReferencePoint = QPointF( 13, -13 );
406 
408  QSizeF mFrameSize;
409 
411  std::unique_ptr<QgsMarkerSymbol> mMarkerSymbol;
412 
413  QgsMargins mContentsMargins;
414 
416  std::unique_ptr<QgsFillSymbol> mFillSymbol;
417 
419  int mBalloonSegment = -1;
420 
422  QPointF mBalloonSegmentPoint1;
423 
425  QPointF mBalloonSegmentPoint2;
426 
428  QgsWeakMapLayerPointer mMapLayer;
429 
431  QgsFeature mFeature;
432 
434  double mSegmentPointWidthMm = 2.64;
435 
436 };
437 
438 #endif // QGSANNOTATION_H
439 
Abstract base class for annotation items which are drawn over a map.
Definition: qgsannotation.h:52
void appearanceChanged()
Emitted whenever the annotation's appearance changes.
QgsMapLayer * mapLayer() const
Returns the map layer associated with the annotation.
QgsMargins contentsMargin() const
Returns the margins (in millimeters) between the outside of the frame and the annotation content.
QgsCoordinateReferenceSystem mapPositionCrs() const
Returns the CRS of the map position, or an invalid CRS if the annotation does not have a fixed map po...
virtual void renderAnnotation(QgsRenderContext &context, QSizeF size) const =0
Renders the annotation's contents to a target /a context at the specified /a size.
void moved()
Emitted when the annotation's position has changed and items need to be moved to reflect this.
QgsFillSymbol * fillSymbol() const
Returns the symbol that is used for rendering the annotation frame.
QgsMarkerSymbol * markerSymbol() const
Returns the symbol that is drawn at the annotation's map position.
QgsPointXY mapPosition() const
Returns the map position of the annotation, if it is attached to a fixed map position.
bool hasFixedMapPosition() const
Returns true if the annotation is attached to a fixed map position, or false if the annotation uses a...
QgsFeature associatedFeature() const
Returns the feature associated with the annotation, or an invalid feature if none has been set.
virtual QgsAnnotation * clone() const =0
Clones the annotation, returning a new copy of the annotation reflecting the annotation's current sta...
bool isVisible() const
Returns true if the annotation is visible and should be rendered.
Definition: qgsannotation.h:91
virtual void readXml(const QDomElement &itemElem, const QgsReadWriteContext &context)=0
Restores the annotation's state from a DOM element.
QPointF relativePosition() const
Returns the relative position of the annotation, if it is not attached to a fixed map position.
virtual void writeXml(QDomElement &elem, QDomDocument &doc, const QgsReadWriteContext &context) const =0
Writes the annotation state to a DOM element.
void mapLayerChanged()
Emitted when the map layer associated with the annotation changes.
This class represents a coordinate reference system (CRS).
The feature class encapsulates a single feature including its id, geometry and a list of field/values...
Definition: qgsfeature.h:56
A fill symbol type, for rendering Polygon and MultiPolygon geometries.
Definition: qgssymbol.h:1307
An annotation item that embeds HTML content.
Base class for all map layer types.
Definition: qgsmaplayer.h:85
The QgsMargins class defines the four margins of a rectangle.
Definition: qgsmargins.h:38
A marker symbol type, for rendering Point and MultiPoint geometries.
Definition: qgssymbol.h:1004
A class to represent a 2D point.
Definition: qgspointxy.h:44
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.
An interface for classes which can visit style entity (e.g.
An annotation which renders the contents of an SVG file.
An annotation item that displays formatted text from a QTextDocument document.
#define SIP_CONVERT_TO_SUBCLASS_CODE(code)
Definition: qgis_sip.h:177
#define SIP_DEPRECATED
Definition: qgis_sip.h:106
#define SIP_TRANSFERTHIS
Definition: qgis_sip.h:53
#define SIP_TRANSFER
Definition: qgis_sip.h:36
#define SIP_FACTORY
Definition: qgis_sip.h:76
#define SIP_END
Definition: qgis_sip.h:194
QPointer< QgsMapLayer > QgsWeakMapLayerPointer
Weak pointer for QgsMapLayer.
Definition: qgsmaplayer.h:1769
const QgsCoordinateReferenceSystem & crs