QGIS API Documentation  3.0.2-Girona (307d082)
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 
47 class CORE_EXPORT QgsAnnotation : public QObject
48 {
49 
50 #ifdef SIP_RUN
52  if ( dynamic_cast< QgsTextAnnotation * >( sipCpp ) )
53  sipType = sipType_QgsTextAnnotation;
54  else if ( dynamic_cast< QgsSvgAnnotation * >( sipCpp ) )
55  sipType = sipType_QgsSvgAnnotation;
56  else if ( dynamic_cast< QgsHtmlAnnotation * >( sipCpp ) )
57  sipType = sipType_QgsHtmlAnnotation;
58  else
59  sipType = NULL;
60  SIP_END
61 #endif
62 
63 
64  Q_OBJECT
65  Q_PROPERTY( bool visible READ isVisible WRITE setVisible )
66  Q_PROPERTY( bool hasFixedMapPosition READ hasFixedMapPosition WRITE setHasFixedMapPosition )
67  Q_PROPERTY( QgsPointXY mapPosition READ mapPosition WRITE setMapPosition )
68  Q_PROPERTY( QSizeF frameSize READ frameSize WRITE setFrameSize )
69 
70  public:
71 
75  QgsAnnotation( QObject *parent SIP_TRANSFERTHIS = nullptr );
76 
81  virtual QgsAnnotation *clone() const = 0 SIP_FACTORY;
82 
87  bool isVisible() const { return mVisible; }
88 
93  void setVisible( bool visible );
94 
103  bool hasFixedMapPosition() const { return mHasFixedMapPosition; }
104 
110  void setHasFixedMapPosition( bool fixed );
111 
119  QgsPointXY mapPosition() const { return mMapPosition; }
120 
126  void setMapPosition( const QgsPointXY &position );
127 
133  QgsCoordinateReferenceSystem mapPositionCrs() const { return mMapPositionCrs; }
134 
139  void setMapPositionCrs( const QgsCoordinateReferenceSystem &crs );
140 
147  QPointF relativePosition() const { return mRelativePosition; }
148 
155  void setRelativePosition( QPointF position );
156 
161  void setFrameOffsetFromReferencePoint( QPointF offset );
162 
167  QPointF frameOffsetFromReferencePoint() const { return mOffsetFromReferencePoint; }
168 
174  void setFrameSize( QSizeF size );
175 
181  QSizeF frameSize() const { return mFrameSize; }
182 
188  void setContentsMargin( const QgsMargins &margins );
189 
195  QgsMargins contentsMargin() const { return mContentsMargins; }
196 
202  void setFillSymbol( QgsFillSymbol *symbol SIP_TRANSFER );
203 
208  QgsFillSymbol *fillSymbol() const { return mFillSymbol.get(); }
209 
213  void render( QgsRenderContext &context ) const;
214 
221  virtual void writeXml( QDomElement &elem, QDomDocument &doc, const QgsReadWriteContext &context ) const = 0;
222 
229  virtual void readXml( const QDomElement &itemElem, const QgsReadWriteContext &context ) = 0;
230 
236  void setMarkerSymbol( QgsMarkerSymbol *symbol SIP_TRANSFER );
237 
242  QgsMarkerSymbol *markerSymbol() const { return mMarkerSymbol.get(); }
243 
250  QgsMapLayer *mapLayer() const { return mMapLayer.data(); }
251 
258  void setMapLayer( QgsMapLayer *layer );
259 
265  QgsFeature associatedFeature() const { return mFeature; }
266 
271  virtual void setAssociatedFeature( const QgsFeature &feature );
272 
273  signals:
274 
276  void appearanceChanged();
277 
282  void moved();
283 
287  void mapLayerChanged();
288 
289  protected:
290 
295  virtual void renderAnnotation( QgsRenderContext &context, QSizeF size ) const = 0;
296 
301  virtual QSizeF minimumFrameSize() const;
302 
309  void _writeXml( QDomElement &itemElem, QDomDocument &doc, const QgsReadWriteContext &context ) const;
310 
317  void _readXml( const QDomElement &annotationElem, const QgsReadWriteContext &context );
318 
325  void copyCommonProperties( QgsAnnotation *target ) const;
326 
327  private:
328 
330  void updateBalloon();
331 
333  QLineF segment( int index ) const;
334 
336  QPointF pointOnLineWithDistance( QPointF startPoint, QPointF directionPoint, double distance ) const;
337 
339  void drawFrame( QgsRenderContext &context ) const;
340 
342  void drawMarkerSymbol( QgsRenderContext &context ) const;
343 
344  bool mVisible = true;
345 
347  bool mHasFixedMapPosition = true;
348 
350  QgsPointXY mMapPosition;
351 
353  QgsCoordinateReferenceSystem mMapPositionCrs;
354 
356  QPointF mRelativePosition;
357 
359  QPointF mOffsetFromReferencePoint = QPointF( 50, -50 );
360 
362  QSizeF mFrameSize;
363 
365  std::unique_ptr<QgsMarkerSymbol> mMarkerSymbol;
366 
367  QgsMargins mContentsMargins;
368 
370  std::unique_ptr<QgsFillSymbol> mFillSymbol;
371 
373  int mBalloonSegment = -1;
374 
376  QPointF mBalloonSegmentPoint1;
377 
379  QPointF mBalloonSegmentPoint2;
380 
382  QgsWeakMapLayerPointer mMapLayer;
383 
385  QgsFeature mFeature;
386 
387 };
388 
389 #endif // QGSANNOTATION_H
390 
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:56
#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:1349
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:47
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:175
#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