QGIS API Documentation  2.13.0-Master
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;
48 
50 
51 class CORE_EXPORT QgsSymbolV2
52 {
53  public:
54 
58  //TODO QGIS 3.0 - move to QgsUnitTypes and rename to SymbolUnit
60  {
61  MM = 0,
66  };
67 
69 
74  {
76  Line,
77  Fill,
78  Hybrid
79  };
80 
85  {
87  ScaleDiameter
88  };
89 
91  {
92  DataDefinedSizeScale = 1,
93  DataDefinedRotation = 2
94  };
95 
96  virtual ~QgsSymbolV2();
97 
99  static QgsSymbolV2* defaultSymbol( QGis::GeometryType geomType );
100 
101  SymbolType type() const { return mType; }
102 
103  // symbol layers handling
104 
111  QgsSymbolLayerV2List symbolLayers() { return mLayers; }
112 
120  QgsSymbolLayerV2* symbolLayer( int layer );
121 
128  int symbolLayerCount() { return mLayers.count(); }
129 
137  bool insertSymbolLayer( int index, QgsSymbolLayerV2* layer );
138 
145  bool appendSymbolLayer( QgsSymbolLayerV2* layer );
146 
148  bool deleteSymbolLayer( int index );
149 
156  QgsSymbolLayerV2* takeSymbolLayer( int index );
157 
159  bool changeSymbolLayer( int index, QgsSymbolLayerV2 *layer );
160 
161  void startRender( QgsRenderContext& context, const QgsFields* fields = nullptr );
162  void stopRender( QgsRenderContext& context );
163 
164  void setColor( const QColor& color );
165  QColor color() const;
166 
170  void drawPreviewIcon( QPainter* painter, QSize size, QgsRenderContext* customContext = nullptr );
171 
173  void exportImage( const QString& path, const QString& format, QSize size );
174 
176  QImage asImage( QSize size, QgsRenderContext* customContext = nullptr );
177 
182  QImage bigSymbolPreviewImage( QgsExpressionContext* expressionContext = nullptr );
183 
184  QString dump() const;
185 
186  virtual QgsSymbolV2* clone() const = 0;
187 
188  void toSld( QDomDocument &doc, QDomElement &element, QgsStringMap props ) const;
189 
190  QgsSymbolV2::OutputUnit outputUnit() const;
191  void setOutputUnit( QgsSymbolV2::OutputUnit u );
192 
193  QgsMapUnitScale mapUnitScale() const;
194  void setMapUnitScale( const QgsMapUnitScale& scale );
195 
197  qreal alpha() const { return mAlpha; }
199  void setAlpha( qreal alpha ) { mAlpha = alpha; }
200 
201  void setRenderHints( int hints ) { mRenderHints = hints; }
202  int renderHints() const { return mRenderHints; }
203 
212  void setClipFeaturesToExtent( bool clipFeaturesToExtent ) { mClipFeaturesToExtent = clipFeaturesToExtent; }
213 
222  bool clipFeaturesToExtent() const { return mClipFeaturesToExtent; }
223 
229  QSet<QString> usedAttributes() const;
230 
234  bool hasDataDefinedProperties() const;
235 
237  void setLayer( const QgsVectorLayer* layer ) { mLayer = layer; }
238  const QgsVectorLayer* layer() const { return mLayer; }
239 
243  void renderFeature( const QgsFeature& feature, QgsRenderContext& context, int layer = -1, bool selected = false, bool drawVertexMarker = false, int currentVertexMarkerType = 0, int currentVertexMarkerSize = 0 );
244 
250  QgsSymbolV2RenderContext* symbolRenderContext();
251 
252  protected:
253  QgsSymbolV2( SymbolType type, const QgsSymbolLayerV2List& layers ); // can't be instantiated
254 
258  static inline void _getPoint( QPointF& pt, QgsRenderContext& context, const QgsPointV2* point )
259  {
260  if ( context.coordinateTransform() )
261  {
262  double x = point->x();
263  double y = point->y();
264  double z = 0.0;
265  context.coordinateTransform()->transformInPlace( x, y, z );
266  pt = QPointF( x, y );
267 
268  }
269  else
270  pt = point->toQPointF();
271 
272  context.mapToPixel().transformInPlace( pt.rx(), pt.ry() );
273  }
274 
279  static const unsigned char* _getLineString( QPolygonF& pts, QgsRenderContext& context, const unsigned char* wkb, bool clipToExtent = true );
280 
285  static const unsigned char* _getPolygon( QPolygonF& pts, QList<QPolygonF>& holes, QgsRenderContext& context, const unsigned char* wkb, bool clipToExtent = true );
286 
291  QgsSymbolLayerV2List cloneLayers() const;
292 
302  void renderUsingLayer( QgsSymbolLayerV2* layer, QgsSymbolV2RenderContext& context );
303 
307  Q_DECL_DEPRECATED bool isSymbolLayerCompatible( SymbolType layerType );
308 
311 
313  qreal mAlpha;
314 
317 
318  const QgsVectorLayer* mLayer; //current vectorlayer
319 
320  private:
322  QgsSymbolV2RenderContext* mSymbolRenderContext;
323 
324  Q_DISABLE_COPY( QgsSymbolV2 )
325 
326 };
327 
329 
330 class CORE_EXPORT QgsSymbolV2RenderContext
331 {
332  public:
333  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() );
335 
336  QgsRenderContext& renderContext() { return mRenderContext; }
337  const QgsRenderContext& renderContext() const { return mRenderContext; }
338 
344  void setOriginalValueVariable( const QVariant& value );
345 
346  QgsSymbolV2::OutputUnit outputUnit() const { return mOutputUnit; }
347  void setOutputUnit( QgsSymbolV2::OutputUnit u ) { mOutputUnit = u; }
348 
349  QgsMapUnitScale mapUnitScale() const { return mMapUnitScale; }
350  void setMapUnitScale( const QgsMapUnitScale& scale ) { mMapUnitScale = scale; }
351 
353  qreal alpha() const { return mAlpha; }
355  void setAlpha( qreal alpha ) { mAlpha = alpha; }
356 
357  bool selected() const { return mSelected; }
358  void setSelected( bool selected ) { mSelected = selected; }
359 
360  int renderHints() const { return mRenderHints; }
361  void setRenderHints( int hints ) { mRenderHints = hints; }
362 
363  void setFeature( const QgsFeature* f ) { mFeature = f; }
365  const QgsFeature* feature() const { return mFeature; }
366 
371  const QgsFields* fields() const { return mFields; }
372 
373  double outputLineWidth( double width ) const;
374  double outputPixelSize( double size ) const;
375 
376  // workaround for sip 4.7. Don't use assignment - will fail with assertion error
378 
384  QgsExpressionContextScope* expressionContextScope();
392  void setExpressionContextScope( QgsExpressionContextScope* contextScope );
393 
394  private:
395  QgsRenderContext& mRenderContext;
396  QgsExpressionContextScope* mExpressionContextScope;
397  QgsSymbolV2::OutputUnit mOutputUnit;
398  QgsMapUnitScale mMapUnitScale;
399  qreal mAlpha;
400  bool mSelected;
401  int mRenderHints;
402  const QgsFeature* mFeature; //current feature
403  const QgsFields* mFields;
404 
405 
407 };
408 
409 
410 
412 
413 
414 
415 class CORE_EXPORT QgsMarkerSymbolV2 : public QgsSymbolV2
416 {
417  public:
421  static QgsMarkerSymbolV2* createSimple( const QgsStringMap& properties );
422 
424 
425  void setAngle( double angle );
426  double angle() const;
427 
433  void setDataDefinedAngle( const QgsDataDefined& dd );
434 
441  QgsDataDefined dataDefinedAngle() const;
442 
449  void setLineAngle( double lineAngle );
450 
451  void setSize( double size );
452  double size() const;
453 
459  void setDataDefinedSize( const QgsDataDefined& dd );
460 
467  QgsDataDefined dataDefinedSize() const;
468 
469  void setScaleMethod( QgsSymbolV2::ScaleMethod scaleMethod );
470  ScaleMethod scaleMethod();
471 
472  void renderPoint( QPointF point, const QgsFeature* f, QgsRenderContext& context, int layer = -1, bool selected = false );
473 
478  QRectF bounds( QPointF point, QgsRenderContext& context ) const;
479 
480  virtual QgsMarkerSymbolV2* clone() const override;
481 
482  private:
483 
484  void renderPointUsingLayer( QgsMarkerSymbolLayerV2* layer, QPointF point, QgsSymbolV2RenderContext& context );
485 
486 };
487 
488 
489 
490 class CORE_EXPORT QgsLineSymbolV2 : public QgsSymbolV2
491 {
492  public:
496  static QgsLineSymbolV2* createSimple( const QgsStringMap& properties );
497 
499 
500  void setWidth( double width );
501  double width() const;
502 
508  void setDataDefinedWidth( const QgsDataDefined& dd );
509 
516  QgsDataDefined dataDefinedWidth() const;
517 
518  void renderPolyline( const QPolygonF& points, const QgsFeature* f, QgsRenderContext& context, int layer = -1, bool selected = false );
519 
520  virtual QgsLineSymbolV2* clone() const override;
521 
522  private:
523 
524  void renderPolylineUsingLayer( QgsLineSymbolLayerV2* layer, const QPolygonF& points, QgsSymbolV2RenderContext& context );
525 
526 };
527 
528 
529 
530 class CORE_EXPORT QgsFillSymbolV2 : public QgsSymbolV2
531 {
532  public:
536  static QgsFillSymbolV2* createSimple( const QgsStringMap& properties );
537 
539  void setAngle( double angle );
540  void renderPolygon( const QPolygonF& points, QList<QPolygonF>* rings, const QgsFeature* f, QgsRenderContext& context, int layer = -1, bool selected = false );
541 
542  virtual QgsFillSymbolV2* clone() const override;
543 
544  private:
545 
546  void renderPolygonUsingLayer( QgsSymbolLayerV2* layer, const QPolygonF &points, QList<QPolygonF> *rings, QgsSymbolV2RenderContext &context );
548  QRectF polygonBounds( const QPolygonF &points, const QList<QPolygonF> *rings ) const;
550  QList<QPolygonF>* translateRings( const QList<QPolygonF> *rings, double dx, double dy ) const;
551 };
552 
553 #endif
554 
555 
556 /*
557 
558 QgsSymbolV2* ps = new QgsPointSymbol();
559 
560 // ----
561 
562 sl = QgsSymbolLayerV2Registry::instance()->createSymbolLayer("SimpleLine", { "color", "..." })
563 
564 // (or)
565 
566 sl = QgsSymbolLayerV2Registry::defaultSymbolLayer(QgsSymbolV2::Line)
567 
568 // (or)
569 
570 QgsSymbolLayerV2* sl = new QgsSimpleLineSymbolLayer(x,y,z);
571 QgsLineSymbol* s = new LineSymbol( [ sl ] );
572 
573 // ----
574 
575 rend = QgsSingleSymbolRenderer( new LineSymbol() );
576 */
577 
578 
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:212
OutputUnit
The unit of the output.
Definition: qgssymbolv2.h:59
int renderHints() const
Definition: qgssymbolv2.h:360
bool mClipFeaturesToExtent
Definition: qgssymbolv2.h:316
A container class for data source field mapping or expression.
GeometryType
Definition: qgis.h:111
const QgsVectorLayer * mLayer
Definition: qgssymbolv2.h:318
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:355
SymbolType type() const
Definition: qgssymbolv2.h:101
virtual QgsSymbolV2 * clone() const =0
QList< OutputUnit > OutputUnitList
Definition: qgssymbolv2.h:68
The output shall be in pixels.
Definition: qgssymbolv2.h:64
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:258
Container of fields for a vector layer.
Definition: qgsfield.h:189
Line symbol.
Definition: qgssymbolv2.h:76
void setRenderHints(int hints)
Definition: qgssymbolv2.h:361
QgsSymbolLayerV2List symbolLayers()
Returns list of symbol layers contained in the symbol.
Definition: qgssymbolv2.h:111
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:309
Marker symbol.
Definition: qgssymbolv2.h:75
void setMapUnitScale(const QgsMapUnitScale &scale)
Definition: qgssymbolv2.h:350
const QgsVectorLayer * layer() const
Definition: qgssymbolv2.h:238
double y() const
Returns the point&#39;s y-coordinate.
Definition: qgspointv2.h:74
qreal alpha() const
Get alpha transparency 1 for opaque, 0 for invisible.
Definition: qgssymbolv2.h:353
Mixed units in symbol layers.
Definition: qgssymbolv2.h:63
void transformInPlace(double &x, double &y, double &z, TransformDirection direction=ForwardTransform) const
void transformInPlace(double &x, double &y) const
Transform device coordinates to map (world) coordinates.
QgsSymbolLayerV2List mLayers
Definition: qgssymbolv2.h:310
QgsMapUnitScale mapUnitScale() const
Definition: qgssymbolv2.h:349
const QgsFeature * feature() const
Current feature being rendered - may be null.
Definition: qgssymbolv2.h:365
The ouput shall be a percentage of another measurement (eg canvas size, feature size) ...
Definition: qgssymbolv2.h:65
qreal alpha() const
Get alpha transparency 1 for opaque, 0 for invisible.
Definition: qgssymbolv2.h:197
void setLayer(const QgsVectorLayer *layer)
Definition: qgssymbolv2.h:237
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:62
int symbolLayerCount()
Returns total number of symbol layers contained in the symbol.
Definition: qgssymbolv2.h:128
qreal mAlpha
Symbol opacity (in the range 0 - 1)
Definition: qgssymbolv2.h:313
void setFeature(const QgsFeature *f)
Definition: qgssymbolv2.h:363
Single scope for storing variables and functions for use within a QgsExpressionContext.
QPointF toQPointF() const
Returns the point as a QPointF.
Definition: qgspointv2.cpp:418
void setOutputUnit(QgsSymbolV2::OutputUnit u)
Definition: qgssymbolv2.h:347
QgsSymbolV2::OutputUnit outputUnit() const
Definition: qgssymbolv2.h:346
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:358
SymbolType
Type of the symbol.
Definition: qgssymbolv2.h:73
Contains information about the context of a rendering operation.
ScaleMethod
Scale method.
Definition: qgssymbolv2.h:84
Struct for storing maximum and minimum scales for measurements in map units.
QList< QgsSymbolLayerV2 * > QgsSymbolLayerV2List
Definition: qgssymbolv2.h:47
QgsRenderContext & renderContext()
Definition: qgssymbolv2.h:336
Fill symbol.
Definition: qgssymbolv2.h:77
qreal & rx()
qreal & ry()
const QgsMapToPixel & mapToPixel() const
const QgsFields * fields() const
Fields of the layer.
Definition: qgssymbolv2.h:371
void setRenderHints(int hints)
Definition: qgssymbolv2.h:201
bool selected() const
Definition: qgssymbolv2.h:357
const QgsRenderContext & renderContext() const
Definition: qgssymbolv2.h:337
Calculate scale by the area.
Definition: qgssymbolv2.h:86
bool clipFeaturesToExtent() const
Returns whether features drawn by the symbol will be clipped to the render context&#39;s extent...
Definition: qgssymbolv2.h:222
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:199
int mRenderHints
Definition: qgssymbolv2.h:315
int renderHints() const
Definition: qgssymbolv2.h:202