QGIS API Documentation  2.18.21-Las Palmas (9fba24a)
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:
340  bool mStarted;
342  QgsSymbolV2RenderContext* mSymbolRenderContext;
343 
344  Q_DISABLE_COPY( QgsSymbolV2 )
345 
346 };
347 
349 
353 class CORE_EXPORT QgsSymbolV2RenderContext
354 {
355  public:
356  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() );
358 
359  QgsRenderContext& renderContext() { return mRenderContext; }
360  const QgsRenderContext& renderContext() const { return mRenderContext; }
361 
367  void setOriginalValueVariable( const QVariant& value );
368 
369  QgsSymbolV2::OutputUnit outputUnit() const { return mOutputUnit; }
370  void setOutputUnit( QgsSymbolV2::OutputUnit u ) { mOutputUnit = u; }
371 
372  QgsMapUnitScale mapUnitScale() const { return mMapUnitScale; }
373  void setMapUnitScale( const QgsMapUnitScale& scale ) { mMapUnitScale = scale; }
374 
376  qreal alpha() const { return mAlpha; }
378  void setAlpha( qreal alpha ) { mAlpha = alpha; }
379 
380  bool selected() const { return mSelected; }
381  void setSelected( bool selected ) { mSelected = selected; }
382 
383  int renderHints() const { return mRenderHints; }
384  void setRenderHints( int hints ) { mRenderHints = hints; }
385 
386  void setFeature( const QgsFeature* f ) { mFeature = f; }
388  const QgsFeature* feature() const { return mFeature; }
389 
394  const QgsFields* fields() const { return mFields; }
395 
399  int geometryPartCount() const { return mGeometryPartCount; }
403  void setGeometryPartCount( int count ) { mGeometryPartCount = count; }
404 
408  int geometryPartNum() const { return mGeometryPartNum; }
412  void setGeometryPartNum( int num ) { mGeometryPartNum = num; }
413 
414  double outputLineWidth( double width ) const;
415  double outputPixelSize( double size ) const;
416 
417  // workaround for sip 4.7. Don't use assignment - will fail with assertion error
419 
425  QgsExpressionContextScope* expressionContextScope();
433  void setExpressionContextScope( QgsExpressionContextScope* contextScope );
434 
435  private:
436  QgsRenderContext& mRenderContext;
437  QgsExpressionContextScope* mExpressionContextScope;
438  QgsSymbolV2::OutputUnit mOutputUnit;
439  QgsMapUnitScale mMapUnitScale;
440  qreal mAlpha;
441  bool mSelected;
442  int mRenderHints;
443  const QgsFeature* mFeature; //current feature
444  const QgsFields* mFields;
445  int mGeometryPartCount;
446  int mGeometryPartNum;
447 
448 
450 };
451 
452 
453 
455 
456 
460 class CORE_EXPORT QgsMarkerSymbolV2 : public QgsSymbolV2
461 {
462  public:
463 
467  static QgsMarkerSymbolV2* createSimple( const QgsStringMap& properties );
468 
470 
476  void setAngle( double angle );
477 
484  double angle() const;
485 
491  void setDataDefinedAngle( const QgsDataDefined& dd );
492 
499  QgsDataDefined dataDefinedAngle() const;
500 
507  void setLineAngle( double lineAngle );
508 
516  void setSize( double size );
517 
524  double size() const;
525 
533  void setSizeUnit( OutputUnit unit );
534 
542  OutputUnit sizeUnit() const;
543 
551  void setSizeMapUnitScale( const QgsMapUnitScale& scale );
552 
561  QgsMapUnitScale sizeMapUnitScale() const;
562 
568  void setDataDefinedSize( const QgsDataDefined& dd );
569 
576  QgsDataDefined dataDefinedSize() const;
577 
578  void setScaleMethod( QgsSymbolV2::ScaleMethod scaleMethod );
579  ScaleMethod scaleMethod();
580 
581  void renderPoint( QPointF point, const QgsFeature* f, QgsRenderContext& context, int layer = -1, bool selected = false );
582 
593  QRectF bounds( QPointF point, QgsRenderContext& context, const QgsFeature &feature = QgsFeature() ) const;
594 
595  virtual QgsMarkerSymbolV2* clone() const override;
596 
597  private:
598 
599  void renderPointUsingLayer( QgsMarkerSymbolLayerV2* layer, QPointF point, QgsSymbolV2RenderContext& context );
600 
601 };
602 
603 
607 class CORE_EXPORT QgsLineSymbolV2 : public QgsSymbolV2
608 {
609  public:
613  static QgsLineSymbolV2* createSimple( const QgsStringMap& properties );
614 
616 
617  void setWidth( double width );
618  double width() const;
619 
625  void setDataDefinedWidth( const QgsDataDefined& dd );
626 
633  QgsDataDefined dataDefinedWidth() const;
634 
635  void renderPolyline( const QPolygonF& points, const QgsFeature* f, QgsRenderContext& context, int layer = -1, bool selected = false );
636 
637  virtual QgsLineSymbolV2* clone() const override;
638 
639  private:
640 
641  void renderPolylineUsingLayer( QgsLineSymbolLayerV2* layer, const QPolygonF& points, QgsSymbolV2RenderContext& context );
642 
643 };
644 
645 
649 class CORE_EXPORT QgsFillSymbolV2 : public QgsSymbolV2
650 {
651  public:
655  static QgsFillSymbolV2* createSimple( const QgsStringMap& properties );
656 
658  void setAngle( double angle );
659  void renderPolygon( const QPolygonF& points, QList<QPolygonF>* rings, const QgsFeature* f, QgsRenderContext& context, int layer = -1, bool selected = false );
660 
661  virtual QgsFillSymbolV2* clone() const override;
662 
663  private:
664 
665  void renderPolygonUsingLayer( QgsSymbolLayerV2* layer, const QPolygonF &points, QList<QPolygonF> *rings, QgsSymbolV2RenderContext &context );
667  QRectF polygonBounds( const QPolygonF &points, const QList<QPolygonF> *rings ) const;
669  QList<QPolygonF>* translateRings( const QList<QPolygonF> *rings, double dx, double dy ) const;
670 };
671 
672 #endif
673 
674 
675 /*
676 
677 QgsSymbolV2* ps = new QgsPointSymbol();
678 
679 // ----
680 
681 sl = QgsSymbolLayerV2Registry::instance()->createSymbolLayer("SimpleLine", { "color", "..." })
682 
683 // (or)
684 
685 sl = QgsSymbolLayerV2Registry::defaultSymbolLayer(QgsSymbolV2::Line)
686 
687 // (or)
688 
689 QgsSymbolLayerV2* sl = new QgsSimpleLineSymbolLayer(x,y,z);
690 QgsLineSymbol* s = new LineSymbol( [ sl ] );
691 
692 // ----
693 
694 rend = QgsSingleSymbolRenderer( new LineSymbol() );
695 */
696 
697 
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:394
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:403
const QgsVectorLayer * mLayer
Definition: qgssymbolv2.h:335
void setAlpha(qreal alpha)
Set alpha transparency 1 for opaque, 0 for invisible.
Definition: qgssymbolv2.h:378
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:384
const QgsFeature * feature() const
Current feature being rendered - may be null.
Definition: qgssymbolv2.h:388
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:373
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:376
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:447
void setFeature(const QgsFeature *f)
Definition: qgssymbolv2.h:386
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:369
void setOutputUnit(QgsSymbolV2::OutputUnit u)
Definition: qgssymbolv2.h:370
int geometryPartCount() const
Part count of current geometry.
Definition: qgssymbolv2.h:399
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:381
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:372
QgsRenderContext & renderContext()
Definition: qgssymbolv2.h:359
int geometryPartNum() const
Part number of current geometry.
Definition: qgssymbolv2.h:408
Fill symbol.
Definition: qgssymbolv2.h:83
qreal & rx()
qreal & ry()
const QgsRenderContext & renderContext() const
Definition: qgssymbolv2.h:360
void setRenderHints(int hints)
Definition: qgssymbolv2.h:208
void setGeometryPartNum(int num)
Sets the part number of current geometry.
Definition: qgssymbolv2.h:412
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