QGIS API Documentation  3.24.2-Tisler (13c1a02865)
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 "qgsproperty.h"
31 #include "qgspropertycollection.h"
32 
33 #include "diagram/qgsdiagram.h"
34 #include "qgsreadwritecontext.h"
35 #include "qgsmapunitscale.h"
36 
37 class QgsDiagramRenderer;
38 class QgsFeature;
39 class QgsRenderContext;
40 class QDomElement;
41 class QgsMapToPixel;
43 class QgsVectorLayer;
45 class QgsLayerTreeLayer;
46 class QgsPaintEffect;
48 class QgsLineSymbol;
49 
50 namespace pal { class Layer; } SIP_SKIP
51 
61 class CORE_EXPORT QgsDiagramLayerSettings
62 {
63  public:
64 
65  //avoid inclusion of QgsPalLabeling
66  enum Placement
67  {
68  AroundPoint = 0, // Point / Polygon
69  OverPoint, // Point / Polygon
70  Line, // Line / Polygon
71  Curved, // Line
72  Horizontal, // Polygon
73  Free // Polygon
74  };
75 
78  {
79  OnLine = 1,
80  AboveLine = 1 << 1,
81  BelowLine = 1 << 2,
82  MapOrientation = 1 << 4,
83  };
84  Q_DECLARE_FLAGS( LinePlacementFlags, LinePlacementFlag )
85 
86 
90  enum Property
91  {
104  };
105 
110  static const QgsPropertiesDefinition &propertyDefinitions();
111 
116 
119 
120  QgsDiagramLayerSettings &operator=( const QgsDiagramLayerSettings &rh );
121 
123 
129  Placement placement() const { return mPlacement; }
130 
137  void setPlacement( Placement value ) { mPlacement = value; }
138 
145  LinePlacementFlags linePlacementFlags() const { return mPlacementFlags; }
146 
154  void setLinePlacementFlags( LinePlacementFlags flags ) { mPlacementFlags = flags; }
155 
164  int priority() const { return mPriority; }
165 
172  void setPriority( int value ) { mPriority = value; }
173 
182  double zIndex() const { return mZIndex; }
183 
191  void setZIndex( double index ) { mZIndex = index; }
192 
198  bool isObstacle() const { return mObstacle; }
199 
206  void setIsObstacle( bool isObstacle ) { mObstacle = isObstacle; }
207 
213  double distance() const { return mDistance; }
214 
221  void setDistance( double distance ) { mDistance = distance; }
222 
228  QgsDiagramRenderer *renderer() { return mRenderer; }
229 
236  const QgsDiagramRenderer *renderer() const { return mRenderer; } SIP_SKIP
237 
244  void setRenderer( QgsDiagramRenderer *diagramRenderer SIP_TRANSFER );
245 
253 
260  void setCoordinateTransform( const QgsCoordinateTransform &transform );
261 
267  bool showAllDiagrams() const { return mShowAll; }
268 
275  void setShowAllDiagrams( bool showAllDiagrams ) { mShowAll = showAllDiagrams; }
276 
281  void readXml( const QDomElement &elem );
282 
287  void writeXml( QDomElement &layerElem, QDomDocument &doc ) const;
288 
295  bool prepare( const QgsExpressionContext &context = QgsExpressionContext() ) const;
296 
302  QSet< QString > referencedFields( const QgsExpressionContext &context = QgsExpressionContext() ) const;
303 
309  QgsPropertyCollection &dataDefinedProperties() { return mDataDefinedProperties; }
310 
318  const QgsPropertyCollection &dataDefinedProperties() const { return mDataDefinedProperties; } SIP_SKIP
319 
327  void setDataDefinedProperties( const QgsPropertyCollection &collection ) { mDataDefinedProperties = collection; }
328 
329  private:
330 
333 
335  Placement mPlacement = AroundPoint;
336 
338  LinePlacementFlags mPlacementFlags = OnLine;
339 
345  int mPriority = 5;
346 
348  double mZIndex = 0.0;
349 
351  bool mObstacle = false;
352 
354  double mDistance = 0.0;
355 
357  QgsDiagramRenderer *mRenderer = nullptr;
358 
360  bool mShowAll = true;
361 
363  QgsPropertyCollection mDataDefinedProperties;
364 
365  static void initPropertyDefinitions();
366 
368  static QgsPropertiesDefinition sPropertyDefinitions;
369 
370 };
371 
381 class CORE_EXPORT QgsDiagramSettings
382 {
383  public:
384 
386  {
388  XHeight
389  };
390 
393  {
394  Up,
397  Right
398  };
399 
405  {
408  };
409 
413 
415  QgsDiagramSettings( const QgsDiagramSettings &other );
416 
417  QgsDiagramSettings &operator=( const QgsDiagramSettings &other );
418 
419  bool enabled = true;
420  QFont font;
421  QList< QColor > categoryColors;
422  QList< QString > categoryAttributes;
424  QList< QString > categoryLabels;
425  QSizeF size; //size
426 
431 
437 
443 
449 
451  QColor penColor;
452  double penWidth = 0.0;
455  double barWidth = 5.0;
456 
458  double opacity = 1.0;
459 
460  bool scaleByArea = true;
461 
466  double rotationOffset = 270;
467 
468  bool scaleBasedVisibility = false;
469 
476  double maximumScale = 0;
477 
484  double minimumScale = 0;
485 
487  double minimumSize = 0.0;
488 
500  double spacing() const { return mSpacing; }
501 
513  void setSpacing( double spacing ) { mSpacing = spacing; }
514 
523  void setSpacingUnit( QgsUnitTypes::RenderUnit unit ) { mSpacingUnit = unit; }
524 
532  QgsUnitTypes::RenderUnit spacingUnit() const { return mSpacingUnit; }
533 
542  void setSpacingMapUnitScale( const QgsMapUnitScale &scale ) { mSpacingMapUnitScale = scale; }
543 
552  const QgsMapUnitScale &spacingMapUnitScale() const { return mSpacingMapUnitScale; }
553 
560  Direction direction() const;
561 
568  void setDirection( Direction direction );
569 
571  void readXml( const QDomElement &elem, const QgsReadWriteContext &context = QgsReadWriteContext() );
573  void writeXml( QDomElement &rendererElem, QDomDocument &doc, const QgsReadWriteContext &context = QgsReadWriteContext() ) const;
574 
580  QList< QgsLayerTreeModelLegendNode * > legendItems( QgsLayerTreeLayer *nodeLayer ) const SIP_FACTORY;
581 
590  QgsLineSymbol *axisLineSymbol() const;
591 
602  void setAxisLineSymbol( QgsLineSymbol *symbol SIP_TRANSFER );
603 
612  bool showAxis() const;
613 
622  void setShowAxis( bool showAxis );
623 
631  QgsPaintEffect *paintEffect() const;
632 
642  void setPaintEffect( QgsPaintEffect *effect SIP_TRANSFER );
643 
644  private:
645 
646  double mSpacing = 0;
648  QgsMapUnitScale mSpacingMapUnitScale;
649  Direction mDirection = Counterclockwise;
650 
651  bool mShowAxis = false;
652  std::unique_ptr< QgsLineSymbol > mAxisLineSymbol;
653  std::unique_ptr< QgsPaintEffect > mPaintEffect;
654 
655 };
656 
663 {
664  public:
665  QSizeF lowerSize;
666  QSizeF upperSize;
667  double lowerValue;
668  double upperValue;
669 
672 
675 };
676 
677 
684 class CORE_EXPORT QgsDiagramRenderer
685 {
686 
687 #ifdef SIP_RUN
689  if ( sipCpp->rendererName() == QLatin1String( "SingleCategory" ) )
690  sipType = sipType_QgsSingleCategoryDiagramRenderer;
691  else if ( sipCpp->rendererName() == QLatin1String( "LinearlyInterpolated" ) )
692  sipType = sipType_QgsLinearlyInterpolatedDiagramRenderer;
693  else
694  sipType = NULL;
695  SIP_END
696 #endif
697 
698  public:
699 
703  QgsDiagramRenderer() = default;
704  virtual ~QgsDiagramRenderer() = default;
705 
710  virtual QgsDiagramRenderer *clone() const = 0 SIP_FACTORY;
711 
713  virtual QSizeF sizeMapUnits( const QgsFeature &feature, const QgsRenderContext &c ) const;
714 
715  virtual QString rendererName() const = 0;
716 
718  virtual QList<QString> diagramAttributes() const = 0;
719 
725  virtual QSet< QString > referencedFields( const QgsExpressionContext &context = QgsExpressionContext() ) const;
726 
730  void renderDiagram( const QgsFeature &feature, QgsRenderContext &c, QPointF pos, const QgsPropertyCollection &properties = QgsPropertyCollection() ) const;
731 
732  void setDiagram( QgsDiagram *d SIP_TRANSFER );
733  QgsDiagram *diagram() const { return mDiagram.get(); }
734 
736  virtual QList<QgsDiagramSettings> diagramSettings() const = 0;
737 
743  virtual void readXml( const QDomElement &elem, const QgsReadWriteContext &context ) = 0;
744 
750  virtual void writeXml( QDomElement &layerElem, QDomDocument &doc, const QgsReadWriteContext &context ) const = 0;
751 
757  virtual QList< QgsLayerTreeModelLegendNode * > legendItems( QgsLayerTreeLayer *nodeLayer ) const SIP_FACTORY;
758 
764  bool attributeLegend() const { return mShowAttributeLegend; }
765 
772  void setAttributeLegend( bool enabled ) { mShowAttributeLegend = enabled; }
773 
774  protected:
775  QgsDiagramRenderer( const QgsDiagramRenderer &other );
776  QgsDiagramRenderer &operator=( const QgsDiagramRenderer &other );
777 
784  virtual bool diagramSettings( const QgsFeature &feature, const QgsRenderContext &c, QgsDiagramSettings &s ) const = 0;
785 
787  virtual QSizeF diagramSize( const QgsFeature &features, const QgsRenderContext &c ) const = 0;
788 
790  void convertSizeToMapUnits( QSizeF &size, const QgsRenderContext &context ) const;
791 
793  static int dpiPaintDevice( const QPainter * );
794 
795  //read / write diagram
796 
801  void _readXml( const QDomElement &elem, const QgsReadWriteContext &context );
802 
807  void _writeXml( QDomElement &rendererElem, QDomDocument &doc, const QgsReadWriteContext &context ) const;
808 
810  std::unique_ptr< QgsDiagram > mDiagram;
811 
813  bool mShowAttributeLegend = true;
814 };
815 
821 {
822  public:
823 
826 
827  QgsSingleCategoryDiagramRenderer *clone() const override SIP_FACTORY;
828 
829  QString rendererName() const override { return QStringLiteral( "SingleCategory" ); }
830 
831  QList<QString> diagramAttributes() const override { return mSettings.categoryAttributes; }
832 
833  void setDiagramSettings( const QgsDiagramSettings &s ) { mSettings = s; }
834 
835  QList<QgsDiagramSettings> diagramSettings() const override;
836 
837  void readXml( const QDomElement &elem, const QgsReadWriteContext &context ) override;
838  void writeXml( QDomElement &layerElem, QDomDocument &doc, const QgsReadWriteContext &context ) const override;
839 
840  QList< QgsLayerTreeModelLegendNode * > legendItems( QgsLayerTreeLayer *nodeLayer ) const override SIP_FACTORY;
841 
842  protected:
843  bool diagramSettings( const QgsFeature &feature, const QgsRenderContext &c, QgsDiagramSettings &s ) const override;
844 
845  QSizeF diagramSize( const QgsFeature &, const QgsRenderContext &c ) const override;
846 
847  private:
848  QgsDiagramSettings mSettings;
849 };
850 
856 {
857  public:
862 
864 
865  QgsLinearlyInterpolatedDiagramRenderer *clone() const override SIP_FACTORY;
866 
868  QList<QgsDiagramSettings> diagramSettings() const override;
869 
870  void setDiagramSettings( const QgsDiagramSettings &s ) { mSettings = s; }
871 
872  QList<QString> diagramAttributes() const override;
873 
874  QSet< QString > referencedFields( const QgsExpressionContext &context = QgsExpressionContext() ) const override;
875 
876  QString rendererName() const override { return QStringLiteral( "LinearlyInterpolated" ); }
877 
878  void setLowerValue( double val ) { mInterpolationSettings.lowerValue = val; }
879  double lowerValue() const { return mInterpolationSettings.lowerValue; }
880 
881  void setUpperValue( double val ) { mInterpolationSettings.upperValue = val; }
882  double upperValue() const { return mInterpolationSettings.upperValue; }
883 
884  void setLowerSize( QSizeF s ) { mInterpolationSettings.lowerSize = s; }
885  QSizeF lowerSize() const { return mInterpolationSettings.lowerSize; }
886 
887  void setUpperSize( QSizeF s ) { mInterpolationSettings.upperSize = s; }
888  QSizeF upperSize() const { return mInterpolationSettings.upperSize; }
889 
895  QString classificationField() const { return mInterpolationSettings.classificationField; }
896 
902  void setClassificationField( const QString &field ) { mInterpolationSettings.classificationField = field; }
903 
904  QString classificationAttributeExpression() const { return mInterpolationSettings.classificationAttributeExpression; }
905  void setClassificationAttributeExpression( const QString &expression ) { mInterpolationSettings.classificationAttributeExpression = expression; }
906 
907  bool classificationAttributeIsExpression() const { return mInterpolationSettings.classificationAttributeIsExpression; }
908  void setClassificationAttributeIsExpression( bool isExpression ) { mInterpolationSettings.classificationAttributeIsExpression = isExpression; }
909 
910  void readXml( const QDomElement &elem, const QgsReadWriteContext &context ) override;
911  void writeXml( QDomElement &layerElem, QDomDocument &doc, const QgsReadWriteContext &context ) const override;
912 
913  QList< QgsLayerTreeModelLegendNode * > legendItems( QgsLayerTreeLayer *nodeLayer ) const override SIP_FACTORY;
914 
919  void setDataDefinedSizeLegend( QgsDataDefinedSizeLegend *settings SIP_TRANSFER );
920 
925  QgsDataDefinedSizeLegend *dataDefinedSizeLegend() const;
926 
927  protected:
928  bool diagramSettings( const QgsFeature &feature, const QgsRenderContext &c, QgsDiagramSettings &s ) const override;
929 
930  QSizeF diagramSize( const QgsFeature &, const QgsRenderContext &c ) const override;
931 
932  private:
933  QgsDiagramSettings mSettings;
934  QgsDiagramInterpolationSettings mInterpolationSettings;
935 
937  QgsDataDefinedSizeLegend *mDataDefinedSizeLegend = nullptr;
938 };
939 
940 #endif // QGSDIAGRAMRENDERER_H
Class for doing transforms between two map coordinate systems.
Object that keeps configuration of appearance of marker symbol's data-defined size in legend.
Additional diagram settings for interpolated size rendering.
QString classificationField
Name of the field for classification.
Stores the settings for rendering of all diagrams for a layer.
LinePlacementFlag
Line placement flags for controlling line based placements.
void setZIndex(double index)
Sets the diagram z-index.
Placement placement() const
Returns the diagram placement.
QgsCoordinateTransform coordinateTransform() const
Returns the coordinate transform associated with the layer, or an invalid transform if no transformat...
QgsDiagramRenderer * renderer()
Returns the diagram renderer associated with the layer.
Property
Data definable properties.
@ StartAngle
Angle offset for pie diagram.
@ Distance
Distance to diagram from feature.
@ IsObstacle
Whether diagram features act as obstacles for other diagrams/labels.
@ PositionX
X-coordinate data defined diagram position.
@ Priority
Diagram priority (between 0 and 10)
@ AlwaysShow
Whether the diagram should always be shown, even if it overlaps other diagrams/labels.
@ Show
Whether to show the diagram.
@ ZIndex
Z-index for diagram ordering.
@ BackgroundColor
Diagram background color.
@ PositionY
Y-coordinate data defined diagram position.
const QgsPropertyCollection & dataDefinedProperties() const
Returns a reference to the diagram's property collection, used for data defined overrides.
bool showAllDiagrams() const
Returns whether the layer should show all diagrams, including overlapping diagrams.
LinePlacementFlags linePlacementFlags() const
Returns the diagram placement flags.
void setShowAllDiagrams(bool showAllDiagrams)
Sets whether the layer should show all diagrams, including overlapping diagrams.
void setDistance(double distance)
Sets the distance between the diagram and the feature.
void setPriority(int value)
Sets the diagram priority.
int priority() const
Returns the diagram priority.
void setIsObstacle(bool isObstacle)
Sets whether the feature associated with a diagram acts as an obstacle for other labels or diagrams.
bool isObstacle() const
Returns whether the feature associated with a diagram acts as an obstacle for other labels or diagram...
const QgsDiagramRenderer * renderer() const
Returns the diagram renderer associated with the layer.
QgsPropertyCollection & dataDefinedProperties()
Returns a reference to the diagram's property collection, used for data defined overrides.
void setPlacement(Placement value)
Sets the diagram placement.
void setLinePlacementFlags(LinePlacementFlags flags)
Sets the the diagram placement flags.
void setDataDefinedProperties(const QgsPropertyCollection &collection)
Sets the diagram's property collection, used for data defined overrides.
double zIndex() const
Returns the diagram z-index.
double distance() const
Returns the distance between the diagram and the feature (in mm).
Evaluates and returns the diagram settings relating to a diagram for a specific feature.
virtual QString rendererName() const =0
virtual QgsDiagramRenderer * clone() const =0
Returns new instance that is equivalent to this one.
virtual QList< QString > diagramAttributes() const =0
Returns attribute indices needed for diagram rendering.
virtual QSizeF diagramSize(const QgsFeature &features, const QgsRenderContext &c) const =0
Returns size of the diagram (in painter units) or an invalid size in case of error.
void setAttributeLegend(bool enabled)
Sets whether the renderer will show legend items for diagram attributes.
QgsDiagram * diagram() const
virtual bool diagramSettings(const QgsFeature &feature, const QgsRenderContext &c, QgsDiagramSettings &s) const =0
Returns diagram settings for a feature (or false if the diagram for the feature is not to be rendered...
virtual void writeXml(QDomElement &layerElem, QDomDocument &doc, const QgsReadWriteContext &context) const =0
Writes diagram state to a DOM element.
std::unique_ptr< QgsDiagram > mDiagram
Reference to the object that does the real diagram rendering.
virtual ~QgsDiagramRenderer()=default
virtual QList< QgsDiagramSettings > diagramSettings() const =0
Returns list with all diagram settings in the renderer.
QgsDiagramRenderer()=default
Constructor for QgsDiagramRenderer.
virtual void readXml(const QDomElement &elem, const QgsReadWriteContext &context)=0
Reads diagram state from a DOM element.
bool attributeLegend() const
Returns true if renderer will show legend items for diagram attributes.
Stores the settings for rendering a single diagram.
QgsUnitTypes::RenderUnit spacingUnit() const
Returns the units for the content spacing.
DiagramOrientation
Orientation of histogram.
QList< QString > categoryAttributes
QList< QString > categoryLabels
QgsMapUnitScale lineSizeScale
Line unit scale.
double spacing() const
Returns the spacing between diagram contents.
const QgsMapUnitScale & spacingMapUnitScale() const
Returns the map unit scale for the content spacing.
QList< QColor > categoryColors
QgsMapUnitScale sizeScale
Diagram size unit scale.
void setSpacing(double spacing)
Sets the spacing between diagram contents.
void setSpacingMapUnitScale(const QgsMapUnitScale &scale)
Sets the map unit scale for the content spacing.
Direction
Angular directions.
@ Counterclockwise
Counter-clockwise orientation.
@ Clockwise
Clockwise orientation.
void setSpacingUnit(QgsUnitTypes::RenderUnit unit)
Sets the unit for the content spacing.
Base class for all diagram types.
Definition: qgsdiagram.h:40
Expression contexts are used to encapsulate the parameters around which a QgsExpression should be eva...
The feature class encapsulates a single feature including its unique ID, geometry and a list of field...
Definition: qgsfeature.h:56
Layer tree node points to a map layer.
The QgsLegendRendererItem class is abstract interface for legend items returned from QgsMapLayerLegen...
A line symbol type, for rendering LineString and MultiLineString geometries.
Definition: qgslinesymbol.h:30
void setDiagramSettings(const QgsDiagramSettings &s)
QString classificationField() const
Returns the field name used for interpolating the diagram size.
void setClassificationAttributeExpression(const QString &expression)
void setClassificationField(const QString &field)
Sets the field name used for interpolating the diagram size.
void setClassificationAttributeIsExpression(bool isExpression)
Perform transforms between map coordinates and device coordinates.
Definition: qgsmaptopixel.h:39
Struct for storing maximum and minimum scales for measurements in map units.
Base class for visual effects which can be applied to QPicture drawings.
A grouped map of multiple QgsProperty objects, each referenced by a integer key value.
The class is used as a container of context for various read/write operations on other objects.
Contains information about the context of a rendering operation.
Renders the diagrams for all features with the same settings.
QList< QString > diagramAttributes() const override
Returns attribute indices needed for diagram rendering.
void setDiagramSettings(const QgsDiagramSettings &s)
QgsSingleCategoryDiagramRenderer()=default
Constructor for QgsSingleCategoryDiagramRenderer.
RenderUnit
Rendering size units.
Definition: qgsunittypes.h:168
@ RenderMillimeters
Millimeters.
Definition: qgsunittypes.h:169
Represents a vector layer which manages a vector based data sets.
A set of features which influence the labeling process.
Definition: layer.h:62
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
#define SIP_CONVERT_TO_SUBCLASS_CODE(code)
Definition: qgis_sip.h:177
#define SIP_SKIP
Definition: qgis_sip.h:126
#define SIP_TRANSFER
Definition: qgis_sip.h:36
#define SIP_FACTORY
Definition: qgis_sip.h:76
#define SIP_END
Definition: qgis_sip.h:194
const QgsField & field
Definition: qgsfield.h:463
QMap< int, QgsPropertyDefinition > QgsPropertiesDefinition
Definition of available properties.