QGIS API Documentation  2.17.0-Master (00653d2)
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 mColor; }
261  void setFillColor( const QColor& color ) override { mColor = color; }
262  void setColor( const QColor& color ) override;
263  virtual QColor color() const override;
264 
265  // new methods
266 
268  Q_DECL_DEPRECATED QString name() const { return encodeShape( mShape ); }
269 
271  Q_DECL_DEPRECATED void setName( const QString& name ) { mName = name; mShape = decodeShape( name ); }
272 
278  QColor borderColor() const { return mBorderColor; }
279 
286  void setBorderColor( const QColor& color ) { mBorderColor = color; }
287 
294  Qt::PenStyle outlineStyle() const { return mOutlineStyle; }
295 
303  void setOutlineStyle( Qt::PenStyle outlineStyle ) { mOutlineStyle = outlineStyle; }
304 
311  Qt::PenJoinStyle penJoinStyle() const { return mPenJoinStyle; }
312 
320  void setPenJoinStyle( Qt::PenJoinStyle style ) { mPenJoinStyle = style; }
321 
327  double outlineWidth() const { return mOutlineWidth; }
328 
335  void setOutlineWidth( double w ) { mOutlineWidth = w; }
336 
343  void setOutlineWidthUnit( QgsSymbolV2::OutputUnit u ) { mOutlineWidthUnit = u; }
344 
350  QgsSymbolV2::OutputUnit outlineWidthUnit() const { return mOutlineWidthUnit; }
351 
358  void setOutlineWidthMapUnitScale( const QgsMapUnitScale& scale ) { mOutlineWidthMapUnitScale = scale; }
359 
365  const QgsMapUnitScale& outlineWidthMapUnitScale() const { return mOutlineWidthMapUnitScale; }
366 
367  protected:
368 
374  void drawMarker( QPainter* p, QgsSymbolV2RenderContext& context );
375 
377  Q_DECL_DEPRECATED bool prepareShape( const QString& name = QString() );
379  Q_DECL_DEPRECATED bool prepareShape( const QString& name, QPolygonF &polygon ) const;
381  Q_DECL_DEPRECATED bool preparePath( QString name = QString() );
382 
386  bool prepareCache( QgsSymbolV2RenderContext& context );
387 
391  Qt::PenStyle mOutlineStyle;
399  Qt::PenJoinStyle mPenJoinStyle;
404 
405  //TODO QGIS 3.0 - remove
407 
420  static const int mMaximumCacheWidth = 3000;
421 
422  private:
423 
424  virtual void draw( QgsSymbolV2RenderContext& context, Shape shape, const QPolygonF& polygon, const QPainterPath& path ) override;
425 };
426 
434 {
435  public:
436 
443  QgsFilledMarkerSymbolLayer( Shape shape = Circle,
444  double size = DEFAULT_SIMPLEMARKER_SIZE,
447 
452  static QgsSymbolLayerV2* create( const QgsStringMap& properties = QgsStringMap() );
453 
454  QString layerType() const override;
455  void startRender( QgsSymbolV2RenderContext& context ) override;
456  void stopRender( QgsSymbolV2RenderContext& context ) override;
457  QgsStringMap properties() const override;
458  QgsFilledMarkerSymbolLayer* clone() const override;
459  virtual QgsSymbolV2* subSymbol() override;
460  virtual bool setSubSymbol( QgsSymbolV2* symbol ) override;
461  virtual double estimateMaxBleed() const override;
462  QSet<QString> usedAttributes() const override;
463  void setColor( const QColor& c ) override;
464  virtual QColor color() const override;
465 
466  private:
467 
468  virtual void draw( QgsSymbolV2RenderContext& context, Shape shape, const QPolygonF& polygon, const QPainterPath& path ) override;
469 
472 };
473 
475 
476 #define DEFAULT_SVGMARKER_NAME "/crosses/Star1.svg"
477 #define DEFAULT_SVGMARKER_SIZE 2*DEFAULT_POINT_SIZE
478 #define DEFAULT_SVGMARKER_ANGLE 0
479 
484 {
485  public:
487  double size = DEFAULT_SVGMARKER_SIZE,
490 
491  // static stuff
492 
493  static QgsSymbolLayerV2* create( const QgsStringMap& properties = QgsStringMap() );
494  static QgsSymbolLayerV2* createFromSld( QDomElement &element );
495 
496  // implemented from base classes
497 
498  QString layerType() const override;
499 
500  void startRender( QgsSymbolV2RenderContext& context ) override;
501 
502  void stopRender( QgsSymbolV2RenderContext& context ) override;
503 
504  void renderPoint( QPointF point, QgsSymbolV2RenderContext& context ) override;
505 
506  QgsStringMap properties() const override;
507 
508  QgsSvgMarkerSymbolLayerV2* clone() const override;
509 
510  void writeSldMarker( QDomDocument &doc, QDomElement &element, const QgsStringMap& props ) const override;
511 
512  QString path() const { return mPath; }
513  void setPath( const QString& path );
514 
515  QColor fillColor() const override { return color(); }
516  void setFillColor( const QColor& color ) override { setColor( color ); }
517 
518  QColor outlineColor() const override { return mOutlineColor; }
519  void setOutlineColor( const QColor& c ) override { mOutlineColor = c; }
520 
521  double outlineWidth() const { return mOutlineWidth; }
522  void setOutlineWidth( double w ) { mOutlineWidth = w; }
523 
524  void setOutlineWidthUnit( QgsSymbolV2::OutputUnit unit ) { mOutlineWidthUnit = unit; }
525  QgsSymbolV2::OutputUnit outlineWidthUnit() const { return mOutlineWidthUnit; }
526 
527  void setOutlineWidthMapUnitScale( const QgsMapUnitScale& scale ) { mOutlineWidthMapUnitScale = scale; }
528  const QgsMapUnitScale& outlineWidthMapUnitScale() const { return mOutlineWidthMapUnitScale; }
529 
530  void setOutputUnit( QgsSymbolV2::OutputUnit unit ) override;
531  QgsSymbolV2::OutputUnit outputUnit() const override;
532 
533  void setMapUnitScale( const QgsMapUnitScale& scale ) override;
534  QgsMapUnitScale mapUnitScale() const override;
535 
536  bool writeDxf( QgsDxfExport& e, double mmMapUnitScaleFactor, const QString& layerName, QgsSymbolV2RenderContext &context, QPointF shift = QPointF( 0.0, 0.0 ) ) const override;
537 
538  QRectF bounds( QPointF point, QgsSymbolV2RenderContext& context ) override;
539 
540  protected:
542 
543  //param(fill), param(outline), param(outline-width) are going
544  //to be replaced in memory
549 
550  private:
551  double calculateSize( QgsSymbolV2RenderContext& context, bool& hasDataDefinedSize ) const;
552  void calculateOffsetAndRotation( QgsSymbolV2RenderContext& context, double scaledSize, QPointF& offset, double& angle ) const;
553 
554 };
555 
556 
558 
559 #define POINT2MM(x) ( (x) * 25.4 / 72 ) // point is 1/72 of inch
560 #define MM2POINT(x) ( (x) * 72 / 25.4 )
561 
562 #define DEFAULT_FONTMARKER_FONT "Dingbats"
563 #define DEFAULT_FONTMARKER_CHR QChar('A')
564 #define DEFAULT_FONTMARKER_SIZE POINT2MM(12)
565 #define DEFAULT_FONTMARKER_COLOR QColor(Qt::black)
566 #define DEFAULT_FONTMARKER_BORDERCOLOR QColor(Qt::white)
567 #define DEFAULT_FONTMARKER_JOINSTYLE Qt::MiterJoin
568 #define DEFAULT_FONTMARKER_ANGLE 0
569 
574 {
575  public:
578  double pointSize = DEFAULT_FONTMARKER_SIZE,
579  const QColor& color = DEFAULT_FONTMARKER_COLOR,
580  double angle = DEFAULT_FONTMARKER_ANGLE );
581 
583 
584  // static stuff
585 
586  static QgsSymbolLayerV2* create( const QgsStringMap& properties = QgsStringMap() );
587  static QgsSymbolLayerV2* createFromSld( QDomElement &element );
588 
589  // implemented from base classes
590 
591  QString layerType() const override;
592 
593  void startRender( QgsSymbolV2RenderContext& context ) override;
594 
595  void stopRender( QgsSymbolV2RenderContext& context ) override;
596 
597  void renderPoint( QPointF point, QgsSymbolV2RenderContext& context ) override;
598 
599  QgsStringMap properties() const override;
600 
601  QgsFontMarkerSymbolLayerV2* clone() const override;
602 
603  void writeSldMarker( QDomDocument &doc, QDomElement &element, const QgsStringMap& props ) const override;
604 
605  // new methods
606 
607  QString fontFamily() const { return mFontFamily; }
608  void setFontFamily( const QString& family ) { mFontFamily = family; }
609 
610  QChar character() const { return mChr; }
611  void setCharacter( QChar ch ) { mChr = ch; }
612 
615  QColor outlineColor() const override { return mOutlineColor; }
618  void setOutlineColor( const QColor& color ) override { mOutlineColor = color; }
619 
622  double outlineWidth() const { return mOutlineWidth; }
625  void setOutlineWidth( double width ) { mOutlineWidth = width; }
626 
629  QgsSymbolV2::OutputUnit outlineWidthUnit() const { return mOutlineWidthUnit; }
632  void setOutlineWidthUnit( QgsSymbolV2::OutputUnit unit ) { mOutlineWidthUnit = unit; }
633 
636  const QgsMapUnitScale& outlineWidthMapUnitScale() const { return mOutlineWidthMapUnitScale; }
639  void setOutlineWidthMapUnitScale( const QgsMapUnitScale& scale ) { mOutlineWidthMapUnitScale = scale; }
640 
643  Qt::PenJoinStyle penJoinStyle() const { return mPenJoinStyle; }
646  void setPenJoinStyle( Qt::PenJoinStyle style ) { mPenJoinStyle = style; }
647 
648  QRectF bounds( QPointF point, QgsSymbolV2RenderContext& context ) override;
649 
650  protected:
651 
655 
656  double mChrWidth;
659  double mOrigSize;
660 
661  private:
662 
663  QColor mOutlineColor;
664  double mOutlineWidth;
665  QgsSymbolV2::OutputUnit mOutlineWidthUnit;
666  QgsMapUnitScale mOutlineWidthMapUnitScale;
667  Qt::PenJoinStyle mPenJoinStyle;
668 
669  QPen mPen;
670  QBrush mBrush;
671 
672  QString characterToRender( QgsSymbolV2RenderContext& context, QPointF& charOffset, double& charWidth );
673  void calculateOffsetAndRotation( QgsSymbolV2RenderContext& context, double scaledSize, bool& hasDataDefinedRotation, QPointF& offset, double& angle ) const;
674  double calculateSize( QgsSymbolV2RenderContext& context );
675 };
676 
677 
678 #endif
679 
680 
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)
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:65
const QgsMapUnitScale & outlineWidthMapUnitScale() const
Returns the map scale for the width of the marker&#39;s outline.
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.
Qt::PenStyle outlineStyle() const
Returns the marker&#39;s outline style (eg solid, dashed, etc)
#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.
virtual QSet< QString > usedAttributes() const
Returns the set of attributes referenced by the layer.
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
#define DEFAULT_FONTMARKER_CHR
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
virtual bool writeDxf(QgsDxfExport &e, double mmMapUnitScaleFactor, const QString &layerName, QgsSymbolV2RenderContext &context, QPointF shift=QPointF(0.0, 0.0)) const
write as DXF
QColor fillColor() const override
Get fill color.
void setShape(Shape shape)
Sets the rendered marker shape.
QMap< QString, QString > QgsStringMap
Definition: qgis.h:492
void setMapUnitScale(const QgsMapUnitScale &scale) override
const QgsMapUnitScale & outlineWidthMapUnitScale() const
#define DEFAULT_SVGMARKER_ANGLE
QPainterPath mPath
Painter path representing shape. If mPolygon is empty then the shape is stored in mPath...
void setOutlineWidthUnit(QgsSymbolV2::OutputUnit unit)
virtual double estimateMaxBleed() const
Returns the estimated maximum distance which the layer style will bleed outside the drawn shape...
void setOutlineWidthUnit(QgsSymbolV2::OutputUnit u)
Sets the unit for the width of the marker&#39;s outline.
Q_DECL_DEPRECATED QString name() const
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.
QColor borderColor() const
Returns the marker&#39;s border color.
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
Qt::PenJoinStyle penJoinStyle() const
Get outline join style.
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.
double outlineWidth() const
Get outline width.
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
#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
QgsSymbolV2::OutputUnit outlineWidthUnit() const
Get outline width unit.
Shape shape() const
Returns the shape for the rendered marker symbol.
virtual void writeSldMarker(QDomDocument &doc, QDomElement &element, const QgsStringMap &props) const
Writes the symbol layer definition as a SLD XML element.
ScaleMethod
Scale method.
Definition: qgssymbolv2.h:90
Struct for storing maximum and minimum scales for measurements in map units.
QgsMapUnitScale mapUnitScale() const override
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.
double outlineWidth() const
Returns the width of the marker&#39;s outline.
void setPenJoinStyle(Qt::PenJoinStyle style)
Sets the marker&#39;s outline join style (eg miter, bevel, etc).
QColor fillColor() const override
Get fill color.
virtual void setColor(const QColor &color)
The fill color.
Qt::PenJoinStyle penJoinStyle() const
Returns the marker&#39;s outline join style (eg miter, bevel, etc).
Abstract base class for marker symbol layers.
void setOutlineWidthUnit(QgsSymbolV2::OutputUnit unit)
Set outline width unit.
QgsSymbolV2::OutputUnit outlineWidthUnit() const
Qt::PenJoinStyle mPenJoinStyle
Outline pen join style.
#define DEFAULT_SIMPLEMARKER_JOINSTYLE
QgsSymbolV2::OutputUnit outlineWidthUnit() const
Returns the unit for the width of the marker&#39;s outline.
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
Q_DECL_DEPRECATED void setName(const QString &name)
QColor outlineColor() const override
Get outline color.
Simple marker symbol layer, consisting of a rendered shape with solid fill color and an outline...
virtual QString ogrFeatureStyle(double mmScaleFactor, double mapUnitScaleFactor) const
#define DEFAULT_SIMPLEMARKER_BORDERCOLOR
QBrush mSelBrush
QBrush to use as fill of selected symbols.
void setOutlineColor(const QColor &color) override
Set outline color.