QGIS API Documentation  3.13.0-Master (9729a43df7)
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 #include "qgsreadwritecontext.h"
37 
38 class QgsDiagramRenderer;
39 class QgsFeature;
40 class QgsRenderContext;
41 class QDomElement;
42 class QgsMapToPixel;
44 class QgsVectorLayer;
46 class QgsLayerTreeLayer;
47 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() == QStringLiteral( "SingleCategory" ) )
689  sipType = sipType_QgsSingleCategoryDiagramRenderer;
690  else if ( sipCpp->rendererName() == QStringLiteral( "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 
708  virtual QgsDiagramRenderer *clone() const = 0 SIP_FACTORY;
709 
711  virtual QSizeF sizeMapUnits( const QgsFeature &feature, const QgsRenderContext &c ) const;
712 
713  virtual QString rendererName() const = 0;
714 
716  virtual QList<QString> diagramAttributes() const = 0;
717 
723  virtual QSet< QString > referencedFields( const QgsExpressionContext &context = QgsExpressionContext() ) const;
724 
728  void renderDiagram( const QgsFeature &feature, QgsRenderContext &c, QPointF pos, const QgsPropertyCollection &properties = QgsPropertyCollection() ) const;
729 
730  void setDiagram( QgsDiagram *d SIP_TRANSFER );
731  QgsDiagram *diagram() const { return mDiagram.get(); }
732 
734  virtual QList<QgsDiagramSettings> diagramSettings() const = 0;
735 
741  virtual void readXml( const QDomElement &elem, const QgsReadWriteContext &context ) = 0;
742 
748  virtual void writeXml( QDomElement &layerElem, QDomDocument &doc, const QgsReadWriteContext &context ) const = 0;
749 
755  virtual QList< QgsLayerTreeModelLegendNode * > legendItems( QgsLayerTreeLayer *nodeLayer ) const SIP_FACTORY;
756 
762  bool attributeLegend() const { return mShowAttributeLegend; }
763 
770  void setAttributeLegend( bool enabled ) { mShowAttributeLegend = enabled; }
771 
772  protected:
773  QgsDiagramRenderer( const QgsDiagramRenderer &other );
774  QgsDiagramRenderer &operator=( const QgsDiagramRenderer &other );
775 
782  virtual bool diagramSettings( const QgsFeature &feature, const QgsRenderContext &c, QgsDiagramSettings &s ) const = 0;
783 
785  virtual QSizeF diagramSize( const QgsFeature &features, const QgsRenderContext &c ) const = 0;
786 
788  void convertSizeToMapUnits( QSizeF &size, const QgsRenderContext &context ) const;
789 
791  static int dpiPaintDevice( const QPainter * );
792 
793  //read / write diagram
794 
799  void _readXml( const QDomElement &elem, const QgsReadWriteContext &context );
800 
805  void _writeXml( QDomElement &rendererElem, QDomDocument &doc, const QgsReadWriteContext &context ) const;
806 
808  std::unique_ptr< QgsDiagram > mDiagram;
809 
811  bool mShowAttributeLegend = true;
812 };
813 
819 {
820  public:
821 
824 
825  QgsSingleCategoryDiagramRenderer *clone() const override SIP_FACTORY;
826 
827  QString rendererName() const override { return QStringLiteral( "SingleCategory" ); }
828 
829  QList<QString> diagramAttributes() const override { return mSettings.categoryAttributes; }
830 
831  void setDiagramSettings( const QgsDiagramSettings &s ) { mSettings = s; }
832 
833  QList<QgsDiagramSettings> diagramSettings() const override;
834 
835  void readXml( const QDomElement &elem, const QgsReadWriteContext &context ) override;
836  void writeXml( QDomElement &layerElem, QDomDocument &doc, const QgsReadWriteContext &context ) const override;
837 
838  QList< QgsLayerTreeModelLegendNode * > legendItems( QgsLayerTreeLayer *nodeLayer ) const override SIP_FACTORY;
839 
840  protected:
841  bool diagramSettings( const QgsFeature &feature, const QgsRenderContext &c, QgsDiagramSettings &s ) const override;
842 
843  QSizeF diagramSize( const QgsFeature &, const QgsRenderContext &c ) const override;
844 
845  private:
846  QgsDiagramSettings mSettings;
847 };
848 
854 {
855  public:
858 
859  QgsLinearlyInterpolatedDiagramRenderer *clone() const override SIP_FACTORY;
860 
862  QList<QgsDiagramSettings> diagramSettings() const override;
863 
864  void setDiagramSettings( const QgsDiagramSettings &s ) { mSettings = s; }
865 
866  QList<QString> diagramAttributes() const override;
867 
868  QSet< QString > referencedFields( const QgsExpressionContext &context = QgsExpressionContext() ) const override;
869 
870  QString rendererName() const override { return QStringLiteral( "LinearlyInterpolated" ); }
871 
872  void setLowerValue( double val ) { mInterpolationSettings.lowerValue = val; }
873  double lowerValue() const { return mInterpolationSettings.lowerValue; }
874 
875  void setUpperValue( double val ) { mInterpolationSettings.upperValue = val; }
876  double upperValue() const { return mInterpolationSettings.upperValue; }
877 
878  void setLowerSize( QSizeF s ) { mInterpolationSettings.lowerSize = s; }
879  QSizeF lowerSize() const { return mInterpolationSettings.lowerSize; }
880 
881  void setUpperSize( QSizeF s ) { mInterpolationSettings.upperSize = s; }
882  QSizeF upperSize() const { return mInterpolationSettings.upperSize; }
883 
889  QString classificationField() const { return mInterpolationSettings.classificationField; }
890 
896  void setClassificationField( const QString &field ) { mInterpolationSettings.classificationField = field; }
897 
898  QString classificationAttributeExpression() const { return mInterpolationSettings.classificationAttributeExpression; }
899  void setClassificationAttributeExpression( const QString &expression ) { mInterpolationSettings.classificationAttributeExpression = expression; }
900 
901  bool classificationAttributeIsExpression() const { return mInterpolationSettings.classificationAttributeIsExpression; }
902  void setClassificationAttributeIsExpression( bool isExpression ) { mInterpolationSettings.classificationAttributeIsExpression = isExpression; }
903 
904  void readXml( const QDomElement &elem, const QgsReadWriteContext &context ) override;
905  void writeXml( QDomElement &layerElem, QDomDocument &doc, const QgsReadWriteContext &context ) const override;
906 
907  QList< QgsLayerTreeModelLegendNode * > legendItems( QgsLayerTreeLayer *nodeLayer ) const override SIP_FACTORY;
908 
913  void setDataDefinedSizeLegend( QgsDataDefinedSizeLegend *settings SIP_TRANSFER );
914 
919  QgsDataDefinedSizeLegend *dataDefinedSizeLegend() const;
920 
921  protected:
922  bool diagramSettings( const QgsFeature &feature, const QgsRenderContext &c, QgsDiagramSettings &s ) const override;
923 
924  QSizeF diagramSize( const QgsFeature &, const QgsRenderContext &c ) const override;
925 
928 
929  private:
930  QgsDiagramSettings mSettings;
931  QgsDiagramInterpolationSettings mInterpolationSettings;
932 
934  QgsDataDefinedSizeLegend *mDataDefinedSizeLegend = nullptr;
935 };
936 
937 #endif // QGSDIAGRAMRENDERER_H
void setClassificationAttributeIsExpression(bool isExpression)
The class is used as a container of context for various read/write operations on other objects...
Distance to diagram from feature.
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...
const QgsMapUnitScale & spacingMapUnitScale() const
Returns the map unit scale for the content spacing.
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.
QgsUnitTypes::RenderUnit spacingUnit() const
Returns the units for the content spacing.
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:61
QgsPropertyCollection & dataDefinedProperties()
Returns a reference to the diagram&#39;s property collection, used for data defined overrides.
Base class for visual effects which can be applied to QPicture drawings.
Angle offset for pie diagram.
void setZIndex(double index)
Sets the diagram z-index.
void setSpacingMapUnitScale(const QgsMapUnitScale &scale)
Sets the map unit scale for the content spacing.
The feature class encapsulates a single feature including its id, geometry and a list of field/values...
Definition: qgsfeature.h:55
Whether diagram features act as obstacles for other diagrams/labels.
A line symbol type, for rendering LineString and MultiLineString geometries.
Definition: qgssymbol.h:1117
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
Z-index for diagram ordering.
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:126
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:189
QgsMapUnitScale sizeScale
Diagram size unit scale.
void setSpacingUnit(QgsUnitTypes::RenderUnit unit)
Sets the unit for the content spacing.
DiagramOrientation
Orientation of histogram.
double spacing() const
Returns the spacing between diagram contents.
void setClassificationField(const QString &field)
Sets the field name used for interpolating the diagram size.
Y-coordinate data defined diagram position.
#define SIP_FACTORY
Definition: qgis_sip.h:76
Whether to show the diagram.
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.
Direction
Angular directions.
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.
X-coordinate data defined diagram position.
bool attributeLegend() const
Returns true if renderer will show legend items for diagram attributes.
Diagram priority (between 0 and 10)
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:172
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 the diagram should always be shown, even if it overlaps other diagrams/labels.
LinePlacementFlag
Line placement flags for controlling line based placements.
QgsMapUnitScale lineSizeScale
Line unit scale.
Counter-clockwise orientation.
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.
void setSpacing(double spacing)
Sets the spacing between diagram contents.
RenderUnit
Rendering size units.
Definition: qgsunittypes.h:166
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.