QGIS API Documentation  2.17.0-Master (973e4b0)
qgsdiagramrendererv2.h
Go to the documentation of this file.
1 /***************************************************************************
2  qgsdiagramrendererv2.h
3  ---------------------
4  begin : March 2011
5  copyright : (C) 2011 by Marco Hugentobler
6  email : marco dot hugentobler at sourcepole dot ch
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 #ifndef QGSDIAGRAMRENDERERV2_H
16 #define QGSDIAGRAMRENDERERV2_H
17 
18 #include <QColor>
19 #include <QFont>
20 #include <QList>
21 #include <QPointF>
22 #include <QSizeF>
23 #include <QDomDocument>
24 
25 #include "qgsfeature.h"
26 #include "qgsexpressioncontext.h"
27 #include "qgssymbollayerv2.h"
28 
29 class QgsDiagram;
31 class QgsFeature;
32 class QgsRenderContext;
33 class QDomElement;
35 class QgsMapToPixel;
36 class QgsVectorLayer;
38 class QgsLayerTreeLayer;
39 
40 namespace pal { class Layer; }
41 
50 class CORE_EXPORT QgsDiagramLayerSettings
51 {
52  public:
53 
54  //avoid inclusion of QgsPalLabeling
55  enum Placement
56  {
57  AroundPoint = 0, // Point / Polygon
58  OverPoint, // Point / Polygon
59  Line, // Line / Polygon
60  Curved, // Line
61  Horizontal, // Polygon
62  Free // Polygon
63  };
64 
67  {
68  OnLine = 1,
69  AboveLine = 2,
70  BelowLine = 4,
71  MapOrientation = 8
72  };
73 
75 
78 
79  QgsDiagramLayerSettings& operator=( const QgsDiagramLayerSettings& rh );
80 
82 
87  //TODO QGIS 3.0 - rename getter to placement()
88  Placement getPlacement() const { return placement; }
89 
95  void setPlacement( Placement value ) { placement = value; }
96 
98  //TODO QGIS 3.0 - make private, rename to mPlacement
100 
106  unsigned int linePlacementFlags() const { return placementFlags; }
107 
114  void setLinePlacementFlags( unsigned int flags ) { placementFlags = flags; }
115 
117  // TODO QGIS 3.0 - make private, rename to mPlacementFlags, use QFlags
118  unsigned int placementFlags;
119 
127  //TODO QGIS 3.0 - rename getter to priority()
128  int getPriority() const { return priority; }
129 
135  void setPriority( int value ) { priority = value; }
136 
140  // TODO QGIS 3.0 - make private, rename to mPriority
141  int priority;
142 
150  //TODO QGIS 3.0 - rename getter to zIndex()
151  double getZIndex() const { return zIndex; }
152 
159  void setZIndex( double index ) { zIndex = index; }
160 
162  // TODO QGIS 3.0 - rename to mZIndex, make private
163  double zIndex;
164 
165 
170  bool isObstacle() const { return obstacle; }
171 
177  void setIsObstacle( bool isObstacle ) { obstacle = isObstacle; }
178 
180  // TODO QGIS 3.0 - rename to mObstacle, make private
181  bool obstacle;
182 
187  double distance() const { return dist; }
188 
194  void setDistance( double distance ) { dist = distance; }
195 
197  // TODO QGIS 3.0 - make private, rename to mDistance
198  double dist;
199 
204  // TODO QGIS 3.0 - rename to renderer()
205  QgsDiagramRendererV2* getRenderer() { return renderer; }
206 
211  // TODO QGIS 3.0 - rename to renderer()
212  const QgsDiagramRendererV2* getRenderer() const { return renderer; }
213 
219  void setRenderer( QgsDiagramRendererV2* diagramRenderer );
220 
222  // TODO QGIS 3.0 - make private, rename to mRenderer
224 
230 
235  const QgsCoordinateTransform* coordinateTransform() const { return ct; }
236 
242  void setCoordinateTransform( QgsCoordinateTransform* transform );
243 
245  // TODO QGIS 3.0 - make private, rename to mCt
247 
249  Q_DECL_DEPRECATED const QgsMapToPixel* xform;
250 
252  Q_DECL_DEPRECATED QgsFields fields;
253 
256 
259 
262 
267  bool showAllDiagrams() const { return showAll; }
268 
274  void setShowAllDiagrams( bool showAllDiagrams ) { showAll = showAllDiagrams; }
275 
277  // TODO QGIS 3.0 - make private, rename to mShowAll
278  bool showAll;
279 
280  void readXML( const QDomElement& elem, const QgsVectorLayer* layer );
281  void writeXML( QDomElement& layerElem, QDomDocument& doc, const QgsVectorLayer* layer ) const;
282 
288  //TODO QGIS 3.0 - remove need for fields parameter
289  QSet< QString > referencedFields( const QgsExpressionContext& context = QgsExpressionContext(), const QgsFields& fields = QgsFields() ) const;
290 
291 };
292 
301 class CORE_EXPORT QgsDiagramSettings
302 {
303  public:
304 
306  enum SizeType
307  {
308  MM,
309  MapUnits
310  };
311 
313  {
315  XHeight
316  };
317 
320  {
321  Up,
324  Right
325  };
326 
328  : enabled( true )
329  , sizeType( QgsSymbolV2::MM )
330  , lineSizeUnit( QgsSymbolV2::MM )
331  , penWidth( 0.0 )
332  , labelPlacementMethod( QgsDiagramSettings::Height )
333  , diagramOrientation( QgsDiagramSettings::Up )
334  , barWidth( 5.0 )
335  , transparency( 0 )
336  , scaleByArea( true )
337  , angleOffset( 90 * 16 ) //top
338  , scaleBasedVisibility( false )
339  , minScaleDenominator( -1 )
340  , maxScaleDenominator( -1 )
341  , minimumSize( 0.0 )
342  {}
343  bool enabled;
349  QSizeF size; //size
350 
354 
359 
364 
369 
372  double penWidth;
375  double barWidth;
376  int transparency; // 0 - 100
379 
381  //scale range (-1 if no lower / upper bound )
384 
386  double minimumSize;
387 
388  void readXML( const QDomElement& elem, const QgsVectorLayer* layer );
389  void writeXML( QDomElement& rendererElem, QDomDocument& doc, const QgsVectorLayer* layer ) const;
390 
395  QList< QgsLayerTreeModelLegendNode* > legendItems( QgsLayerTreeLayer* nodeLayer ) const;
396 
397 };
398 
404 {
405  public:
408  double lowerValue;
409  double upperValue;
410 
412  //TODO QGIS 3.0 - don't store index, store field name
414 
417 };
418 
419 
425 class CORE_EXPORT QgsDiagramRendererV2
426 {
427  public:
428 
430  virtual ~QgsDiagramRendererV2();
431 
434  virtual QgsDiagramRendererV2* clone() const = 0;
435 
437  virtual QSizeF sizeMapUnits( const QgsFeature& feature, const QgsRenderContext& c ) const;
438 
439  virtual QString rendererName() const = 0;
440 
442  virtual QList<QString> diagramAttributes() const = 0;
443 
449  //TODO QGIS 3.0 - remove need for fields parameter
450  virtual QSet< QString > referencedFields( const QgsExpressionContext& context = QgsExpressionContext(), const QgsFields& fields = QgsFields() ) const;
451 
452  void renderDiagram( const QgsFeature& feature, QgsRenderContext& c, QPointF pos ) const;
453 
454  void setDiagram( QgsDiagram* d );
455  QgsDiagram* diagram() const { return mDiagram; }
456 
458  virtual QList<QgsDiagramSettings> diagramSettings() const = 0;
459 
460  virtual void readXML( const QDomElement& elem, const QgsVectorLayer* layer ) = 0;
461  virtual void writeXML( QDomElement& layerElem, QDomDocument& doc, const QgsVectorLayer* layer ) const = 0;
462 
467  virtual QList< QgsLayerTreeModelLegendNode* > legendItems( QgsLayerTreeLayer* nodeLayer ) const;
468 
474  bool attributeLegend() const { return mShowAttributeLegend; }
475 
482  void setAttributeLegend( bool enabled ) { mShowAttributeLegend = enabled; }
483 
490  bool sizeLegend() const { return mShowSizeLegend; }
491 
499  void setSizeLegend( bool enabled ) { mShowSizeLegend = enabled; }
500 
506  QgsMarkerSymbolV2* sizeLegendSymbol() const { return mSizeLegendSymbol.data(); }
507 
514  void setSizeLegendSymbol( QgsMarkerSymbolV2* symbol ) { mSizeLegendSymbol.reset( symbol ); }
515 
516  protected:
518  QgsDiagramRendererV2& operator=( const QgsDiagramRendererV2& other );
519 
525  virtual bool diagramSettings( const QgsFeature &feature, const QgsRenderContext& c, QgsDiagramSettings& s ) const = 0;
526 
528  virtual QSizeF diagramSize( const QgsFeature& features, const QgsRenderContext& c ) const = 0;
529 
531  void convertSizeToMapUnits( QSizeF& size, const QgsRenderContext& context ) const;
532 
534  static int dpiPaintDevice( const QPainter* );
535 
536  //read / write diagram
537  void _readXML( const QDomElement& elem, const QgsVectorLayer* layer );
538  void _writeXML( QDomElement& rendererElem, QDomDocument& doc, const QgsVectorLayer* layer ) const;
539 
542 
545 
548 
551 };
552 
557 {
558  public:
561 
562  QgsSingleCategoryDiagramRenderer* clone() const override;
563 
564  QString rendererName() const override { return "SingleCategory"; }
565 
566  QList<QString> diagramAttributes() const override { return mSettings.categoryAttributes; }
567 
568  void setDiagramSettings( const QgsDiagramSettings& s ) { mSettings = s; }
569 
570  QList<QgsDiagramSettings> diagramSettings() const override;
571 
572  void readXML( const QDomElement& elem, const QgsVectorLayer* layer ) override;
573  void writeXML( QDomElement& layerElem, QDomDocument& doc, const QgsVectorLayer* layer ) const override;
574 
575  QList< QgsLayerTreeModelLegendNode* > legendItems( QgsLayerTreeLayer* nodeLayer ) const override;
576 
577  protected:
578  bool diagramSettings( const QgsFeature &feature, const QgsRenderContext& c, QgsDiagramSettings& s ) const override;
579 
580  QSizeF diagramSize( const QgsFeature&, const QgsRenderContext& c ) const override;
581 
582  private:
583  QgsDiagramSettings mSettings;
584 };
585 
590 {
591  public:
594 
595  QgsLinearlyInterpolatedDiagramRenderer* clone() const override;
596 
598  QList<QgsDiagramSettings> diagramSettings() const override;
599 
600  void setDiagramSettings( const QgsDiagramSettings& s ) { mSettings = s; }
601 
602  QList<QString> diagramAttributes() const override;
603 
604  virtual QSet< QString > referencedFields( const QgsExpressionContext& context = QgsExpressionContext(), const QgsFields& fields = QgsFields() ) const override;
605 
606  QString rendererName() const override { return "LinearlyInterpolated"; }
607 
608  void setLowerValue( double val ) { mInterpolationSettings.lowerValue = val; }
609  double lowerValue() const { return mInterpolationSettings.lowerValue; }
610 
611  void setUpperValue( double val ) { mInterpolationSettings.upperValue = val; }
612  double upperValue() const { return mInterpolationSettings.upperValue; }
613 
614  void setLowerSize( QSizeF s ) { mInterpolationSettings.lowerSize = s; }
615  QSizeF lowerSize() const { return mInterpolationSettings.lowerSize; }
616 
617  void setUpperSize( QSizeF s ) { mInterpolationSettings.upperSize = s; }
618  QSizeF upperSize() const { return mInterpolationSettings.upperSize; }
619 
620  int classificationAttribute() const { return mInterpolationSettings.classificationAttribute; }
621  void setClassificationAttribute( int index ) { mInterpolationSettings.classificationAttribute = index; }
622 
623  QString classificationAttributeExpression() const { return mInterpolationSettings.classificationAttributeExpression; }
624  void setClassificationAttributeExpression( const QString& expression ) { mInterpolationSettings.classificationAttributeExpression = expression; }
625 
626  bool classificationAttributeIsExpression() const { return mInterpolationSettings.classificationAttributeIsExpression; }
627  void setClassificationAttributeIsExpression( bool isExpression ) { mInterpolationSettings.classificationAttributeIsExpression = isExpression; }
628 
629  void readXML( const QDomElement& elem, const QgsVectorLayer* layer ) override;
630  void writeXML( QDomElement& layerElem, QDomDocument& doc, const QgsVectorLayer* layer ) const override;
631 
632  QList< QgsLayerTreeModelLegendNode* > legendItems( QgsLayerTreeLayer* nodeLayer ) const override;
633 
634  protected:
635  bool diagramSettings( const QgsFeature &feature, const QgsRenderContext& c, QgsDiagramSettings& s ) const override;
636 
637  QSizeF diagramSize( const QgsFeature&, const QgsRenderContext& c ) const override;
638 
639  private:
640  QgsDiagramSettings mSettings;
641  QgsDiagramInterpolationSettings mInterpolationSettings;
642 };
643 
644 #endif // QGSDIAGRAMRENDERERV2_H
void setClassificationAttributeIsExpression(bool isExpression)
double zIndex
Z-index of diagrams, where diagrams with a higher z-index are drawn on top of diagrams with a lower z...
void setLinePlacementFlags(unsigned int flags)
Sets the the diagram placement flags.
double minimumSize
Scale diagrams smaller than mMinimumSize to mMinimumSize.
static unsigned index
OutputUnit
The unit of the output.
Definition: qgssymbolv2.h:65
void setClassificationAttributeExpression(const QString &expression)
double distance() const
Returns the distance between the diagram and the feature (in mm).
bool showAll
Whether to show all diagrams, including overlapping diagrams.
Renders the diagrams for all features with the same settings.
void setAttributeLegend(bool enabled)
Sets whether the renderer will show legend items for diagram attributes.
QList< QString > categoryAttributes
int yPosColumn
Attribute index for y coordinate (or -1 if position not data defined)
QgsDiagram * diagram() const
A set of features which influence the labelling process.
Definition: layer.h:58
void setSizeLegend(bool enabled)
Sets whether the renderer will show legend items for diagram sizes.
unsigned int placementFlags
Diagram placement flags.
Container of fields for a vector layer.
Definition: qgsfield.h:209
bool mShowAttributeLegend
Whether to show an attribute legend for the diagrams.
QgsCoordinateTransform * coordinateTransform()
Returns the coordinate transform associated with the layer.
void setZIndex(double index)
Sets the diagram z-index.
The feature class encapsulates a single feature including its id, geometry and a list of field/values...
Definition: qgsfeature.h:187
bool mShowSizeLegend
Whether to show a size legend for the diagrams.
QgsCoordinateTransform * ct
Associated coordinate transform. Owned by this object.
Evaluates and returns the diagram settings relating to a diagram for a specific feature.
Perform transforms between map coordinates and device coordinates.
Definition: qgsmaptopixel.h:34
DiagramOrientation diagramOrientation
int xPosColumn
Attribute index for x coordinate (or -1 if position not data defined)
QgsSymbolV2::OutputUnit sizeType
Diagram size unit.
bool isObstacle() const
Returns whether the feature associated with a diagram acts as an obstacle for other labels or diagram...
const QgsCoordinateTransform * coordinateTransform() const
Returns the coordinate transform associated with the layer.
QList< QString > diagramAttributes() const override
Returns attribute indices needed for diagram rendering.
void setShowAllDiagrams(bool showAllDiagrams)
Sets whether the layer should show all diagrams, including overlapping diagrams.
bool obstacle
Whether associated feature acts as an obstacle for other labels or diagrams.
void setDiagramSettings(const QgsDiagramSettings &s)
double getZIndex() const
Returns the diagram z-index.
QScopedPointer< QgsMarkerSymbolV2 > mSizeLegendSymbol
Marker symbol to use in size legends.
Expression contexts are used to encapsulate the parameters around which a QgsExpression should be eva...
LinePlacementFlags
Line placement flags for controlling line based placements.
QgsMapUnitScale sizeScale
Diagram size unit scale.
DiagramOrientation
Orientation of histogram.
Q_DECL_DEPRECATED QgsFields fields
QgsDiagramRendererV2 * renderer
Associated diagram renderer. Owned by this object.
Placement placement
Diagram placement.
void setSizeLegendSymbol(QgsMarkerSymbolV2 *symbol)
Sets the marker symbol used for rendering the diagram size legend.
QString rendererName() const override
Base class for all diagram types.
Definition: qgsdiagram.h:36
double dist
Distance between diagram and the feature (in mm)
QgsDiagramRendererV2 * getRenderer()
Returns the diagram renderer associated with the layer.
Stores the settings for rendering of all diagrams for a layer.
void setPriority(int value)
Sets the diagram priority.
const QgsDiagramRendererV2 * getRenderer() const
Returns the diagram renderer associated with the layer.
Additional diagram settings for interpolated size rendering.
Contains information about the context of a rendering operation.
unsigned int linePlacementFlags() const
Returns the diagram placement flags.
Struct for storing maximum and minimum scales for measurements in map units.
bool showAllDiagrams() const
Returns whether the layer should show all diagrams, including overlapping diagrams.
Placement getPlacement() const
Returns the diagram placement.
void setPlacement(Placement value)
Sets the diagram placement.
int showColumn
Attribute index for visibility (or -1 if visibility not data defined)
Class for doing transforms between two map coordinate systems.
The QgsLegendRendererItem class is abstract interface for legend items returned from QgsMapLayerLegen...
int priority
Placement priority, where 0 = low and 10 = high.
QList< QString > categoryLabels
QgsMarkerSymbolV2 * sizeLegendSymbol() const
Returns the marker symbol used for rendering the diagram size legend.
int classificationAttribute
Index of the classification attribute.
Q_DECL_DEPRECATED const QgsMapToPixel * xform
QgsMapUnitScale lineSizeScale
Line unit scale.
void setDiagramSettings(const QgsDiagramSettings &s)
int getPriority() const
Returns the diagram priority.
Represents a vector layer which manages a vector based data sets.
QList< QColor > categoryColors
QgsDiagram * mDiagram
Reference to the object that does the real diagram rendering.
bool attributeLegend() const
Returns true if renderer will show legend items for diagram attributes.
bool sizeLegend() const
Returns true if renderer will show legend items for diagram sizes.
Stores the settings for rendering a single diagram.
LabelPlacementMethod labelPlacementMethod
QgsSymbolV2::OutputUnit lineSizeUnit
Line unit index.
Layer tree node points to a map layer.
void setIsObstacle(bool isObstacle)
Sets whether the feature associated with a diagram acts as an obstacle for other labels or diagrams...
void setDistance(double distance)
Sets the distance between the diagram and the feature.