QGIS API Documentation  3.18.1-Zürich (202f1bf7e5)
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"
33 
34 #include "diagram/qgsdiagram.h"
35 #include "qgsreadwritecontext.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 
49 namespace pal { class Layer; } SIP_SKIP
50 
60 class CORE_EXPORT QgsDiagramLayerSettings
61 {
62  public:
63 
64  //avoid inclusion of QgsPalLabeling
65  enum Placement
66  {
67  AroundPoint = 0, // Point / Polygon
68  OverPoint, // Point / Polygon
69  Line, // Line / Polygon
70  Curved, // Line
71  Horizontal, // Polygon
72  Free // Polygon
73  };
74 
77  {
78  OnLine = 1,
79  AboveLine = 1 << 1,
80  BelowLine = 1 << 2,
81  MapOrientation = 1 << 4,
82  };
83  Q_DECLARE_FLAGS( LinePlacementFlags, LinePlacementFlag )
84 
85 
89  enum Property
90  {
103  };
104 
109  static const QgsPropertiesDefinition &propertyDefinitions();
110 
115 
118 
119  QgsDiagramLayerSettings &operator=( const QgsDiagramLayerSettings &rh );
120 
122 
128  Placement placement() const { return mPlacement; }
129 
136  void setPlacement( Placement value ) { mPlacement = value; }
137 
144  LinePlacementFlags linePlacementFlags() const { return mPlacementFlags; }
145 
153  void setLinePlacementFlags( LinePlacementFlags flags ) { mPlacementFlags = flags; }
154 
163  int priority() const { return mPriority; }
164 
171  void setPriority( int value ) { mPriority = value; }
172 
181  double zIndex() const { return mZIndex; }
182 
190  void setZIndex( double index ) { mZIndex = index; }
191 
197  bool isObstacle() const { return mObstacle; }
198 
205  void setIsObstacle( bool isObstacle ) { mObstacle = isObstacle; }
206 
212  double distance() const { return mDistance; }
213 
220  void setDistance( double distance ) { mDistance = distance; }
221 
227  QgsDiagramRenderer *renderer() { return mRenderer; }
228 
235  const QgsDiagramRenderer *renderer() const { return mRenderer; } SIP_SKIP
236 
243  void setRenderer( QgsDiagramRenderer *diagramRenderer SIP_TRANSFER );
244 
252 
259  void setCoordinateTransform( const QgsCoordinateTransform &transform );
260 
266  bool showAllDiagrams() const { return mShowAll; }
267 
274  void setShowAllDiagrams( bool showAllDiagrams ) { mShowAll = showAllDiagrams; }
275 
280  void readXml( const QDomElement &elem );
281 
286  void writeXml( QDomElement &layerElem, QDomDocument &doc ) const;
287 
294  bool prepare( const QgsExpressionContext &context = QgsExpressionContext() ) const;
295 
301  QSet< QString > referencedFields( const QgsExpressionContext &context = QgsExpressionContext() ) const;
302 
308  QgsPropertyCollection &dataDefinedProperties() { return mDataDefinedProperties; }
309 
317  const QgsPropertyCollection &dataDefinedProperties() const { return mDataDefinedProperties; } SIP_SKIP
318 
326  void setDataDefinedProperties( const QgsPropertyCollection &collection ) { mDataDefinedProperties = collection; }
327 
328  private:
329 
332 
334  Placement mPlacement = AroundPoint;
335 
337  LinePlacementFlags mPlacementFlags = OnLine;
338 
344  int mPriority = 5;
345 
347  double mZIndex = 0.0;
348 
350  bool mObstacle = false;
351 
353  double mDistance = 0.0;
354 
356  QgsDiagramRenderer *mRenderer = nullptr;
357 
359  bool mShowAll = true;
360 
362  QgsPropertyCollection mDataDefinedProperties;
363 
364  static void initPropertyDefinitions();
365 
367  static QgsPropertiesDefinition sPropertyDefinitions;
368 
369 };
370 
380 class CORE_EXPORT QgsDiagramSettings
381 {
382  public:
383 
385  {
387  XHeight
388  };
389 
392  {
393  Up,
396  Right
397  };
398 
404  {
407  };
408 
412 
414  QgsDiagramSettings( const QgsDiagramSettings &other );
415 
416  QgsDiagramSettings &operator=( const QgsDiagramSettings &other );
417 
418  bool enabled = true;
419  QFont font;
420  QList< QColor > categoryColors;
421  QList< QString > categoryAttributes;
423  QList< QString > categoryLabels;
424  QSizeF size; //size
425 
430 
436 
442 
448 
450  QColor penColor;
451  double penWidth = 0.0;
454  double barWidth = 5.0;
455 
457  double opacity = 1.0;
458 
459  bool scaleByArea = true;
460 
465  double rotationOffset = 270;
466 
467  bool scaleBasedVisibility = false;
468 
475  double maximumScale = 0;
476 
483  double minimumScale = 0;
484 
486  double minimumSize = 0.0;
487 
499  double spacing() const { return mSpacing; }
500 
512  void setSpacing( double spacing ) { mSpacing = spacing; }
513 
522  void setSpacingUnit( QgsUnitTypes::RenderUnit unit ) { mSpacingUnit = unit; }
523 
531  QgsUnitTypes::RenderUnit spacingUnit() const { return mSpacingUnit; }
532 
541  void setSpacingMapUnitScale( const QgsMapUnitScale &scale ) { mSpacingMapUnitScale = scale; }
542 
551  const QgsMapUnitScale &spacingMapUnitScale() const { return mSpacingMapUnitScale; }
552 
559  Direction direction() const;
560 
567  void setDirection( Direction direction );
568 
570  void readXml( const QDomElement &elem, const QgsReadWriteContext &context = QgsReadWriteContext() );
572  void writeXml( QDomElement &rendererElem, QDomDocument &doc, const QgsReadWriteContext &context = QgsReadWriteContext() ) const;
573 
579  QList< QgsLayerTreeModelLegendNode * > legendItems( QgsLayerTreeLayer *nodeLayer ) const SIP_FACTORY;
580 
589  QgsLineSymbol *axisLineSymbol() const;
590 
601  void setAxisLineSymbol( QgsLineSymbol *symbol SIP_TRANSFER );
602 
611  bool showAxis() const;
612 
621  void setShowAxis( bool showAxis );
622 
630  QgsPaintEffect *paintEffect() const;
631 
641  void setPaintEffect( QgsPaintEffect *effect SIP_TRANSFER );
642 
643  private:
644 
645  double mSpacing = 0;
647  QgsMapUnitScale mSpacingMapUnitScale;
648  Direction mDirection = Counterclockwise;
649 
650  bool mShowAxis = false;
651  std::unique_ptr< QgsLineSymbol > mAxisLineSymbol;
652  std::unique_ptr< QgsPaintEffect > mPaintEffect;
653 
654 };
655 
662 {
663  public:
664  QSizeF lowerSize;
665  QSizeF upperSize;
666  double lowerValue;
667  double upperValue;
668 
671 
674 };
675 
676 
683 class CORE_EXPORT QgsDiagramRenderer
684 {
685 
686 #ifdef SIP_RUN
688  if ( sipCpp->rendererName() == QLatin1String( "SingleCategory" ) )
689  sipType = sipType_QgsSingleCategoryDiagramRenderer;
690  else if ( sipCpp->rendererName() == QLatin1String( "LinearlyInterpolated" ) )
691  sipType = sipType_QgsLinearlyInterpolatedDiagramRenderer;
692  else
693  sipType = NULL;
694  SIP_END
695 #endif
696 
697  public:
698 
702  QgsDiagramRenderer() = default;
703  virtual ~QgsDiagramRenderer() = default;
704 
709  virtual QgsDiagramRenderer *clone() const = 0 SIP_FACTORY;
710 
712  virtual QSizeF sizeMapUnits( const QgsFeature &feature, const QgsRenderContext &c ) const;
713 
714  virtual QString rendererName() const = 0;
715 
717  virtual QList<QString> diagramAttributes() const = 0;
718 
724  virtual QSet< QString > referencedFields( const QgsExpressionContext &context = QgsExpressionContext() ) const;
725 
729  void renderDiagram( const QgsFeature &feature, QgsRenderContext &c, QPointF pos, const QgsPropertyCollection &properties = QgsPropertyCollection() ) const;
730 
731  void setDiagram( QgsDiagram *d SIP_TRANSFER );
732  QgsDiagram *diagram() const { return mDiagram.get(); }
733 
735  virtual QList<QgsDiagramSettings> diagramSettings() const = 0;
736 
742  virtual void readXml( const QDomElement &elem, const QgsReadWriteContext &context ) = 0;
743 
749  virtual void writeXml( QDomElement &layerElem, QDomDocument &doc, const QgsReadWriteContext &context ) const = 0;
750 
756  virtual QList< QgsLayerTreeModelLegendNode * > legendItems( QgsLayerTreeLayer *nodeLayer ) const SIP_FACTORY;
757 
763  bool attributeLegend() const { return mShowAttributeLegend; }
764 
771  void setAttributeLegend( bool enabled ) { mShowAttributeLegend = enabled; }
772 
773  protected:
774  QgsDiagramRenderer( const QgsDiagramRenderer &other );
775  QgsDiagramRenderer &operator=( const QgsDiagramRenderer &other );
776 
783  virtual bool diagramSettings( const QgsFeature &feature, const QgsRenderContext &c, QgsDiagramSettings &s ) const = 0;
784 
786  virtual QSizeF diagramSize( const QgsFeature &features, const QgsRenderContext &c ) const = 0;
787 
789  void convertSizeToMapUnits( QSizeF &size, const QgsRenderContext &context ) const;
790 
792  static int dpiPaintDevice( const QPainter * );
793 
794  //read / write diagram
795 
800  void _readXml( const QDomElement &elem, const QgsReadWriteContext &context );
801 
806  void _writeXml( QDomElement &rendererElem, QDomDocument &doc, const QgsReadWriteContext &context ) const;
807 
809  std::unique_ptr< QgsDiagram > mDiagram;
810 
812  bool mShowAttributeLegend = true;
813 };
814 
820 {
821  public:
822 
825 
826  QgsSingleCategoryDiagramRenderer *clone() const override SIP_FACTORY;
827 
828  QString rendererName() const override { return QStringLiteral( "SingleCategory" ); }
829 
830  QList<QString> diagramAttributes() const override { return mSettings.categoryAttributes; }
831 
832  void setDiagramSettings( const QgsDiagramSettings &s ) { mSettings = s; }
833 
834  QList<QgsDiagramSettings> diagramSettings() const override;
835 
836  void readXml( const QDomElement &elem, const QgsReadWriteContext &context ) override;
837  void writeXml( QDomElement &layerElem, QDomDocument &doc, const QgsReadWriteContext &context ) const override;
838 
839  QList< QgsLayerTreeModelLegendNode * > legendItems( QgsLayerTreeLayer *nodeLayer ) const override SIP_FACTORY;
840 
841  protected:
842  bool diagramSettings( const QgsFeature &feature, const QgsRenderContext &c, QgsDiagramSettings &s ) const override;
843 
844  QSizeF diagramSize( const QgsFeature &, const QgsRenderContext &c ) const override;
845 
846  private:
847  QgsDiagramSettings mSettings;
848 };
849 
855 {
856  public:
859 
860  QgsLinearlyInterpolatedDiagramRenderer *clone() const override SIP_FACTORY;
861 
863  QList<QgsDiagramSettings> diagramSettings() const override;
864 
865  void setDiagramSettings( const QgsDiagramSettings &s ) { mSettings = s; }
866 
867  QList<QString> diagramAttributes() const override;
868 
869  QSet< QString > referencedFields( const QgsExpressionContext &context = QgsExpressionContext() ) const override;
870 
871  QString rendererName() const override { return QStringLiteral( "LinearlyInterpolated" ); }
872 
873  void setLowerValue( double val ) { mInterpolationSettings.lowerValue = val; }
874  double lowerValue() const { return mInterpolationSettings.lowerValue; }
875 
876  void setUpperValue( double val ) { mInterpolationSettings.upperValue = val; }
877  double upperValue() const { return mInterpolationSettings.upperValue; }
878 
879  void setLowerSize( QSizeF s ) { mInterpolationSettings.lowerSize = s; }
880  QSizeF lowerSize() const { return mInterpolationSettings.lowerSize; }
881 
882  void setUpperSize( QSizeF s ) { mInterpolationSettings.upperSize = s; }
883  QSizeF upperSize() const { return mInterpolationSettings.upperSize; }
884 
890  QString classificationField() const { return mInterpolationSettings.classificationField; }
891 
897  void setClassificationField( const QString &field ) { mInterpolationSettings.classificationField = field; }
898 
899  QString classificationAttributeExpression() const { return mInterpolationSettings.classificationAttributeExpression; }
900  void setClassificationAttributeExpression( const QString &expression ) { mInterpolationSettings.classificationAttributeExpression = expression; }
901 
902  bool classificationAttributeIsExpression() const { return mInterpolationSettings.classificationAttributeIsExpression; }
903  void setClassificationAttributeIsExpression( bool isExpression ) { mInterpolationSettings.classificationAttributeIsExpression = isExpression; }
904 
905  void readXml( const QDomElement &elem, const QgsReadWriteContext &context ) override;
906  void writeXml( QDomElement &layerElem, QDomDocument &doc, const QgsReadWriteContext &context ) const override;
907 
908  QList< QgsLayerTreeModelLegendNode * > legendItems( QgsLayerTreeLayer *nodeLayer ) const override SIP_FACTORY;
909 
914  void setDataDefinedSizeLegend( QgsDataDefinedSizeLegend *settings SIP_TRANSFER );
915 
920  QgsDataDefinedSizeLegend *dataDefinedSizeLegend() const;
921 
922  protected:
923  bool diagramSettings( const QgsFeature &feature, const QgsRenderContext &c, QgsDiagramSettings &s ) const override;
924 
925  QSizeF diagramSize( const QgsFeature &, const QgsRenderContext &c ) const override;
926 
929 
930  private:
931  QgsDiagramSettings mSettings;
932  QgsDiagramInterpolationSettings mInterpolationSettings;
933 
935  QgsDataDefinedSizeLegend *mDataDefinedSizeLegend = nullptr;
936 
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 id, geometry and a list of field/values...
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: qgssymbol.h:1204
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:167
@ RenderMillimeters
Millimeters.
Definition: qgsunittypes.h:168
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:472
QMap< int, QgsPropertyDefinition > QgsPropertiesDefinition
Definition of available properties.