QGIS API Documentation  3.2.0-Bonn (bc43194)
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 "qgsrendercontext.h"
26 #include "qgssymbol.h"
27 #include "qgsmargins.h"
28 #include "qgsmaplayer.h"
29 
48 class CORE_EXPORT QgsAnnotation : public QObject
49 {
50 
51 #ifdef SIP_RUN
53  if ( dynamic_cast< QgsTextAnnotation * >( sipCpp ) )
54  sipType = sipType_QgsTextAnnotation;
55  else if ( dynamic_cast< QgsSvgAnnotation * >( sipCpp ) )
56  sipType = sipType_QgsSvgAnnotation;
57  else if ( dynamic_cast< QgsHtmlAnnotation * >( sipCpp ) )
58  sipType = sipType_QgsHtmlAnnotation;
59  else
60  sipType = NULL;
61  SIP_END
62 #endif
63 
64 
65  Q_OBJECT
66  Q_PROPERTY( bool visible READ isVisible WRITE setVisible )
67  Q_PROPERTY( bool hasFixedMapPosition READ hasFixedMapPosition WRITE setHasFixedMapPosition )
68  Q_PROPERTY( QgsPointXY mapPosition READ mapPosition WRITE setMapPosition )
69  Q_PROPERTY( QSizeF frameSize READ frameSize WRITE setFrameSize )
70 
71  public:
72 
76  QgsAnnotation( QObject *parent SIP_TRANSFERTHIS = nullptr );
77 
82  virtual QgsAnnotation *clone() const = 0 SIP_FACTORY;
83 
88  bool isVisible() const { return mVisible; }
89 
94  void setVisible( bool visible );
95 
104  bool hasFixedMapPosition() const { return mHasFixedMapPosition; }
105 
111  void setHasFixedMapPosition( bool fixed );
112 
120  QgsPointXY mapPosition() const { return mMapPosition; }
121 
127  void setMapPosition( const QgsPointXY &position );
128 
134  QgsCoordinateReferenceSystem mapPositionCrs() const { return mMapPositionCrs; }
135 
140  void setMapPositionCrs( const QgsCoordinateReferenceSystem &crs );
141 
148  QPointF relativePosition() const { return mRelativePosition; }
149 
156  void setRelativePosition( QPointF position );
157 
162  void setFrameOffsetFromReferencePoint( QPointF offset );
163 
168  QPointF frameOffsetFromReferencePoint() const { return mOffsetFromReferencePoint; }
169 
175  void setFrameSize( QSizeF size );
176 
182  QSizeF frameSize() const { return mFrameSize; }
183 
189  void setContentsMargin( const QgsMargins &margins );
190 
196  QgsMargins contentsMargin() const { return mContentsMargins; }
197 
203  void setFillSymbol( QgsFillSymbol *symbol SIP_TRANSFER );
204 
209  QgsFillSymbol *fillSymbol() const { return mFillSymbol.get(); }
210 
214  void render( QgsRenderContext &context ) const;
215 
222  virtual void writeXml( QDomElement &elem, QDomDocument &doc, const QgsReadWriteContext &context ) const = 0;
223 
230  virtual void readXml( const QDomElement &itemElem, const QgsReadWriteContext &context ) = 0;
231 
237  void setMarkerSymbol( QgsMarkerSymbol *symbol SIP_TRANSFER );
238 
243  QgsMarkerSymbol *markerSymbol() const { return mMarkerSymbol.get(); }
244 
251  QgsMapLayer *mapLayer() const { return mMapLayer.data(); }
252 
259  void setMapLayer( QgsMapLayer *layer );
260 
266  QgsFeature associatedFeature() const { return mFeature; }
267 
272  virtual void setAssociatedFeature( const QgsFeature &feature );
273 
274  signals:
275 
277  void appearanceChanged();
278 
283  void moved();
284 
288  void mapLayerChanged();
289 
290  protected:
291 
296  virtual void renderAnnotation( QgsRenderContext &context, QSizeF size ) const = 0;
297 
302  virtual QSizeF minimumFrameSize() const;
303 
310  void _writeXml( QDomElement &itemElem, QDomDocument &doc, const QgsReadWriteContext &context ) const;
311 
318  void _readXml( const QDomElement &annotationElem, const QgsReadWriteContext &context );
319 
326  void copyCommonProperties( QgsAnnotation *target ) const;
327 
328  private:
329 
331  void updateBalloon();
332 
334  QLineF segment( int index ) const;
335 
337  QPointF pointOnLineWithDistance( QPointF startPoint, QPointF directionPoint, double distance ) const;
338 
340  void drawFrame( QgsRenderContext &context ) const;
341 
343  void drawMarkerSymbol( QgsRenderContext &context ) const;
344 
345  bool mVisible = true;
346 
348  bool mHasFixedMapPosition = true;
349 
351  QgsPointXY mMapPosition;
352 
354  QgsCoordinateReferenceSystem mMapPositionCrs;
355 
357  QPointF mRelativePosition;
358 
360  QPointF mOffsetFromReferencePoint = QPointF( 50, -50 );
361 
363  QSizeF mFrameSize;
364 
366  std::unique_ptr<QgsMarkerSymbol> mMarkerSymbol;
367 
368  QgsMargins mContentsMargins;
369 
371  std::unique_ptr<QgsFillSymbol> mFillSymbol;
372 
374  int mBalloonSegment = -1;
375 
377  QPointF mBalloonSegmentPoint1;
378 
380  QPointF mBalloonSegmentPoint2;
381 
383  QgsWeakMapLayerPointer mMapLayer;
384 
386  QgsFeature mFeature;
387 
388 };
389 
390 #endif // QGSANNOTATION_H
391 
The class is used as a container of context for various read/write operations on other objects...
Base class for all map layer types.
Definition: qgsmaplayer.h:61
#define SIP_TRANSFERTHIS
Definition: qgis_sip.h:46
A class to represent a 2D point.
Definition: qgspointxy.h:43
The feature class encapsulates a single feature including its id, geometry and a list of field/values...
Definition: qgsfeature.h:62
QPointer< QgsMapLayer > QgsWeakMapLayerPointer
Weak pointer for QgsMapLayer.
Definition: qgsmaplayer.h:1385
QPointF relativePosition() const
Returns the relative position of the annotation, if it is not attached to a fixed map position...
Abstract base class for annotation items which are drawn over a map.
Definition: qgsannotation.h:48
QgsPointXY mapPosition() const
Returns the map position of the annotation, if it is attached to a fixed map position.
QgsMargins contentsMargin() const
Returns the margins (in millimeters) between the outside of the frame and the annotation content...
#define SIP_TRANSFER
Definition: qgis_sip.h:36
#define SIP_END
Definition: qgis_sip.h:182
#define SIP_FACTORY
Definition: qgis_sip.h:69
QgsMarkerSymbol * markerSymbol() const
Returns the symbol that is drawn at the annotation&#39;s map position.
QgsMapLayer * mapLayer() const
Returns the map layer associated with the annotation.
QSizeF frameSize() const
Returns the size of the annotation&#39;s frame (the main area in which the annotation&#39;s content is drawn)...
QgsCoordinateReferenceSystem mapPositionCrs() const
Returns the CRS of the map position, or an invalid CRS if the annotation does not have a fixed map po...
Contains information about the context of a rendering operation.
QPointF frameOffsetFromReferencePoint() const
Returns the annotation&#39;s frame&#39;s offset from the mapPosition() reference point.
This class represents a coordinate reference system (CRS).
#define SIP_CONVERT_TO_SUBCLASS_CODE(code)
Definition: qgis_sip.h:165
QgsFeature associatedFeature() const
Returns the feature associated with the annotation, or an invalid feature if none has been set...
bool hasFixedMapPosition() const
Returns true if the annotation is attached to a fixed map position, or false if the annotation uses a...
QgsFillSymbol * fillSymbol() const
Returns the symbol that is used for rendering the annotation frame.
The QgsMargins class defines the four margins of a rectangle.
Definition: qgsmargins.h:37