QGIS API Documentation
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 
322  qreal mAlpha;
323 
326 
327  const QgsVectorLayer* mLayer; //current vectorlayer
328 
329  private:
331  QgsSymbolV2RenderContext* mSymbolRenderContext;
332 
333  Q_DISABLE_COPY( QgsSymbolV2 )
334 
335 };
336 
338 
339 class CORE_EXPORT QgsSymbolV2RenderContext
340 {
341  public:
342  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() );
344 
345  QgsRenderContext& renderContext() { return mRenderContext; }
346  const QgsRenderContext& renderContext() const { return mRenderContext; }
347 
353  void setOriginalValueVariable( const QVariant& value );
354 
355  QgsSymbolV2::OutputUnit outputUnit() const { return mOutputUnit; }
356  void setOutputUnit( QgsSymbolV2::OutputUnit u ) { mOutputUnit = u; }
357 
358  QgsMapUnitScale mapUnitScale() const { return mMapUnitScale; }
359  void setMapUnitScale( const QgsMapUnitScale& scale ) { mMapUnitScale = scale; }
360 
362  qreal alpha() const { return mAlpha; }
364  void setAlpha( qreal alpha ) { mAlpha = alpha; }
365 
366  bool selected() const { return mSelected; }
367  void setSelected( bool selected ) { mSelected = selected; }
368 
369  int renderHints() const { return mRenderHints; }
370  void setRenderHints( int hints ) { mRenderHints = hints; }
371 
372  void setFeature( const QgsFeature* f ) { mFeature = f; }
374  const QgsFeature* feature() const { return mFeature; }
375 
380  const QgsFields* fields() const { return mFields; }
381 
385  int geometryPartCount() const { return mGeometryPartCount; }
389  void setGeometryPartCount( int count ) { mGeometryPartCount = count; }
390 
394  int geometryPartNum() const { return mGeometryPartNum; }
398  void setGeometryPartNum( int num ) { mGeometryPartNum = num; }
399 
400  double outputLineWidth( double width ) const;
401  double outputPixelSize( double size ) const;
402 
403  // workaround for sip 4.7. Don't use assignment - will fail with assertion error
405 
411  QgsExpressionContextScope* expressionContextScope();
419  void setExpressionContextScope( QgsExpressionContextScope* contextScope );
420 
421  private:
422  QgsRenderContext& mRenderContext;
423  QgsExpressionContextScope* mExpressionContextScope;
424  QgsSymbolV2::OutputUnit mOutputUnit;
425  QgsMapUnitScale mMapUnitScale;
426  qreal mAlpha;
427  bool mSelected;
428  int mRenderHints;
429  const QgsFeature* mFeature; //current feature
430  const QgsFields* mFields;
431  int mGeometryPartCount;
432  int mGeometryPartNum;
433 
434 
436 };
437 
438 
439 
441 
442 
443 
444 class CORE_EXPORT QgsMarkerSymbolV2 : public QgsSymbolV2
445 {
446  public:
447 
451  static QgsMarkerSymbolV2* createSimple( const QgsStringMap& properties );
452 
454 
460  void setAngle( double angle );
461 
468  double angle() const;
469 
475  void setDataDefinedAngle( const QgsDataDefined& dd );
476 
483  QgsDataDefined dataDefinedAngle() const;
484 
491  void setLineAngle( double lineAngle );
492 
500  void setSize( double size );
501 
508  double size() const;
509 
517  void setSizeUnit( OutputUnit unit );
518 
526  OutputUnit sizeUnit() const;
527 
535  void setSizeMapUnitScale( const QgsMapUnitScale& scale );
536 
545  QgsMapUnitScale sizeMapUnitScale() const;
546 
552  void setDataDefinedSize( const QgsDataDefined& dd );
553 
560  QgsDataDefined dataDefinedSize() const;
561 
562  void setScaleMethod( QgsSymbolV2::ScaleMethod scaleMethod );
563  ScaleMethod scaleMethod();
564 
565  void renderPoint( QPointF point, const QgsFeature* f, QgsRenderContext& context, int layer = -1, bool selected = false );
566 
577  QRectF bounds( QPointF point, QgsRenderContext& context, const QgsFeature &feature = QgsFeature() ) const;
578 
579  virtual QgsMarkerSymbolV2* clone() const override;
580 
581  private:
582 
583  void renderPointUsingLayer( QgsMarkerSymbolLayerV2* layer, QPointF point, QgsSymbolV2RenderContext& context );
584 
585 };
586 
587 
588 
589 class CORE_EXPORT QgsLineSymbolV2 : public QgsSymbolV2
590 {
591  public:
595  static QgsLineSymbolV2* createSimple( const QgsStringMap& properties );
596 
598 
599  void setWidth( double width );
600  double width() const;
601 
607  void setDataDefinedWidth( const QgsDataDefined& dd );
608 
615  QgsDataDefined dataDefinedWidth() const;
616 
617  void renderPolyline( const QPolygonF& points, const QgsFeature* f, QgsRenderContext& context, int layer = -1, bool selected = false );
618 
619  virtual QgsLineSymbolV2* clone() const override;
620 
621  private:
622 
623  void renderPolylineUsingLayer( QgsLineSymbolLayerV2* layer, const QPolygonF& points, QgsSymbolV2RenderContext& context );
624 
625 };
626 
627 
628 
629 class CORE_EXPORT QgsFillSymbolV2 : public QgsSymbolV2
630 {
631  public:
635  static QgsFillSymbolV2* createSimple( const QgsStringMap& properties );
636 
638  void setAngle( double angle );
639  void renderPolygon( const QPolygonF& points, QList<QPolygonF>* rings, const QgsFeature* f, QgsRenderContext& context, int layer = -1, bool selected = false );
640 
641  virtual QgsFillSymbolV2* clone() const override;
642 
643  private:
644 
645  void renderPolygonUsingLayer( QgsSymbolLayerV2* layer, const QPolygonF &points, QList<QPolygonF> *rings, QgsSymbolV2RenderContext &context );
647  QRectF polygonBounds( const QPolygonF &points, const QList<QPolygonF> *rings ) const;
649  QList<QPolygonF>* translateRings( const QList<QPolygonF> *rings, double dx, double dy ) const;
650 };
651 
652 #endif
653 
654 
655 /*
656 
657 QgsSymbolV2* ps = new QgsPointSymbol();
658 
659 // ----
660 
661 sl = QgsSymbolLayerV2Registry::instance()->createSymbolLayer("SimpleLine", { "color", "..." })
662 
663 // (or)
664 
665 sl = QgsSymbolLayerV2Registry::defaultSymbolLayer(QgsSymbolV2::Line)
666 
667 // (or)
668 
669 QgsSymbolLayerV2* sl = new QgsSimpleLineSymbolLayer(x,y,z);
670 QgsLineSymbol* s = new LineSymbol( [ sl ] );
671 
672 // ----
673 
674 rend = QgsSingleSymbolRenderer( new LineSymbol() );
675 */
676 
677 
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:369
bool mClipFeaturesToExtent
Definition: qgssymbolv2.h:325
A container class for data source field mapping or expression.
GeometryType
Definition: qgis.h:111
void setGeometryPartCount(int count)
Sets the part count of current geometry.
Definition: qgssymbolv2.h:389
const QgsVectorLayer * mLayer
Definition: qgssymbolv2.h:327
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:364
SymbolType type() const
Definition: qgssymbolv2.h:104
virtual QString dump() const
for debugging
static QgsConstWkbPtr _getLineString(QPolygonF &pts, QgsRenderContext &context, QgsConstWkbPtr wkb, bool clipToExtent=true)
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:187
Line symbol.
Definition: qgssymbolv2.h:79
void setRenderHints(int hints)
Definition: qgssymbolv2.h:370
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:318
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:359
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:362
QString type() const
Definition: qgsrendererv2.h:83
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:319
QgsMapUnitScale mapUnitScale() const
Definition: qgssymbolv2.h:358
const QgsFeature * feature() const
Current feature being rendered - may be null.
Definition: qgssymbolv2.h:374
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:394
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:322
void setFeature(const QgsFeature *f)
Definition: qgssymbolv2.h:372
Single scope for storing variables and functions for use within a QgsExpressionContext.
QPointF toQPointF() const
Returns the point as a QPointF.
Definition: qgspointv2.cpp:425
void setOutputUnit(QgsSymbolV2::OutputUnit u)
Definition: qgssymbolv2.h:356
QgsSymbolV2::OutputUnit outputUnit() const
Definition: qgssymbolv2.h:355
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:367
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:345
Fill symbol.
Definition: qgssymbolv2.h:80
int geometryPartCount() const
Part count of current geometry.
Definition: qgssymbolv2.h:385
qreal & rx()
qreal & ry()
const QgsMapToPixel & mapToPixel() const
const QgsFields * fields() const
Fields of the layer.
Definition: qgssymbolv2.h:380
void setRenderHints(int hints)
Definition: qgssymbolv2.h:204
bool selected() const
Definition: qgssymbolv2.h:366
const QgsRenderContext & renderContext() const
Definition: qgssymbolv2.h:346
void setGeometryPartNum(int num)
Sets the part number of current geometry.
Definition: qgssymbolv2.h:398
Calculate scale by the area.
Definition: qgssymbolv2.h:89
static QgsConstWkbPtr _getPolygon(QPolygonF &pts, QList< QPolygonF > &holes, QgsRenderContext &context, QgsConstWkbPtr wkb, bool clipToExtent=true)
static QgsConstWkbPtr _getPoint(QPointF &pt, QgsRenderContext &context, QgsConstWkbPtr wkb)
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:324
int renderHints() const
Definition: qgssymbolv2.h:205