QGIS API Documentation  2.14.0-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 
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 
382  double outputLineWidth( double width ) const;
383  double outputPixelSize( double size ) const;
384 
385  // workaround for sip 4.7. Don't use assignment - will fail with assertion error
387 
393  QgsExpressionContextScope* expressionContextScope();
401  void setExpressionContextScope( QgsExpressionContextScope* contextScope );
402 
403  private:
404  QgsRenderContext& mRenderContext;
405  QgsExpressionContextScope* mExpressionContextScope;
406  QgsSymbolV2::OutputUnit mOutputUnit;
407  QgsMapUnitScale mMapUnitScale;
408  qreal mAlpha;
409  bool mSelected;
410  int mRenderHints;
411  const QgsFeature* mFeature; //current feature
412  const QgsFields* mFields;
413 
414 
416 };
417 
418 
419 
421 
422 
423 
424 class CORE_EXPORT QgsMarkerSymbolV2 : public QgsSymbolV2
425 {
426  public:
430  static QgsMarkerSymbolV2* createSimple( const QgsStringMap& properties );
431 
433 
434  void setAngle( double angle );
435  double angle() const;
436 
442  void setDataDefinedAngle( const QgsDataDefined& dd );
443 
450  QgsDataDefined dataDefinedAngle() const;
451 
458  void setLineAngle( double lineAngle );
459 
460  void setSize( double size );
461  double size() const;
462 
468  void setDataDefinedSize( const QgsDataDefined& dd );
469 
476  QgsDataDefined dataDefinedSize() const;
477 
478  void setScaleMethod( QgsSymbolV2::ScaleMethod scaleMethod );
479  ScaleMethod scaleMethod();
480 
481  void renderPoint( QPointF point, const QgsFeature* f, QgsRenderContext& context, int layer = -1, bool selected = false );
482 
487  QRectF bounds( QPointF point, QgsRenderContext& context ) const;
488 
489  virtual QgsMarkerSymbolV2* clone() const override;
490 
491  private:
492 
493  void renderPointUsingLayer( QgsMarkerSymbolLayerV2* layer, QPointF point, QgsSymbolV2RenderContext& context );
494 
495 };
496 
497 
498 
499 class CORE_EXPORT QgsLineSymbolV2 : public QgsSymbolV2
500 {
501  public:
505  static QgsLineSymbolV2* createSimple( const QgsStringMap& properties );
506 
508 
509  void setWidth( double width );
510  double width() const;
511 
517  void setDataDefinedWidth( const QgsDataDefined& dd );
518 
525  QgsDataDefined dataDefinedWidth() const;
526 
527  void renderPolyline( const QPolygonF& points, const QgsFeature* f, QgsRenderContext& context, int layer = -1, bool selected = false );
528 
529  virtual QgsLineSymbolV2* clone() const override;
530 
531  private:
532 
533  void renderPolylineUsingLayer( QgsLineSymbolLayerV2* layer, const QPolygonF& points, QgsSymbolV2RenderContext& context );
534 
535 };
536 
537 
538 
539 class CORE_EXPORT QgsFillSymbolV2 : public QgsSymbolV2
540 {
541  public:
545  static QgsFillSymbolV2* createSimple( const QgsStringMap& properties );
546 
548  void setAngle( double angle );
549  void renderPolygon( const QPolygonF& points, QList<QPolygonF>* rings, const QgsFeature* f, QgsRenderContext& context, int layer = -1, bool selected = false );
550 
551  virtual QgsFillSymbolV2* clone() const override;
552 
553  private:
554 
555  void renderPolygonUsingLayer( QgsSymbolLayerV2* layer, const QPolygonF &points, QList<QPolygonF> *rings, QgsSymbolV2RenderContext &context );
557  QRectF polygonBounds( const QPolygonF &points, const QList<QPolygonF> *rings ) const;
559  QList<QPolygonF>* translateRings( const QList<QPolygonF> *rings, double dx, double dy ) const;
560 };
561 
562 #endif
563 
564 
565 /*
566 
567 QgsSymbolV2* ps = new QgsPointSymbol();
568 
569 // ----
570 
571 sl = QgsSymbolLayerV2Registry::instance()->createSymbolLayer("SimpleLine", { "color", "..." })
572 
573 // (or)
574 
575 sl = QgsSymbolLayerV2Registry::defaultSymbolLayer(QgsSymbolV2::Line)
576 
577 // (or)
578 
579 QgsSymbolLayerV2* sl = new QgsSimpleLineSymbolLayer(x,y,z);
580 QgsLineSymbol* s = new LineSymbol( [ sl ] );
581 
582 // ----
583 
584 rend = QgsSingleSymbolRenderer( new LineSymbol() );
585 */
586 
587 
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
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...
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
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
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