QGIS API Documentation  2.15.0-Master (972fc9f)
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 
399 //additional diagram settings for interpolated size rendering
401 {
402  public:
405  double lowerValue;
406  double upperValue;
407 
409  //TODO QGIS 3.0 - don't store index, store field name
411 
414 };
415 
416 
422 class CORE_EXPORT QgsDiagramRendererV2
423 {
424  public:
425 
427  virtual ~QgsDiagramRendererV2();
428 
431  virtual QgsDiagramRendererV2* clone() const = 0;
432 
434  virtual QSizeF sizeMapUnits( const QgsFeature& feature, const QgsRenderContext& c ) const;
435 
436  virtual QString rendererName() const = 0;
437 
439  virtual QList<QString> diagramAttributes() const = 0;
440 
446  //TODO QGIS 3.0 - remove need for fields parameter
447  virtual QSet< QString > referencedFields( const QgsExpressionContext& context = QgsExpressionContext(), const QgsFields& fields = QgsFields() ) const;
448 
449  void renderDiagram( const QgsFeature& feature, QgsRenderContext& c, QPointF pos ) const;
450 
451  void setDiagram( QgsDiagram* d );
452  QgsDiagram* diagram() const { return mDiagram; }
453 
455  virtual QList<QgsDiagramSettings> diagramSettings() const = 0;
456 
457  virtual void readXML( const QDomElement& elem, const QgsVectorLayer* layer ) = 0;
458  virtual void writeXML( QDomElement& layerElem, QDomDocument& doc, const QgsVectorLayer* layer ) const = 0;
459 
464  virtual QList< QgsLayerTreeModelLegendNode* > legendItems( QgsLayerTreeLayer* nodeLayer ) const;
465 
471  bool attributeLegend() const { return mShowAttributeLegend; }
472 
479  void setAttributeLegend( bool enabled ) { mShowAttributeLegend = enabled; }
480 
487  bool sizeLegend() const { return mShowSizeLegend; }
488 
496  void setSizeLegend( bool enabled ) { mShowSizeLegend = enabled; }
497 
503  QgsMarkerSymbolV2* sizeLegendSymbol() const { return mSizeLegendSymbol.data(); }
504 
511  void setSizeLegendSymbol( QgsMarkerSymbolV2* symbol ) { mSizeLegendSymbol.reset( symbol ); }
512 
513  protected:
515  QgsDiagramRendererV2& operator=( const QgsDiagramRendererV2& other );
516 
522  virtual bool diagramSettings( const QgsFeature &feature, const QgsRenderContext& c, QgsDiagramSettings& s ) const = 0;
523 
525  virtual QSizeF diagramSize( const QgsFeature& features, const QgsRenderContext& c ) const = 0;
526 
528  void convertSizeToMapUnits( QSizeF& size, const QgsRenderContext& context ) const;
529 
531  static int dpiPaintDevice( const QPainter* );
532 
533  //read / write diagram
534  void _readXML( const QDomElement& elem, const QgsVectorLayer* layer );
535  void _writeXML( QDomElement& rendererElem, QDomDocument& doc, const QgsVectorLayer* layer ) const;
536 
539 
542 
545 
548 };
549 
552 {
553  public:
556 
557  QgsSingleCategoryDiagramRenderer* clone() const override;
558 
559  QString rendererName() const override { return "SingleCategory"; }
560 
561  QList<QString> diagramAttributes() const override { return mSettings.categoryAttributes; }
562 
563  void setDiagramSettings( const QgsDiagramSettings& s ) { mSettings = s; }
564 
565  QList<QgsDiagramSettings> diagramSettings() const override;
566 
567  void readXML( const QDomElement& elem, const QgsVectorLayer* layer ) override;
568  void writeXML( QDomElement& layerElem, QDomDocument& doc, const QgsVectorLayer* layer ) const override;
569 
570  QList< QgsLayerTreeModelLegendNode* > legendItems( QgsLayerTreeLayer* nodeLayer ) const override;
571 
572  protected:
573  bool diagramSettings( const QgsFeature &feature, const QgsRenderContext& c, QgsDiagramSettings& s ) const override;
574 
575  QSizeF diagramSize( const QgsFeature&, const QgsRenderContext& c ) const override;
576 
577  private:
578  QgsDiagramSettings mSettings;
579 };
580 
582 {
583  public:
586 
587  QgsLinearlyInterpolatedDiagramRenderer* clone() const override;
588 
590  QList<QgsDiagramSettings> diagramSettings() const override;
591 
592  void setDiagramSettings( const QgsDiagramSettings& s ) { mSettings = s; }
593 
594  QList<QString> diagramAttributes() const override;
595 
596  virtual QSet< QString > referencedFields( const QgsExpressionContext& context = QgsExpressionContext(), const QgsFields& fields = QgsFields() ) const override;
597 
598  QString rendererName() const override { return "LinearlyInterpolated"; }
599 
600  void setLowerValue( double val ) { mInterpolationSettings.lowerValue = val; }
601  double lowerValue() const { return mInterpolationSettings.lowerValue; }
602 
603  void setUpperValue( double val ) { mInterpolationSettings.upperValue = val; }
604  double upperValue() const { return mInterpolationSettings.upperValue; }
605 
606  void setLowerSize( QSizeF s ) { mInterpolationSettings.lowerSize = s; }
607  QSizeF lowerSize() const { return mInterpolationSettings.lowerSize; }
608 
609  void setUpperSize( QSizeF s ) { mInterpolationSettings.upperSize = s; }
610  QSizeF upperSize() const { return mInterpolationSettings.upperSize; }
611 
612  int classificationAttribute() const { return mInterpolationSettings.classificationAttribute; }
613  void setClassificationAttribute( int index ) { mInterpolationSettings.classificationAttribute = index; }
614 
615  QString classificationAttributeExpression() const { return mInterpolationSettings.classificationAttributeExpression; }
616  void setClassificationAttributeExpression( const QString& expression ) { mInterpolationSettings.classificationAttributeExpression = expression; }
617 
618  bool classificationAttributeIsExpression() const { return mInterpolationSettings.classificationAttributeIsExpression; }
619  void setClassificationAttributeIsExpression( bool isExpression ) { mInterpolationSettings.classificationAttributeIsExpression = isExpression; }
620 
621  void readXML( const QDomElement& elem, const QgsVectorLayer* layer ) override;
622  void writeXML( QDomElement& layerElem, QDomDocument& doc, const QgsVectorLayer* layer ) const override;
623 
624  QList< QgsLayerTreeModelLegendNode* > legendItems( QgsLayerTreeLayer* nodeLayer ) const override;
625 
626  protected:
627  bool diagramSettings( const QgsFeature &feature, const QgsRenderContext& c, QgsDiagramSettings& s ) const override;
628 
629  QSizeF diagramSize( const QgsFeature&, const QgsRenderContext& c ) const override;
630 
631  private:
632  QgsDiagramSettings mSettings;
633  QgsDiagramInterpolationSettings mInterpolationSettings;
634 };
635 
636 #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:62
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:55
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:193
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:35
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.
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.