QGIS API Documentation  2.17.0-Master (2f94c66)
qgssymbolv2.h
Go to the documentation of this file.
1 /***************************************************************************
2  qgssymbolv2.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 QGSSYMBOLV2_H
17 #define QGSSYMBOLV2_H
18 
19 #include "qgis.h"
20 #include <QList>
21 #include <QMap>
22 #include "qgsmapunitscale.h"
23 #include "qgsgeometry.h"
24 #include "qgspointv2.h"
25 
26 class QColor;
27 class QImage;
28 class QPainter;
29 class QSize;
30 class QPointF;
31 class QPolygonF;
32 
33 class QDomDocument;
34 class QDomElement;
35 //class
36 
37 class QgsFeature;
38 class QgsFields;
39 class QgsSymbolLayerV2;
40 class QgsRenderContext;
41 class QgsVectorLayer;
42 class QgsPaintEffect;
46 class QgsDataDefined;
49 
51 
55 class CORE_EXPORT QgsSymbolV2
56 {
57  friend class QgsFeatureRendererV2;
58 
59  public:
60 
64  //TODO QGIS 3.0 - move to QgsUnitTypes and rename to SymbolUnit
66  {
67  MM = 0,
72  };
73 
75 
80  {
82  Line,
83  Fill,
84  Hybrid
85  };
86 
91  {
93  ScaleDiameter
94  };
95 
97  {
98  DataDefinedSizeScale = 1,
99  DataDefinedRotation = 2
100  };
101 
102  virtual ~QgsSymbolV2();
103 
105  static QgsSymbolV2* defaultSymbol( QGis::GeometryType geomType );
106 
107  SymbolType type() const { return mType; }
108 
109  // symbol layers handling
110 
117  QgsSymbolLayerV2List symbolLayers() { return mLayers; }
118 
126  QgsSymbolLayerV2* symbolLayer( int layer );
127 
134  int symbolLayerCount() { return mLayers.count(); }
135 
143  bool insertSymbolLayer( int index, QgsSymbolLayerV2* layer );
144 
151  bool appendSymbolLayer( QgsSymbolLayerV2* layer );
152 
154  bool deleteSymbolLayer( int index );
155 
162  QgsSymbolLayerV2* takeSymbolLayer( int index );
163 
165  bool changeSymbolLayer( int index, QgsSymbolLayerV2 *layer );
166 
167  void startRender( QgsRenderContext& context, const QgsFields* fields = nullptr );
168  void stopRender( QgsRenderContext& context );
169 
170  void setColor( const QColor& color );
171  QColor color() const;
172 
176  void drawPreviewIcon( QPainter* painter, QSize size, QgsRenderContext* customContext = nullptr );
177 
179  void exportImage( const QString& path, const QString& format, QSize size );
180 
182  QImage asImage( QSize size, QgsRenderContext* customContext = nullptr );
183 
188  QImage bigSymbolPreviewImage( QgsExpressionContext* expressionContext = nullptr );
189 
190  QString dump() const;
191 
192  virtual QgsSymbolV2* clone() const = 0;
193 
194  void toSld( QDomDocument &doc, QDomElement &element, QgsStringMap props ) const;
195 
196  QgsSymbolV2::OutputUnit outputUnit() const;
197  void setOutputUnit( QgsSymbolV2::OutputUnit u );
198 
199  QgsMapUnitScale mapUnitScale() const;
200  void setMapUnitScale( const QgsMapUnitScale& scale );
201 
203  qreal alpha() const { return mAlpha; }
205  void setAlpha( qreal alpha ) { mAlpha = alpha; }
206 
207  void setRenderHints( int hints ) { mRenderHints = hints; }
208  int renderHints() const { return mRenderHints; }
209 
218  void setClipFeaturesToExtent( bool clipFeaturesToExtent ) { mClipFeaturesToExtent = clipFeaturesToExtent; }
219 
228  bool clipFeaturesToExtent() const { return mClipFeaturesToExtent; }
229 
236 
240  bool hasDataDefinedProperties() const;
241 
243  void setLayer( const QgsVectorLayer* layer ) { mLayer = layer; }
244  const QgsVectorLayer* layer() const { return mLayer; }
245 
249  void renderFeature( const QgsFeature& feature, QgsRenderContext& context, int layer = -1, bool selected = false, bool drawVertexMarker = false, int currentVertexMarkerType = 0, int currentVertexMarkerSize = 0 );
250 
256  QgsSymbolV2RenderContext* symbolRenderContext();
257 
258  protected:
259  QgsSymbolV2( SymbolType type, const QgsSymbolLayerV2List& layers ); // can't be instantiated
260 
264  static inline void _getPoint( QPointF& pt, QgsRenderContext& context, const QgsPointV2* point )
265  {
266  if ( context.coordinateTransform() )
267  {
268  double x = point->x();
269  double y = point->y();
270  double z = 0.0;
271  context.coordinateTransform()->transformInPlace( x, y, z );
272  pt = QPointF( x, y );
273 
274  }
275  else
276  pt = point->toQPointF();
277 
278  context.mapToPixel().transformInPlace( pt.rx(), pt.ry() );
279  }
280 
285  static QgsConstWkbPtr _getPoint( QPointF& pt, QgsRenderContext& context, QgsConstWkbPtr& wkb );
286 
291  static QgsConstWkbPtr _getLineString( QPolygonF& pts, QgsRenderContext& context, QgsConstWkbPtr& wkb, bool clipToExtent = true );
292 
297  static QgsConstWkbPtr _getPolygon( QPolygonF& pts, QList<QPolygonF>& holes, QgsRenderContext& context, QgsConstWkbPtr& wkb, bool clipToExtent = true );
298 
303  QgsSymbolLayerV2List cloneLayers() const;
304 
314  void renderUsingLayer( QgsSymbolLayerV2* layer, QgsSymbolV2RenderContext& context );
315 
319  Q_DECL_DEPRECATED bool isSymbolLayerCompatible( SymbolType layerType );
320 
323  void renderVertexMarker( QPointF pt, QgsRenderContext& context, int currentVertexMarkerType, int currentVertexMarkerSize );
324 
327 
329  qreal mAlpha;
330 
333 
334  const QgsVectorLayer* mLayer; //current vectorlayer
335 
336  private:
338  QgsSymbolV2RenderContext* mSymbolRenderContext;
339 
340  Q_DISABLE_COPY( QgsSymbolV2 )
341 
342 };
343 
345 
349 class CORE_EXPORT QgsSymbolV2RenderContext
350 {
351  public:
352  QgsSymbolV2RenderContext( QgsRenderContext& c, QgsSymbolV2::OutputUnit u, qreal alpha = 1.0, bool selected = false, int renderHints = 0, const QgsFeature* f = nullptr, const QgsFields* fields = nullptr, const QgsMapUnitScale& mapUnitScale = QgsMapUnitScale() );
354 
355  QgsRenderContext& renderContext() { return mRenderContext; }
356  const QgsRenderContext& renderContext() const { return mRenderContext; }
357 
363  void setOriginalValueVariable( const QVariant& value );
364 
365  QgsSymbolV2::OutputUnit outputUnit() const { return mOutputUnit; }
366  void setOutputUnit( QgsSymbolV2::OutputUnit u ) { mOutputUnit = u; }
367 
368  QgsMapUnitScale mapUnitScale() const { return mMapUnitScale; }
369  void setMapUnitScale( const QgsMapUnitScale& scale ) { mMapUnitScale = scale; }
370 
372  qreal alpha() const { return mAlpha; }
374  void setAlpha( qreal alpha ) { mAlpha = alpha; }
375 
376  bool selected() const { return mSelected; }
377  void setSelected( bool selected ) { mSelected = selected; }
378 
379  int renderHints() const { return mRenderHints; }
380  void setRenderHints( int hints ) { mRenderHints = hints; }
381 
382  void setFeature( const QgsFeature* f ) { mFeature = f; }
384  const QgsFeature* feature() const { return mFeature; }
385 
390  const QgsFields* fields() const { return mFields; }
391 
395  int geometryPartCount() const { return mGeometryPartCount; }
399  void setGeometryPartCount( int count ) { mGeometryPartCount = count; }
400 
404  int geometryPartNum() const { return mGeometryPartNum; }
408  void setGeometryPartNum( int num ) { mGeometryPartNum = num; }
409 
410  double outputLineWidth( double width ) const;
411  double outputPixelSize( double size ) const;
412 
413  // workaround for sip 4.7. Don't use assignment - will fail with assertion error
415 
421  QgsExpressionContextScope* expressionContextScope();
429  void setExpressionContextScope( QgsExpressionContextScope* contextScope );
430 
431  private:
432  QgsRenderContext& mRenderContext;
433  QgsExpressionContextScope* mExpressionContextScope;
434  QgsSymbolV2::OutputUnit mOutputUnit;
435  QgsMapUnitScale mMapUnitScale;
436  qreal mAlpha;
437  bool mSelected;
438  int mRenderHints;
439  const QgsFeature* mFeature; //current feature
440  const QgsFields* mFields;
441  int mGeometryPartCount;
442  int mGeometryPartNum;
443 
444 
446 };
447 
448 
449 
451 
452 
456 class CORE_EXPORT QgsMarkerSymbolV2 : public QgsSymbolV2
457 {
458  public:
459 
463  static QgsMarkerSymbolV2* createSimple( const QgsStringMap& properties );
464 
466 
472  void setAngle( double angle );
473 
480  double angle() const;
481 
487  void setDataDefinedAngle( const QgsDataDefined& dd );
488 
495  QgsDataDefined dataDefinedAngle() const;
496 
503  void setLineAngle( double lineAngle );
504 
512  void setSize( double size );
513 
520  double size() const;
521 
529  void setSizeUnit( OutputUnit unit );
530 
538  OutputUnit sizeUnit() const;
539 
547  void setSizeMapUnitScale( const QgsMapUnitScale& scale );
548 
557  QgsMapUnitScale sizeMapUnitScale() const;
558 
564  void setDataDefinedSize( const QgsDataDefined& dd );
565 
572  QgsDataDefined dataDefinedSize() const;
573 
574  void setScaleMethod( QgsSymbolV2::ScaleMethod scaleMethod );
575  ScaleMethod scaleMethod();
576 
577  void renderPoint( QPointF point, const QgsFeature* f, QgsRenderContext& context, int layer = -1, bool selected = false );
578 
589  QRectF bounds( QPointF point, QgsRenderContext& context, const QgsFeature &feature = QgsFeature() ) const;
590 
591  virtual QgsMarkerSymbolV2* clone() const override;
592 
593  private:
594 
595  void renderPointUsingLayer( QgsMarkerSymbolLayerV2* layer, QPointF point, QgsSymbolV2RenderContext& context );
596 
597 };
598 
599 
603 class CORE_EXPORT QgsLineSymbolV2 : public QgsSymbolV2
604 {
605  public:
609  static QgsLineSymbolV2* createSimple( const QgsStringMap& properties );
610 
612 
613  void setWidth( double width );
614  double width() const;
615 
621  void setDataDefinedWidth( const QgsDataDefined& dd );
622 
629  QgsDataDefined dataDefinedWidth() const;
630 
631  void renderPolyline( const QPolygonF& points, const QgsFeature* f, QgsRenderContext& context, int layer = -1, bool selected = false );
632 
633  virtual QgsLineSymbolV2* clone() const override;
634 
635  private:
636 
637  void renderPolylineUsingLayer( QgsLineSymbolLayerV2* layer, const QPolygonF& points, QgsSymbolV2RenderContext& context );
638 
639 };
640 
641 
645 class CORE_EXPORT QgsFillSymbolV2 : public QgsSymbolV2
646 {
647  public:
651  static QgsFillSymbolV2* createSimple( const QgsStringMap& properties );
652 
654  void setAngle( double angle );
655  void renderPolygon( const QPolygonF& points, QList<QPolygonF>* rings, const QgsFeature* f, QgsRenderContext& context, int layer = -1, bool selected = false );
656 
657  virtual QgsFillSymbolV2* clone() const override;
658 
659  private:
660 
661  void renderPolygonUsingLayer( QgsSymbolLayerV2* layer, const QPolygonF &points, QList<QPolygonF> *rings, QgsSymbolV2RenderContext &context );
663  QRectF polygonBounds( const QPolygonF &points, const QList<QPolygonF> *rings ) const;
665  QList<QPolygonF>* translateRings( const QList<QPolygonF> *rings, double dx, double dy ) const;
666 };
667 
668 #endif
669 
670 
671 /*
672 
673 QgsSymbolV2* ps = new QgsPointSymbol();
674 
675 // ----
676 
677 sl = QgsSymbolLayerV2Registry::instance()->createSymbolLayer("SimpleLine", { "color", "..." })
678 
679 // (or)
680 
681 sl = QgsSymbolLayerV2Registry::defaultSymbolLayer(QgsSymbolV2::Line)
682 
683 // (or)
684 
685 QgsSymbolLayerV2* sl = new QgsSimpleLineSymbolLayer(x,y,z);
686 QgsLineSymbol* s = new LineSymbol( [ sl ] );
687 
688 // ----
689 
690 rend = QgsSingleSymbolRenderer( new LineSymbol() );
691 */
692 
693 
static QgsConstWkbPtr _getPolygon(QPolygonF &pts, QList< QPolygonF > &holes, QgsRenderContext &context, QgsConstWkbPtr &wkb, bool clipToExtent=true)
Creates a polygon in screen coordinates from a wkb string in map coordinates.
static unsigned index
void setClipFeaturesToExtent(bool clipFeaturesToExtent)
Sets whether features drawn by the symbol should be clipped to the render context&#39;s extent...
Definition: qgssymbolv2.h:218
OutputUnit
The unit of the output.
Definition: qgssymbolv2.h:65
int renderHints() const
Definition: qgssymbolv2.h:379
bool mClipFeaturesToExtent
Definition: qgssymbolv2.h:332
A container class for data source field mapping or expression.
GeometryType
Definition: qgis.h:115
void setGeometryPartCount(int count)
Sets the part count of current geometry.
Definition: qgssymbolv2.h:399
const QgsVectorLayer * mLayer
Definition: qgssymbolv2.h:334
double x() const
Returns the point&#39;s x-coordinate.
Definition: qgspointv2.h:68
void setAlpha(qreal alpha)
Set alpha transparency 1 for opaque, 0 for invisible.
Definition: qgssymbolv2.h:374
SymbolType type() const
Definition: qgssymbolv2.h:107
virtual QString dump() const
for debugging
virtual QgsSymbolV2 * clone() const =0
QList< OutputUnit > OutputUnitList
Definition: qgssymbolv2.h:74
The output shall be in pixels.
Definition: qgssymbolv2.h:70
Base class for visual effects which can be applied to QPicture drawings.
static void _getPoint(QPointF &pt, QgsRenderContext &context, const QgsPointV2 *point)
Creates a point in screen coordinates from a QgsPointV2 in map coordinates.
Definition: qgssymbolv2.h:264
Container of fields for a vector layer.
Definition: qgsfield.h:252
Line symbol.
Definition: qgssymbolv2.h:82
void setRenderHints(int hints)
Definition: qgssymbolv2.h:380
QgsSymbolLayerV2List symbolLayers()
Returns list of symbol layers contained in the symbol.
Definition: qgssymbolv2.h:117
virtual QList< QString > usedAttributes()=0
Returns a set of attributes required for this renderer.
The feature class encapsulates a single feature including its id, geometry and a list of field/values...
Definition: qgsfeature.h:187
const QgsCoordinateTransform * coordinateTransform() const
SymbolType mType
Definition: qgssymbolv2.h:325
static QgsConstWkbPtr _getLineString(QPolygonF &pts, QgsRenderContext &context, QgsConstWkbPtr &wkb, bool clipToExtent=true)
Creates a line string in screen coordinates from a wkb string in map coordinates. ...
Marker symbol.
Definition: qgssymbolv2.h:81
virtual void startRender(QgsRenderContext &context, const QgsFields &fields)=0
Needs to be called when a new render cycle is started.
void setMapUnitScale(const QgsMapUnitScale &scale)
Definition: qgssymbolv2.h:369
const QgsVectorLayer * layer() const
Definition: qgssymbolv2.h:244
double y() const
Returns the point&#39;s y-coordinate.
Definition: qgspointv2.h:74
virtual void stopRender(QgsRenderContext &context)=0
Needs to be called when a render cycle has finished to clean up.
qreal alpha() const
Get alpha transparency 1 for opaque, 0 for invisible.
Definition: qgssymbolv2.h:372
QString type() const
Definition: qgsrendererv2.h:92
void renderVertexMarker(QPointF pt, QgsRenderContext &context)
render editing vertex marker at specified point
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.
Mixed units in symbol layers.
Definition: qgssymbolv2.h:69
void transformInPlace(double &x, double &y, double &z, TransformDirection direction=ForwardTransform) const
virtual QgsFeatureRendererV2 * clone() const =0
void transformInPlace(double &x, double &y) const
Transform device coordinates to map coordinates.
QgsSymbolLayerV2List mLayers
Definition: qgssymbolv2.h:326
QgsMapUnitScale mapUnitScale() const
Definition: qgssymbolv2.h:368
const QgsFeature * feature() const
Current feature being rendered - may be null.
Definition: qgssymbolv2.h:384
The ouput shall be a percentage of another measurement (eg canvas size, feature size) ...
Definition: qgssymbolv2.h:71
qreal alpha() const
Get alpha transparency 1 for opaque, 0 for invisible.
Definition: qgssymbolv2.h:203
void setLayer(const QgsVectorLayer *layer)
Definition: qgssymbolv2.h:243
Point geometry type, with support for z-dimension and m-values.
Definition: qgspointv2.h:34
Expression contexts are used to encapsulate the parameters around which a QgsExpression should be eva...
int geometryPartNum() const
Part number of current geometry.
Definition: qgssymbolv2.h:404
The output shall be in map unitx.
Definition: qgssymbolv2.h:68
int symbolLayerCount()
Returns total number of symbol layers contained in the symbol.
Definition: qgssymbolv2.h:134
qreal mAlpha
Symbol opacity (in the range 0 - 1)
Definition: qgssymbolv2.h:329
void setFeature(const QgsFeature *f)
Definition: qgssymbolv2.h:382
Single scope for storing variables and functions for use within a QgsExpressionContext.
QPointF toQPointF() const
Returns the point as a QPointF.
Definition: qgspointv2.cpp:438
void setOutputUnit(QgsSymbolV2::OutputUnit u)
Definition: qgssymbolv2.h:366
QgsSymbolV2::OutputUnit outputUnit() const
Definition: qgssymbolv2.h:365
double ANALYSIS_EXPORT angle(Point3D *p1, Point3D *p2, Point3D *p3, Point3D *p4)
Calculates the angle between two segments (in 2 dimension, z-values are ignored)
void setSelected(bool selected)
Definition: qgssymbolv2.h:377
virtual void toSld(QDomDocument &doc, QDomElement &element) const
used from subclasses to create SLD Rule elements following SLD v1.1 specs
SymbolType
Type of the symbol.
Definition: qgssymbolv2.h:79
Contains information about the context of a rendering operation.
ScaleMethod
Scale method.
Definition: qgssymbolv2.h:90
Struct for storing maximum and minimum scales for measurements in map units.
QList< QgsSymbolLayerV2 * > QgsSymbolLayerV2List
Definition: qgssymbolv2.h:48
QgsRenderContext & renderContext()
Definition: qgssymbolv2.h:355
Fill symbol.
Definition: qgssymbolv2.h:83
int geometryPartCount() const
Part count of current geometry.
Definition: qgssymbolv2.h:395
qreal & rx()
qreal & ry()
const QgsMapToPixel & mapToPixel() const
const QgsFields * fields() const
Fields of the layer.
Definition: qgssymbolv2.h:390
void setRenderHints(int hints)
Definition: qgssymbolv2.h:207
bool selected() const
Definition: qgssymbolv2.h:376
const QgsRenderContext & renderContext() const
Definition: qgssymbolv2.h:356
void setGeometryPartNum(int num)
Sets the part number of current geometry.
Definition: qgssymbolv2.h:408
Abstract base class for marker symbol layers.
static QgsConstWkbPtr _getPoint(QPointF &pt, QgsRenderContext &context, QgsConstWkbPtr &wkb)
Creates a point in screen coordinates from a wkb string in map coordinates.
Calculate scale by the area.
Definition: qgssymbolv2.h:92
bool clipFeaturesToExtent() const
Returns whether features drawn by the symbol will be clipped to the render context&#39;s extent...
Definition: qgssymbolv2.h:228
Represents a vector layer which manages a vector based data sets.
void setAlpha(qreal alpha)
Set alpha transparency 1 for opaque, 0 for invisible.
Definition: qgssymbolv2.h:205
int mRenderHints
Definition: qgssymbolv2.h:331
int renderHints() const
Definition: qgssymbolv2.h:208