QGIS API Documentation  3.0.2-Girona (307d082)
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 = nullptr;
400  bool mClipFeaturesToExtent = true;
401 
402  const QgsVectorLayer *mLayer = nullptr; //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 = nullptr, const QgsFeature *f = nullptr, const QgsFields &fields = QgsFields(), const QgsMapUnitScale &mapUnitScale = QgsMapUnitScale() );
446 
448  QgsSymbolRenderContext( const QgsSymbolRenderContext &rh ) = delete;
449 
453  QgsRenderContext &renderContext() { return mRenderContext; }
454 
459  const QgsRenderContext &renderContext() const { return mRenderContext; } SIP_SKIP
460 
467  void setOriginalValueVariable( const QVariant &value );
468 
470  QgsUnitTypes::RenderUnit outputUnit() const { return mOutputUnit; }
471 
473  void setOutputUnit( QgsUnitTypes::RenderUnit u ) { mOutputUnit = u; }
474 
475  QgsMapUnitScale mapUnitScale() const { return mMapUnitScale; }
476  void setMapUnitScale( const QgsMapUnitScale &scale ) { mMapUnitScale = scale; }
477 
483  qreal opacity() const { return mOpacity; }
484 
490  void setOpacity( qreal opacity ) { mOpacity = opacity; }
491 
492  bool selected() const { return mSelected; }
493  void setSelected( bool selected ) { mSelected = selected; }
494 
499  QgsSymbol::RenderHints renderHints() const { return mRenderHints; }
500 
505  void setRenderHints( QgsSymbol::RenderHints hints ) { mRenderHints = hints; }
506 
507  void setFeature( const QgsFeature *f ) { mFeature = f; }
509  const QgsFeature *feature() const { return mFeature; }
510 
516  void setOriginalGeometryType( QgsWkbTypes::GeometryType type ) { mOriginalGeometryType = type; }
517 
526  QgsWkbTypes::GeometryType originalGeometryType() const { return mOriginalGeometryType; }
527 
534  QgsFields fields() const { return mFields; }
535 
540  int geometryPartCount() const { return mGeometryPartCount; }
541 
546  void setGeometryPartCount( int count ) { mGeometryPartCount = count; }
547 
552  int geometryPartNum() const { return mGeometryPartNum; }
553 
558  void setGeometryPartNum( int num ) { mGeometryPartNum = num; }
559 
560  double outputLineWidth( double width ) const;
561  double outputPixelSize( double size ) const;
562 
563  // workaround for sip 4.7. Don't use assignment - will fail with assertion error
564  QgsSymbolRenderContext &operator=( const QgsSymbolRenderContext & );
565 
571  QgsExpressionContextScope *expressionContextScope();
572 
580  void setExpressionContextScope( QgsExpressionContextScope *contextScope SIP_TRANSFER );
581 
582  private:
583 
584 #ifdef SIP_RUN
585  QgsSymbolRenderContext( const QgsSymbolRenderContext &rh ) SIP_FORCE;
586 #endif
587 
588  QgsRenderContext &mRenderContext;
589  std::unique_ptr< QgsExpressionContextScope > mExpressionContextScope;
590  QgsUnitTypes::RenderUnit mOutputUnit;
591  QgsMapUnitScale mMapUnitScale;
592  qreal mOpacity = 1.0;
593  bool mSelected;
594  QgsSymbol::RenderHints mRenderHints;
595  const QgsFeature *mFeature; //current feature
596  QgsFields mFields;
597  int mGeometryPartCount;
598  int mGeometryPartNum;
600 };
601 
602 
603 
605 
606 
611 class CORE_EXPORT QgsMarkerSymbol : public QgsSymbol
612 {
613  public:
614 
619  static QgsMarkerSymbol *createSimple( const QgsStringMap &properties ) SIP_FACTORY;
620 
622 
629  void setAngle( double symbolAngle );
630 
638  double angle() const;
639 
645  void setDataDefinedAngle( const QgsProperty &property );
646 
654  QgsProperty dataDefinedAngle() const;
655 
663  void setLineAngle( double lineAngle );
664 
673  void setSize( double size );
674 
682  double size() const;
683 
692  void setSizeUnit( QgsUnitTypes::RenderUnit unit );
693 
702  QgsUnitTypes::RenderUnit sizeUnit() const;
703 
712  void setSizeMapUnitScale( const QgsMapUnitScale &scale );
713 
723  QgsMapUnitScale sizeMapUnitScale() const;
724 
730  void setDataDefinedSize( const QgsProperty &property );
731 
739  QgsProperty dataDefinedSize() const;
740 
741  void setScaleMethod( QgsSymbol::ScaleMethod scaleMethod );
742  ScaleMethod scaleMethod();
743 
744  void renderPoint( QPointF point, const QgsFeature *f, QgsRenderContext &context, int layer = -1, bool selected = false );
745 
757  QRectF bounds( QPointF point, QgsRenderContext &context, const QgsFeature &feature = QgsFeature() ) const;
758 
759  QgsMarkerSymbol *clone() const override SIP_FACTORY;
760 
761  private:
762 
763  void renderPointUsingLayer( QgsMarkerSymbolLayer *layer, QPointF point, QgsSymbolRenderContext &context );
764 
765 };
766 
767 
772 class CORE_EXPORT QgsLineSymbol : public QgsSymbol
773 {
774  public:
775 
780  static QgsLineSymbol *createSimple( const QgsStringMap &properties ) SIP_FACTORY;
781 
783 
784  void setWidth( double width );
785  double width() const;
786 
792  void setDataDefinedWidth( const QgsProperty &property );
793 
801  QgsProperty dataDefinedWidth() const;
802 
803  void renderPolyline( const QPolygonF &points, const QgsFeature *f, QgsRenderContext &context, int layer = -1, bool selected = false );
804 
805  QgsLineSymbol *clone() const override SIP_FACTORY;
806 
807  private:
808 
809  void renderPolylineUsingLayer( QgsLineSymbolLayer *layer, const QPolygonF &points, QgsSymbolRenderContext &context );
810 
811 };
812 
813 
818 class CORE_EXPORT QgsFillSymbol : public QgsSymbol
819 {
820  public:
821 
826  static QgsFillSymbol *createSimple( const QgsStringMap &properties ) SIP_FACTORY;
827 
829  void setAngle( double angle );
830  void renderPolygon( const QPolygonF &points, QList<QPolygonF> *rings, const QgsFeature *f, QgsRenderContext &context, int layer = -1, bool selected = false );
831 
832  QgsFillSymbol *clone() const override SIP_FACTORY;
833 
834  private:
835 
836  void renderPolygonUsingLayer( QgsSymbolLayer *layer, const QPolygonF &points, QList<QPolygonF> *rings, QgsSymbolRenderContext &context );
838  QRectF polygonBounds( const QPolygonF &points, const QList<QPolygonF> *rings ) const;
840  QList<QPolygonF> *translateRings( const QList<QPolygonF> *rings, double dx, double dy ) const;
841 };
842 
843 #endif
844 
double y
Definition: qgspoint.h:42
QgsFields fields() const
Fields of the layer.
Definition: qgssymbol.h:534
QPointF toQPointF() const
Returns the point as a QPointF.
Definition: qgspoint.cpp:582
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
Returns a reference to the context&#39;s render context.
Definition: qgssymbol.h:459
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:473
bool clipFeaturesToExtent() const
Returns whether features drawn by the symbol will be clipped to the render context&#39;s extent...
Definition: qgssymbol.h:298
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:546
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:479
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:516
#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:558
Calculate scale by the area.
Definition: qgssymbol.h:96
qreal opacity() const
Returns the opacity for the symbol.
Definition: qgssymbol.h:483
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:475
#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:493
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()
Returns a reference to the context&#39;s render context.
Definition: qgssymbol.h:453
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:505
bool selected() const
Definition: qgssymbol.h:492
QgsWkbTypes::GeometryType originalGeometryType() const
Returns the geometry type for the original feature geometry being rendered.
Definition: qgssymbol.h:526
void setFeature(const QgsFeature *f)
Definition: qgssymbol.h:507
int geometryPartNum() const
Part number of current geometry.
Definition: qgssymbol.h:552
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.
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:509
void setOpacity(qreal opacity)
Sets the opacity for the symbol.
Definition: qgssymbol.h:490
#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:476
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:540
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:499
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:470
double x
Definition: qgspoint.h:41