QGIS API Documentation 3.36.0-Maidenhead (09951dc0acf)
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 };
82 Q_DECLARE_FLAGS( LinePlacementFlags, LinePlacementFlag )
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
115
118
119 QgsDiagramLayerSettings &operator=( const QgsDiagramLayerSettings &rh );
120
122
127 Placement placement() const { return mPlacement; }
128
134 void setPlacement( Placement value ) { mPlacement = value; }
135
141 LinePlacementFlags linePlacementFlags() const { return mPlacementFlags; }
142
149 void setLinePlacementFlags( LinePlacementFlags flags ) { mPlacementFlags = flags; }
150
158 int priority() const { return mPriority; }
159
165 void setPriority( int value ) { mPriority = value; }
166
174 double zIndex() const { return mZIndex; }
175
182 void setZIndex( double index ) { mZIndex = index; }
183
188 bool isObstacle() const { return mObstacle; }
189
195 void setIsObstacle( bool isObstacle ) { mObstacle = isObstacle; }
196
201 double distance() const { return mDistance; }
202
208 void setDistance( double distance ) { mDistance = distance; }
209
214 QgsDiagramRenderer *renderer() { return mRenderer; }
215
221 const QgsDiagramRenderer *renderer() const { return mRenderer; } SIP_SKIP
222
228 void setRenderer( QgsDiagramRenderer *diagramRenderer SIP_TRANSFER );
229
236
242 void setCoordinateTransform( const QgsCoordinateTransform &transform );
243
248 bool showAllDiagrams() const { return mShowAll; }
249
255 void setShowAllDiagrams( bool showAllDiagrams ) { mShowAll = showAllDiagrams; }
256
261 void readXml( const QDomElement &elem );
262
267 void writeXml( QDomElement &layerElem, QDomDocument &doc ) const;
268
274 bool prepare( const QgsExpressionContext &context = QgsExpressionContext() ) const;
275
280 QSet< QString > referencedFields( const QgsExpressionContext &context = QgsExpressionContext() ) const;
281
286 QgsPropertyCollection &dataDefinedProperties() { return mDataDefinedProperties; }
287
294 const QgsPropertyCollection &dataDefinedProperties() const { return mDataDefinedProperties; } SIP_SKIP
295
302 void setDataDefinedProperties( const QgsPropertyCollection &collection ) { mDataDefinedProperties = collection; }
303
304 private:
305
308
310 Placement mPlacement = AroundPoint;
311
313 LinePlacementFlags mPlacementFlags = OnLine;
314
320 int mPriority = 5;
321
323 double mZIndex = 0.0;
324
326 bool mObstacle = false;
327
329 double mDistance = 0.0;
330
332 QgsDiagramRenderer *mRenderer = nullptr;
333
335 bool mShowAll = true;
336
338 QgsPropertyCollection mDataDefinedProperties;
339
340 static void initPropertyDefinitions();
341
343 static QgsPropertiesDefinition sPropertyDefinitions;
344
345};
346
356class CORE_EXPORT QgsDiagramSettings
357{
358 public:
359
361 {
363 XHeight
364 };
365
374
384
388
391
392 QgsDiagramSettings &operator=( const QgsDiagramSettings &other );
393
394 bool enabled = true;
395 QFont font;
396 QList< QColor > categoryColors;
397 QList< QString > categoryAttributes;
398 QList< QString > categoryLabels;
399 QSizeF size; //size
400
405
410
415
420
422 QColor penColor;
423 double penWidth = 0.0;
426 double barWidth = 5.0;
427
429 double opacity = 1.0;
430
431 bool scaleByArea = true;
432
436 double rotationOffset = 270;
437
438 bool scaleBasedVisibility = false;
439
446 double maximumScale = 0;
447
454 double minimumScale = 0;
455
457 double minimumSize = 0.0;
458
470 double spacing() const { return mSpacing; }
471
483 void setSpacing( double spacing ) { mSpacing = spacing; }
484
493 void setSpacingUnit( Qgis::RenderUnit unit ) { mSpacingUnit = unit; }
494
502 Qgis::RenderUnit spacingUnit() const { return mSpacingUnit; }
503
512 void setSpacingMapUnitScale( const QgsMapUnitScale &scale ) { mSpacingMapUnitScale = scale; }
513
522 const QgsMapUnitScale &spacingMapUnitScale() const { return mSpacingMapUnitScale; }
523
530 Direction direction() const;
531
538 void setDirection( Direction direction );
539
541 void readXml( const QDomElement &elem, const QgsReadWriteContext &context = QgsReadWriteContext() );
543 void writeXml( QDomElement &rendererElem, QDomDocument &doc, const QgsReadWriteContext &context = QgsReadWriteContext() ) const;
544
549 QList< QgsLayerTreeModelLegendNode * > legendItems( QgsLayerTreeLayer *nodeLayer ) const SIP_FACTORY;
550
559 QgsLineSymbol *axisLineSymbol() const;
560
571 void setAxisLineSymbol( QgsLineSymbol *symbol SIP_TRANSFER );
572
581 bool showAxis() const;
582
591 void setShowAxis( bool showAxis );
592
600 QgsPaintEffect *paintEffect() const;
601
611 void setPaintEffect( QgsPaintEffect *effect SIP_TRANSFER );
612
613 private:
614
615 double mSpacing = 0;
617 QgsMapUnitScale mSpacingMapUnitScale;
618 Direction mDirection = Counterclockwise;
619
620 bool mShowAxis = false;
621 std::unique_ptr< QgsLineSymbol > mAxisLineSymbol;
622 std::unique_ptr< QgsPaintEffect > mPaintEffect;
623
624};
625
645
646
653class CORE_EXPORT QgsDiagramRenderer
654{
655
656#ifdef SIP_RUN
658 if ( sipCpp->rendererName() == QLatin1String( "SingleCategory" ) )
659 sipType = sipType_QgsSingleCategoryDiagramRenderer;
660 else if ( sipCpp->rendererName() == QLatin1String( "LinearlyInterpolated" ) )
661 sipType = sipType_QgsLinearlyInterpolatedDiagramRenderer;
662 else
663 sipType = NULL;
664 SIP_END
665#endif
666
667 public:
668
673 virtual ~QgsDiagramRenderer() = default;
674
679
681 virtual QSizeF sizeMapUnits( const QgsFeature &feature, const QgsRenderContext &c ) const;
682
683 virtual QString rendererName() const = 0;
684
686 virtual QList<QString> diagramAttributes() const = 0;
687
692 virtual QSet< QString > referencedFields( const QgsExpressionContext &context = QgsExpressionContext() ) const;
693
697 void renderDiagram( const QgsFeature &feature, QgsRenderContext &c, QPointF pos, const QgsPropertyCollection &properties = QgsPropertyCollection() ) const;
698
699 void setDiagram( QgsDiagram *d SIP_TRANSFER );
700 QgsDiagram *diagram() const { return mDiagram.get(); }
701
703 virtual QList<QgsDiagramSettings> diagramSettings() const = 0;
704
710 virtual void readXml( const QDomElement &elem, const QgsReadWriteContext &context ) = 0;
711
717 virtual void writeXml( QDomElement &layerElem, QDomDocument &doc, const QgsReadWriteContext &context ) const = 0;
718
723 virtual QList< QgsLayerTreeModelLegendNode * > legendItems( QgsLayerTreeLayer *nodeLayer ) const SIP_FACTORY;
724
729 bool attributeLegend() const { return mShowAttributeLegend; }
730
736 void setAttributeLegend( bool enabled ) { mShowAttributeLegend = enabled; }
737
738 protected:
740 QgsDiagramRenderer &operator=( const QgsDiagramRenderer &other );
741
748 virtual bool diagramSettings( const QgsFeature &feature, const QgsRenderContext &c, QgsDiagramSettings &s ) const = 0;
749
751 virtual QSizeF diagramSize( const QgsFeature &features, const QgsRenderContext &c ) const = 0;
752
754 void convertSizeToMapUnits( QSizeF &size, const QgsRenderContext &context ) const;
755
757 static int dpiPaintDevice( const QPainter * );
758
759 //read / write diagram
760
765 void _readXml( const QDomElement &elem, const QgsReadWriteContext &context );
766
771 void _writeXml( QDomElement &rendererElem, QDomDocument &doc, const QgsReadWriteContext &context ) const;
772
774 std::unique_ptr< QgsDiagram > mDiagram;
775
777 bool mShowAttributeLegend = true;
778};
779
785{
786 public:
787
790
791 QgsSingleCategoryDiagramRenderer *clone() const override SIP_FACTORY;
792
793 QString rendererName() const override { return QStringLiteral( "SingleCategory" ); }
794
795 QList<QString> diagramAttributes() const override { return mSettings.categoryAttributes; }
796
797 void setDiagramSettings( const QgsDiagramSettings &s ) { mSettings = s; }
798
799 QList<QgsDiagramSettings> diagramSettings() const override;
800
801 void readXml( const QDomElement &elem, const QgsReadWriteContext &context ) override;
802 void writeXml( QDomElement &layerElem, QDomDocument &doc, const QgsReadWriteContext &context ) const override;
803
804 QList< QgsLayerTreeModelLegendNode * > legendItems( QgsLayerTreeLayer *nodeLayer ) const override SIP_FACTORY;
805
806 protected:
807 bool diagramSettings( const QgsFeature &feature, const QgsRenderContext &c, QgsDiagramSettings &s ) const override;
808
809 QSizeF diagramSize( const QgsFeature &, const QgsRenderContext &c ) const override;
810
811 private:
812 QgsDiagramSettings mSettings;
813};
814
820{
821 public:
826
828
830
832 QList<QgsDiagramSettings> diagramSettings() const override;
833
834 void setDiagramSettings( const QgsDiagramSettings &s ) { mSettings = s; }
835
836 QList<QString> diagramAttributes() const override;
837
838 QSet< QString > referencedFields( const QgsExpressionContext &context = QgsExpressionContext() ) const override;
839
840 QString rendererName() const override { return QStringLiteral( "LinearlyInterpolated" ); }
841
842 void setLowerValue( double val ) { mInterpolationSettings.lowerValue = val; }
843 double lowerValue() const { return mInterpolationSettings.lowerValue; }
844
845 void setUpperValue( double val ) { mInterpolationSettings.upperValue = val; }
846 double upperValue() const { return mInterpolationSettings.upperValue; }
847
848 void setLowerSize( QSizeF s ) { mInterpolationSettings.lowerSize = s; }
849 QSizeF lowerSize() const { return mInterpolationSettings.lowerSize; }
850
851 void setUpperSize( QSizeF s ) { mInterpolationSettings.upperSize = s; }
852 QSizeF upperSize() const { return mInterpolationSettings.upperSize; }
853
858 QString classificationField() const { return mInterpolationSettings.classificationField; }
859
864 void setClassificationField( const QString &field ) { mInterpolationSettings.classificationField = field; }
865
866 QString classificationAttributeExpression() const { return mInterpolationSettings.classificationAttributeExpression; }
867 void setClassificationAttributeExpression( const QString &expression ) { mInterpolationSettings.classificationAttributeExpression = expression; }
868
869 bool classificationAttributeIsExpression() const { return mInterpolationSettings.classificationAttributeIsExpression; }
870 void setClassificationAttributeIsExpression( bool isExpression ) { mInterpolationSettings.classificationAttributeIsExpression = isExpression; }
871
872 void readXml( const QDomElement &elem, const QgsReadWriteContext &context ) override;
873 void writeXml( QDomElement &layerElem, QDomDocument &doc, const QgsReadWriteContext &context ) const override;
874
875 QList< QgsLayerTreeModelLegendNode * > legendItems( QgsLayerTreeLayer *nodeLayer ) const override SIP_FACTORY;
876
880 void setDataDefinedSizeLegend( QgsDataDefinedSizeLegend *settings SIP_TRANSFER );
881
885 QgsDataDefinedSizeLegend *dataDefinedSizeLegend() const;
886
887 protected:
888 bool diagramSettings( const QgsFeature &feature, const QgsRenderContext &c, QgsDiagramSettings &s ) const override;
889
890 QSizeF diagramSize( const QgsFeature &, const QgsRenderContext &c ) const override;
891
892 private:
893 QgsDiagramSettings mSettings;
894 QgsDiagramInterpolationSettings mInterpolationSettings;
895
897 QgsDataDefinedSizeLegend *mDataDefinedSizeLegend = nullptr;
898};
899
900#endif // QGSDIAGRAMRENDERER_H
RenderUnit
Rendering size units.
Definition qgis.h:4193
@ 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...
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
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.
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)
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: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.