QGIS API Documentation  2.12.0-Lyon
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 
24 class QColor;
25 class QImage;
26 class QPainter;
27 class QSize;
28 class QPointF;
29 class QPolygonF;
30 
31 class QDomDocument;
32 class QDomElement;
33 //class
34 
35 class QgsFeature;
36 class QgsFields;
37 class QgsSymbolLayerV2;
38 class QgsRenderContext;
39 class QgsVectorLayer;
40 class QgsPaintEffect;
45 
47 
48 class CORE_EXPORT QgsSymbolV2
49 {
50  public:
51 
56  {
57  MM = 0,
60  Pixel
61  };
62 
64 
69  {
71  Line,
72  Fill
73  };
74 
79  {
81  ScaleDiameter
82  };
83 
85  {
86  DataDefinedSizeScale = 1,
87  DataDefinedRotation = 2
88  };
89 
90  virtual ~QgsSymbolV2();
91 
93  static QgsSymbolV2* defaultSymbol( QGis::GeometryType geomType );
94 
95  SymbolType type() const { return mType; }
96 
97  // symbol layers handling
98 
105  QgsSymbolLayerV2List symbolLayers() { return mLayers; }
106 
114  QgsSymbolLayerV2* symbolLayer( int layer );
115 
122  int symbolLayerCount() { return mLayers.count(); }
123 
131  bool insertSymbolLayer( int index, QgsSymbolLayerV2* layer );
132 
139  bool appendSymbolLayer( QgsSymbolLayerV2* layer );
140 
142  bool deleteSymbolLayer( int index );
143 
150  QgsSymbolLayerV2* takeSymbolLayer( int index );
151 
153  bool changeSymbolLayer( int index, QgsSymbolLayerV2 *layer );
154 
155  void startRender( QgsRenderContext& context, const QgsFields* fields = 0 );
156  void stopRender( QgsRenderContext& context );
157 
158  void setColor( const QColor& color );
159  QColor color() const;
160 
164  void drawPreviewIcon( QPainter* painter, QSize size, QgsRenderContext* customContext = 0 );
165 
167  void exportImage( const QString& path, const QString& format, const QSize& size );
168 
170  QImage asImage( QSize size, QgsRenderContext* customContext = 0 );
171 
176  QImage bigSymbolPreviewImage( QgsExpressionContext* expressionContext = 0 );
177 
178  QString dump() const;
179 
180  virtual QgsSymbolV2* clone() const = 0;
181 
182  void toSld( QDomDocument &doc, QDomElement &element, QgsStringMap props ) const;
183 
184  QgsSymbolV2::OutputUnit outputUnit() const;
185  void setOutputUnit( QgsSymbolV2::OutputUnit u );
186 
187  QgsMapUnitScale mapUnitScale() const;
188  void setMapUnitScale( const QgsMapUnitScale& scale );
189 
191  qreal alpha() const { return mAlpha; }
193  void setAlpha( qreal alpha ) { mAlpha = alpha; }
194 
195  void setRenderHints( int hints ) { mRenderHints = hints; }
196  int renderHints() const { return mRenderHints; }
197 
206  void setClipFeaturesToExtent( bool clipFeaturesToExtent ) { mClipFeaturesToExtent = clipFeaturesToExtent; }
207 
216  bool clipFeaturesToExtent() const { return mClipFeaturesToExtent; }
217 
218  QSet<QString> usedAttributes() const;
219 
221  void setLayer( const QgsVectorLayer* layer ) { mLayer = layer; }
222  const QgsVectorLayer* layer() const { return mLayer; }
223 
224  protected:
225  QgsSymbolV2( SymbolType type, const QgsSymbolLayerV2List& layers ); // can't be instantiated
226 
227  QgsSymbolLayerV2List cloneLayers() const;
228 
231  bool isSymbolLayerCompatible( SymbolType t );
232 
235 
237  qreal mAlpha;
238 
241 
242  const QgsVectorLayer* mLayer; //current vectorlayer
243 
244 };
245 
247 
248 class CORE_EXPORT QgsSymbolV2RenderContext
249 {
250  public:
251  QgsSymbolV2RenderContext( QgsRenderContext& c, QgsSymbolV2::OutputUnit u, qreal alpha = 1.0, bool selected = false, int renderHints = 0, const QgsFeature* f = 0, const QgsFields* fields = 0, const QgsMapUnitScale& mapUnitScale = QgsMapUnitScale() );
253 
254  QgsRenderContext& renderContext() { return mRenderContext; }
255  const QgsRenderContext& renderContext() const { return mRenderContext; }
256 
262  void setOriginalValueVariable( const QVariant& value );
263 
264  QgsSymbolV2::OutputUnit outputUnit() const { return mOutputUnit; }
265  void setOutputUnit( QgsSymbolV2::OutputUnit u ) { mOutputUnit = u; }
266 
267  QgsMapUnitScale mapUnitScale() const { return mMapUnitScale; }
268  void setMapUnitScale( const QgsMapUnitScale& scale ) { mMapUnitScale = scale; }
269 
271  qreal alpha() const { return mAlpha; }
273  void setAlpha( qreal alpha ) { mAlpha = alpha; }
274 
275  bool selected() const { return mSelected; }
276  void setSelected( bool selected ) { mSelected = selected; }
277 
278  int renderHints() const { return mRenderHints; }
279  void setRenderHints( int hints ) { mRenderHints = hints; }
280 
281  void setFeature( const QgsFeature* f ) { mFeature = f; }
283  const QgsFeature* feature() const { return mFeature; }
284 
289  const QgsFields* fields() const { return mFields; }
290 
291  double outputLineWidth( double width ) const;
292  double outputPixelSize( double size ) const;
293 
294  // workaround for sip 4.7. Don't use assignment - will fail with assertion error
296 
297  private:
298  QgsRenderContext& mRenderContext;
299  QgsSymbolV2::OutputUnit mOutputUnit;
300  QgsMapUnitScale mMapUnitScale;
301  qreal mAlpha;
302  bool mSelected;
303  int mRenderHints;
304  const QgsFeature* mFeature; //current feature
305  const QgsFields* mFields;
306 };
307 
308 
309 
311 
312 
313 
314 class CORE_EXPORT QgsMarkerSymbolV2 : public QgsSymbolV2
315 {
316  public:
320  static QgsMarkerSymbolV2* createSimple( const QgsStringMap& properties );
321 
323 
324  void setAngle( double angle );
325  double angle() const;
326 
332  void setDataDefinedAngle( const QgsDataDefined& dd );
333 
340  QgsDataDefined dataDefinedAngle() const;
341 
348  void setLineAngle( double lineAngle );
349 
350  void setSize( double size );
351  double size() const;
352 
358  void setDataDefinedSize( const QgsDataDefined& dd );
359 
366  QgsDataDefined dataDefinedSize() const;
367 
368  void setScaleMethod( QgsSymbolV2::ScaleMethod scaleMethod );
369  ScaleMethod scaleMethod();
370 
371  void renderPoint( const QPointF& point, const QgsFeature* f, QgsRenderContext& context, int layer = -1, bool selected = false );
372 
373  virtual QgsSymbolV2* clone() const override;
374 
375  private:
376 
377  void renderPointUsingLayer( QgsMarkerSymbolLayerV2* layer, const QPointF& point, QgsSymbolV2RenderContext& context );
378 
379 };
380 
381 
382 
383 class CORE_EXPORT QgsLineSymbolV2 : public QgsSymbolV2
384 {
385  public:
389  static QgsLineSymbolV2* createSimple( const QgsStringMap& properties );
390 
392 
393  void setWidth( double width );
394  double width() const;
395 
401  void setDataDefinedWidth( const QgsDataDefined& dd );
402 
409  QgsDataDefined dataDefinedWidth() const;
410 
411  void renderPolyline( const QPolygonF& points, const QgsFeature* f, QgsRenderContext& context, int layer = -1, bool selected = false );
412 
413  virtual QgsSymbolV2* clone() const override;
414 
415  private:
416 
417  void renderPolylineUsingLayer( QgsLineSymbolLayerV2* layer, const QPolygonF& points, QgsSymbolV2RenderContext& context );
418 
419 };
420 
421 
422 
423 class CORE_EXPORT QgsFillSymbolV2 : public QgsSymbolV2
424 {
425  public:
429  static QgsFillSymbolV2* createSimple( const QgsStringMap& properties );
430 
432  void setAngle( double angle );
433  void renderPolygon( const QPolygonF& points, QList<QPolygonF>* rings, const QgsFeature* f, QgsRenderContext& context, int layer = -1, bool selected = false );
434 
435  virtual QgsSymbolV2* clone() const override;
436 
437  private:
438 
439  void renderPolygonUsingLayer( QgsSymbolLayerV2* layer, const QPolygonF &points, QList<QPolygonF> *rings, QgsSymbolV2RenderContext &context );
441  QRectF polygonBounds( const QPolygonF &points, const QList<QPolygonF> *rings ) const;
443  QList<QPolygonF>* translateRings( const QList<QPolygonF> *rings, double dx, double dy ) const;
444 };
445 
446 #endif
447 
448 
449 /*
450 
451 QgsSymbolV2* ps = new QgsPointSymbol();
452 
453 // ----
454 
455 sl = QgsSymbolLayerV2Registry::instance()->createSymbolLayer("SimpleLine", { "color", "..." })
456 
457 // (or)
458 
459 sl = QgsSymbolLayerV2Registry::defaultSymbolLayer(QgsSymbolV2::Line)
460 
461 // (or)
462 
463 QgsSymbolLayerV2* sl = new QgsSimpleLineSymbolLayer(x,y,z);
464 QgsLineSymbol* s = new LineSymbol( [ sl ] );
465 
466 // ----
467 
468 rend = QgsSingleSymbolRenderer( new LineSymbol() );
469 */
470 
471 
static unsigned index
void setClipFeaturesToExtent(bool clipFeaturesToExtent)
Sets whether features drawn by the symbol should be clipped to the render context's extent...
Definition: qgssymbolv2.h:206
OutputUnit
The unit of the output.
Definition: qgssymbolv2.h:55
int renderHints() const
Definition: qgssymbolv2.h:278
bool mClipFeaturesToExtent
Definition: qgssymbolv2.h:240
A container class for data source field mapping or expression.
const QgsVectorLayer * mLayer
Definition: qgssymbolv2.h:242
void setAlpha(qreal alpha)
Set alpha transparency 1 for opaque, 0 for invisible.
Definition: qgssymbolv2.h:273
SymbolType type() const
Definition: qgssymbolv2.h:95
virtual QgsSymbolV2 * clone() const =0
QList< OutputUnit > OutputUnitList
Definition: qgssymbolv2.h:63
Base class for visual effects which can be applied to QPicture drawings.
Container of fields for a vector layer.
Definition: qgsfield.h:177
Line symbol.
Definition: qgssymbolv2.h:71
GeometryType
Definition: qgis.h:104
void setRenderHints(int hints)
Definition: qgssymbolv2.h:279
QgsSymbolLayerV2List symbolLayers()
Returns list of symbol layers contained in the symbol.
Definition: qgssymbolv2.h:105
The feature class encapsulates a single feature including its id, geometry and a list of field/values...
Definition: qgsfeature.h:176
SymbolType mType
Definition: qgssymbolv2.h:233
Marker symbol.
Definition: qgssymbolv2.h:70
void setMapUnitScale(const QgsMapUnitScale &scale)
Definition: qgssymbolv2.h:268
const QgsVectorLayer * layer() const
Definition: qgssymbolv2.h:222
qreal alpha() const
Get alpha transparency 1 for opaque, 0 for invisible.
Definition: qgssymbolv2.h:271
Mixed units in symbol layers.
Definition: qgssymbolv2.h:59
QgsSymbolLayerV2List mLayers
Definition: qgssymbolv2.h:234
QgsMapUnitScale mapUnitScale() const
Definition: qgssymbolv2.h:267
const QgsFeature * feature() const
Current feature being rendered - may be null.
Definition: qgssymbolv2.h:283
qreal alpha() const
Get alpha transparency 1 for opaque, 0 for invisible.
Definition: qgssymbolv2.h:191
void setLayer(const QgsVectorLayer *layer)
Definition: qgssymbolv2.h:221
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:58
int symbolLayerCount()
Returns total number of symbol layers contained in the symbol.
Definition: qgssymbolv2.h:122
qreal mAlpha
Symbol opacity (in the range 0 - 1)
Definition: qgssymbolv2.h:237
void setFeature(const QgsFeature *f)
Definition: qgssymbolv2.h:281
void setOutputUnit(QgsSymbolV2::OutputUnit u)
Definition: qgssymbolv2.h:265
QgsSymbolV2::OutputUnit outputUnit() const
Definition: qgssymbolv2.h:264
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:276
SymbolType
Type of the symbol.
Definition: qgssymbolv2.h:68
Contains information about the context of a rendering operation.
ScaleMethod
Scale method.
Definition: qgssymbolv2.h:78
Struct for storing maximum and minimum scales for measurements in map units.
QList< QgsSymbolLayerV2 * > QgsSymbolLayerV2List
Definition: qgssymbolv2.h:44
QgsRenderContext & renderContext()
Definition: qgssymbolv2.h:254
const QgsFields * fields() const
Fields of the layer.
Definition: qgssymbolv2.h:289
void setRenderHints(int hints)
Definition: qgssymbolv2.h:195
bool selected() const
Definition: qgssymbolv2.h:275
const QgsRenderContext & renderContext() const
Definition: qgssymbolv2.h:255
Calculate scale by the area.
Definition: qgssymbolv2.h:80
bool clipFeaturesToExtent() const
Returns whether features drawn by the symbol will be clipped to the render context's extent...
Definition: qgssymbolv2.h:216
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:193
int mRenderHints
Definition: qgssymbolv2.h:239
int renderHints() const
Definition: qgssymbolv2.h:196