QGIS API Documentation  2.15.0-Master (5f66276)
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 
52 class CORE_EXPORT QgsSymbolV2
53 {
54  friend class QgsFeatureRendererV2;
55 
56  public:
57 
61  //TODO QGIS 3.0 - move to QgsUnitTypes and rename to SymbolUnit
63  {
64  MM = 0,
69  };
70 
72 
77  {
79  Line,
80  Fill,
81  Hybrid
82  };
83 
88  {
90  ScaleDiameter
91  };
92 
94  {
95  DataDefinedSizeScale = 1,
96  DataDefinedRotation = 2
97  };
98 
99  virtual ~QgsSymbolV2();
100 
102  static QgsSymbolV2* defaultSymbol( QGis::GeometryType geomType );
103 
104  SymbolType type() const { return mType; }
105 
106  // symbol layers handling
107 
114  QgsSymbolLayerV2List symbolLayers() { return mLayers; }
115 
123  QgsSymbolLayerV2* symbolLayer( int layer );
124 
131  int symbolLayerCount() { return mLayers.count(); }
132 
140  bool insertSymbolLayer( int index, QgsSymbolLayerV2* layer );
141 
148  bool appendSymbolLayer( QgsSymbolLayerV2* layer );
149 
151  bool deleteSymbolLayer( int index );
152 
159  QgsSymbolLayerV2* takeSymbolLayer( int index );
160 
162  bool changeSymbolLayer( int index, QgsSymbolLayerV2 *layer );
163 
164  void startRender( QgsRenderContext& context, const QgsFields* fields = nullptr );
165  void stopRender( QgsRenderContext& context );
166 
167  void setColor( const QColor& color );
168  QColor color() const;
169 
173  void drawPreviewIcon( QPainter* painter, QSize size, QgsRenderContext* customContext = nullptr );
174 
176  void exportImage( const QString& path, const QString& format, QSize size );
177 
179  QImage asImage( QSize size, QgsRenderContext* customContext = nullptr );
180 
185  QImage bigSymbolPreviewImage( QgsExpressionContext* expressionContext = nullptr );
186 
187  QString dump() const;
188 
189  virtual QgsSymbolV2* clone() const = 0;
190 
191  void toSld( QDomDocument &doc, QDomElement &element, QgsStringMap props ) const;
192 
193  QgsSymbolV2::OutputUnit outputUnit() const;
194  void setOutputUnit( QgsSymbolV2::OutputUnit u );
195 
196  QgsMapUnitScale mapUnitScale() const;
197  void setMapUnitScale( const QgsMapUnitScale& scale );
198 
200  qreal alpha() const { return mAlpha; }
202  void setAlpha( qreal alpha ) { mAlpha = alpha; }
203 
204  void setRenderHints( int hints ) { mRenderHints = hints; }
205  int renderHints() const { return mRenderHints; }
206 
215  void setClipFeaturesToExtent( bool clipFeaturesToExtent ) { mClipFeaturesToExtent = clipFeaturesToExtent; }
216 
225  bool clipFeaturesToExtent() const { return mClipFeaturesToExtent; }
226 
233 
237  bool hasDataDefinedProperties() const;
238 
240  void setLayer( const QgsVectorLayer* layer ) { mLayer = layer; }
241  const QgsVectorLayer* layer() const { return mLayer; }
242 
246  void renderFeature( const QgsFeature& feature, QgsRenderContext& context, int layer = -1, bool selected = false, bool drawVertexMarker = false, int currentVertexMarkerType = 0, int currentVertexMarkerSize = 0 );
247 
253  QgsSymbolV2RenderContext* symbolRenderContext();
254 
255  protected:
256  QgsSymbolV2( SymbolType type, const QgsSymbolLayerV2List& layers ); // can't be instantiated
257 
261  static inline void _getPoint( QPointF& pt, QgsRenderContext& context, const QgsPointV2* point )
262  {
263  if ( context.coordinateTransform() )
264  {
265  double x = point->x();
266  double y = point->y();
267  double z = 0.0;
268  context.coordinateTransform()->transformInPlace( x, y, z );
269  pt = QPointF( x, y );
270 
271  }
272  else
273  pt = point->toQPointF();
274 
275  context.mapToPixel().transformInPlace( pt.rx(), pt.ry() );
276  }
277 
282  static QgsConstWkbPtr _getPoint( QPointF& pt, QgsRenderContext& context, QgsConstWkbPtr& wkb );
283 
288  static QgsConstWkbPtr _getLineString( QPolygonF& pts, QgsRenderContext& context, QgsConstWkbPtr& wkb, bool clipToExtent = true );
289 
294  static QgsConstWkbPtr _getPolygon( QPolygonF& pts, QList<QPolygonF>& holes, QgsRenderContext& context, QgsConstWkbPtr& wkb, bool clipToExtent = true );
295 
300  QgsSymbolLayerV2List cloneLayers() const;
301 
311  void renderUsingLayer( QgsSymbolLayerV2* layer, QgsSymbolV2RenderContext& context );
312 
316  Q_DECL_DEPRECATED bool isSymbolLayerCompatible( SymbolType layerType );
317 
320  void renderVertexMarker( QPointF pt, QgsRenderContext& context, int currentVertexMarkerType, int currentVertexMarkerSize );
321 
324 
326  qreal mAlpha;
327 
330 
331  const QgsVectorLayer* mLayer; //current vectorlayer
332 
333  private:
335  QgsSymbolV2RenderContext* mSymbolRenderContext;
336 
337  Q_DISABLE_COPY( QgsSymbolV2 )
338 
339 };
340 
342 
343 class CORE_EXPORT QgsSymbolV2RenderContext
344 {
345  public:
346  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() );
348 
349  QgsRenderContext& renderContext() { return mRenderContext; }
350  const QgsRenderContext& renderContext() const { return mRenderContext; }
351 
357  void setOriginalValueVariable( const QVariant& value );
358 
359  QgsSymbolV2::OutputUnit outputUnit() const { return mOutputUnit; }
360  void setOutputUnit( QgsSymbolV2::OutputUnit u ) { mOutputUnit = u; }
361 
362  QgsMapUnitScale mapUnitScale() const { return mMapUnitScale; }
363  void setMapUnitScale( const QgsMapUnitScale& scale ) { mMapUnitScale = scale; }
364 
366  qreal alpha() const { return mAlpha; }
368  void setAlpha( qreal alpha ) { mAlpha = alpha; }
369 
370  bool selected() const { return mSelected; }
371  void setSelected( bool selected ) { mSelected = selected; }
372 
373  int renderHints() const { return mRenderHints; }
374  void setRenderHints( int hints ) { mRenderHints = hints; }
375 
376  void setFeature( const QgsFeature* f ) { mFeature = f; }
378  const QgsFeature* feature() const { return mFeature; }
379 
384  const QgsFields* fields() const { return mFields; }
385 
389  int geometryPartCount() const { return mGeometryPartCount; }
393  void setGeometryPartCount( int count ) { mGeometryPartCount = count; }
394 
398  int geometryPartNum() const { return mGeometryPartNum; }
402  void setGeometryPartNum( int num ) { mGeometryPartNum = num; }
403 
404  double outputLineWidth( double width ) const;
405  double outputPixelSize( double size ) const;
406 
407  // workaround for sip 4.7. Don't use assignment - will fail with assertion error
409 
415  QgsExpressionContextScope* expressionContextScope();
423  void setExpressionContextScope( QgsExpressionContextScope* contextScope );
424 
425  private:
426  QgsRenderContext& mRenderContext;
427  QgsExpressionContextScope* mExpressionContextScope;
428  QgsSymbolV2::OutputUnit mOutputUnit;
429  QgsMapUnitScale mMapUnitScale;
430  qreal mAlpha;
431  bool mSelected;
432  int mRenderHints;
433  const QgsFeature* mFeature; //current feature
434  const QgsFields* mFields;
435  int mGeometryPartCount;
436  int mGeometryPartNum;
437 
438 
440 };
441 
442 
443 
445 
446 
447 
448 class CORE_EXPORT QgsMarkerSymbolV2 : public QgsSymbolV2
449 {
450  public:
451 
455  static QgsMarkerSymbolV2* createSimple( const QgsStringMap& properties );
456 
458 
464  void setAngle( double angle );
465 
472  double angle() const;
473 
479  void setDataDefinedAngle( const QgsDataDefined& dd );
480 
487  QgsDataDefined dataDefinedAngle() const;
488 
495  void setLineAngle( double lineAngle );
496 
504  void setSize( double size );
505 
512  double size() const;
513 
521  void setSizeUnit( OutputUnit unit );
522 
530  OutputUnit sizeUnit() const;
531 
539  void setSizeMapUnitScale( const QgsMapUnitScale& scale );
540 
549  QgsMapUnitScale sizeMapUnitScale() const;
550 
556  void setDataDefinedSize( const QgsDataDefined& dd );
557 
564  QgsDataDefined dataDefinedSize() const;
565 
566  void setScaleMethod( QgsSymbolV2::ScaleMethod scaleMethod );
567  ScaleMethod scaleMethod();
568 
569  void renderPoint( QPointF point, const QgsFeature* f, QgsRenderContext& context, int layer = -1, bool selected = false );
570 
581  QRectF bounds( QPointF point, QgsRenderContext& context, const QgsFeature &feature = QgsFeature() ) const;
582 
583  virtual QgsMarkerSymbolV2* clone() const override;
584 
585  private:
586 
587  void renderPointUsingLayer( QgsMarkerSymbolLayerV2* layer, QPointF point, QgsSymbolV2RenderContext& context );
588 
589 };
590 
591 
592 
593 class CORE_EXPORT QgsLineSymbolV2 : public QgsSymbolV2
594 {
595  public:
599  static QgsLineSymbolV2* createSimple( const QgsStringMap& properties );
600 
602 
603  void setWidth( double width );
604  double width() const;
605 
611  void setDataDefinedWidth( const QgsDataDefined& dd );
612 
619  QgsDataDefined dataDefinedWidth() const;
620 
621  void renderPolyline( const QPolygonF& points, const QgsFeature* f, QgsRenderContext& context, int layer = -1, bool selected = false );
622 
623  virtual QgsLineSymbolV2* clone() const override;
624 
625  private:
626 
627  void renderPolylineUsingLayer( QgsLineSymbolLayerV2* layer, const QPolygonF& points, QgsSymbolV2RenderContext& context );
628 
629 };
630 
631 
632 
633 class CORE_EXPORT QgsFillSymbolV2 : public QgsSymbolV2
634 {
635  public:
639  static QgsFillSymbolV2* createSimple( const QgsStringMap& properties );
640 
642  void setAngle( double angle );
643  void renderPolygon( const QPolygonF& points, QList<QPolygonF>* rings, const QgsFeature* f, QgsRenderContext& context, int layer = -1, bool selected = false );
644 
645  virtual QgsFillSymbolV2* clone() const override;
646 
647  private:
648 
649  void renderPolygonUsingLayer( QgsSymbolLayerV2* layer, const QPolygonF &points, QList<QPolygonF> *rings, QgsSymbolV2RenderContext &context );
651  QRectF polygonBounds( const QPolygonF &points, const QList<QPolygonF> *rings ) const;
653  QList<QPolygonF>* translateRings( const QList<QPolygonF> *rings, double dx, double dy ) const;
654 };
655 
656 #endif
657 
658 
659 /*
660 
661 QgsSymbolV2* ps = new QgsPointSymbol();
662 
663 // ----
664 
665 sl = QgsSymbolLayerV2Registry::instance()->createSymbolLayer("SimpleLine", { "color", "..." })
666 
667 // (or)
668 
669 sl = QgsSymbolLayerV2Registry::defaultSymbolLayer(QgsSymbolV2::Line)
670 
671 // (or)
672 
673 QgsSymbolLayerV2* sl = new QgsSimpleLineSymbolLayer(x,y,z);
674 QgsLineSymbol* s = new LineSymbol( [ sl ] );
675 
676 // ----
677 
678 rend = QgsSingleSymbolRenderer( new LineSymbol() );
679 */
680 
681 
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:215
OutputUnit
The unit of the output.
Definition: qgssymbolv2.h:62
int renderHints() const
Definition: qgssymbolv2.h:373
bool mClipFeaturesToExtent
Definition: qgssymbolv2.h:329
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:393
const QgsVectorLayer * mLayer
Definition: qgssymbolv2.h:331
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:368
SymbolType type() const
Definition: qgssymbolv2.h:104
virtual QString dump() const
for debugging
virtual QgsSymbolV2 * clone() const =0
QList< OutputUnit > OutputUnitList
Definition: qgssymbolv2.h:71
The output shall be in pixels.
Definition: qgssymbolv2.h:67
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:261
Container of fields for a vector layer.
Definition: qgsfield.h:193
Line symbol.
Definition: qgssymbolv2.h:79
void setRenderHints(int hints)
Definition: qgssymbolv2.h:374
QgsSymbolLayerV2List symbolLayers()
Returns list of symbol layers contained in the symbol.
Definition: qgssymbolv2.h:114
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:322
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:78
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:363
const QgsVectorLayer * layer() const
Definition: qgssymbolv2.h:241
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:366
QString type() const
Definition: qgsrendererv2.h:86
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:66
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:323
QgsMapUnitScale mapUnitScale() const
Definition: qgssymbolv2.h:362
const QgsFeature * feature() const
Current feature being rendered - may be null.
Definition: qgssymbolv2.h:378
The ouput shall be a percentage of another measurement (eg canvas size, feature size) ...
Definition: qgssymbolv2.h:68
qreal alpha() const
Get alpha transparency 1 for opaque, 0 for invisible.
Definition: qgssymbolv2.h:200
void setLayer(const QgsVectorLayer *layer)
Definition: qgssymbolv2.h:240
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:398
The output shall be in map unitx.
Definition: qgssymbolv2.h:65
int symbolLayerCount()
Returns total number of symbol layers contained in the symbol.
Definition: qgssymbolv2.h:131
qreal mAlpha
Symbol opacity (in the range 0 - 1)
Definition: qgssymbolv2.h:326
void setFeature(const QgsFeature *f)
Definition: qgssymbolv2.h:376
Single scope for storing variables and functions for use within a QgsExpressionContext.
QPointF toQPointF() const
Returns the point as a QPointF.
Definition: qgspointv2.cpp:432
void setOutputUnit(QgsSymbolV2::OutputUnit u)
Definition: qgssymbolv2.h:360
QgsSymbolV2::OutputUnit outputUnit() const
Definition: qgssymbolv2.h:359
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:371
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:76
Contains information about the context of a rendering operation.
ScaleMethod
Scale method.
Definition: qgssymbolv2.h:87
Struct for storing maximum and minimum scales for measurements in map units.
QList< QgsSymbolLayerV2 * > QgsSymbolLayerV2List
Definition: qgssymbolv2.h:48
QgsRenderContext & renderContext()
Definition: qgssymbolv2.h:349
Fill symbol.
Definition: qgssymbolv2.h:80
int geometryPartCount() const
Part count of current geometry.
Definition: qgssymbolv2.h:389
qreal & rx()
qreal & ry()
const QgsMapToPixel & mapToPixel() const
const QgsFields * fields() const
Fields of the layer.
Definition: qgssymbolv2.h:384
void setRenderHints(int hints)
Definition: qgssymbolv2.h:204
bool selected() const
Definition: qgssymbolv2.h:370
const QgsRenderContext & renderContext() const
Definition: qgssymbolv2.h:350
void setGeometryPartNum(int num)
Sets the part number of current geometry.
Definition: qgssymbolv2.h:402
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:89
bool clipFeaturesToExtent() const
Returns whether features drawn by the symbol will be clipped to the render context&#39;s extent...
Definition: qgssymbolv2.h:225
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:202
int mRenderHints
Definition: qgssymbolv2.h:328
int renderHints() const
Definition: qgssymbolv2.h:205