QGIS API Documentation  3.6.0-Noosa (5873452)
qgsdiagramrenderer.h
Go to the documentation of this file.
1 /***************************************************************************
2  qgsdiagramrenderer.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 QGSDIAGRAMRENDERER_H
16 #define QGSDIAGRAMRENDERER_H
17 
18 #include "qgis_core.h"
19 #include "qgis_sip.h"
20 #include <QColor>
21 #include <QFont>
22 #include <QList>
23 #include <QPointF>
24 #include <QSizeF>
25 #include <QDomDocument>
26 
27 #include "qgsexpressioncontext.h"
28 #include "qgsfields.h"
29 #include "qgscoordinatetransform.h"
30 #include "qgssymbol.h"
31 #include "qgsproperty.h"
32 #include "qgspropertycollection.h"
34 
35 #include "diagram/qgsdiagram.h"
36 
37 class QgsDiagramRenderer;
38 class QgsFeature;
39 class QgsRenderContext;
40 class QDomElement;
41 class QgsMapToPixel;
43 class QgsVectorLayer;
45 class QgsLayerTreeLayer;
46 
47 namespace pal { class Layer; } SIP_SKIP
48 
58 class CORE_EXPORT QgsDiagramLayerSettings
59 {
60  public:
61 
62  //avoid inclusion of QgsPalLabeling
63  enum Placement
64  {
65  AroundPoint = 0, // Point / Polygon
66  OverPoint, // Point / Polygon
67  Line, // Line / Polygon
68  Curved, // Line
69  Horizontal, // Polygon
70  Free // Polygon
71  };
72 
75  {
76  OnLine = 1,
77  AboveLine = 1 << 1,
78  BelowLine = 1 << 2,
79  MapOrientation = 1 << 4,
80  };
81  Q_DECLARE_FLAGS( LinePlacementFlags, LinePlacementFlag )
82 
83 
87  enum Property
88  {
98  Show,
101  };
102 
107  static const QgsPropertiesDefinition &propertyDefinitions();
108 
113 
116 
117  QgsDiagramLayerSettings &operator=( const QgsDiagramLayerSettings &rh );
118 
120 
126  Placement placement() const { return mPlacement; }
127 
134  void setPlacement( Placement value ) { mPlacement = value; }
135 
142  LinePlacementFlags linePlacementFlags() const { return mPlacementFlags; }
143 
151  void setLinePlacementFlags( LinePlacementFlags flags ) { mPlacementFlags = flags; }
152 
161  int priority() const { return mPriority; }
162 
169  void setPriority( int value ) { mPriority = value; }
170 
179  double zIndex() const { return mZIndex; }
180 
188  void setZIndex( double index ) { mZIndex = index; }
189 
195  bool isObstacle() const { return mObstacle; }
196 
203  void setIsObstacle( bool isObstacle ) { mObstacle = isObstacle; }
204 
210  double distance() const { return mDistance; }
211 
218  void setDistance( double distance ) { mDistance = distance; }
219 
225  QgsDiagramRenderer *renderer() { return mRenderer; }
226 
233  const QgsDiagramRenderer *renderer() const { return mRenderer; } SIP_SKIP
234 
241  void setRenderer( QgsDiagramRenderer *diagramRenderer SIP_TRANSFER );
242 
250 
257  void setCoordinateTransform( const QgsCoordinateTransform &transform );
258 
264  bool showAllDiagrams() const { return mShowAll; }
265 
272  void setShowAllDiagrams( bool showAllDiagrams ) { mShowAll = showAllDiagrams; }
273 
278  void readXml( const QDomElement &elem );
279 
284  void writeXml( QDomElement &layerElem, QDomDocument &doc ) const;
285 
292  bool prepare( const QgsExpressionContext &context = QgsExpressionContext() ) const;
293 
299  QSet< QString > referencedFields( const QgsExpressionContext &context = QgsExpressionContext() ) const;
300 
306  QgsPropertyCollection &dataDefinedProperties() { return mDataDefinedProperties; }
307 
315  const QgsPropertyCollection &dataDefinedProperties() const { return mDataDefinedProperties; } SIP_SKIP
316 
324  void setDataDefinedProperties( const QgsPropertyCollection &collection ) { mDataDefinedProperties = collection; }
325 
326  private:
327 
330 
332  Placement mPlacement = AroundPoint;
333 
335  LinePlacementFlags mPlacementFlags = OnLine;
336 
342  int mPriority = 5;
343 
345  double mZIndex = 0.0;
346 
348  bool mObstacle = false;
349 
351  double mDistance = 0.0;
352 
354  QgsDiagramRenderer *mRenderer = nullptr;
355 
357  bool mShowAll = true;
358 
360  QgsPropertyCollection mDataDefinedProperties;
361 
362  static void initPropertyDefinitions();
363 
365  static QgsPropertiesDefinition sPropertyDefinitions;
366 
367 };
368 
378 class CORE_EXPORT QgsDiagramSettings
379 {
380  public:
381 
383  {
385  XHeight
386  };
387 
390  {
391  Up,
394  Right
395  };
396 
398  QgsDiagramSettings() = default;
399 
400  bool enabled = true;
401  QFont font;
402  QList< QColor > categoryColors;
403  QList< QString > categoryAttributes;
405  QList< QString > categoryLabels;
406  QSizeF size; //size
407 
412 
418 
424 
430 
432  QColor penColor;
433  double penWidth = 0.0;
436  double barWidth = 5.0;
437 
439  double opacity = 1.0;
440 
441  bool scaleByArea = true;
442 
447  double rotationOffset = 270;
448 
449  bool scaleBasedVisibility = false;
450 
457  double maximumScale = 0;
458 
465  double minimumScale = 0;
466 
468  double minimumSize = 0.0;
469 
471  void readXml( const QDomElement &elem );
473  void writeXml( QDomElement &rendererElem, QDomDocument &doc ) const;
474 
480  QList< QgsLayerTreeModelLegendNode * > legendItems( QgsLayerTreeLayer *nodeLayer ) const SIP_FACTORY;
481 
482 };
483 
490 {
491  public:
492  QSizeF lowerSize;
493  QSizeF upperSize;
494  double lowerValue;
495  double upperValue;
496 
499 
502 };
503 
504 
511 class CORE_EXPORT QgsDiagramRenderer
512 {
513 
514 #ifdef SIP_RUN
516  if ( sipCpp->rendererName() == QStringLiteral( "SingleCategory" ) )
517  sipType = sipType_QgsSingleCategoryDiagramRenderer;
518  else if ( sipCpp->rendererName() == QStringLiteral( "LinearlyInterpolated" ) )
519  sipType = sipType_QgsLinearlyInterpolatedDiagramRenderer;
520  else
521  sipType = NULL;
522  SIP_END
523 #endif
524 
525  public:
526 
530  QgsDiagramRenderer() = default;
531  virtual ~QgsDiagramRenderer() = default;
532 
536  virtual QgsDiagramRenderer *clone() const = 0 SIP_FACTORY;
537 
539  virtual QSizeF sizeMapUnits( const QgsFeature &feature, const QgsRenderContext &c ) const;
540 
541  virtual QString rendererName() const = 0;
542 
544  virtual QList<QString> diagramAttributes() const = 0;
545 
551  virtual QSet< QString > referencedFields( const QgsExpressionContext &context = QgsExpressionContext() ) const;
552 
556  void renderDiagram( const QgsFeature &feature, QgsRenderContext &c, QPointF pos, const QgsPropertyCollection &properties = QgsPropertyCollection() ) const;
557 
558  void setDiagram( QgsDiagram *d SIP_TRANSFER );
559  QgsDiagram *diagram() const { return mDiagram.get(); }
560 
562  virtual QList<QgsDiagramSettings> diagramSettings() const = 0;
563 
569  virtual void readXml( const QDomElement &elem, const QgsReadWriteContext &context ) = 0;
570 
576  virtual void writeXml( QDomElement &layerElem, QDomDocument &doc, const QgsReadWriteContext &context ) const = 0;
577 
583  virtual QList< QgsLayerTreeModelLegendNode * > legendItems( QgsLayerTreeLayer *nodeLayer ) const SIP_FACTORY;
584 
590  bool attributeLegend() const { return mShowAttributeLegend; }
591 
598  void setAttributeLegend( bool enabled ) { mShowAttributeLegend = enabled; }
599 
600  protected:
601  QgsDiagramRenderer( const QgsDiagramRenderer &other );
602  QgsDiagramRenderer &operator=( const QgsDiagramRenderer &other );
603 
610  virtual bool diagramSettings( const QgsFeature &feature, const QgsRenderContext &c, QgsDiagramSettings &s ) const = 0;
611 
613  virtual QSizeF diagramSize( const QgsFeature &features, const QgsRenderContext &c ) const = 0;
614 
616  void convertSizeToMapUnits( QSizeF &size, const QgsRenderContext &context ) const;
617 
619  static int dpiPaintDevice( const QPainter * );
620 
621  //read / write diagram
622 
627  void _readXml( const QDomElement &elem, const QgsReadWriteContext &context );
628 
633  void _writeXml( QDomElement &rendererElem, QDomDocument &doc, const QgsReadWriteContext &context ) const;
634 
636  std::unique_ptr< QgsDiagram > mDiagram;
637 
639  bool mShowAttributeLegend = true;
640 };
641 
647 {
648  public:
649 
652 
653  QgsSingleCategoryDiagramRenderer *clone() const override SIP_FACTORY;
654 
655  QString rendererName() const override { return QStringLiteral( "SingleCategory" ); }
656 
657  QList<QString> diagramAttributes() const override { return mSettings.categoryAttributes; }
658 
659  void setDiagramSettings( const QgsDiagramSettings &s ) { mSettings = s; }
660 
661  QList<QgsDiagramSettings> diagramSettings() const override;
662 
663  void readXml( const QDomElement &elem, const QgsReadWriteContext &context ) override;
664  void writeXml( QDomElement &layerElem, QDomDocument &doc, const QgsReadWriteContext &context ) const override;
665 
666  QList< QgsLayerTreeModelLegendNode * > legendItems( QgsLayerTreeLayer *nodeLayer ) const override SIP_FACTORY;
667 
668  protected:
669  bool diagramSettings( const QgsFeature &feature, const QgsRenderContext &c, QgsDiagramSettings &s ) const override;
670 
671  QSizeF diagramSize( const QgsFeature &, const QgsRenderContext &c ) const override;
672 
673  private:
674  QgsDiagramSettings mSettings;
675 };
676 
682 {
683  public:
686 
687  QgsLinearlyInterpolatedDiagramRenderer *clone() const override SIP_FACTORY;
688 
690  QList<QgsDiagramSettings> diagramSettings() const override;
691 
692  void setDiagramSettings( const QgsDiagramSettings &s ) { mSettings = s; }
693 
694  QList<QString> diagramAttributes() const override;
695 
696  QSet< QString > referencedFields( const QgsExpressionContext &context = QgsExpressionContext() ) const override;
697 
698  QString rendererName() const override { return QStringLiteral( "LinearlyInterpolated" ); }
699 
700  void setLowerValue( double val ) { mInterpolationSettings.lowerValue = val; }
701  double lowerValue() const { return mInterpolationSettings.lowerValue; }
702 
703  void setUpperValue( double val ) { mInterpolationSettings.upperValue = val; }
704  double upperValue() const { return mInterpolationSettings.upperValue; }
705 
706  void setLowerSize( QSizeF s ) { mInterpolationSettings.lowerSize = s; }
707  QSizeF lowerSize() const { return mInterpolationSettings.lowerSize; }
708 
709  void setUpperSize( QSizeF s ) { mInterpolationSettings.upperSize = s; }
710  QSizeF upperSize() const { return mInterpolationSettings.upperSize; }
711 
717  QString classificationField() const { return mInterpolationSettings.classificationField; }
718 
724  void setClassificationField( const QString &field ) { mInterpolationSettings.classificationField = field; }
725 
726  QString classificationAttributeExpression() const { return mInterpolationSettings.classificationAttributeExpression; }
727  void setClassificationAttributeExpression( const QString &expression ) { mInterpolationSettings.classificationAttributeExpression = expression; }
728 
729  bool classificationAttributeIsExpression() const { return mInterpolationSettings.classificationAttributeIsExpression; }
730  void setClassificationAttributeIsExpression( bool isExpression ) { mInterpolationSettings.classificationAttributeIsExpression = isExpression; }
731 
732  void readXml( const QDomElement &elem, const QgsReadWriteContext &context ) override;
733  void writeXml( QDomElement &layerElem, QDomDocument &doc, const QgsReadWriteContext &context ) const override;
734 
735  QList< QgsLayerTreeModelLegendNode * > legendItems( QgsLayerTreeLayer *nodeLayer ) const override SIP_FACTORY;
736 
741  void setDataDefinedSizeLegend( QgsDataDefinedSizeLegend *settings SIP_TRANSFER );
742 
747  QgsDataDefinedSizeLegend *dataDefinedSizeLegend() const;
748 
749  protected:
750  bool diagramSettings( const QgsFeature &feature, const QgsRenderContext &c, QgsDiagramSettings &s ) const override;
751 
752  QSizeF diagramSize( const QgsFeature &, const QgsRenderContext &c ) const override;
753 
756 
757  private:
758  QgsDiagramSettings mSettings;
759  QgsDiagramInterpolationSettings mInterpolationSettings;
760 
762  QgsDataDefinedSizeLegend *mDataDefinedSizeLegend = nullptr;
763 };
764 
765 #endif // QGSDIAGRAMRENDERER_H
void setClassificationAttributeIsExpression(bool isExpression)
The class is used as a container of context for various read/write operations on other objects...
y-coordinate data defined diagram position
Property
Data definable properties.
void setClassificationAttributeExpression(const QString &expression)
bool isObstacle() const
Returns whether the feature associated with a diagram acts as an obstacle for other labels or diagram...
QgsCoordinateTransform coordinateTransform() const
Returns the coordinate transform associated with the layer, or an invalid transform if no transformat...
Renders the diagrams for all features with the same settings.
QList< QString > categoryAttributes
QgsDiagramRenderer * renderer()
Returns the diagram renderer associated with the layer.
int priority() const
Returns the diagram priority.
A set of features which influence the labeling process.
Definition: layer.h:63
QgsPropertyCollection & dataDefinedProperties()
Returns a reference to the diagram&#39;s property collection, used for data defined overrides.
Whether the diagram should always be shown, even if it overlaps other diagrams/labels.
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:55
Z-index for diagram ordering.
As part of the API refactoring and improvements which landed in the Processing API was substantially reworked from the x version This was done in order to allow much of the underlying Processing framework to be ported into c
void setDataDefinedProperties(const QgsPropertyCollection &collection)
Sets the diagram&#39;s property collection, used for data defined overrides.
Perform transforms between map coordinates and device coordinates.
Definition: qgsmaptopixel.h:37
Diagram priority (between 0 and 10)
QgsDiagram * diagram() const
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.
void setDiagramSettings(const QgsDiagramSettings &s)
#define SIP_SKIP
Definition: qgis_sip.h:119
Evaluates and returns the diagram settings relating to a diagram for a specific feature.
void setAttributeLegend(bool enabled)
Sets whether the renderer will show legend items for diagram attributes.
Expression contexts are used to encapsulate the parameters around which a QgsExpression should be eva...
QString classificationField() const
Returns the field name used for interpolating the diagram size.
#define SIP_TRANSFER
Definition: qgis_sip.h:36
#define SIP_END
Definition: qgis_sip.h:182
QgsMapUnitScale sizeScale
Diagram size unit scale.
DiagramOrientation
Orientation of histogram.
void setClassificationField(const QString &field)
Sets the field name used for interpolating the diagram size.
x-coordinate data defined diagram position
#define SIP_FACTORY
Definition: qgis_sip.h:69
Whether diagram features act as obstacles for other diagrams/labels.
double zIndex() const
Returns the diagram z-index.
std::unique_ptr< QgsDiagram > mDiagram
Reference to the object that does the real diagram rendering.
const QgsPropertyCollection & dataDefinedProperties() const
Returns a reference to the diagram&#39;s property collection, used for data defined overrides.
QString rendererName() const override
Base class for all diagram types.
Definition: qgsdiagram.h:38
QMap< int, QgsPropertyDefinition > QgsPropertiesDefinition
Definition of available properties.
LinePlacementFlags linePlacementFlags() const
Returns the diagram placement flags.
const QgsDiagramRenderer * renderer() const
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.
Placement placement() const
Returns the diagram placement.
double distance() const
Returns the distance between the diagram and the feature (in mm).
Additional diagram settings for interpolated size rendering.
Contains information about the context of a rendering operation.
bool attributeLegend() const
Returns true if renderer will show legend items for diagram attributes.
Distance to diagram from feature.
void setLinePlacementFlags(LinePlacementFlags flags)
Sets the the diagram placement flags.
Struct for storing maximum and minimum scales for measurements in map units.
QString classificationField
Name of the field for classification.
#define SIP_CONVERT_TO_SUBCLASS_CODE(code)
Definition: qgis_sip.h:165
void setPlacement(Placement value)
Sets the diagram placement.
Class for doing transforms between two map coordinate systems.
The QgsLegendRendererItem class is abstract interface for legend items returned from QgsMapLayerLegen...
A grouped map of multiple QgsProperty objects, each referenced by a integer key value.
QList< QString > categoryLabels
Whether to show the diagram.
LinePlacementFlag
Line placement flags for controlling line based placements.
QgsMapUnitScale lineSizeScale
Line unit scale.
void setDiagramSettings(const QgsDiagramSettings &s)
Represents a vector layer which manages a vector based data sets.
Object that keeps configuration of appearance of marker symbol&#39;s data-defined size in legend...
QList< QColor > categoryColors
Stores the settings for rendering a single diagram.
RenderUnit
Rendering size units.
Definition: qgsunittypes.h:111
bool showAllDiagrams() const
Returns whether the layer should show all diagrams, including overlapping diagrams.
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.