QGIS API Documentation  3.6.0-Noosa (5873452)
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 class QgsRenderContext;
30 
49 class CORE_EXPORT QgsAnnotation : public QObject
50 {
51 
52 #ifdef SIP_RUN
54  if ( dynamic_cast< QgsTextAnnotation * >( sipCpp ) )
55  sipType = sipType_QgsTextAnnotation;
56  else if ( dynamic_cast< QgsSvgAnnotation * >( sipCpp ) )
57  sipType = sipType_QgsSvgAnnotation;
58  else if ( dynamic_cast< QgsHtmlAnnotation * >( sipCpp ) )
59  sipType = sipType_QgsHtmlAnnotation;
60  else
61  sipType = NULL;
62  SIP_END
63 #endif
64 
65 
66  Q_OBJECT
67  Q_PROPERTY( bool visible READ isVisible WRITE setVisible )
68  Q_PROPERTY( bool hasFixedMapPosition READ hasFixedMapPosition WRITE setHasFixedMapPosition )
69  Q_PROPERTY( QgsPointXY mapPosition READ mapPosition WRITE setMapPosition )
70  Q_PROPERTY( QSizeF frameSize READ frameSize WRITE setFrameSize )
71 
72  public:
73 
77  QgsAnnotation( QObject *parent SIP_TRANSFERTHIS = nullptr );
78 
83  virtual QgsAnnotation *clone() const = 0 SIP_FACTORY;
84 
89  bool isVisible() const { return mVisible; }
90 
95  void setVisible( bool visible );
96 
105  bool hasFixedMapPosition() const { return mHasFixedMapPosition; }
106 
112  void setHasFixedMapPosition( bool fixed );
113 
121  QgsPointXY mapPosition() const { return mMapPosition; }
122 
128  void setMapPosition( const QgsPointXY &position );
129 
135  QgsCoordinateReferenceSystem mapPositionCrs() const { return mMapPositionCrs; }
136 
141  void setMapPositionCrs( const QgsCoordinateReferenceSystem &crs );
142 
149  QPointF relativePosition() const { return mRelativePosition; }
150 
157  void setRelativePosition( QPointF position );
158 
163  void setFrameOffsetFromReferencePoint( QPointF offset );
164 
169  QPointF frameOffsetFromReferencePoint() const { return mOffsetFromReferencePoint; }
170 
176  void setFrameSize( QSizeF size );
177 
183  QSizeF frameSize() const { return mFrameSize; }
184 
190  void setContentsMargin( const QgsMargins &margins );
191 
197  QgsMargins contentsMargin() const { return mContentsMargins; }
198 
204  void setFillSymbol( QgsFillSymbol *symbol SIP_TRANSFER );
205 
210  QgsFillSymbol *fillSymbol() const { return mFillSymbol.get(); }
211 
215  void render( QgsRenderContext &context ) const;
216 
223  virtual void writeXml( QDomElement &elem, QDomDocument &doc, const QgsReadWriteContext &context ) const = 0;
224 
231  virtual void readXml( const QDomElement &itemElem, const QgsReadWriteContext &context ) = 0;
232 
238  void setMarkerSymbol( QgsMarkerSymbol *symbol SIP_TRANSFER );
239 
244  QgsMarkerSymbol *markerSymbol() const { return mMarkerSymbol.get(); }
245 
252  QgsMapLayer *mapLayer() const { return mMapLayer.data(); }
253 
260  void setMapLayer( QgsMapLayer *layer );
261 
267  QgsFeature associatedFeature() const { return mFeature; }
268 
273  virtual void setAssociatedFeature( const QgsFeature &feature );
274 
275  signals:
276 
278  void appearanceChanged();
279 
284  void moved();
285 
289  void mapLayerChanged();
290 
291  protected:
292 
297  virtual void renderAnnotation( QgsRenderContext &context, QSizeF size ) const = 0;
298 
303  virtual QSizeF minimumFrameSize() const;
304 
311  void _writeXml( QDomElement &itemElem, QDomDocument &doc, const QgsReadWriteContext &context ) const;
312 
319  void _readXml( const QDomElement &annotationElem, const QgsReadWriteContext &context );
320 
327  void copyCommonProperties( QgsAnnotation *target ) const;
328 
329  private:
330 
332  void updateBalloon();
333 
335  QLineF segment( int index ) const;
336 
338  QPointF pointOnLineWithDistance( QPointF startPoint, QPointF directionPoint, double distance ) const;
339 
341  void drawFrame( QgsRenderContext &context ) const;
342 
344  void drawMarkerSymbol( QgsRenderContext &context ) const;
345 
346  bool mVisible = true;
347 
349  bool mHasFixedMapPosition = true;
350 
352  QgsPointXY mMapPosition;
353 
355  QgsCoordinateReferenceSystem mMapPositionCrs;
356 
358  QPointF mRelativePosition;
359 
361  QPointF mOffsetFromReferencePoint = QPointF( 50, -50 );
362 
364  QSizeF mFrameSize;
365 
367  std::unique_ptr<QgsMarkerSymbol> mMarkerSymbol;
368 
369  QgsMargins mContentsMargins;
370 
372  std::unique_ptr<QgsFillSymbol> mFillSymbol;
373 
375  int mBalloonSegment = -1;
376 
378  QPointF mBalloonSegmentPoint1;
379 
381  QPointF mBalloonSegmentPoint2;
382 
384  QgsWeakMapLayerPointer mMapLayer;
385 
387  QgsFeature mFeature;
388 
389 };
390 
391 #endif // QGSANNOTATION_H
392 
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:64
#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:55
const QgsCoordinateReferenceSystem & crs
QPointer< QgsMapLayer > QgsWeakMapLayerPointer
Weak pointer for QgsMapLayer.
Definition: qgsmaplayer.h:1548
A marker symbol type, for rendering Point and MultiPoint geometries.
Definition: qgssymbol.h:732
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:49
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...
A fill symbol type, for rendering Polygon and MultiPolygon geometries.
Definition: qgssymbol.h:1003
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