QGIS API Documentation 3.39.0-Master (3aed037ce22)
Loading...
Searching...
No Matches
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 };
83
84 // *INDENT-OFF*
85
86
90 {
91 BackgroundColor,
92 StrokeColor,
93 StrokeWidth,
94 PositionX,
95 PositionY,
96 Distance,
97 Priority,
98 ZIndex,
99 IsObstacle,
100 Show,
101 AlwaysShow,
102 StartAngle,
103 };
104 // *INDENT-ON*
105
109 static const QgsPropertiesDefinition &propertyDefinitions();
110
112
114
115 QgsDiagramLayerSettings &operator=( const QgsDiagramLayerSettings &rh );
116
118
123 Placement placement() const { return mPlacement; }
124
130 void setPlacement( Placement value ) { mPlacement = value; }
131
137 LinePlacementFlags linePlacementFlags() const { return mPlacementFlags; }
138
145 void setLinePlacementFlags( LinePlacementFlags flags ) { mPlacementFlags = flags; }
146
154 int priority() const { return mPriority; }
155
161 void setPriority( int value ) { mPriority = value; }
162
170 double zIndex() const { return mZIndex; }
171
178 void setZIndex( double index ) { mZIndex = index; }
179
184 bool isObstacle() const { return mObstacle; }
185
191 void setIsObstacle( bool isObstacle ) { mObstacle = isObstacle; }
192
197 double distance() const { return mDistance; }
198
204 void setDistance( double distance ) { mDistance = distance; }
205
210 QgsDiagramRenderer *renderer() { return mRenderer; }
211
217 const QgsDiagramRenderer *renderer() const { return mRenderer; } SIP_SKIP
218
224 void setRenderer( QgsDiagramRenderer *diagramRenderer SIP_TRANSFER );
225
232
238 void setCoordinateTransform( const QgsCoordinateTransform &transform );
239
244 bool showAllDiagrams() const { return mShowAll; }
245
251 void setShowAllDiagrams( bool showAllDiagrams ) { mShowAll = showAllDiagrams; }
252
257 void readXml( const QDomElement &elem );
258
263 void writeXml( QDomElement &layerElem, QDomDocument &doc ) const;
264
270 bool prepare( const QgsExpressionContext &context = QgsExpressionContext() ) const;
271
276 QSet< QString > referencedFields( const QgsExpressionContext &context = QgsExpressionContext() ) const;
277
282 QgsPropertyCollection &dataDefinedProperties() { return mDataDefinedProperties; }
283
290 const QgsPropertyCollection &dataDefinedProperties() const { return mDataDefinedProperties; } SIP_SKIP
291
298 void setDataDefinedProperties( const QgsPropertyCollection &collection ) { mDataDefinedProperties = collection; }
299
300 private:
301
304
306 Placement mPlacement = AroundPoint;
307
309 LinePlacementFlags mPlacementFlags = OnLine;
310
316 int mPriority = 5;
317
319 double mZIndex = 0.0;
320
322 bool mObstacle = false;
323
325 double mDistance = 0.0;
326
328 QgsDiagramRenderer *mRenderer = nullptr;
329
331 bool mShowAll = true;
332
334 QgsPropertyCollection mDataDefinedProperties;
335
336 static void initPropertyDefinitions();
337
339 static QgsPropertiesDefinition sPropertyDefinitions;
340
341};
342
352class CORE_EXPORT QgsDiagramSettings
353{
354 public:
355
357 {
359 XHeight
360 };
361
370
380
384
385 QgsDiagramSettings &operator=( const QgsDiagramSettings &other );
386
387 bool enabled = true;
388 QFont font;
389 QList< QColor > categoryColors;
390 QList< QString > categoryAttributes;
391 QList< QString > categoryLabels;
392 QSizeF size; //size
393
398
403
408
413
415 QColor penColor;
416 double penWidth = 0.0;
419 double barWidth = 5.0;
420
422 double opacity = 1.0;
423
424 bool scaleByArea = true;
425
429 double rotationOffset = 270;
430
431 bool scaleBasedVisibility = false;
432
439 double maximumScale = 0;
440
447 double minimumScale = 0;
448
450 double minimumSize = 0.0;
451
463 double spacing() const { return mSpacing; }
464
476 void setSpacing( double spacing ) { mSpacing = spacing; }
477
486 void setSpacingUnit( Qgis::RenderUnit unit ) { mSpacingUnit = unit; }
487
495 Qgis::RenderUnit spacingUnit() const { return mSpacingUnit; }
496
505 void setSpacingMapUnitScale( const QgsMapUnitScale &scale ) { mSpacingMapUnitScale = scale; }
506
515 const QgsMapUnitScale &spacingMapUnitScale() const { return mSpacingMapUnitScale; }
516
523 Direction direction() const;
524
531 void setDirection( Direction direction );
532
534 void readXml( const QDomElement &elem, const QgsReadWriteContext &context = QgsReadWriteContext() );
536 void writeXml( QDomElement &rendererElem, QDomDocument &doc, const QgsReadWriteContext &context = QgsReadWriteContext() ) const;
537
542 QList< QgsLayerTreeModelLegendNode * > legendItems( QgsLayerTreeLayer *nodeLayer ) const SIP_FACTORY;
543
552 QgsLineSymbol *axisLineSymbol() const;
553
564 void setAxisLineSymbol( QgsLineSymbol *symbol SIP_TRANSFER );
565
574 bool showAxis() const;
575
584 void setShowAxis( bool showAxis );
585
593 QgsPaintEffect *paintEffect() const;
594
604 void setPaintEffect( QgsPaintEffect *effect SIP_TRANSFER );
605
606 private:
607
608 double mSpacing = 0;
610 QgsMapUnitScale mSpacingMapUnitScale;
611 Direction mDirection = Counterclockwise;
612
613 bool mShowAxis = false;
614 std::unique_ptr< QgsLineSymbol > mAxisLineSymbol;
615 std::unique_ptr< QgsPaintEffect > mPaintEffect;
616
617};
618
638
639
646class CORE_EXPORT QgsDiagramRenderer
647{
648
649#ifdef SIP_RUN
651 if ( sipCpp->rendererName() == QLatin1String( "SingleCategory" ) )
652 sipType = sipType_QgsSingleCategoryDiagramRenderer;
653 else if ( sipCpp->rendererName() == QLatin1String( "LinearlyInterpolated" ) )
654 sipType = sipType_QgsLinearlyInterpolatedDiagramRenderer;
655 else
656 sipType = NULL;
657 SIP_END
658#endif
659
660 public:
661
663 virtual ~QgsDiagramRenderer() = default;
664
669
671 virtual QSizeF sizeMapUnits( const QgsFeature &feature, const QgsRenderContext &c ) const;
672
673 virtual QString rendererName() const = 0;
674
676 virtual QList<QString> diagramAttributes() const = 0;
677
682 virtual QSet< QString > referencedFields( const QgsExpressionContext &context = QgsExpressionContext() ) const;
683
687 void renderDiagram( const QgsFeature &feature, QgsRenderContext &c, QPointF pos, const QgsPropertyCollection &properties = QgsPropertyCollection() ) const;
688
689 void setDiagram( QgsDiagram *d SIP_TRANSFER );
690 QgsDiagram *diagram() const { return mDiagram.get(); }
691
693 virtual QList<QgsDiagramSettings> diagramSettings() const = 0;
694
700 virtual void readXml( const QDomElement &elem, const QgsReadWriteContext &context ) = 0;
701
707 virtual void writeXml( QDomElement &layerElem, QDomDocument &doc, const QgsReadWriteContext &context ) const = 0;
708
713 virtual QList< QgsLayerTreeModelLegendNode * > legendItems( QgsLayerTreeLayer *nodeLayer ) const SIP_FACTORY;
714
719 bool attributeLegend() const { return mShowAttributeLegend; }
720
726 void setAttributeLegend( bool enabled ) { mShowAttributeLegend = enabled; }
727
728 protected:
730 QgsDiagramRenderer &operator=( const QgsDiagramRenderer &other );
731
738 virtual bool diagramSettings( const QgsFeature &feature, const QgsRenderContext &c, QgsDiagramSettings &s ) const = 0;
739
741 virtual QSizeF diagramSize( const QgsFeature &features, const QgsRenderContext &c ) const = 0;
742
744 void convertSizeToMapUnits( QSizeF &size, const QgsRenderContext &context ) const;
745
747 static int dpiPaintDevice( const QPainter * );
748
749 //read / write diagram
750
755 void _readXml( const QDomElement &elem, const QgsReadWriteContext &context );
756
761 void _writeXml( QDomElement &rendererElem, QDomDocument &doc, const QgsReadWriteContext &context ) const;
762
764 std::unique_ptr< QgsDiagram > mDiagram;
765
767 bool mShowAttributeLegend = true;
768};
769
775{
776 public:
777
779
780 QgsSingleCategoryDiagramRenderer *clone() const override SIP_FACTORY;
781
782 QString rendererName() const override { return QStringLiteral( "SingleCategory" ); }
783
784 QList<QString> diagramAttributes() const override { return mSettings.categoryAttributes; }
785
786 void setDiagramSettings( const QgsDiagramSettings &s ) { mSettings = s; }
787
788 QList<QgsDiagramSettings> diagramSettings() const override;
789
790 void readXml( const QDomElement &elem, const QgsReadWriteContext &context ) override;
791 void writeXml( QDomElement &layerElem, QDomDocument &doc, const QgsReadWriteContext &context ) const override;
792
793 QList< QgsLayerTreeModelLegendNode * > legendItems( QgsLayerTreeLayer *nodeLayer ) const override SIP_FACTORY;
794
795 protected:
796 bool diagramSettings( const QgsFeature &feature, const QgsRenderContext &c, QgsDiagramSettings &s ) const override;
797
798 QSizeF diagramSize( const QgsFeature &, const QgsRenderContext &c ) const override;
799
800 private:
801 QgsDiagramSettings mSettings;
802};
803
809{
810 public:
814
816
818
820 QList<QgsDiagramSettings> diagramSettings() const override;
821
822 void setDiagramSettings( const QgsDiagramSettings &s ) { mSettings = s; }
823
824 QList<QString> diagramAttributes() const override;
825
826 QSet< QString > referencedFields( const QgsExpressionContext &context = QgsExpressionContext() ) const override;
827
828 QString rendererName() const override { return QStringLiteral( "LinearlyInterpolated" ); }
829
830 void setLowerValue( double val ) { mInterpolationSettings.lowerValue = val; }
831 double lowerValue() const { return mInterpolationSettings.lowerValue; }
832
833 void setUpperValue( double val ) { mInterpolationSettings.upperValue = val; }
834 double upperValue() const { return mInterpolationSettings.upperValue; }
835
836 void setLowerSize( QSizeF s ) { mInterpolationSettings.lowerSize = s; }
837 QSizeF lowerSize() const { return mInterpolationSettings.lowerSize; }
838
839 void setUpperSize( QSizeF s ) { mInterpolationSettings.upperSize = s; }
840 QSizeF upperSize() const { return mInterpolationSettings.upperSize; }
841
846 QString classificationField() const { return mInterpolationSettings.classificationField; }
847
852 void setClassificationField( const QString &field ) { mInterpolationSettings.classificationField = field; }
853
854 QString classificationAttributeExpression() const { return mInterpolationSettings.classificationAttributeExpression; }
855 void setClassificationAttributeExpression( const QString &expression ) { mInterpolationSettings.classificationAttributeExpression = expression; }
856
857 bool classificationAttributeIsExpression() const { return mInterpolationSettings.classificationAttributeIsExpression; }
858 void setClassificationAttributeIsExpression( bool isExpression ) { mInterpolationSettings.classificationAttributeIsExpression = isExpression; }
859
860 void readXml( const QDomElement &elem, const QgsReadWriteContext &context ) override;
861 void writeXml( QDomElement &layerElem, QDomDocument &doc, const QgsReadWriteContext &context ) const override;
862
863 QList< QgsLayerTreeModelLegendNode * > legendItems( QgsLayerTreeLayer *nodeLayer ) const override SIP_FACTORY;
864
868 void setDataDefinedSizeLegend( QgsDataDefinedSizeLegend *settings SIP_TRANSFER );
869
873 QgsDataDefinedSizeLegend *dataDefinedSizeLegend() const;
874
875 protected:
876 bool diagramSettings( const QgsFeature &feature, const QgsRenderContext &c, QgsDiagramSettings &s ) const override;
877
878 QSizeF diagramSize( const QgsFeature &, const QgsRenderContext &c ) const override;
879
880 private:
881 QgsDiagramSettings mSettings;
882 QgsDiagramInterpolationSettings mInterpolationSettings;
883
885 QgsDataDefinedSizeLegend *mDataDefinedSizeLegend = nullptr;
886};
887
888#endif // QGSDIAGRAMRENDERER_H
RenderUnit
Rendering size units.
Definition qgis.h:4594
@ Millimeters
Millimeters.
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...
QFlags< LinePlacementFlag > LinePlacementFlags
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.
Property
Data definable properties.
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
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:58
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.
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.
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)
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:191
#define SIP_ENUM_BASETYPE(type)
Definition qgis_sip.h:278
#define SIP_SKIP
Definition qgis_sip.h:126
#define SIP_MONKEYPATCH_SCOPEENUM_UNNEST(OUTSIDE_CLASS, FORMERNAME)
Definition qgis_sip.h:271
#define SIP_TRANSFER
Definition qgis_sip.h:36
#define SIP_FACTORY
Definition qgis_sip.h:76
#define SIP_END
Definition qgis_sip.h:208
QMap< int, QgsPropertyDefinition > QgsPropertiesDefinition
Definition of available properties.