QGIS API Documentation  3.4.3-Madeira (2f64a3c)
qgssymbol.h
Go to the documentation of this file.
1 /***************************************************************************
2  qgssymbol.h
3  ---------------------
4  begin : November 2009
5  copyright : (C) 2009 by Martin Dobias
6  email : wonder dot sk at gmail dot com
7  ***************************************************************************
8  * *
9  * This program is free software; you can redistribute it and/or modify *
10  * it under the terms of the GNU General Public License as published by *
11  * the Free Software Foundation; either version 2 of the License, or *
12  * (at your option) any later version. *
13  * *
14  ***************************************************************************/
15 
16 #ifndef QGSSYMBOL_H
17 #define QGSSYMBOL_H
18 
19 #include "qgis_core.h"
20 #include "qgis.h"
21 #include <QList>
22 #include <QMap>
23 #include "qgsmapunitscale.h"
24 #include "qgsfields.h"
25 #include "qgsrendercontext.h"
26 #include "qgsproperty.h"
27 
28 class QColor;
29 class QImage;
30 class QPainter;
31 class QSize;
32 class QPointF;
33 class QPolygonF;
34 class QDomDocument;
35 class QDomElement;
36 
37 class QgsFields;
38 class QgsSymbolLayer;
39 class QgsRenderContext;
40 class QgsVectorLayer;
41 class QgsPaintEffect;
43 class QgsLineSymbolLayer;
44 class QgsFillSymbolLayer;
46 class QgsFeature;
47 class QgsFeatureRenderer;
48 class QgsCurve;
49 class QgsPolygon;
51 class QgsPoint;
52 
53 typedef QList<QgsSymbolLayer *> QgsSymbolLayerList;
54 
61 class CORE_EXPORT QgsSymbol
62 {
63 
64 #ifdef SIP_RUN
66  switch ( sipCpp->type() )
67  {
68  case QgsSymbol::Marker: sipType = sipType_QgsMarkerSymbol; break;
69  case QgsSymbol::Line: sipType = sipType_QgsLineSymbol; break;
70  case QgsSymbol::Fill: sipType = sipType_QgsFillSymbol; break;
71  default: sipType = 0; break;
72  }
73  SIP_END
74 #endif
75 
76  friend class QgsFeatureRenderer;
77 
78  public:
79 
84  {
86  Line,
87  Fill,
88  Hybrid
89  };
90 
95  {
97  ScaleDiameter
98  };
99 
100 
103  {
104  DynamicRotation = 2,
105  };
106  Q_DECLARE_FLAGS( RenderHints, RenderHint )
107 
108  virtual ~QgsSymbol();
109 
115  static QgsSymbol *defaultSymbol( QgsWkbTypes::GeometryType geomType ) SIP_FACTORY;
116 
120  SymbolType type() const { return mType; }
121 
122  // symbol layers handling
123 
131  QgsSymbolLayerList symbolLayers() { return mLayers; }
132 
141  QgsSymbolLayer *symbolLayer( int layer );
142 
150  int symbolLayerCount() const { return mLayers.count(); }
151 
159  bool insertSymbolLayer( int index, QgsSymbolLayer *layer SIP_TRANSFER );
160 
167  bool appendSymbolLayer( QgsSymbolLayer *layer SIP_TRANSFER );
168 
172  bool deleteSymbolLayer( int index );
173 
180  QgsSymbolLayer *takeSymbolLayer( int index ) SIP_TRANSFERBACK;
181 
189  bool changeSymbolLayer( int index, QgsSymbolLayer *layer SIP_TRANSFER );
190 
200  void startRender( QgsRenderContext &context, const QgsFields &fields = QgsFields() );
201 
208  void stopRender( QgsRenderContext &context );
209 
220  void setColor( const QColor &color );
221 
230  QColor color() const;
231 
242  void drawPreviewIcon( QPainter *painter, QSize size, QgsRenderContext *customContext = nullptr );
243 
253  void exportImage( const QString &path, const QString &format, QSize size );
254 
264  QImage asImage( QSize size, QgsRenderContext *customContext = nullptr );
265 
274  QImage bigSymbolPreviewImage( QgsExpressionContext *expressionContext = nullptr );
275 
279  QString dump() const;
280 
286  virtual QgsSymbol *clone() const = 0 SIP_FACTORY;
287 
291  void toSld( QDomDocument &doc, QDomElement &element, QgsStringMap props ) const;
292 
301  QgsUnitTypes::RenderUnit outputUnit() const;
302 
311  void setOutputUnit( QgsUnitTypes::RenderUnit unit );
312 
322  QgsMapUnitScale mapUnitScale() const;
323 
332  void setMapUnitScale( const QgsMapUnitScale &scale );
333 
339  qreal opacity() const { return mOpacity; }
340 
346  void setOpacity( qreal opacity ) { mOpacity = opacity; }
347 
352  void setRenderHints( RenderHints hints ) { mRenderHints = hints; }
353 
358  RenderHints renderHints() const { return mRenderHints; }
359 
369  void setClipFeaturesToExtent( bool clipFeaturesToExtent ) { mClipFeaturesToExtent = clipFeaturesToExtent; }
370 
380  bool clipFeaturesToExtent() const { return mClipFeaturesToExtent; }
381 
391  void setForceRHR( bool force ) { mForceRHR = force; }
392 
402  bool forceRHR() const { return mForceRHR; }
403 
409  QSet<QString> usedAttributes( const QgsRenderContext &context ) const;
410 
415  bool hasDataDefinedProperties() const;
416 
421  Q_DECL_DEPRECATED void setLayer( const QgsVectorLayer *layer ) SIP_DEPRECATED;
422 
426  Q_DECL_DEPRECATED const QgsVectorLayer *layer() const SIP_DEPRECATED;
427 
432  void renderFeature( const QgsFeature &feature, QgsRenderContext &context, int layer = -1, bool selected = false, bool drawVertexMarker = false, int currentVertexMarkerType = 0, int currentVertexMarkerSize = 0 );
433 
439  QgsSymbolRenderContext *symbolRenderContext();
440 
441  protected:
442  QgsSymbol( SymbolType type, const QgsSymbolLayerList &layers SIP_TRANSFER ); // can't be instantiated
443 
447  static inline QPointF _getPoint( QgsRenderContext &context, const QgsPoint &point )
448  {
449  QPointF pt;
450  if ( context.coordinateTransform().isValid() )
451  {
452  double x = point.x();
453  double y = point.y();
454  double z = 0.0;
455  context.coordinateTransform().transformInPlace( x, y, z );
456  pt = QPointF( x, y );
457 
458  }
459  else
460  pt = point.toQPointF();
461 
462  context.mapToPixel().transformInPlace( pt.rx(), pt.ry() );
463  return pt;
464  }
465 
469  static QPolygonF _getLineString( QgsRenderContext &context, const QgsCurve &curve, bool clipToExtent = true );
470 
477  static QPolygonF _getPolygonRing( QgsRenderContext &context, const QgsCurve &curve, bool clipToExtent, bool isExteriorRing = false, bool correctRingOrientation = false );
478 
486  static void _getPolygon( QPolygonF &pts, QList<QPolygonF> &holes, QgsRenderContext &context, const QgsPolygon &polygon, bool clipToExtent = true, bool correctRingOrientation = false );
487 
492  QgsSymbolLayerList cloneLayers() const SIP_FACTORY;
493 
503  void renderUsingLayer( QgsSymbolLayer *layer, QgsSymbolRenderContext &context );
504 
509  void renderVertexMarker( QPointF pt, QgsRenderContext &context, int currentVertexMarkerType, int currentVertexMarkerSize );
510 
513 
515  qreal mOpacity = 1.0;
516 
517  RenderHints mRenderHints = nullptr;
518  bool mClipFeaturesToExtent = true;
519  bool mForceRHR = false;
520 
521  Q_DECL_DEPRECATED const QgsVectorLayer *mLayer = nullptr; //current vectorlayer
522 
523  private:
524 #ifdef SIP_RUN
525  QgsSymbol( const QgsSymbol & );
526 #endif
527 
532  bool mStarted = false;
533 
535  std::unique_ptr< QgsSymbolRenderContext > mSymbolRenderContext;
536 
537  Q_DISABLE_COPY( QgsSymbol )
538 
539 };
540 
541 Q_DECLARE_OPERATORS_FOR_FLAGS( QgsSymbol::RenderHints )
542 
543 
549 class CORE_EXPORT QgsSymbolRenderContext
550 {
551  public:
552 
564  QgsSymbolRenderContext( QgsRenderContext &c, QgsUnitTypes::RenderUnit u, qreal opacity = 1.0, bool selected = false, QgsSymbol::RenderHints renderHints = nullptr, const QgsFeature *f = nullptr, const QgsFields &fields = QgsFields(), const QgsMapUnitScale &mapUnitScale = QgsMapUnitScale() );
565 
567  QgsSymbolRenderContext( const QgsSymbolRenderContext &rh ) = delete;
568 
572  QgsRenderContext &renderContext() { return mRenderContext; }
573 
578  const QgsRenderContext &renderContext() const { return mRenderContext; } SIP_SKIP
579 
586  void setOriginalValueVariable( const QVariant &value );
587 
589  QgsUnitTypes::RenderUnit outputUnit() const { return mOutputUnit; }
590 
592  void setOutputUnit( QgsUnitTypes::RenderUnit u ) { mOutputUnit = u; }
593 
594  QgsMapUnitScale mapUnitScale() const { return mMapUnitScale; }
595  void setMapUnitScale( const QgsMapUnitScale &scale ) { mMapUnitScale = scale; }
596 
602  qreal opacity() const { return mOpacity; }
603 
609  void setOpacity( qreal opacity ) { mOpacity = opacity; }
610 
611  bool selected() const { return mSelected; }
612  void setSelected( bool selected ) { mSelected = selected; }
613 
618  QgsSymbol::RenderHints renderHints() const { return mRenderHints; }
619 
624  void setRenderHints( QgsSymbol::RenderHints hints ) { mRenderHints = hints; }
625 
626  void setFeature( const QgsFeature *f ) { mFeature = f; }
628  const QgsFeature *feature() const { return mFeature; }
629 
635  void setOriginalGeometryType( QgsWkbTypes::GeometryType type ) { mOriginalGeometryType = type; }
636 
645  QgsWkbTypes::GeometryType originalGeometryType() const { return mOriginalGeometryType; }
646 
653  QgsFields fields() const { return mFields; }
654 
659  int geometryPartCount() const { return mGeometryPartCount; }
660 
665  void setGeometryPartCount( int count ) { mGeometryPartCount = count; }
666 
671  int geometryPartNum() const { return mGeometryPartNum; }
672 
677  void setGeometryPartNum( int num ) { mGeometryPartNum = num; }
678 
679  double outputLineWidth( double width ) const;
680  double outputPixelSize( double size ) const;
681 
682  // workaround for sip 4.7. Don't use assignment - will fail with assertion error
683  QgsSymbolRenderContext &operator=( const QgsSymbolRenderContext & );
684 
690  QgsExpressionContextScope *expressionContextScope();
691 
699  void setExpressionContextScope( QgsExpressionContextScope *contextScope SIP_TRANSFER );
700 
701  private:
702 
703 #ifdef SIP_RUN
704  QgsSymbolRenderContext( const QgsSymbolRenderContext &rh ) SIP_FORCE;
705 #endif
706 
707  QgsRenderContext &mRenderContext;
708  std::unique_ptr< QgsExpressionContextScope > mExpressionContextScope;
709  QgsUnitTypes::RenderUnit mOutputUnit;
710  QgsMapUnitScale mMapUnitScale;
711  qreal mOpacity = 1.0;
712  bool mSelected;
713  QgsSymbol::RenderHints mRenderHints;
714  const QgsFeature *mFeature; //current feature
715  QgsFields mFields;
716  int mGeometryPartCount;
717  int mGeometryPartNum;
719 };
720 
721 
722 
724 
725 
730 class CORE_EXPORT QgsMarkerSymbol : public QgsSymbol
731 {
732  public:
733 
738  static QgsMarkerSymbol *createSimple( const QgsStringMap &properties ) SIP_FACTORY;
739 
741 
748  void setAngle( double symbolAngle );
749 
757  double angle() const;
758 
764  void setDataDefinedAngle( const QgsProperty &property );
765 
773  QgsProperty dataDefinedAngle() const;
774 
782  void setLineAngle( double lineAngle );
783 
792  void setSize( double size );
793 
801  double size() const;
802 
811  void setSizeUnit( QgsUnitTypes::RenderUnit unit );
812 
821  QgsUnitTypes::RenderUnit sizeUnit() const;
822 
831  void setSizeMapUnitScale( const QgsMapUnitScale &scale );
832 
842  QgsMapUnitScale sizeMapUnitScale() const;
843 
849  void setDataDefinedSize( const QgsProperty &property );
850 
858  QgsProperty dataDefinedSize() const;
859 
860  void setScaleMethod( QgsSymbol::ScaleMethod scaleMethod );
861  ScaleMethod scaleMethod();
862 
863  void renderPoint( QPointF point, const QgsFeature *f, QgsRenderContext &context, int layer = -1, bool selected = false );
864 
876  QRectF bounds( QPointF point, QgsRenderContext &context, const QgsFeature &feature = QgsFeature() ) const;
877 
878  QgsMarkerSymbol *clone() const override SIP_FACTORY;
879 
880  private:
881 
882  void renderPointUsingLayer( QgsMarkerSymbolLayer *layer, QPointF point, QgsSymbolRenderContext &context );
883 
884 };
885 
886 
891 class CORE_EXPORT QgsLineSymbol : public QgsSymbol
892 {
893  public:
894 
899  static QgsLineSymbol *createSimple( const QgsStringMap &properties ) SIP_FACTORY;
900 
902 
903  void setWidth( double width );
904  double width() const;
905 
911  void setDataDefinedWidth( const QgsProperty &property );
912 
920  QgsProperty dataDefinedWidth() const;
921 
922  void renderPolyline( const QPolygonF &points, const QgsFeature *f, QgsRenderContext &context, int layer = -1, bool selected = false );
923 
924  QgsLineSymbol *clone() const override SIP_FACTORY;
925 
926  private:
927 
928  void renderPolylineUsingLayer( QgsLineSymbolLayer *layer, const QPolygonF &points, QgsSymbolRenderContext &context );
929 
930 };
931 
932 
937 class CORE_EXPORT QgsFillSymbol : public QgsSymbol
938 {
939  public:
940 
945  static QgsFillSymbol *createSimple( const QgsStringMap &properties ) SIP_FACTORY;
946 
948  void setAngle( double angle );
949  void renderPolygon( const QPolygonF &points, QList<QPolygonF> *rings, const QgsFeature *f, QgsRenderContext &context, int layer = -1, bool selected = false );
950 
951  QgsFillSymbol *clone() const override SIP_FACTORY;
952 
953  private:
954 
955  void renderPolygonUsingLayer( QgsSymbolLayer *layer, const QPolygonF &points, QList<QPolygonF> *rings, QgsSymbolRenderContext &context );
957  QRectF polygonBounds( const QPolygonF &points, const QList<QPolygonF> *rings ) const;
959  QList<QPolygonF> *translateRings( const QList<QPolygonF> *rings, double dx, double dy ) const;
960 };
961 
962 #endif
963 
double y
Definition: qgspoint.h:42
QgsFields fields() const
Fields of the layer.
Definition: qgssymbol.h:653
Abstract base class for all rendered symbols.
Definition: qgssymbol.h:61
QPointF toQPointF() const
Returns the point as a QPointF.
Definition: qgspoint.h:264
virtual void toSld(QDomDocument &doc, QDomElement &element, const QgsStringMap &props=QgsStringMap()) const
used from subclasses to create SLD Rule elements following SLD v1.1 specs
Definition: qgsrenderer.h:300
const QgsRenderContext & renderContext() const
Returns a reference to the context&#39;s render context.
Definition: qgssymbol.h:578
static QPointF _getPoint(QgsRenderContext &context, const QgsPoint &point)
Creates a point in screen coordinates from a QgsPoint in map coordinates.
Definition: qgssymbol.h:447
Base class for visual effects which can be applied to QPicture drawings.
void setOutputUnit(QgsUnitTypes::RenderUnit u)
Sets the output unit for the context.
Definition: qgssymbol.h:592
bool clipFeaturesToExtent() const
Returns whether features drawn by the symbol will be clipped to the render context&#39;s extent...
Definition: qgssymbol.h:380
Container of fields for a vector layer.
Definition: qgsfields.h:42
void setGeometryPartCount(int count)
Sets the part count of current geometry.
Definition: qgssymbol.h:665
int symbolLayerCount() const
Returns the total number of symbol layers contained in the symbol.
Definition: qgssymbol.h:150
Line symbol.
Definition: qgssymbol.h:86
The feature class encapsulates a single feature including its id, geometry and a list of field/values...
Definition: qgsfeature.h:55
#define SIP_TRANSFERBACK
Definition: qgis_sip.h:41
QMap< QString, QString > QgsStringMap
Definition: qgis.h:570
double ANALYSIS_EXPORT angle(QgsPoint *p1, QgsPoint *p2, QgsPoint *p3, QgsPoint *p4)
Calculates the angle between two segments (in 2 dimension, z-values are ignored)
Definition: MathUtils.cpp:786
bool isValid() const
Returns true if the coordinate transform is valid, ie both the source and destination CRS have been s...
As part of the API refactoring and improvements which landed in the Processing API was substantially reworked from the x version This was done in order to allow much of the underlying Processing framework to be ported into c
void transformInPlace(double &x, double &y) const
Transform device coordinates to map coordinates.
SymbolType
Type of the symbol.
Definition: qgssymbol.h:83
virtual QSet< QString > usedAttributes(const QgsRenderContext &context) const =0
Returns a list of attributes required by this renderer.
SymbolType mType
Definition: qgssymbol.h:511
QgsSymbolLayerList mLayers
Definition: qgssymbol.h:512
QString type() const
Definition: qgsrenderer.h:129
void setOriginalGeometryType(QgsWkbTypes::GeometryType type)
Sets the geometry type for the original feature geometry being rendered.
Definition: qgssymbol.h:635
virtual bool renderFeature(const QgsFeature &feature, QgsRenderContext &context, int layer=-1, bool selected=false, bool drawVertexMarker=false)
Render a feature using this renderer in the given context.
bool forceRHR() const
Returns true if polygon features drawn by the symbol will be reoriented to follow the standard right-...
Definition: qgssymbol.h:402
#define SIP_SKIP
Definition: qgis_sip.h:119
void setOpacity(qreal opacity)
Sets the opacity for the symbol.
Definition: qgssymbol.h:346
void setGeometryPartNum(int num)
Sets the part number of current geometry.
Definition: qgssymbol.h:677
Calculate scale by the area.
Definition: qgssymbol.h:96
qreal opacity() const
Returns the opacity for the symbol.
Definition: qgssymbol.h:602
void setRenderHints(RenderHints hints)
Sets rendering hint flags for the symbol.
Definition: qgssymbol.h:352
Expression contexts are used to encapsulate the parameters around which a QgsExpression should be eva...
#define SIP_FORCE
Definition: qgis_sip.h:124
#define SIP_TRANSFER
Definition: qgis_sip.h:36
QgsMapUnitScale mapUnitScale() const
Definition: qgssymbol.h:594
#define SIP_END
Definition: qgis_sip.h:182
QList< QgsSymbolLayer * > QgsSymbolLayerList
Definition: qgssymbol.h:51
QgsCoordinateTransform coordinateTransform() const
Returns the current coordinate transform for the context.
void setSelected(bool selected)
Definition: qgssymbol.h:612
Abstract base class for curved geometry type.
Definition: qgscurve.h:35
#define SIP_FACTORY
Definition: qgis_sip.h:69
ScaleMethod
Scale method.
Definition: qgssymbol.h:94
Single scope for storing variables and functions for use within a QgsExpressionContext.
A store for object properties.
Definition: qgsproperty.h:229
QgsRenderContext & renderContext()
Returns a reference to the context&#39;s render context.
Definition: qgssymbol.h:572
#define SIP_DEPRECATED
Definition: qgis_sip.h:99
Point geometry type, with support for z-dimension and m-values.
Definition: qgspoint.h:37
void setRenderHints(QgsSymbol::RenderHints hints)
Sets rendering hint flags for the symbol.
Definition: qgssymbol.h:624
bool selected() const
Definition: qgssymbol.h:611
QgsWkbTypes::GeometryType originalGeometryType() const
Returns the geometry type for the original feature geometry being rendered.
Definition: qgssymbol.h:645
void transformInPlace(double &x, double &y, double &z, TransformDirection direction=ForwardTransform) const SIP_THROW(QgsCsException)
Transforms an array of x, y and z double coordinates in place, from the source CRS to the destination...
void setFeature(const QgsFeature *f)
Definition: qgssymbol.h:626
int geometryPartNum() const
Part number of current geometry.
Definition: qgssymbol.h:671
GeometryType
The geometry types are used to group QgsWkbTypes::Type in a coarse way.
Definition: qgswkbtypes.h:138
Marker symbol.
Definition: qgssymbol.h:85
RenderHint
Flags controlling behavior of symbols during rendering.
Definition: qgssymbol.h:102
Fill symbol.
Definition: qgssymbol.h:87
Contains information about the context of a rendering operation.
Abstract base class for marker symbol layers.
const QgsMapToPixel & mapToPixel() const
SymbolType type() const
Returns the symbol&#39;s type.
Definition: qgssymbol.h:120
Struct for storing maximum and minimum scales for measurements in map units.
virtual void startRender(QgsRenderContext &context, const QgsFields &fields)
Must be called when a new render cycle is started.
Definition: qgsrenderer.cpp:92
virtual QgsSymbol * clone() const =0
Returns a deep copy of this symbol.
RenderHints renderHints() const
Returns the rendering hint flags for the symbol.
Definition: qgssymbol.h:358
const QgsFeature * feature() const
Current feature being rendered - may be null.
Definition: qgssymbol.h:628
void setOpacity(qreal opacity)
Sets the opacity for the symbol.
Definition: qgssymbol.h:609
#define SIP_CONVERT_TO_SUBCLASS_CODE(code)
Definition: qgis_sip.h:165
virtual void stopRender(QgsRenderContext &context)
Must be called when a render cycle has finished, to allow the renderer to clean up.
QgsSymbolLayerList symbolLayers()
Returns the list of symbol layers contained in the symbol.
Definition: qgssymbol.h:131
void setClipFeaturesToExtent(bool clipFeaturesToExtent)
Sets whether features drawn by the symbol should be clipped to the render context&#39;s extent...
Definition: qgssymbol.h:369
qreal opacity() const
Returns the opacity for the symbol.
Definition: qgssymbol.h:339
void setMapUnitScale(const QgsMapUnitScale &scale)
Definition: qgssymbol.h:595
Polygon geometry type.
Definition: qgspolygon.h:31
void setForceRHR(bool force)
Sets whether polygon features drawn by the symbol should be reoriented to follow the standard right-h...
Definition: qgssymbol.h:391
Represents a vector layer which manages a vector based data sets.
virtual QgsFeatureRenderer * clone() const =0
Create a deep copy of this renderer.
int geometryPartCount() const
Part count of current geometry.
Definition: qgssymbol.h:659
RenderUnit
Rendering size units.
Definition: qgsunittypes.h:110
QgsSymbol::RenderHints renderHints() const
Returns the rendering hint flags for the symbol.
Definition: qgssymbol.h:618
void renderVertexMarker(QPointF pt, QgsRenderContext &context)
render editing vertex marker at specified point
virtual QString dump() const
Returns debug information about this renderer.
QgsUnitTypes::RenderUnit outputUnit() const
Returns the output unit for the context.
Definition: qgssymbol.h:589
double x
Definition: qgspoint.h:41