QGIS API Documentation  2.14.11-Essen
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 
192  void toSld( QDomDocument &doc, QDomElement &element, const QgsStringMap& props ) const;
193 
194  QgsSymbolV2::OutputUnit outputUnit() const;
195  void setOutputUnit( QgsSymbolV2::OutputUnit u );
196 
197  QgsMapUnitScale mapUnitScale() const;
198  void setMapUnitScale( const QgsMapUnitScale& scale );
199 
201  qreal alpha() const { return mAlpha; }
203  void setAlpha( qreal alpha ) { mAlpha = alpha; }
204 
205  void setRenderHints( int hints ) { mRenderHints = hints; }
206  int renderHints() const { return mRenderHints; }
207 
216  void setClipFeaturesToExtent( bool clipFeaturesToExtent ) { mClipFeaturesToExtent = clipFeaturesToExtent; }
217 
226  bool clipFeaturesToExtent() const { return mClipFeaturesToExtent; }
227 
234 
238  bool hasDataDefinedProperties() const;
239 
241  void setLayer( const QgsVectorLayer* layer ) { mLayer = layer; }
242  const QgsVectorLayer* layer() const { return mLayer; }
243 
247  void renderFeature( const QgsFeature& feature, QgsRenderContext& context, int layer = -1, bool selected = false, bool drawVertexMarker = false, int currentVertexMarkerType = 0, int currentVertexMarkerSize = 0 );
248 
254  QgsSymbolV2RenderContext* symbolRenderContext();
255 
256  protected:
257  QgsSymbolV2( SymbolType type, const QgsSymbolLayerV2List& layers ); // can't be instantiated
258 
262  static inline void _getPoint( QPointF& pt, QgsRenderContext& context, const QgsPointV2* point )
263  {
264  if ( context.coordinateTransform() )
265  {
266  double x = point->x();
267  double y = point->y();
268  double z = 0.0;
269  context.coordinateTransform()->transformInPlace( x, y, z );
270  pt = QPointF( x, y );
271 
272  }
273  else
274  pt = point->toQPointF();
275 
276  context.mapToPixel().transformInPlace( pt.rx(), pt.ry() );
277  }
278 
283  static QgsConstWkbPtr _getPoint( QPointF& pt, QgsRenderContext& context, QgsConstWkbPtr wkb );
284 
289  static QgsConstWkbPtr _getLineString( QPolygonF& pts, QgsRenderContext& context, QgsConstWkbPtr wkb, bool clipToExtent = true );
290 
295  static QgsConstWkbPtr _getPolygon( QPolygonF& pts, QList<QPolygonF>& holes, QgsRenderContext& context, QgsConstWkbPtr wkb, bool clipToExtent = true );
296 
301  QgsSymbolLayerV2List cloneLayers() const;
302 
312  void renderUsingLayer( QgsSymbolLayerV2* layer, QgsSymbolV2RenderContext& context );
313 
317  Q_DECL_DEPRECATED bool isSymbolLayerCompatible( SymbolType layerType );
318 
321 
323  qreal mAlpha;
324 
327 
328  const QgsVectorLayer* mLayer; //current vectorlayer
329 
330  private:
332  QgsSymbolV2RenderContext* mSymbolRenderContext;
333 
334  Q_DISABLE_COPY( QgsSymbolV2 )
335 
336 };
337 
339 
340 class CORE_EXPORT QgsSymbolV2RenderContext
341 {
342  public:
343  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() );
345 
346  QgsRenderContext& renderContext() { return mRenderContext; }
347  const QgsRenderContext& renderContext() const { return mRenderContext; }
348 
354  void setOriginalValueVariable( const QVariant& value );
355 
356  QgsSymbolV2::OutputUnit outputUnit() const { return mOutputUnit; }
357  void setOutputUnit( QgsSymbolV2::OutputUnit u ) { mOutputUnit = u; }
358 
359  QgsMapUnitScale mapUnitScale() const { return mMapUnitScale; }
360  void setMapUnitScale( const QgsMapUnitScale& scale ) { mMapUnitScale = scale; }
361 
363  qreal alpha() const { return mAlpha; }
365  void setAlpha( qreal alpha ) { mAlpha = alpha; }
366 
367  bool selected() const { return mSelected; }
368  void setSelected( bool selected ) { mSelected = selected; }
369 
370  int renderHints() const { return mRenderHints; }
371  void setRenderHints( int hints ) { mRenderHints = hints; }
372 
373  void setFeature( const QgsFeature* f ) { mFeature = f; }
375  const QgsFeature* feature() const { return mFeature; }
376 
381  const QgsFields* fields() const { return mFields; }
382 
383  double outputLineWidth( double width ) const;
384  double outputPixelSize( double size ) const;
385 
386  // workaround for sip 4.7. Don't use assignment - will fail with assertion error
388 
394  QgsExpressionContextScope* expressionContextScope();
402  void setExpressionContextScope( QgsExpressionContextScope* contextScope );
403 
404  private:
405  QgsRenderContext& mRenderContext;
406  QgsExpressionContextScope* mExpressionContextScope;
407  QgsSymbolV2::OutputUnit mOutputUnit;
408  QgsMapUnitScale mMapUnitScale;
409  qreal mAlpha;
410  bool mSelected;
411  int mRenderHints;
412  const QgsFeature* mFeature; //current feature
413  const QgsFields* mFields;
414 
415 
417 };
418 
419 
420 
422 
423 
424 
425 class CORE_EXPORT QgsMarkerSymbolV2 : public QgsSymbolV2
426 {
427  public:
428 
432  static QgsMarkerSymbolV2* createSimple( const QgsStringMap& properties );
433 
435 
441  void setAngle( double angle );
442 
449  double angle() const;
450 
456  void setDataDefinedAngle( const QgsDataDefined& dd );
457 
464  QgsDataDefined dataDefinedAngle() const;
465 
472  void setLineAngle( double lineAngle );
473 
481  void setSize( double size );
482 
489  double size() const;
490 
498  void setSizeUnit( OutputUnit unit );
499 
507  OutputUnit sizeUnit() const;
508 
516  void setSizeMapUnitScale( const QgsMapUnitScale& scale );
517 
526  QgsMapUnitScale sizeMapUnitScale() const;
527 
533  void setDataDefinedSize( const QgsDataDefined& dd );
534 
541  QgsDataDefined dataDefinedSize() const;
542 
543  void setScaleMethod( QgsSymbolV2::ScaleMethod scaleMethod );
544  ScaleMethod scaleMethod();
545 
546  void renderPoint( QPointF point, const QgsFeature* f, QgsRenderContext& context, int layer = -1, bool selected = false );
547 
558  QRectF bounds( QPointF point, QgsRenderContext& context, const QgsFeature &feature = QgsFeature() ) const;
559 
560  virtual QgsMarkerSymbolV2* clone() const override;
561 
562  private:
563 
564  void renderPointUsingLayer( QgsMarkerSymbolLayerV2* layer, QPointF point, QgsSymbolV2RenderContext& context );
565 
566 };
567 
568 
569 
570 class CORE_EXPORT QgsLineSymbolV2 : public QgsSymbolV2
571 {
572  public:
576  static QgsLineSymbolV2* createSimple( const QgsStringMap& properties );
577 
579 
580  void setWidth( double width );
581  double width() const;
582 
588  void setDataDefinedWidth( const QgsDataDefined& dd );
589 
596  QgsDataDefined dataDefinedWidth() const;
597 
598  void renderPolyline( const QPolygonF& points, const QgsFeature* f, QgsRenderContext& context, int layer = -1, bool selected = false );
599 
600  virtual QgsLineSymbolV2* clone() const override;
601 
602  private:
603 
604  void renderPolylineUsingLayer( QgsLineSymbolLayerV2* layer, const QPolygonF& points, QgsSymbolV2RenderContext& context );
605 
606 };
607 
608 
609 
610 class CORE_EXPORT QgsFillSymbolV2 : public QgsSymbolV2
611 {
612  public:
616  static QgsFillSymbolV2* createSimple( const QgsStringMap& properties );
617 
619  void setAngle( double angle );
620  void renderPolygon( const QPolygonF& points, QList<QPolygonF>* rings, const QgsFeature* f, QgsRenderContext& context, int layer = -1, bool selected = false );
621 
622  virtual QgsFillSymbolV2* clone() const override;
623 
624  private:
625 
626  void renderPolygonUsingLayer( QgsSymbolLayerV2* layer, const QPolygonF &points, QList<QPolygonF> *rings, QgsSymbolV2RenderContext &context );
628  QRectF polygonBounds( const QPolygonF &points, const QList<QPolygonF> *rings ) const;
630  QList<QPolygonF>* translateRings( const QList<QPolygonF> *rings, double dx, double dy ) const;
631 };
632 
633 #endif
634 
635 
636 /*
637 
638 QgsSymbolV2* ps = new QgsPointSymbol();
639 
640 // ----
641 
642 sl = QgsSymbolLayerV2Registry::instance()->createSymbolLayer("SimpleLine", { "color", "..." })
643 
644 // (or)
645 
646 sl = QgsSymbolLayerV2Registry::defaultSymbolLayer(QgsSymbolV2::Line)
647 
648 // (or)
649 
650 QgsSymbolLayerV2* sl = new QgsSimpleLineSymbolLayer(x,y,z);
651 QgsLineSymbol* s = new LineSymbol( [ sl ] );
652 
653 // ----
654 
655 rend = QgsSingleSymbolRenderer( new LineSymbol() );
656 */
657 
658 
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:216
const QgsFields * fields() const
Fields of the layer.
Definition: qgssymbolv2.h:381
OutputUnit
The unit of the output.
Definition: qgssymbolv2.h:62
bool mClipFeaturesToExtent
Definition: qgssymbolv2.h:326
A container class for data source field mapping or expression.
GeometryType
Definition: qgis.h:111
const QgsVectorLayer * mLayer
Definition: qgssymbolv2.h:328
void setAlpha(qreal alpha)
Set alpha transparency 1 for opaque, 0 for invisible.
Definition: qgssymbolv2.h:365
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:262
Container of fields for a vector layer.
Definition: qgsfield.h:187
SymbolType type() const
Definition: qgssymbolv2.h:104
Line symbol.
Definition: qgssymbolv2.h:79
void setRenderHints(int hints)
Definition: qgssymbolv2.h:371
const QgsFeature * feature() const
Current feature being rendered - may be null.
Definition: qgssymbolv2.h:375
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
double y() const
Returns the point&#39;s y-coordinate.
Definition: qgspointv2.h:74
SymbolType mType
Definition: qgssymbolv2.h:319
virtual void toSld(QDomDocument &doc, QDomElement &element) const
used from subclasses to create SLD Rule elements following SLD v1.1 specs
Marker symbol.
Definition: qgssymbolv2.h:78
int renderHints() const
Definition: qgssymbolv2.h:206
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:360
virtual void stopRender(QgsRenderContext &context)=0
Needs to be called when a render cycle has finished to clean up.
void transformInPlace(double &x, double &y) const
Transform device coordinates to map coordinates.
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
virtual QgsFeatureRendererV2 * clone() const =0
QgsSymbolLayerV2List mLayers
Definition: qgssymbolv2.h:320
virtual QString dump() const
for debugging
const QgsVectorLayer * layer() const
Definition: qgssymbolv2.h:242
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:363
void setLayer(const QgsVectorLayer *layer)
Definition: qgssymbolv2.h:241
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...
QString type() const
Definition: qgsrendererv2.h:83
The output shall be in map unitx.
Definition: qgssymbolv2.h:65
const QgsCoordinateTransform * coordinateTransform() const
int symbolLayerCount()
Returns total number of symbol layers contained in the symbol.
Definition: qgssymbolv2.h:131
double x() const
Returns the point&#39;s x-coordinate.
Definition: qgspointv2.h:68
qreal mAlpha
Symbol opacity (in the range 0 - 1)
Definition: qgssymbolv2.h:323
QPointF toQPointF() const
Returns the point as a QPointF.
Definition: qgspointv2.cpp:425
void setFeature(const QgsFeature *f)
Definition: qgssymbolv2.h:373
Single scope for storing variables and functions for use within a QgsExpressionContext.
qreal alpha() const
Get alpha transparency 1 for opaque, 0 for invisible.
Definition: qgssymbolv2.h:201
QgsSymbolV2::OutputUnit outputUnit() const
Definition: qgssymbolv2.h:356
void setOutputUnit(QgsSymbolV2::OutputUnit u)
Definition: qgssymbolv2.h:357
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:368
bool clipFeaturesToExtent() const
Returns whether features drawn by the symbol will be clipped to the render context&#39;s extent...
Definition: qgssymbolv2.h:226
SymbolType
Type of the symbol.
Definition: qgssymbolv2.h:76
Contains information about the context of a rendering operation.
const QgsMapToPixel & mapToPixel() const
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
QgsMapUnitScale mapUnitScale() const
Definition: qgssymbolv2.h:359
QgsRenderContext & renderContext()
Definition: qgssymbolv2.h:346
Fill symbol.
Definition: qgssymbolv2.h:80
qreal & rx()
qreal & ry()
const QgsRenderContext & renderContext() const
Definition: qgssymbolv2.h:347
void setRenderHints(int hints)
Definition: qgssymbolv2.h:205
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)
void transformInPlace(double &x, double &y, double &z, TransformDirection direction=ForwardTransform) const
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:203
int mRenderHints
Definition: qgssymbolv2.h:325