QGIS API Documentation  2.99.0-Master (53aba61)
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 "qgspoint.h"
25 #include "qgsfeature.h"
26 #include "qgsfields.h"
27 #include "qgsrendercontext.h"
28 #include "qgsproperty.h"
29 
30 class QColor;
31 class QImage;
32 class QPainter;
33 class QSize;
34 class QPointF;
35 class QPolygonF;
36 
37 class QDomDocument;
38 class QDomElement;
39 //class
40 
41 class QgsFields;
42 class QgsSymbolLayer;
43 class QgsRenderContext;
44 class QgsVectorLayer;
45 class QgsPaintEffect;
47 class QgsLineSymbolLayer;
48 class QgsFillSymbolLayer;
50 class QgsFeatureRenderer;
51 class QgsCurve;
52 class QgsPolygonV2;
54 
55 typedef QList<QgsSymbolLayer *> QgsSymbolLayerList;
56 
60 class CORE_EXPORT QgsSymbol
61 {
62 
63 #ifdef SIP_RUN
65  switch ( sipCpp->type() )
66  {
67  case QgsSymbol::Marker: sipType = sipType_QgsMarkerSymbol; break;
68  case QgsSymbol::Line: sipType = sipType_QgsLineSymbol; break;
69  case QgsSymbol::Fill: sipType = sipType_QgsFillSymbol; break;
70  default: sipType = 0; break;
71  }
72  SIP_END
73 #endif
74 
75  friend class QgsFeatureRenderer;
76 
77  public:
78 
83  {
85  Line,
86  Fill,
87  Hybrid
88  };
89 
94  {
96  ScaleDiameter
97  };
98 
99 
102  {
103  DynamicRotation = 2,
104  };
105  Q_DECLARE_FLAGS( RenderHints, RenderHint )
106 
107  virtual ~QgsSymbol();
108 
110  static QgsSymbol *defaultSymbol( QgsWkbTypes::GeometryType geomType ) SIP_FACTORY;
111 
112  SymbolType type() const { return mType; }
113 
114  // symbol layers handling
115 
122  QgsSymbolLayerList symbolLayers() { return mLayers; }
123 
131  QgsSymbolLayer *symbolLayer( int layer );
132 
139  int symbolLayerCount() const { return mLayers.count(); }
140 
148  bool insertSymbolLayer( int index, QgsSymbolLayer *layer SIP_TRANSFER );
149 
156  bool appendSymbolLayer( QgsSymbolLayer *layer SIP_TRANSFER );
157 
159  bool deleteSymbolLayer( int index );
160 
167  QgsSymbolLayer *takeSymbolLayer( int index ) SIP_TRANSFERBACK;
168 
170  bool changeSymbolLayer( int index, QgsSymbolLayer *layer SIP_TRANSFER );
171 
180  void startRender( QgsRenderContext &context, const QgsFields &fields = QgsFields() );
181 
187  void stopRender( QgsRenderContext &context );
188 
189  void setColor( const QColor &color );
190  QColor color() const;
191 
195  void drawPreviewIcon( QPainter *painter, QSize size, QgsRenderContext *customContext = nullptr );
196 
198  void exportImage( const QString &path, const QString &format, QSize size );
199 
201  QImage asImage( QSize size, QgsRenderContext *customContext = nullptr );
202 
207  QImage bigSymbolPreviewImage( QgsExpressionContext *expressionContext = nullptr );
208 
209  QString dump() const;
210 
216  virtual QgsSymbol *clone() const = 0 SIP_FACTORY;
217 
218  void toSld( QDomDocument &doc, QDomElement &element, QgsStringMap props ) const;
219 
227  QgsUnitTypes::RenderUnit outputUnit() const;
228 
236  void setOutputUnit( QgsUnitTypes::RenderUnit unit );
237 
238  QgsMapUnitScale mapUnitScale() const;
239  void setMapUnitScale( const QgsMapUnitScale &scale );
240 
246  qreal opacity() const { return mOpacity; }
247 
253  void setOpacity( qreal opacity ) { mOpacity = opacity; }
254 
258  void setRenderHints( RenderHints hints ) { mRenderHints = hints; }
259 
263  RenderHints renderHints() const { return mRenderHints; }
264 
273  void setClipFeaturesToExtent( bool clipFeaturesToExtent ) { mClipFeaturesToExtent = clipFeaturesToExtent; }
274 
283  bool clipFeaturesToExtent() const { return mClipFeaturesToExtent; }
284 
290  QSet<QString> usedAttributes( const QgsRenderContext &context ) const;
291 
295  bool hasDataDefinedProperties() const;
296 
298  void setLayer( const QgsVectorLayer *layer ) { mLayer = layer; }
299  const QgsVectorLayer *layer() const { return mLayer; }
300 
305  void renderFeature( const QgsFeature &feature, QgsRenderContext &context, int layer = -1, bool selected = false, bool drawVertexMarker = false, int currentVertexMarkerType = 0, int currentVertexMarkerSize = 0 );
306 
312  QgsSymbolRenderContext *symbolRenderContext();
313 
314  protected:
315  QgsSymbol( SymbolType type, const QgsSymbolLayerList &layers SIP_TRANSFER ); // can't be instantiated
316 
320  static inline QPointF _getPoint( QgsRenderContext &context, const QgsPoint &point )
321  {
322  QPointF pt;
323  if ( context.coordinateTransform().isValid() )
324  {
325  double x = point.x();
326  double y = point.y();
327  double z = 0.0;
328  context.coordinateTransform().transformInPlace( x, y, z );
329  pt = QPointF( x, y );
330 
331  }
332  else
333  pt = point.toQPointF();
334 
335  context.mapToPixel().transformInPlace( pt.rx(), pt.ry() );
336  return pt;
337  }
338 
342  static QPolygonF _getLineString( QgsRenderContext &context, const QgsCurve &curve, bool clipToExtent = true );
343 
347  static QPolygonF _getPolygonRing( QgsRenderContext &context, const QgsCurve &curve, bool clipToExtent );
348 
352  static void _getPolygon( QPolygonF &pts, QList<QPolygonF> &holes, QgsRenderContext &context, const QgsPolygonV2 &polygon, bool clipToExtent = true );
353 
358  QgsSymbolLayerList cloneLayers() const SIP_FACTORY;
359 
369  void renderUsingLayer( QgsSymbolLayer *layer, QgsSymbolRenderContext &context );
370 
373  void renderVertexMarker( QPointF pt, QgsRenderContext &context, int currentVertexMarkerType, int currentVertexMarkerSize );
374 
377 
379  qreal mOpacity = 1.0;
380 
381  RenderHints mRenderHints;
383 
384  const QgsVectorLayer *mLayer; //current vectorlayer
385 
386  private:
387 #ifdef SIP_RUN
388  QgsSymbol( const QgsSymbol & );
389 #endif
390 
393  bool mStarted = false;
394 
396  std::unique_ptr< QgsSymbolRenderContext > mSymbolRenderContext;
397 
398  Q_DISABLE_COPY( QgsSymbol )
399 
400 };
401 
402 Q_DECLARE_OPERATORS_FOR_FLAGS( QgsSymbol::RenderHints )
403 
404 
409 class CORE_EXPORT QgsSymbolRenderContext
410 {
411  public:
412 
423  QgsSymbolRenderContext( QgsRenderContext &c, QgsUnitTypes::RenderUnit u, qreal opacity = 1.0, bool selected = false, QgsSymbol::RenderHints renderHints = 0, const QgsFeature *f = nullptr, const QgsFields &fields = QgsFields(), const QgsMapUnitScale &mapUnitScale = QgsMapUnitScale() );
424 
425  QgsRenderContext &renderContext() { return mRenderContext; }
426  const QgsRenderContext &renderContext() const { return mRenderContext; } SIP_SKIP
427 
433  void setOriginalValueVariable( const QVariant &value );
434 
436  QgsUnitTypes::RenderUnit outputUnit() const { return mOutputUnit; }
437 
439  void setOutputUnit( QgsUnitTypes::RenderUnit u ) { mOutputUnit = u; }
440 
441  QgsMapUnitScale mapUnitScale() const { return mMapUnitScale; }
442  void setMapUnitScale( const QgsMapUnitScale &scale ) { mMapUnitScale = scale; }
443 
449  qreal opacity() const { return mOpacity; }
450 
456  void setOpacity( qreal opacity ) { mOpacity = opacity; }
457 
458  bool selected() const { return mSelected; }
459  void setSelected( bool selected ) { mSelected = selected; }
460 
464  QgsSymbol::RenderHints renderHints() const { return mRenderHints; }
465 
469  void setRenderHints( QgsSymbol::RenderHints hints ) { mRenderHints = hints; }
470 
471  void setFeature( const QgsFeature *f ) { mFeature = f; }
473  const QgsFeature *feature() const { return mFeature; }
474 
480  void setOriginalGeometryType( QgsWkbTypes::GeometryType type ) { mOriginalGeometryType = type; }
481 
490  QgsWkbTypes::GeometryType originalGeometryType() const { return mOriginalGeometryType; }
491 
496  QgsFields fields() const { return mFields; }
497 
501  int geometryPartCount() const { return mGeometryPartCount; }
502 
506  void setGeometryPartCount( int count ) { mGeometryPartCount = count; }
507 
511  int geometryPartNum() const { return mGeometryPartNum; }
512 
516  void setGeometryPartNum( int num ) { mGeometryPartNum = num; }
517 
518  double outputLineWidth( double width ) const;
519  double outputPixelSize( double size ) const;
520 
521  // workaround for sip 4.7. Don't use assignment - will fail with assertion error
522  QgsSymbolRenderContext &operator=( const QgsSymbolRenderContext & );
523 
529  QgsExpressionContextScope *expressionContextScope();
530 
538  void setExpressionContextScope( QgsExpressionContextScope *contextScope SIP_TRANSFER );
539 
540  private:
541  QgsSymbolRenderContext( const QgsSymbolRenderContext &rh ) SIP_FORCE;
542 
543  QgsRenderContext &mRenderContext;
544  std::unique_ptr< QgsExpressionContextScope > mExpressionContextScope;
545  QgsUnitTypes::RenderUnit mOutputUnit;
546  QgsMapUnitScale mMapUnitScale;
547  qreal mOpacity = 1.0;
548  bool mSelected;
549  QgsSymbol::RenderHints mRenderHints;
550  const QgsFeature *mFeature; //current feature
551  QgsFields mFields;
552  int mGeometryPartCount;
553  int mGeometryPartNum;
555 };
556 
557 
558 
560 
561 
565 class CORE_EXPORT QgsMarkerSymbol : public QgsSymbol
566 {
567  public:
568 
572  static QgsMarkerSymbol *createSimple( const QgsStringMap &properties ) SIP_FACTORY;
573 
575 
581  void setAngle( double symbolAngle );
582 
589  double angle() const;
590 
595  void setDataDefinedAngle( const QgsProperty &property );
596 
603  QgsProperty dataDefinedAngle() const;
604 
611  void setLineAngle( double lineAngle );
612 
620  void setSize( double size );
621 
628  double size() const;
629 
637  void setSizeUnit( QgsUnitTypes::RenderUnit unit );
638 
646  QgsUnitTypes::RenderUnit sizeUnit() const;
647 
655  void setSizeMapUnitScale( const QgsMapUnitScale &scale );
656 
665  QgsMapUnitScale sizeMapUnitScale() const;
666 
671  void setDataDefinedSize( const QgsProperty &property );
672 
679  QgsProperty dataDefinedSize() const;
680 
681  void setScaleMethod( QgsSymbol::ScaleMethod scaleMethod );
682  ScaleMethod scaleMethod();
683 
684  void renderPoint( QPointF point, const QgsFeature *f, QgsRenderContext &context, int layer = -1, bool selected = false );
685 
696  QRectF bounds( QPointF point, QgsRenderContext &context, const QgsFeature &feature = QgsFeature() ) const;
697 
698  virtual QgsMarkerSymbol *clone() const override SIP_FACTORY;
699 
700  private:
701 
702  void renderPointUsingLayer( QgsMarkerSymbolLayer *layer, QPointF point, QgsSymbolRenderContext &context );
703 
704 };
705 
706 
710 class CORE_EXPORT QgsLineSymbol : public QgsSymbol
711 {
712  public:
713 
717  static QgsLineSymbol *createSimple( const QgsStringMap &properties ) SIP_FACTORY;
718 
720 
721  void setWidth( double width );
722  double width() const;
723 
728  void setDataDefinedWidth( const QgsProperty &property );
729 
736  QgsProperty dataDefinedWidth() const;
737 
738  void renderPolyline( const QPolygonF &points, const QgsFeature *f, QgsRenderContext &context, int layer = -1, bool selected = false );
739 
740  virtual QgsLineSymbol *clone() const override SIP_FACTORY;
741 
742  private:
743 
744  void renderPolylineUsingLayer( QgsLineSymbolLayer *layer, const QPolygonF &points, QgsSymbolRenderContext &context );
745 
746 };
747 
748 
752 class CORE_EXPORT QgsFillSymbol : public QgsSymbol
753 {
754  public:
755 
759  static QgsFillSymbol *createSimple( const QgsStringMap &properties ) SIP_FACTORY;
760 
762  void setAngle( double angle );
763  void renderPolygon( const QPolygonF &points, QList<QPolygonF> *rings, const QgsFeature *f, QgsRenderContext &context, int layer = -1, bool selected = false );
764 
765  virtual QgsFillSymbol *clone() const override SIP_FACTORY;
766 
767  private:
768 
769  void renderPolygonUsingLayer( QgsSymbolLayer *layer, const QPolygonF &points, QList<QPolygonF> *rings, QgsSymbolRenderContext &context );
771  QRectF polygonBounds( const QPolygonF &points, const QList<QPolygonF> *rings ) const;
773  QList<QPolygonF> *translateRings( const QList<QPolygonF> *rings, double dx, double dy ) const;
774 };
775 
776 #endif
777 
double y
Definition: qgspoint.h:41
QgsFields fields() const
Fields of the layer.
Definition: qgssymbol.h:496
QPointF toQPointF() const
Returns the point as a QPointF.
Definition: qgspoint.cpp:525
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:275
const QgsRenderContext & renderContext() const
Definition: qgssymbol.h:426
static QPointF _getPoint(QgsRenderContext &context, const QgsPoint &point)
Creates a point in screen coordinates from a QgsPoint in map coordinates.
Definition: qgssymbol.h:320
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:439
bool clipFeaturesToExtent() const
Returns whether features drawn by the symbol will be clipped to the render context&#39;s extent...
Definition: qgssymbol.h:283
bool mClipFeaturesToExtent
Definition: qgssymbol.h:382
Container of fields for a vector layer.
Definition: qgsfields.h:41
void setGeometryPartCount(int count)
Sets the part count of current geometry.
Definition: qgssymbol.h:506
int symbolLayerCount() const
Returns total number of symbol layers contained in the symbol.
Definition: qgssymbol.h:139
Line symbol.
Definition: qgssymbol.h:85
The feature class encapsulates a single feature including its id, geometry and a list of field/values...
Definition: qgsfeature.h:61
#define SIP_TRANSFERBACK
Definition: qgis_sip.h:41
QMap< QString, QString > QgsStringMap
Definition: qgis.h:366
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...
void transformInPlace(double &x, double &y) const
Transform device coordinates to map coordinates.
SymbolType
Type of the symbol.
Definition: qgssymbol.h:82
virtual void startRender(QgsRenderContext &context, const QgsFields &fields)=0
Needs to be called when a new render cycle is started.
Polygon geometry type.
Definition: qgspolygon.h:30
virtual QSet< QString > usedAttributes(const QgsRenderContext &context) const =0
Return a list of attributes required by this renderer.
SymbolType mType
Definition: qgssymbol.h:375
QgsSymbolLayerList mLayers
Definition: qgssymbol.h:376
QString type() const
Definition: qgsrenderer.h:124
void setOriginalGeometryType(QgsWkbTypes::GeometryType type)
Sets the geometry type for the original feature geometry being rendered.
Definition: qgssymbol.h:480
const QgsVectorLayer * mLayer
Definition: qgssymbol.h:384
#define SIP_SKIP
Definition: qgis_sip.h:119
void setOpacity(qreal opacity)
Sets the opacity for the symbol.
Definition: qgssymbol.h:253
void setGeometryPartNum(int num)
Sets the part number of current geometry.
Definition: qgssymbol.h:516
Calculate scale by the area.
Definition: qgssymbol.h:95
qreal opacity() const
Returns the opacity for the symbol.
Definition: qgssymbol.h:449
void setRenderHints(RenderHints hints)
Sets rendering hint flags for the symbol.
Definition: qgssymbol.h:258
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:441
#define SIP_END
Definition: qgis_sip.h:175
QList< QgsSymbolLayer * > QgsSymbolLayerList
Definition: qgssymbol.h:53
QgsCoordinateTransform coordinateTransform() const
Returns the current coordinate transform for the context, or an invalid transform is no coordinate tr...
void setSelected(bool selected)
Definition: qgssymbol.h:459
Abstract base class for curved geometry type.
Definition: qgscurve.h:34
#define SIP_FACTORY
Definition: qgis_sip.h:69
ScaleMethod
Scale method.
Definition: qgssymbol.h:93
Single scope for storing variables and functions for use within a QgsExpressionContext.
A store for object properties.
Definition: qgsproperty.h:189
const QgsVectorLayer * layer() const
Definition: qgssymbol.h:299
QgsRenderContext & renderContext()
Definition: qgssymbol.h:425
Point geometry type, with support for z-dimension and m-values.
Definition: qgspoint.h:36
void setRenderHints(QgsSymbol::RenderHints hints)
Sets rendering hint flags for the symbol.
Definition: qgssymbol.h:469
bool selected() const
Definition: qgssymbol.h:458
QgsWkbTypes::GeometryType originalGeometryType() const
Returns the geometry type for the original feature geometry being rendered.
Definition: qgssymbol.h:490
void setFeature(const QgsFeature *f)
Definition: qgssymbol.h:471
int geometryPartNum() const
Part number of current geometry.
Definition: qgssymbol.h:511
GeometryType
The geometry types are used to group QgsWkbTypes::Type in a coarse way.
Definition: qgswkbtypes.h:136
Marker symbol.
Definition: qgssymbol.h:84
RenderHint
Flags controlling behavior of symbols during rendering.
Definition: qgssymbol.h:101
Fill symbol.
Definition: qgssymbol.h:86
Contains information about the context of a rendering operation.
Abstract base class for marker symbol layers.
virtual void stopRender(QgsRenderContext &context)=0
Needs to be called when a render cycle has finished to clean up.
const QgsMapToPixel & mapToPixel() const
SymbolType type() const
Definition: qgssymbol.h:112
Struct for storing maximum and minimum scales for measurements in map units.
RenderHints mRenderHints
Definition: qgssymbol.h:381
virtual QgsSymbol * clone() const =0
Get a deep copy of this symbol.
void setLayer(const QgsVectorLayer *layer)
Definition: qgssymbol.h:298
RenderHints renderHints() const
Returns the rendering hint flags for the symbol.
Definition: qgssymbol.h:263
const QgsFeature * feature() const
Current feature being rendered - may be null.
Definition: qgssymbol.h:473
void setOpacity(qreal opacity)
Sets the opacity for the symbol.
Definition: qgssymbol.h:456
#define SIP_CONVERT_TO_SUBCLASS_CODE(code)
Definition: qgis_sip.h:165
QgsSymbolLayerList symbolLayers()
Returns list of symbol layers contained in the symbol.
Definition: qgssymbol.h:122
void setClipFeaturesToExtent(bool clipFeaturesToExtent)
Sets whether features drawn by the symbol should be clipped to the render context&#39;s extent...
Definition: qgssymbol.h:273
qreal opacity() const
Returns the opacity for the symbol.
Definition: qgssymbol.h:246
void setMapUnitScale(const QgsMapUnitScale &scale)
Definition: qgssymbol.h:442
void transformInPlace(double &x, double &y, double &z, TransformDirection direction=ForwardTransform) const
Transforms an array of x, y and z double coordinates in place, from the source CRS to the destination...
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:501
virtual bool renderFeature(QgsFeature &feature, QgsRenderContext &context, int layer=-1, bool selected=false, bool drawVertexMarker=false)
Render a feature using this renderer in the given context.
Definition: qgsrenderer.cpp:97
RenderUnit
Rendering size units.
Definition: qgsunittypes.h:98
QgsSymbol::RenderHints renderHints() const
Returns the rendering hint flags for the symbol.
Definition: qgssymbol.h:464
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:436
double x
Definition: qgspoint.h:40