QGIS API Documentation  2.17.0-Master (00653d2)
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 
195  void toSld( QDomDocument &doc, QDomElement &element, const QgsStringMap& props ) const;
196 
197  QgsSymbolV2::OutputUnit outputUnit() const;
198  void setOutputUnit( QgsSymbolV2::OutputUnit u );
199 
200  QgsMapUnitScale mapUnitScale() const;
201  void setMapUnitScale( const QgsMapUnitScale& scale );
202 
204  qreal alpha() const { return mAlpha; }
206  void setAlpha( qreal alpha ) { mAlpha = alpha; }
207 
208  void setRenderHints( int hints ) { mRenderHints = hints; }
209  int renderHints() const { return mRenderHints; }
210 
219  void setClipFeaturesToExtent( bool clipFeaturesToExtent ) { mClipFeaturesToExtent = clipFeaturesToExtent; }
220 
229  bool clipFeaturesToExtent() const { return mClipFeaturesToExtent; }
230 
237 
241  bool hasDataDefinedProperties() const;
242 
244  void setLayer( const QgsVectorLayer* layer ) { mLayer = layer; }
245  const QgsVectorLayer* layer() const { return mLayer; }
246 
250  void renderFeature( const QgsFeature& feature, QgsRenderContext& context, int layer = -1, bool selected = false, bool drawVertexMarker = false, int currentVertexMarkerType = 0, int currentVertexMarkerSize = 0 );
251 
257  QgsSymbolV2RenderContext* symbolRenderContext();
258 
259  protected:
260  QgsSymbolV2( SymbolType type, const QgsSymbolLayerV2List& layers ); // can't be instantiated
261 
265  static inline void _getPoint( QPointF& pt, QgsRenderContext& context, const QgsPointV2* point )
266  {
267  if ( context.coordinateTransform() )
268  {
269  double x = point->x();
270  double y = point->y();
271  double z = 0.0;
272  context.coordinateTransform()->transformInPlace( x, y, z );
273  pt = QPointF( x, y );
274 
275  }
276  else
277  pt = point->toQPointF();
278 
279  context.mapToPixel().transformInPlace( pt.rx(), pt.ry() );
280  }
281 
286  static QgsConstWkbPtr _getPoint( QPointF& pt, QgsRenderContext& context, QgsConstWkbPtr& wkb );
287 
292  static QgsConstWkbPtr _getLineString( QPolygonF& pts, QgsRenderContext& context, QgsConstWkbPtr& wkb, bool clipToExtent = true );
293 
298  static QgsConstWkbPtr _getPolygon( QPolygonF& pts, QList<QPolygonF>& holes, QgsRenderContext& context, QgsConstWkbPtr& wkb, bool clipToExtent = true );
299 
304  QgsSymbolLayerV2List cloneLayers() const;
305 
315  void renderUsingLayer( QgsSymbolLayerV2* layer, QgsSymbolV2RenderContext& context );
316 
320  Q_DECL_DEPRECATED bool isSymbolLayerCompatible( SymbolType layerType );
321 
324  void renderVertexMarker( QPointF pt, QgsRenderContext& context, int currentVertexMarkerType, int currentVertexMarkerSize );
325 
328 
330  qreal mAlpha;
331 
334 
335  const QgsVectorLayer* mLayer; //current vectorlayer
336 
337  private:
339  QgsSymbolV2RenderContext* mSymbolRenderContext;
340 
341  Q_DISABLE_COPY( QgsSymbolV2 )
342 
343 };
344 
346 
350 class CORE_EXPORT QgsSymbolV2RenderContext
351 {
352  public:
353  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() );
355 
356  QgsRenderContext& renderContext() { return mRenderContext; }
357  const QgsRenderContext& renderContext() const { return mRenderContext; }
358 
364  void setOriginalValueVariable( const QVariant& value );
365 
366  QgsSymbolV2::OutputUnit outputUnit() const { return mOutputUnit; }
367  void setOutputUnit( QgsSymbolV2::OutputUnit u ) { mOutputUnit = u; }
368 
369  QgsMapUnitScale mapUnitScale() const { return mMapUnitScale; }
370  void setMapUnitScale( const QgsMapUnitScale& scale ) { mMapUnitScale = scale; }
371 
373  qreal alpha() const { return mAlpha; }
375  void setAlpha( qreal alpha ) { mAlpha = alpha; }
376 
377  bool selected() const { return mSelected; }
378  void setSelected( bool selected ) { mSelected = selected; }
379 
380  int renderHints() const { return mRenderHints; }
381  void setRenderHints( int hints ) { mRenderHints = hints; }
382 
383  void setFeature( const QgsFeature* f ) { mFeature = f; }
385  const QgsFeature* feature() const { return mFeature; }
386 
391  const QgsFields* fields() const { return mFields; }
392 
396  int geometryPartCount() const { return mGeometryPartCount; }
400  void setGeometryPartCount( int count ) { mGeometryPartCount = count; }
401 
405  int geometryPartNum() const { return mGeometryPartNum; }
409  void setGeometryPartNum( int num ) { mGeometryPartNum = num; }
410 
411  double outputLineWidth( double width ) const;
412  double outputPixelSize( double size ) const;
413 
414  // workaround for sip 4.7. Don't use assignment - will fail with assertion error
416 
422  QgsExpressionContextScope* expressionContextScope();
430  void setExpressionContextScope( QgsExpressionContextScope* contextScope );
431 
432  private:
433  QgsRenderContext& mRenderContext;
434  QgsExpressionContextScope* mExpressionContextScope;
435  QgsSymbolV2::OutputUnit mOutputUnit;
436  QgsMapUnitScale mMapUnitScale;
437  qreal mAlpha;
438  bool mSelected;
439  int mRenderHints;
440  const QgsFeature* mFeature; //current feature
441  const QgsFields* mFields;
442  int mGeometryPartCount;
443  int mGeometryPartNum;
444 
445 
447 };
448 
449 
450 
452 
453 
457 class CORE_EXPORT QgsMarkerSymbolV2 : public QgsSymbolV2
458 {
459  public:
460 
464  static QgsMarkerSymbolV2* createSimple( const QgsStringMap& properties );
465 
467 
473  void setAngle( double angle );
474 
481  double angle() const;
482 
488  void setDataDefinedAngle( const QgsDataDefined& dd );
489 
496  QgsDataDefined dataDefinedAngle() const;
497 
504  void setLineAngle( double lineAngle );
505 
513  void setSize( double size );
514 
521  double size() const;
522 
530  void setSizeUnit( OutputUnit unit );
531 
539  OutputUnit sizeUnit() const;
540 
548  void setSizeMapUnitScale( const QgsMapUnitScale& scale );
549 
558  QgsMapUnitScale sizeMapUnitScale() const;
559 
565  void setDataDefinedSize( const QgsDataDefined& dd );
566 
573  QgsDataDefined dataDefinedSize() const;
574 
575  void setScaleMethod( QgsSymbolV2::ScaleMethod scaleMethod );
576  ScaleMethod scaleMethod();
577 
578  void renderPoint( QPointF point, const QgsFeature* f, QgsRenderContext& context, int layer = -1, bool selected = false );
579 
590  QRectF bounds( QPointF point, QgsRenderContext& context, const QgsFeature &feature = QgsFeature() ) const;
591 
592  virtual QgsMarkerSymbolV2* clone() const override;
593 
594  private:
595 
596  void renderPointUsingLayer( QgsMarkerSymbolLayerV2* layer, QPointF point, QgsSymbolV2RenderContext& context );
597 
598 };
599 
600 
604 class CORE_EXPORT QgsLineSymbolV2 : public QgsSymbolV2
605 {
606  public:
610  static QgsLineSymbolV2* createSimple( const QgsStringMap& properties );
611 
613 
614  void setWidth( double width );
615  double width() const;
616 
622  void setDataDefinedWidth( const QgsDataDefined& dd );
623 
630  QgsDataDefined dataDefinedWidth() const;
631 
632  void renderPolyline( const QPolygonF& points, const QgsFeature* f, QgsRenderContext& context, int layer = -1, bool selected = false );
633 
634  virtual QgsLineSymbolV2* clone() const override;
635 
636  private:
637 
638  void renderPolylineUsingLayer( QgsLineSymbolLayerV2* layer, const QPolygonF& points, QgsSymbolV2RenderContext& context );
639 
640 };
641 
642 
646 class CORE_EXPORT QgsFillSymbolV2 : public QgsSymbolV2
647 {
648  public:
652  static QgsFillSymbolV2* createSimple( const QgsStringMap& properties );
653 
655  void setAngle( double angle );
656  void renderPolygon( const QPolygonF& points, QList<QPolygonF>* rings, const QgsFeature* f, QgsRenderContext& context, int layer = -1, bool selected = false );
657 
658  virtual QgsFillSymbolV2* clone() const override;
659 
660  private:
661 
662  void renderPolygonUsingLayer( QgsSymbolLayerV2* layer, const QPolygonF &points, QList<QPolygonF> *rings, QgsSymbolV2RenderContext &context );
664  QRectF polygonBounds( const QPolygonF &points, const QList<QPolygonF> *rings ) const;
666  QList<QPolygonF>* translateRings( const QList<QPolygonF> *rings, double dx, double dy ) const;
667 };
668 
669 #endif
670 
671 
672 /*
673 
674 QgsSymbolV2* ps = new QgsPointSymbol();
675 
676 // ----
677 
678 sl = QgsSymbolLayerV2Registry::instance()->createSymbolLayer("SimpleLine", { "color", "..." })
679 
680 // (or)
681 
682 sl = QgsSymbolLayerV2Registry::defaultSymbolLayer(QgsSymbolV2::Line)
683 
684 // (or)
685 
686 QgsSymbolLayerV2* sl = new QgsSimpleLineSymbolLayer(x,y,z);
687 QgsLineSymbol* s = new LineSymbol( [ sl ] );
688 
689 // ----
690 
691 rend = QgsSingleSymbolRenderer( new LineSymbol() );
692 */
693 
694 
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:219
const QgsFields * fields() const
Fields of the layer.
Definition: qgssymbolv2.h:391
OutputUnit
The unit of the output.
Definition: qgssymbolv2.h:65
bool mClipFeaturesToExtent
Definition: qgssymbolv2.h:333
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:400
const QgsVectorLayer * mLayer
Definition: qgssymbolv2.h:335
void setAlpha(qreal alpha)
Set alpha transparency 1 for opaque, 0 for invisible.
Definition: qgssymbolv2.h:375
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:265
Container of fields for a vector layer.
Definition: qgsfield.h:252
SymbolType type() const
Definition: qgssymbolv2.h:107
Line symbol.
Definition: qgssymbolv2.h:82
void setRenderHints(int hints)
Definition: qgssymbolv2.h:381
const QgsFeature * feature() const
Current feature being rendered - may be null.
Definition: qgssymbolv2.h:385
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
double y() const
Returns the point&#39;s y-coordinate.
Definition: qgspointv2.h:74
SymbolType mType
Definition: qgssymbolv2.h:326
virtual void toSld(QDomDocument &doc, QDomElement &element) const
used from subclasses to create SLD Rule elements following SLD v1.1 specs
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
int renderHints() const
Definition: qgssymbolv2.h:209
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:370
virtual void stopRender(QgsRenderContext &context)=0
Needs to be called when a render cycle has finished to clean up.
void renderVertexMarker(QPointF pt, QgsRenderContext &context)
render editing vertex marker at specified point
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:69
virtual QgsFeatureRendererV2 * clone() const =0
QgsSymbolLayerV2List mLayers
Definition: qgssymbolv2.h:327
virtual QString dump() const
for debugging
const QgsVectorLayer * layer() const
Definition: qgssymbolv2.h:245
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:373
void setLayer(const QgsVectorLayer *layer)
Definition: qgssymbolv2.h:244
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:92
The output shall be in map unitx.
Definition: qgssymbolv2.h:68
const QgsCoordinateTransform * coordinateTransform() const
int symbolLayerCount()
Returns total number of symbol layers contained in the symbol.
Definition: qgssymbolv2.h:134
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:330
QPointF toQPointF() const
Returns the point as a QPointF.
Definition: qgspointv2.cpp:438
void setFeature(const QgsFeature *f)
Definition: qgssymbolv2.h:383
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:204
QgsSymbolV2::OutputUnit outputUnit() const
Definition: qgssymbolv2.h:366
void setOutputUnit(QgsSymbolV2::OutputUnit u)
Definition: qgssymbolv2.h:367
int geometryPartCount() const
Part count of current geometry.
Definition: qgssymbolv2.h:396
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:378
bool clipFeaturesToExtent() const
Returns whether features drawn by the symbol will be clipped to the render context&#39;s extent...
Definition: qgssymbolv2.h:229
SymbolType
Type of the symbol.
Definition: qgssymbolv2.h:79
Contains information about the context of a rendering operation.
const QgsMapToPixel & mapToPixel() const
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
QgsMapUnitScale mapUnitScale() const
Definition: qgssymbolv2.h:369
QgsRenderContext & renderContext()
Definition: qgssymbolv2.h:356
int geometryPartNum() const
Part number of current geometry.
Definition: qgssymbolv2.h:405
Fill symbol.
Definition: qgssymbolv2.h:83
qreal & rx()
qreal & ry()
const QgsRenderContext & renderContext() const
Definition: qgssymbolv2.h:357
void setRenderHints(int hints)
Definition: qgssymbolv2.h:208
void setGeometryPartNum(int num)
Sets the part number of current geometry.
Definition: qgssymbolv2.h:409
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
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:206
int mRenderHints
Definition: qgssymbolv2.h:332