QGIS API Documentation  2.15.0-Master (02a0ebe)
qgsmarkersymbollayerv2.h
Go to the documentation of this file.
1 /***************************************************************************
2  qgsmarkersymbollayerv2.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 QGSMARKERSYMBOLLAYERV2_H
17 #define QGSMARKERSYMBOLLAYERV2_H
18 
19 #include "qgssymbollayerv2.h"
20 #include "qgsvectorlayer.h"
21 
22 #define DEFAULT_SIMPLEMARKER_NAME "circle"
23 #define DEFAULT_SIMPLEMARKER_COLOR QColor(255,0,0)
24 #define DEFAULT_SIMPLEMARKER_BORDERCOLOR QColor(0,0,0)
25 #define DEFAULT_SIMPLEMARKER_JOINSTYLE Qt::BevelJoin
26 #define DEFAULT_SIMPLEMARKER_SIZE DEFAULT_POINT_SIZE
27 #define DEFAULT_SIMPLEMARKER_ANGLE 0
28 
29 #include <QPen>
30 #include <QBrush>
31 #include <QPicture>
32 #include <QPolygonF>
33 #include <QFont>
34 
42 {
43  public:
44 
46  enum Shape
47  {
54  Star,
60  Line,
71  };
72 
74  static QList< Shape > availableShapes();
75 
80  static bool shapeIsFilled( Shape shape );
81 
88  QgsSimpleMarkerSymbolLayerBase( Shape shape = Circle,
89  double size = DEFAULT_SIMPLEMARKER_SIZE,
92 
96  Shape shape() const { return mShape; }
97 
102  void setShape( Shape shape ) { mShape = shape; }
103 
111  static Shape decodeShape( const QString& name, bool* ok = nullptr );
112 
118  static QString encodeShape( Shape shape );
119 
120  void startRender( QgsSymbolV2RenderContext& context ) override;
121  void stopRender( QgsSymbolV2RenderContext& context ) override;
122  void renderPoint( QPointF point, QgsSymbolV2RenderContext& context ) override;
123  QRectF bounds( QPointF point, QgsSymbolV2RenderContext& context ) override;
124 
125  protected:
126 
129  bool prepareMarkerShape( Shape shape );
130 
133  bool prepareMarkerPath( Shape symbol );
134 
141  bool shapeToPolygon( Shape shape, QPolygonF &polygon ) const;
142 
149  double calculateSize( QgsSymbolV2RenderContext& context, bool& hasDataDefinedSize ) const;
150 
159  void calculateOffsetAndRotation( QgsSymbolV2RenderContext& context, double scaledSize, bool& hasDataDefinedRotation, QPointF& offset, double& angle ) const;
160 
163 
166 
169 
170  private:
171 
179  virtual void draw( QgsSymbolV2RenderContext& context, Shape shape, const QPolygonF& polygon, const QPainterPath& path ) = 0;
180 };
181 
187 {
188  public:
189 
204  Q_DECL_DEPRECATED QgsSimpleMarkerSymbolLayerV2( const QString& name,
205  const QColor& color = DEFAULT_SIMPLEMARKER_COLOR,
206  const QColor& borderColor = DEFAULT_SIMPLEMARKER_BORDERCOLOR,
207  double size = DEFAULT_SIMPLEMARKER_SIZE,
210  Qt::PenJoinStyle penJoinStyle = DEFAULT_SIMPLEMARKER_JOINSTYLE );
211 
221  QgsSimpleMarkerSymbolLayerV2( Shape shape = Circle,
222  double size = DEFAULT_SIMPLEMARKER_SIZE,
225  const QColor& color = DEFAULT_SIMPLEMARKER_COLOR,
226  const QColor& borderColor = DEFAULT_SIMPLEMARKER_BORDERCOLOR,
227  Qt::PenJoinStyle penJoinStyle = DEFAULT_SIMPLEMARKER_JOINSTYLE );
228 
229  // static methods
230 
235  static QgsSymbolLayerV2* create( const QgsStringMap& properties = QgsStringMap() );
236 
241  static QgsSymbolLayerV2* createFromSld( QDomElement &element );
242 
243  // reimplemented from base classes
244 
245  QString layerType() const override;
246  void startRender( QgsSymbolV2RenderContext& context ) override;
247  void renderPoint( QPointF point, QgsSymbolV2RenderContext& context ) override;
248  QgsStringMap properties() const override;
249  QgsSimpleMarkerSymbolLayerV2* clone() const override;
250  void writeSldMarker( QDomDocument &doc, QDomElement &element, const QgsStringMap& props ) const override;
251  QString ogrFeatureStyle( double mmScaleFactor, double mapUnitScaleFactor ) const override;
252  bool writeDxf( QgsDxfExport &e, double mmMapUnitScaleFactor, const QString &layerName, QgsSymbolV2RenderContext &context, QPointF shift = QPointF( 0.0, 0.0 ) ) const override;
253  void setOutputUnit( QgsSymbolV2::OutputUnit unit ) override;
254  QgsSymbolV2::OutputUnit outputUnit() const override;
255  void setMapUnitScale( const QgsMapUnitScale& scale ) override;
256  QgsMapUnitScale mapUnitScale() const override;
257  QRectF bounds( QPointF point, QgsSymbolV2RenderContext& context ) override;
258  QColor outlineColor() const override { return borderColor(); }
259  void setOutlineColor( const QColor& color ) override { setBorderColor( color ); }
260  QColor fillColor() const override { return color(); }
261  void setFillColor( const QColor& color ) override { setColor( color ); }
262 
263  // new methods
264 
266  Q_DECL_DEPRECATED QString name() const { return encodeShape( mShape ); }
267 
269  Q_DECL_DEPRECATED void setName( const QString& name ) { mName = name; mShape = decodeShape( name ); }
270 
276  QColor borderColor() const { return mBorderColor; }
277 
284  void setBorderColor( const QColor& color ) { mBorderColor = color; }
285 
292  Qt::PenStyle outlineStyle() const { return mOutlineStyle; }
293 
301  void setOutlineStyle( Qt::PenStyle outlineStyle ) { mOutlineStyle = outlineStyle; }
302 
309  Qt::PenJoinStyle penJoinStyle() const { return mPenJoinStyle; }
310 
318  void setPenJoinStyle( Qt::PenJoinStyle style ) { mPenJoinStyle = style; }
319 
325  double outlineWidth() const { return mOutlineWidth; }
326 
333  void setOutlineWidth( double w ) { mOutlineWidth = w; }
334 
341  void setOutlineWidthUnit( QgsSymbolV2::OutputUnit u ) { mOutlineWidthUnit = u; }
342 
348  QgsSymbolV2::OutputUnit outlineWidthUnit() const { return mOutlineWidthUnit; }
349 
356  void setOutlineWidthMapUnitScale( const QgsMapUnitScale& scale ) { mOutlineWidthMapUnitScale = scale; }
357 
363  const QgsMapUnitScale& outlineWidthMapUnitScale() const { return mOutlineWidthMapUnitScale; }
364 
365  protected:
366 
372  void drawMarker( QPainter* p, QgsSymbolV2RenderContext& context );
373 
375  Q_DECL_DEPRECATED bool prepareShape( const QString& name = QString() );
377  Q_DECL_DEPRECATED bool prepareShape( const QString& name, QPolygonF &polygon ) const;
379  Q_DECL_DEPRECATED bool preparePath( QString name = QString() );
380 
384  bool prepareCache( QgsSymbolV2RenderContext& context );
385 
389  Qt::PenStyle mOutlineStyle;
397  Qt::PenJoinStyle mPenJoinStyle;
402 
403  //TODO QGIS 3.0 - remove
405 
418  static const int mMaximumCacheWidth = 3000;
419 
420  private:
421 
422  virtual void draw( QgsSymbolV2RenderContext& context, Shape shape, const QPolygonF& polygon, const QPainterPath& path ) override;
423 };
424 
432 {
433  public:
434 
441  QgsFilledMarkerSymbolLayer( Shape shape = Circle,
442  double size = DEFAULT_SIMPLEMARKER_SIZE,
445 
450  static QgsSymbolLayerV2* create( const QgsStringMap& properties = QgsStringMap() );
451 
452  QString layerType() const override;
453  void startRender( QgsSymbolV2RenderContext& context ) override;
454  void stopRender( QgsSymbolV2RenderContext& context ) override;
455  QgsStringMap properties() const override;
456  QgsFilledMarkerSymbolLayer* clone() const override;
457  virtual QgsSymbolV2* subSymbol() override;
458  virtual bool setSubSymbol( QgsSymbolV2* symbol ) override;
459  virtual double estimateMaxBleed() const override;
460  QSet<QString> usedAttributes() const override;
461  void setColor( const QColor& c ) override;
462  virtual QColor color() const override;
463 
464  private:
465 
466  virtual void draw( QgsSymbolV2RenderContext& context, Shape shape, const QPolygonF& polygon, const QPainterPath& path ) override;
467 
470 };
471 
473 
474 #define DEFAULT_SVGMARKER_NAME "/crosses/Star1.svg"
475 #define DEFAULT_SVGMARKER_SIZE 2*DEFAULT_POINT_SIZE
476 #define DEFAULT_SVGMARKER_ANGLE 0
477 
479 {
480  public:
482  double size = DEFAULT_SVGMARKER_SIZE,
485 
486  // static stuff
487 
488  static QgsSymbolLayerV2* create( const QgsStringMap& properties = QgsStringMap() );
489  static QgsSymbolLayerV2* createFromSld( QDomElement &element );
490 
491  // implemented from base classes
492 
493  QString layerType() const override;
494 
495  void startRender( QgsSymbolV2RenderContext& context ) override;
496 
497  void stopRender( QgsSymbolV2RenderContext& context ) override;
498 
499  void renderPoint( QPointF point, QgsSymbolV2RenderContext& context ) override;
500 
501  QgsStringMap properties() const override;
502 
503  QgsSvgMarkerSymbolLayerV2* clone() const override;
504 
505  void writeSldMarker( QDomDocument &doc, QDomElement &element, const QgsStringMap& props ) const override;
506 
507  QString path() const { return mPath; }
508  void setPath( const QString& path );
509 
510  QColor fillColor() const override { return color(); }
511  void setFillColor( const QColor& color ) override { setColor( color ); }
512 
513  QColor outlineColor() const override { return mOutlineColor; }
514  void setOutlineColor( const QColor& c ) override { mOutlineColor = c; }
515 
516  double outlineWidth() const { return mOutlineWidth; }
517  void setOutlineWidth( double w ) { mOutlineWidth = w; }
518 
519  void setOutlineWidthUnit( QgsSymbolV2::OutputUnit unit ) { mOutlineWidthUnit = unit; }
520  QgsSymbolV2::OutputUnit outlineWidthUnit() const { return mOutlineWidthUnit; }
521 
522  void setOutlineWidthMapUnitScale( const QgsMapUnitScale& scale ) { mOutlineWidthMapUnitScale = scale; }
523  const QgsMapUnitScale& outlineWidthMapUnitScale() const { return mOutlineWidthMapUnitScale; }
524 
525  void setOutputUnit( QgsSymbolV2::OutputUnit unit ) override;
526  QgsSymbolV2::OutputUnit outputUnit() const override;
527 
528  void setMapUnitScale( const QgsMapUnitScale& scale ) override;
529  QgsMapUnitScale mapUnitScale() const override;
530 
531  bool writeDxf( QgsDxfExport& e, double mmMapUnitScaleFactor, const QString& layerName, QgsSymbolV2RenderContext &context, QPointF shift = QPointF( 0.0, 0.0 ) ) const override;
532 
533  QRectF bounds( QPointF point, QgsSymbolV2RenderContext& context ) override;
534 
535  protected:
537 
538  //param(fill), param(outline), param(outline-width) are going
539  //to be replaced in memory
544 
545  private:
546  double calculateSize( QgsSymbolV2RenderContext& context, bool& hasDataDefinedSize ) const;
547  void calculateOffsetAndRotation( QgsSymbolV2RenderContext& context, double scaledSize, QPointF& offset, double& angle ) const;
548 
549 };
550 
551 
553 
554 #define POINT2MM(x) ( (x) * 25.4 / 72 ) // point is 1/72 of inch
555 #define MM2POINT(x) ( (x) * 72 / 25.4 )
556 
557 #define DEFAULT_FONTMARKER_FONT "Dingbats"
558 #define DEFAULT_FONTMARKER_CHR QChar('A')
559 #define DEFAULT_FONTMARKER_SIZE POINT2MM(12)
560 #define DEFAULT_FONTMARKER_COLOR QColor(Qt::black)
561 #define DEFAULT_FONTMARKER_BORDERCOLOR QColor(Qt::white)
562 #define DEFAULT_FONTMARKER_JOINSTYLE Qt::MiterJoin
563 #define DEFAULT_FONTMARKER_ANGLE 0
564 
566 {
567  public:
570  double pointSize = DEFAULT_FONTMARKER_SIZE,
571  const QColor& color = DEFAULT_FONTMARKER_COLOR,
572  double angle = DEFAULT_FONTMARKER_ANGLE );
573 
575 
576  // static stuff
577 
578  static QgsSymbolLayerV2* create( const QgsStringMap& properties = QgsStringMap() );
579  static QgsSymbolLayerV2* createFromSld( QDomElement &element );
580 
581  // implemented from base classes
582 
583  QString layerType() const override;
584 
585  void startRender( QgsSymbolV2RenderContext& context ) override;
586 
587  void stopRender( QgsSymbolV2RenderContext& context ) override;
588 
589  void renderPoint( QPointF point, QgsSymbolV2RenderContext& context ) override;
590 
591  QgsStringMap properties() const override;
592 
593  QgsFontMarkerSymbolLayerV2* clone() const override;
594 
595  void writeSldMarker( QDomDocument &doc, QDomElement &element, const QgsStringMap& props ) const override;
596 
597  // new methods
598 
599  QString fontFamily() const { return mFontFamily; }
600  void setFontFamily( const QString& family ) { mFontFamily = family; }
601 
602  QChar character() const { return mChr; }
603  void setCharacter( QChar ch ) { mChr = ch; }
604 
607  QColor outlineColor() const override { return mOutlineColor; }
610  void setOutlineColor( const QColor& color ) override { mOutlineColor = color; }
611 
614  double outlineWidth() const { return mOutlineWidth; }
617  void setOutlineWidth( double width ) { mOutlineWidth = width; }
618 
621  QgsSymbolV2::OutputUnit outlineWidthUnit() const { return mOutlineWidthUnit; }
624  void setOutlineWidthUnit( QgsSymbolV2::OutputUnit unit ) { mOutlineWidthUnit = unit; }
625 
628  const QgsMapUnitScale& outlineWidthMapUnitScale() const { return mOutlineWidthMapUnitScale; }
631  void setOutlineWidthMapUnitScale( const QgsMapUnitScale& scale ) { mOutlineWidthMapUnitScale = scale; }
632 
635  Qt::PenJoinStyle penJoinStyle() const { return mPenJoinStyle; }
638  void setPenJoinStyle( Qt::PenJoinStyle style ) { mPenJoinStyle = style; }
639 
640  QRectF bounds( QPointF point, QgsSymbolV2RenderContext& context ) override;
641 
642  protected:
643 
647 
648  double mChrWidth;
651  double mOrigSize;
652 
653  private:
654 
655  QColor mOutlineColor;
656  double mOutlineWidth;
657  QgsSymbolV2::OutputUnit mOutlineWidthUnit;
658  QgsMapUnitScale mOutlineWidthMapUnitScale;
659  Qt::PenJoinStyle mPenJoinStyle;
660 
661  QPen mPen;
662  QBrush mBrush;
663 
664  QString characterToRender( QgsSymbolV2RenderContext& context, QPointF& charOffset, double& charWidth );
665  void calculateOffsetAndRotation( QgsSymbolV2RenderContext& context, double scaledSize, bool& hasDataDefinedRotation, QPointF& offset, double& angle ) const;
666  double calculateSize( QgsSymbolV2RenderContext& context );
667 };
668 
669 
670 #endif
671 
672 
virtual QSet< QString > usedAttributes() const
Returns the set of attributes referenced by the layer.
QColor borderColor() const
Returns the marker&#39;s border color.
virtual void renderPoint(QPointF point, QgsSymbolV2RenderContext &context)=0
Renders a marker at the specified point.
QRectF bounds(QPointF point, QgsSymbolV2RenderContext &context) override
Returns the approximate bounding box of the marker symbol layer, taking into account any data defined...
void setOutlineStyle(Qt::PenStyle outlineStyle)
Sets the marker&#39;s outline style (eg solid, dashed, etc)
QgsSymbolV2::OutputUnit outlineWidthUnit() const
void setOutlineColor(const QColor &color) override
Set outline color.
void setPenJoinStyle(Qt::PenJoinStyle style)
Set outline join style.
QgsSymbolV2::OutputUnit outputUnit() const override
OutputUnit
The unit of the output.
Definition: qgssymbolv2.h:62
QgsMapUnitScale mOutlineWidthMapUnitScale
Outline width map unit scale.
void startRender(QgsSymbolV2RenderContext &context) override
virtual QgsSymbolLayerV2 * clone() const =0
Shall be reimplemented by subclasses to create a deep copy of the instance.
#define DEFAULT_FONTMARKER_COLOR
void renderPoint(QPointF point, QgsSymbolV2RenderContext &context) override
Renders a marker at the specified point.
virtual QRectF bounds(QPointF point, QgsSymbolV2RenderContext &context)
Returns the approximate bounding box of the marker symbol layer, taking into account any data defined...
Abstract base class for simple marker symbol layers.
QgsSymbolV2::OutputUnit mOutlineWidthUnit
void setOutlineColor(const QColor &c) override
Set outline color.
void setOutlineWidthMapUnitScale(const QgsMapUnitScale &scale)
Set outline width map unit scale.
#define DEFAULT_SIMPLEMARKER_ANGLE
virtual bool writeDxf(QgsDxfExport &e, double mmMapUnitScaleFactor, const QString &layerName, QgsSymbolV2RenderContext &context, QPointF shift=QPointF(0.0, 0.0)) const
write as DXF
virtual void writeSldMarker(QDomDocument &doc, QDomElement &element, const QgsStringMap &props) const
Writes the symbol layer definition as a SLD XML element.
#define DEFAULT_FONTMARKER_CHR
Q_DECL_DEPRECATED QString name() const
void setOutlineWidth(double w)
Sets the width of the marker&#39;s outline.
#define DEFAULT_SIMPLEMARKER_COLOR
QImage mCache
Cached image of marker, if using cached version.
void setFillColor(const QColor &color) override
Set fill color.
bool mUsingCache
True if using cached images of markers for drawing.
static Shape decodeShape(const QString &name, bool *ok=nullptr)
Attempts to decode a string representation of a shape name to the corresponding shape.
void stopRender(QgsSymbolV2RenderContext &context) override
QColor fillColor() const override
Get fill color.
void setShape(Shape shape)
Sets the rendered marker shape.
QMap< QString, QString > QgsStringMap
Definition: qgis.h:491
void setMapUnitScale(const QgsMapUnitScale &scale) override
#define DEFAULT_SVGMARKER_ANGLE
virtual double estimateMaxBleed() const
Returns the estimated maximum distance which the layer style will bleed outside the drawn shape...
QPainterPath mPath
Painter path representing shape. If mPolygon is empty then the shape is stored in mPath...
void setOutlineWidthUnit(QgsSymbolV2::OutputUnit unit)
Qt::PenJoinStyle penJoinStyle() const
Get outline join style.
void setOutlineWidthUnit(QgsSymbolV2::OutputUnit u)
Sets the unit for the width of the marker&#39;s outline.
void setFontFamily(const QString &family)
#define DEFAULT_SCALE_METHOD
Qt::PenStyle mOutlineStyle
Outline style.
void setFillColor(const QColor &color) override
Set fill color.
QColor outlineColor() const override
Get outline color.
double outlineWidth() const
Returns the width of the marker&#39;s outline.
virtual QgsStringMap properties() const =0
Should be reimplemented by subclasses to return a string map that contains the configuration informat...
static QString encodeShape(Shape shape)
Encodes a shape to its string representation.
#define DEFAULT_FONTMARKER_ANGLE
virtual QColor color() const
The fill color.
void setOutlineWidthMapUnitScale(const QgsMapUnitScale &scale)
QgsSymbolV2::OutputUnit mOutlineWidthUnit
Outline width units.
#define DEFAULT_SVGMARKER_SIZE
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 setOutlineWidth(double width)
Set outline width.
QBrush mBrush
QBrush corresponding to marker&#39;s fill style.
virtual QString layerType() const =0
Returns a string that represents this layer type.
void setOutlineWidthMapUnitScale(const QgsMapUnitScale &scale)
Sets the map scale for the width of the marker&#39;s outline.
#define DEFAULT_FONTMARKER_SIZE
virtual QgsSymbolV2 * subSymbol()
void startRender(QgsSymbolV2RenderContext &context) override
#define DEFAULT_FONTMARKER_FONT
QgsSymbolV2::OutputUnit outlineWidthUnit() const
Returns the unit for the width of the marker&#39;s outline.
#define DEFAULT_SVGMARKER_NAME
void setBorderColor(const QColor &color)
Sets the marker&#39;s border color.
const QgsMapUnitScale & outlineWidthMapUnitScale() const
Get outline width map unit scale.
#define DEFAULT_SIMPLEMARKER_SIZE
Qt::PenJoinStyle penJoinStyle() const
Returns the marker&#39;s outline join style (eg miter, bevel, etc).
ScaleMethod
Scale method.
Definition: qgssymbolv2.h:87
const QgsMapUnitScale & outlineWidthMapUnitScale() const
Returns the map scale for the width of the marker&#39;s outline.
Struct for storing maximum and minimum scales for measurements in map units.
QgsMapUnitScale mapUnitScale() const override
Qt::PenStyle outlineStyle() const
Returns the marker&#39;s outline style (eg solid, dashed, etc)
QImage mSelCache
Cached image of selected marker, if using cached version.
QPen mSelPen
QPen to use as outline of selected symbols.
QPen mPen
QPen corresponding to marker&#39;s outline style.
void setPenJoinStyle(Qt::PenJoinStyle style)
Sets the marker&#39;s outline join style (eg miter, bevel, etc).
QColor fillColor() const override
Get fill color.
double outlineWidth() const
Get outline width.
virtual void setColor(const QColor &color)
The fill color.
Abstract base class for marker symbol layers.
void setOutlineWidthUnit(QgsSymbolV2::OutputUnit unit)
Set outline width unit.
Qt::PenJoinStyle mPenJoinStyle
Outline pen join style.
#define DEFAULT_SIMPLEMARKER_JOINSTYLE
Filled marker symbol layer, consisting of a shape which is rendered using a QgsFillSymbolV2.
virtual bool setSubSymbol(QgsSymbolV2 *symbol)
set layer&#39;s subsymbol. takes ownership of the passed symbol
virtual void stopRender(QgsSymbolV2RenderContext &context)=0
QColor outlineColor() const override
Get outline color.
QPolygonF mPolygon
Polygon of points in shape. If polygon is empty then shape is using mPath.
void setOutputUnit(QgsSymbolV2::OutputUnit unit) override
const QgsMapUnitScale & outlineWidthMapUnitScale() const
Q_DECL_DEPRECATED void setName(const QString &name)
virtual QString ogrFeatureStyle(double mmScaleFactor, double mapUnitScaleFactor) const
QColor outlineColor() const override
Get outline color.
Simple marker symbol layer, consisting of a rendered shape with solid fill color and an outline...
Shape shape() const
Returns the shape for the rendered marker symbol.
#define DEFAULT_SIMPLEMARKER_BORDERCOLOR
QBrush mSelBrush
QBrush to use as fill of selected symbols.
void setOutlineColor(const QColor &color) override
Set outline color.
QgsSymbolV2::OutputUnit outlineWidthUnit() const
Get outline width unit.