QGIS API Documentation 3.32.0-Lima (311a8cb8a6)
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
30
31#include "qgsdiagram.h"
32#include "qgsreadwritecontext.h"
33#include "qgsmapunitscale.h"
34
36class QgsFeature;
38class QDomElement;
39class QgsMapToPixel;
41class QgsVectorLayer;
44class QgsPaintEffect;
46class QgsLineSymbol;
47
48namespace pal { class Layer; } SIP_SKIP
49
59class CORE_EXPORT QgsDiagramLayerSettings
60{
61 public:
62
63 //avoid inclusion of QgsPalLabeling
65 {
66 AroundPoint = 0, // Point / Polygon
67 OverPoint, // Point / Polygon
68 Line, // Line / Polygon
69 Curved, // Line
70 Horizontal, // Polygon
71 Free // Polygon
72 };
73
76 {
77 OnLine = 1,
78 AboveLine = 1 << 1,
79 BelowLine = 1 << 2,
80 MapOrientation = 1 << 4,
81 };
82 Q_DECLARE_FLAGS( LinePlacementFlags, LinePlacementFlag )
83
84
89 {
102 };
103
108 static const QgsPropertiesDefinition &propertyDefinitions();
109
114
117
118 QgsDiagramLayerSettings &operator=( const QgsDiagramLayerSettings &rh );
119
121
127 Placement placement() const { return mPlacement; }
128
135 void setPlacement( Placement value ) { mPlacement = value; }
136
143 LinePlacementFlags linePlacementFlags() const { return mPlacementFlags; }
144
152 void setLinePlacementFlags( LinePlacementFlags flags ) { mPlacementFlags = flags; }
153
162 int priority() const { return mPriority; }
163
170 void setPriority( int value ) { mPriority = value; }
171
180 double zIndex() const { return mZIndex; }
181
189 void setZIndex( double index ) { mZIndex = index; }
190
196 bool isObstacle() const { return mObstacle; }
197
204 void setIsObstacle( bool isObstacle ) { mObstacle = isObstacle; }
205
211 double distance() const { return mDistance; }
212
219 void setDistance( double distance ) { mDistance = distance; }
220
226 QgsDiagramRenderer *renderer() { return mRenderer; }
227
234 const QgsDiagramRenderer *renderer() const { return mRenderer; } SIP_SKIP
235
242 void setRenderer( QgsDiagramRenderer *diagramRenderer SIP_TRANSFER );
243
251
258 void setCoordinateTransform( const QgsCoordinateTransform &transform );
259
265 bool showAllDiagrams() const { return mShowAll; }
266
273 void setShowAllDiagrams( bool showAllDiagrams ) { mShowAll = showAllDiagrams; }
274
279 void readXml( const QDomElement &elem );
280
285 void writeXml( QDomElement &layerElem, QDomDocument &doc ) const;
286
293 bool prepare( const QgsExpressionContext &context = QgsExpressionContext() ) const;
294
300 QSet< QString > referencedFields( const QgsExpressionContext &context = QgsExpressionContext() ) const;
301
307 QgsPropertyCollection &dataDefinedProperties() { return mDataDefinedProperties; }
308
316 const QgsPropertyCollection &dataDefinedProperties() const { return mDataDefinedProperties; } SIP_SKIP
317
325 void setDataDefinedProperties( const QgsPropertyCollection &collection ) { mDataDefinedProperties = collection; }
326
327 private:
328
331
333 Placement mPlacement = AroundPoint;
334
336 LinePlacementFlags mPlacementFlags = OnLine;
337
343 int mPriority = 5;
344
346 double mZIndex = 0.0;
347
349 bool mObstacle = false;
350
352 double mDistance = 0.0;
353
355 QgsDiagramRenderer *mRenderer = nullptr;
356
358 bool mShowAll = true;
359
361 QgsPropertyCollection mDataDefinedProperties;
362
363 static void initPropertyDefinitions();
364
366 static QgsPropertiesDefinition sPropertyDefinitions;
367
368};
369
379class CORE_EXPORT QgsDiagramSettings
380{
381 public:
382
384 {
386 XHeight
387 };
388
391 {
395 Right
396 };
397
403 {
406 };
407
411
414
415 QgsDiagramSettings &operator=( const QgsDiagramSettings &other );
416
417 bool enabled = true;
418 QFont font;
419 QList< QColor > categoryColors;
420 QList< QString > categoryAttributes;
422 QList< QString > categoryLabels;
423 QSizeF size; //size
424
428 Qgis::RenderUnit sizeType = Qgis::RenderUnit::Millimeters;
429
435
440 Qgis::RenderUnit lineSizeUnit = Qgis::RenderUnit::Millimeters;
441
447
449 QColor penColor;
450 double penWidth = 0.0;
453 double barWidth = 5.0;
454
456 double opacity = 1.0;
457
458 bool scaleByArea = true;
459
464 double rotationOffset = 270;
465
466 bool scaleBasedVisibility = false;
467
474 double maximumScale = 0;
475
482 double minimumScale = 0;
483
485 double minimumSize = 0.0;
486
498 double spacing() const { return mSpacing; }
499
511 void setSpacing( double spacing ) { mSpacing = spacing; }
512
521 void setSpacingUnit( Qgis::RenderUnit unit ) { mSpacingUnit = unit; }
522
530 Qgis::RenderUnit spacingUnit() const { return mSpacingUnit; }
531
540 void setSpacingMapUnitScale( const QgsMapUnitScale &scale ) { mSpacingMapUnitScale = scale; }
541
550 const QgsMapUnitScale &spacingMapUnitScale() const { return mSpacingMapUnitScale; }
551
558 Direction direction() const;
559
566 void setDirection( Direction direction );
567
569 void readXml( const QDomElement &elem, const QgsReadWriteContext &context = QgsReadWriteContext() );
571 void writeXml( QDomElement &rendererElem, QDomDocument &doc, const QgsReadWriteContext &context = QgsReadWriteContext() ) const;
572
578 QList< QgsLayerTreeModelLegendNode * > legendItems( QgsLayerTreeLayer *nodeLayer ) const SIP_FACTORY;
579
588 QgsLineSymbol *axisLineSymbol() const;
589
600 void setAxisLineSymbol( QgsLineSymbol *symbol SIP_TRANSFER );
601
610 bool showAxis() const;
611
620 void setShowAxis( bool showAxis );
621
629 QgsPaintEffect *paintEffect() const;
630
640 void setPaintEffect( QgsPaintEffect *effect SIP_TRANSFER );
641
642 private:
643
644 double mSpacing = 0;
645 Qgis::RenderUnit mSpacingUnit = Qgis::RenderUnit::Millimeters;
646 QgsMapUnitScale mSpacingMapUnitScale;
647 Direction mDirection = Counterclockwise;
648
649 bool mShowAxis = false;
650 std::unique_ptr< QgsLineSymbol > mAxisLineSymbol;
651 std::unique_ptr< QgsPaintEffect > mPaintEffect;
652
653};
654
661{
662 public:
663 QSizeF lowerSize;
664 QSizeF upperSize;
667
670
673};
674
675
682class CORE_EXPORT QgsDiagramRenderer
683{
684
685#ifdef SIP_RUN
687 if ( sipCpp->rendererName() == QLatin1String( "SingleCategory" ) )
688 sipType = sipType_QgsSingleCategoryDiagramRenderer;
689 else if ( sipCpp->rendererName() == QLatin1String( "LinearlyInterpolated" ) )
690 sipType = sipType_QgsLinearlyInterpolatedDiagramRenderer;
691 else
692 sipType = NULL;
693 SIP_END
694#endif
695
696 public:
697
702 virtual ~QgsDiagramRenderer() = default;
703
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:
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:
860
862
864
866 QList<QgsDiagramSettings> diagramSettings() const override;
867
868 void setDiagramSettings( const QgsDiagramSettings &s ) { mSettings = s; }
869
870 QList<QString> diagramAttributes() const override;
871
872 QSet< QString > referencedFields( const QgsExpressionContext &context = QgsExpressionContext() ) const override;
873
874 QString rendererName() const override { return QStringLiteral( "LinearlyInterpolated" ); }
875
876 void setLowerValue( double val ) { mInterpolationSettings.lowerValue = val; }
877 double lowerValue() const { return mInterpolationSettings.lowerValue; }
878
879 void setUpperValue( double val ) { mInterpolationSettings.upperValue = val; }
880 double upperValue() const { return mInterpolationSettings.upperValue; }
881
882 void setLowerSize( QSizeF s ) { mInterpolationSettings.lowerSize = s; }
883 QSizeF lowerSize() const { return mInterpolationSettings.lowerSize; }
884
885 void setUpperSize( QSizeF s ) { mInterpolationSettings.upperSize = s; }
886 QSizeF upperSize() const { return mInterpolationSettings.upperSize; }
887
893 QString classificationField() const { return mInterpolationSettings.classificationField; }
894
900 void setClassificationField( const QString &field ) { mInterpolationSettings.classificationField = field; }
901
902 QString classificationAttributeExpression() const { return mInterpolationSettings.classificationAttributeExpression; }
903 void setClassificationAttributeExpression( const QString &expression ) { mInterpolationSettings.classificationAttributeExpression = expression; }
904
905 bool classificationAttributeIsExpression() const { return mInterpolationSettings.classificationAttributeIsExpression; }
906 void setClassificationAttributeIsExpression( bool isExpression ) { mInterpolationSettings.classificationAttributeIsExpression = isExpression; }
907
908 void readXml( const QDomElement &elem, const QgsReadWriteContext &context ) override;
909 void writeXml( QDomElement &layerElem, QDomDocument &doc, const QgsReadWriteContext &context ) const override;
910
911 QList< QgsLayerTreeModelLegendNode * > legendItems( QgsLayerTreeLayer *nodeLayer ) const override SIP_FACTORY;
912
917 void setDataDefinedSizeLegend( QgsDataDefinedSizeLegend *settings SIP_TRANSFER );
918
923 QgsDataDefinedSizeLegend *dataDefinedSizeLegend() const;
924
925 protected:
926 bool diagramSettings( const QgsFeature &feature, const QgsRenderContext &c, QgsDiagramSettings &s ) const override;
927
928 QSizeF diagramSize( const QgsFeature &, const QgsRenderContext &c ) const override;
929
930 private:
931 QgsDiagramSettings mSettings;
932 QgsDiagramInterpolationSettings mInterpolationSettings;
933
935 QgsDataDefinedSizeLegend *mDataDefinedSizeLegend = nullptr;
936};
937
938#endif // QGSDIAGRAMRENDERER_H
RenderUnit
Rendering size units.
Definition: qgis.h:3441
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...
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.
bool showAllDiagrams() const
Returns whether the layer should show all diagrams, including overlapping diagrams.
QgsPropertyCollection & dataDefinedProperties()
Returns a reference to the diagram's property collection, used for data defined overrides.
LinePlacementFlags linePlacementFlags() const
Returns the diagram placement flags.
const QgsPropertyCollection & dataDefinedProperties() const
Returns a reference to the diagram's property collection, used for data defined overrides.
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.
const QgsDiagramRenderer * renderer() const
Returns the diagram renderer associated with the layer.
void setIsObstacle(bool isObstacle)
Sets whether the feature associated with a diagram acts as an obstacle for other labels or diagrams.
QgsDiagramRenderer * renderer()
Returns the diagram renderer associated with the layer.
bool isObstacle() const
Returns whether the feature associated with a diagram acts as an obstacle for other labels or diagram...
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
QgsDiagram * diagram() const
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.
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.
virtual QList< QString > diagramAttributes() const =0
Returns attribute indices needed for diagram rendering.
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.
virtual QgsDiagramRenderer * clone() const =0
Returns new instance that is equivalent to this one.
Stores the settings for rendering a single diagram.
DiagramOrientation
Orientation of histogram.
void setSpacingUnit(Qgis::RenderUnit unit)
Sets the unit for the content spacing.
QList< QString > categoryAttributes
QList< QString > categoryLabels
QgsMapUnitScale lineSizeScale
Line unit scale.
double spacing() const
Returns the spacing between diagram contents.
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.
const QgsMapUnitScale & spacingMapUnitScale() const
Returns the map unit scale for the content spacing.
Direction
Angular directions.
@ Counterclockwise
Counter-clockwise orientation.
@ Clockwise
Clockwise orientation.
Qgis::RenderUnit spacingUnit() const
Returns the units 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.
void setDiagramSettings(const QgsDiagramSettings &s)
QgsSingleCategoryDiagramRenderer()=default
Constructor for QgsSingleCategoryDiagramRenderer.
QList< QString > diagramAttributes() const override
Returns attribute indices needed for diagram rendering.
Represents a vector layer which manages a vector based data sets.
A set of features which influence the labeling process.
Definition: layer.h:63
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:186
#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:203
const QgsField & field
Definition: qgsfield.h:554
QMap< int, QgsPropertyDefinition > QgsPropertiesDefinition
Definition of available properties.