QGIS API Documentation  2.99.0-Master (cd0ba91)
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 QgsPolygon;
54 
55 typedef QList<QgsSymbolLayer *> QgsSymbolLayerList;
56 
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 
111  static QgsSymbol *defaultSymbol( QgsWkbTypes::GeometryType geomType ) SIP_FACTORY;
112 
113  SymbolType type() const { return mType; }
114 
115  // symbol layers handling
116 
124  QgsSymbolLayerList symbolLayers() { return mLayers; }
125 
134  QgsSymbolLayer *symbolLayer( int layer );
135 
143  int symbolLayerCount() const { return mLayers.count(); }
144 
152  bool insertSymbolLayer( int index, QgsSymbolLayer *layer SIP_TRANSFER );
153 
160  bool appendSymbolLayer( QgsSymbolLayer *layer SIP_TRANSFER );
161 
163  bool deleteSymbolLayer( int index );
164 
171  QgsSymbolLayer *takeSymbolLayer( int index ) SIP_TRANSFERBACK;
172 
174  bool changeSymbolLayer( int index, QgsSymbolLayer *layer SIP_TRANSFER );
175 
185  void startRender( QgsRenderContext &context, const QgsFields &fields = QgsFields() );
186 
193  void stopRender( QgsRenderContext &context );
194 
195  void setColor( const QColor &color );
196  QColor color() const;
197 
203  void drawPreviewIcon( QPainter *painter, QSize size, QgsRenderContext *customContext = nullptr );
204 
206  void exportImage( const QString &path, const QString &format, QSize size );
207 
209  QImage asImage( QSize size, QgsRenderContext *customContext = nullptr );
210 
216  QImage bigSymbolPreviewImage( QgsExpressionContext *expressionContext = nullptr );
217 
218  QString dump() const;
219 
225  virtual QgsSymbol *clone() const = 0 SIP_FACTORY;
226 
227  void toSld( QDomDocument &doc, QDomElement &element, QgsStringMap props ) const;
228 
237  QgsUnitTypes::RenderUnit outputUnit() const;
238 
247  void setOutputUnit( QgsUnitTypes::RenderUnit unit );
248 
249  QgsMapUnitScale mapUnitScale() const;
250  void setMapUnitScale( const QgsMapUnitScale &scale );
251 
257  qreal opacity() const { return mOpacity; }
258 
264  void setOpacity( qreal opacity ) { mOpacity = opacity; }
265 
270  void setRenderHints( RenderHints hints ) { mRenderHints = hints; }
271 
276  RenderHints renderHints() const { return mRenderHints; }
277 
287  void setClipFeaturesToExtent( bool clipFeaturesToExtent ) { mClipFeaturesToExtent = clipFeaturesToExtent; }
288 
298  bool clipFeaturesToExtent() const { return mClipFeaturesToExtent; }
299 
305  QSet<QString> usedAttributes( const QgsRenderContext &context ) const;
306 
311  bool hasDataDefinedProperties() const;
312 
314  void setLayer( const QgsVectorLayer *layer ) { mLayer = layer; }
315  const QgsVectorLayer *layer() const { return mLayer; }
316 
321  void renderFeature( const QgsFeature &feature, QgsRenderContext &context, int layer = -1, bool selected = false, bool drawVertexMarker = false, int currentVertexMarkerType = 0, int currentVertexMarkerSize = 0 );
322 
328  QgsSymbolRenderContext *symbolRenderContext();
329 
330  protected:
331  QgsSymbol( SymbolType type, const QgsSymbolLayerList &layers SIP_TRANSFER ); // can't be instantiated
332 
336  static inline QPointF _getPoint( QgsRenderContext &context, const QgsPoint &point )
337  {
338  QPointF pt;
339  if ( context.coordinateTransform().isValid() )
340  {
341  double x = point.x();
342  double y = point.y();
343  double z = 0.0;
344  context.coordinateTransform().transformInPlace( x, y, z );
345  pt = QPointF( x, y );
346 
347  }
348  else
349  pt = point.toQPointF();
350 
351  context.mapToPixel().transformInPlace( pt.rx(), pt.ry() );
352  return pt;
353  }
354 
358  static QPolygonF _getLineString( QgsRenderContext &context, const QgsCurve &curve, bool clipToExtent = true );
359 
363  static QPolygonF _getPolygonRing( QgsRenderContext &context, const QgsCurve &curve, bool clipToExtent );
364 
368  static void _getPolygon( QPolygonF &pts, QList<QPolygonF> &holes, QgsRenderContext &context, const QgsPolygon &polygon, bool clipToExtent = true );
369 
374  QgsSymbolLayerList cloneLayers() const SIP_FACTORY;
375 
385  void renderUsingLayer( QgsSymbolLayer *layer, QgsSymbolRenderContext &context );
386 
391  void renderVertexMarker( QPointF pt, QgsRenderContext &context, int currentVertexMarkerType, int currentVertexMarkerSize );
392 
395 
397  qreal mOpacity = 1.0;
398 
399  RenderHints mRenderHints;
401 
402  const QgsVectorLayer *mLayer; //current vectorlayer
403 
404  private:
405 #ifdef SIP_RUN
406  QgsSymbol( const QgsSymbol & );
407 #endif
408 
413  bool mStarted = false;
414 
416  std::unique_ptr< QgsSymbolRenderContext > mSymbolRenderContext;
417 
418  Q_DISABLE_COPY( QgsSymbol )
419 
420 };
421 
422 Q_DECLARE_OPERATORS_FOR_FLAGS( QgsSymbol::RenderHints )
423 
424 
430 class CORE_EXPORT QgsSymbolRenderContext
431 {
432  public:
433 
445  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() );
446 
448  QgsSymbolRenderContext( const QgsSymbolRenderContext &rh ) = delete;
449 
450  QgsRenderContext &renderContext() { return mRenderContext; }
451  const QgsRenderContext &renderContext() const { return mRenderContext; } SIP_SKIP
452 
459  void setOriginalValueVariable( const QVariant &value );
460 
462  QgsUnitTypes::RenderUnit outputUnit() const { return mOutputUnit; }
463 
465  void setOutputUnit( QgsUnitTypes::RenderUnit u ) { mOutputUnit = u; }
466 
467  QgsMapUnitScale mapUnitScale() const { return mMapUnitScale; }
468  void setMapUnitScale( const QgsMapUnitScale &scale ) { mMapUnitScale = scale; }
469 
475  qreal opacity() const { return mOpacity; }
476 
482  void setOpacity( qreal opacity ) { mOpacity = opacity; }
483 
484  bool selected() const { return mSelected; }
485  void setSelected( bool selected ) { mSelected = selected; }
486 
491  QgsSymbol::RenderHints renderHints() const { return mRenderHints; }
492 
497  void setRenderHints( QgsSymbol::RenderHints hints ) { mRenderHints = hints; }
498 
499  void setFeature( const QgsFeature *f ) { mFeature = f; }
501  const QgsFeature *feature() const { return mFeature; }
502 
508  void setOriginalGeometryType( QgsWkbTypes::GeometryType type ) { mOriginalGeometryType = type; }
509 
518  QgsWkbTypes::GeometryType originalGeometryType() const { return mOriginalGeometryType; }
519 
526  QgsFields fields() const { return mFields; }
527 
532  int geometryPartCount() const { return mGeometryPartCount; }
533 
538  void setGeometryPartCount( int count ) { mGeometryPartCount = count; }
539 
544  int geometryPartNum() const { return mGeometryPartNum; }
545 
550  void setGeometryPartNum( int num ) { mGeometryPartNum = num; }
551 
552  double outputLineWidth( double width ) const;
553  double outputPixelSize( double size ) const;
554 
555  // workaround for sip 4.7. Don't use assignment - will fail with assertion error
556  QgsSymbolRenderContext &operator=( const QgsSymbolRenderContext & );
557 
563  QgsExpressionContextScope *expressionContextScope();
564 
572  void setExpressionContextScope( QgsExpressionContextScope *contextScope SIP_TRANSFER );
573 
574  private:
575 
576 #ifdef SIP_RUN
577  QgsSymbolRenderContext( const QgsSymbolRenderContext &rh ) SIP_FORCE;
578 #endif
579 
580  QgsRenderContext &mRenderContext;
581  std::unique_ptr< QgsExpressionContextScope > mExpressionContextScope;
582  QgsUnitTypes::RenderUnit mOutputUnit;
583  QgsMapUnitScale mMapUnitScale;
584  qreal mOpacity = 1.0;
585  bool mSelected;
586  QgsSymbol::RenderHints mRenderHints;
587  const QgsFeature *mFeature; //current feature
588  QgsFields mFields;
589  int mGeometryPartCount;
590  int mGeometryPartNum;
592 };
593 
594 
595 
597 
598 
603 class CORE_EXPORT QgsMarkerSymbol : public QgsSymbol
604 {
605  public:
606 
611  static QgsMarkerSymbol *createSimple( const QgsStringMap &properties ) SIP_FACTORY;
612 
614 
621  void setAngle( double symbolAngle );
622 
630  double angle() const;
631 
637  void setDataDefinedAngle( const QgsProperty &property );
638 
646  QgsProperty dataDefinedAngle() const;
647 
655  void setLineAngle( double lineAngle );
656 
665  void setSize( double size );
666 
674  double size() const;
675 
684  void setSizeUnit( QgsUnitTypes::RenderUnit unit );
685 
694  QgsUnitTypes::RenderUnit sizeUnit() const;
695 
704  void setSizeMapUnitScale( const QgsMapUnitScale &scale );
705 
715  QgsMapUnitScale sizeMapUnitScale() const;
716 
722  void setDataDefinedSize( const QgsProperty &property );
723 
731  QgsProperty dataDefinedSize() const;
732 
733  void setScaleMethod( QgsSymbol::ScaleMethod scaleMethod );
734  ScaleMethod scaleMethod();
735 
736  void renderPoint( QPointF point, const QgsFeature *f, QgsRenderContext &context, int layer = -1, bool selected = false );
737 
749  QRectF bounds( QPointF point, QgsRenderContext &context, const QgsFeature &feature = QgsFeature() ) const;
750 
751  virtual QgsMarkerSymbol *clone() const override SIP_FACTORY;
752 
753  private:
754 
755  void renderPointUsingLayer( QgsMarkerSymbolLayer *layer, QPointF point, QgsSymbolRenderContext &context );
756 
757 };
758 
759 
764 class CORE_EXPORT QgsLineSymbol : public QgsSymbol
765 {
766  public:
767 
772  static QgsLineSymbol *createSimple( const QgsStringMap &properties ) SIP_FACTORY;
773 
775 
776  void setWidth( double width );
777  double width() const;
778 
784  void setDataDefinedWidth( const QgsProperty &property );
785 
793  QgsProperty dataDefinedWidth() const;
794 
795  void renderPolyline( const QPolygonF &points, const QgsFeature *f, QgsRenderContext &context, int layer = -1, bool selected = false );
796 
797  virtual QgsLineSymbol *clone() const override SIP_FACTORY;
798 
799  private:
800 
801  void renderPolylineUsingLayer( QgsLineSymbolLayer *layer, const QPolygonF &points, QgsSymbolRenderContext &context );
802 
803 };
804 
805 
810 class CORE_EXPORT QgsFillSymbol : public QgsSymbol
811 {
812  public:
813 
818  static QgsFillSymbol *createSimple( const QgsStringMap &properties ) SIP_FACTORY;
819 
821  void setAngle( double angle );
822  void renderPolygon( const QPolygonF &points, QList<QPolygonF> *rings, const QgsFeature *f, QgsRenderContext &context, int layer = -1, bool selected = false );
823 
824  virtual QgsFillSymbol *clone() const override SIP_FACTORY;
825 
826  private:
827 
828  void renderPolygonUsingLayer( QgsSymbolLayer *layer, const QPolygonF &points, QList<QPolygonF> *rings, QgsSymbolRenderContext &context );
830  QRectF polygonBounds( const QPolygonF &points, const QList<QPolygonF> *rings ) const;
832  QList<QPolygonF> *translateRings( const QList<QPolygonF> *rings, double dx, double dy ) const;
833 };
834 
835 #endif
836 
double y
Definition: qgspoint.h:42
QgsFields fields() const
Fields of the layer.
Definition: qgssymbol.h:526
QPointF toQPointF() const
Returns the point as a QPointF.
Definition: qgspoint.cpp:563
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:297
const QgsRenderContext & renderContext() const
Definition: qgssymbol.h:451
static QPointF _getPoint(QgsRenderContext &context, const QgsPoint &point)
Creates a point in screen coordinates from a QgsPoint in map coordinates.
Definition: qgssymbol.h:336
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:465
bool clipFeaturesToExtent() const
Returns whether features drawn by the symbol will be clipped to the render context&#39;s extent...
Definition: qgssymbol.h:298
bool mClipFeaturesToExtent
Definition: qgssymbol.h:400
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:538
int symbolLayerCount() const
Returns total number of symbol layers contained in the symbol.
Definition: qgssymbol.h:143
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:62
#define SIP_TRANSFERBACK
Definition: qgis_sip.h:41
QMap< QString, QString > QgsStringMap
Definition: qgis.h:443
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:83
virtual QSet< QString > usedAttributes(const QgsRenderContext &context) const =0
Return a list of attributes required by this renderer.
SymbolType mType
Definition: qgssymbol.h:393
QgsSymbolLayerList mLayers
Definition: qgssymbol.h:394
QString type() const
Definition: qgsrenderer.h:126
void setOriginalGeometryType(QgsWkbTypes::GeometryType type)
Sets the geometry type for the original feature geometry being rendered.
Definition: qgssymbol.h:508
const QgsVectorLayer * mLayer
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:264
void setGeometryPartNum(int num)
Sets the part number of current geometry.
Definition: qgssymbol.h:550
Calculate scale by the area.
Definition: qgssymbol.h:96
qreal opacity() const
Returns the opacity for the symbol.
Definition: qgssymbol.h:475
void setRenderHints(RenderHints hints)
Sets rendering hint flags for the symbol.
Definition: qgssymbol.h:270
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:467
#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:485
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
const QgsVectorLayer * layer() const
Definition: qgssymbol.h:315
QgsRenderContext & renderContext()
Definition: qgssymbol.h:450
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:497
bool selected() const
Definition: qgssymbol.h:484
QgsWkbTypes::GeometryType originalGeometryType() const
Returns the geometry type for the original feature geometry being rendered.
Definition: qgssymbol.h:518
void setFeature(const QgsFeature *f)
Definition: qgssymbol.h:499
int geometryPartNum() const
Part number of current geometry.
Definition: qgssymbol.h:544
GeometryType
The geometry types are used to group QgsWkbTypes::Type in a coarse way.
Definition: qgswkbtypes.h:137
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
Definition: qgssymbol.h:113
Struct for storing maximum and minimum scales for measurements in map units.
RenderHints mRenderHints
Definition: qgssymbol.h:399
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
Get a deep copy of this symbol.
void setLayer(const QgsVectorLayer *layer)
Definition: qgssymbol.h:314
RenderHints renderHints() const
Returns the rendering hint flags for the symbol.
Definition: qgssymbol.h:276
const QgsFeature * feature() const
Current feature being rendered - may be null.
Definition: qgssymbol.h:501
void setOpacity(qreal opacity)
Sets the opacity for the symbol.
Definition: qgssymbol.h:482
#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 list of symbol layers contained in the symbol.
Definition: qgssymbol.h:124
void setClipFeaturesToExtent(bool clipFeaturesToExtent)
Sets whether features drawn by the symbol should be clipped to the render context&#39;s extent...
Definition: qgssymbol.h:287
qreal opacity() const
Returns the opacity for the symbol.
Definition: qgssymbol.h:257
void setMapUnitScale(const QgsMapUnitScale &scale)
Definition: qgssymbol.h:468
Polygon geometry type.
Definition: qgspolygon.h:31
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:532
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.
RenderUnit
Rendering size units.
Definition: qgsunittypes.h:100
QgsSymbol::RenderHints renderHints() const
Returns the rendering hint flags for the symbol.
Definition: qgssymbol.h:491
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:462
double x
Definition: qgspoint.h:41