QGIS API Documentation  3.2.0-Bonn (bc43194)
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 QGSDIAGRAMRENDERERV2_H
16 #define QGSDIAGRAMRENDERERV2_H
17 
18 #include "qgis_core.h"
19 #include "qgis.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 
37 class QgsDiagramRenderer;
38 class QgsFeature;
39 class QgsRenderContext;
40 class QDomElement;
41 class QgsMapToPixel;
43 class QgsVectorLayer;
45 class QgsLayerTreeLayer;
46 
47 namespace pal { class Layer; } SIP_SKIP
48 
58 class CORE_EXPORT QgsDiagramLayerSettings
59 {
60  public:
61 
62  //avoid inclusion of QgsPalLabeling
63  enum Placement
64  {
65  AroundPoint = 0, // Point / Polygon
66  OverPoint, // Point / Polygon
67  Line, // Line / Polygon
68  Curved, // Line
69  Horizontal, // Polygon
70  Free // Polygon
71  };
72 
75  {
76  OnLine = 1,
77  AboveLine = 1 << 1,
78  BelowLine = 1 << 2,
79  MapOrientation = 1 << 4,
80  };
81  Q_DECLARE_FLAGS( LinePlacementFlags, LinePlacementFlag )
82 
83 
87  enum Property
88  {
98  Show,
101  };
102 
107  static const QgsPropertiesDefinition &propertyDefinitions();
108 
113 
116 
117  QgsDiagramLayerSettings &operator=( const QgsDiagramLayerSettings &rh );
118 
120 
126  Placement placement() const { return mPlacement; }
127 
134  void setPlacement( Placement value ) { mPlacement = value; }
135 
142  LinePlacementFlags linePlacementFlags() const { return mPlacementFlags; }
143 
151  void setLinePlacementFlags( LinePlacementFlags flags ) { mPlacementFlags = flags; }
152 
161  int priority() const { return mPriority; }
162 
169  void setPriority( int value ) { mPriority = value; }
170 
179  double zIndex() const { return mZIndex; }
180 
188  void setZIndex( double index ) { mZIndex = index; }
189 
195  bool isObstacle() const { return mObstacle; }
196 
203  void setIsObstacle( bool isObstacle ) { mObstacle = isObstacle; }
204 
210  double distance() const { return mDistance; }
211 
218  void setDistance( double distance ) { mDistance = distance; }
219 
225  QgsDiagramRenderer *renderer() { return mRenderer; }
226 
233  const QgsDiagramRenderer *renderer() const { return mRenderer; } SIP_SKIP
234 
241  void setRenderer( QgsDiagramRenderer *diagramRenderer SIP_TRANSFER );
242 
250 
257  void setCoordinateTransform( const QgsCoordinateTransform &transform );
258 
264  bool showAllDiagrams() const { return mShowAll; }
265 
272  void setShowAllDiagrams( bool showAllDiagrams ) { mShowAll = showAllDiagrams; }
273 
278  void readXml( const QDomElement &elem );
279 
284  void writeXml( QDomElement &layerElem, QDomDocument &doc ) const;
285 
292  bool prepare( const QgsExpressionContext &context = QgsExpressionContext() ) const;
293 
299  QSet< QString > referencedFields( const QgsExpressionContext &context = QgsExpressionContext() ) const;
300 
306  QgsPropertyCollection &dataDefinedProperties() { return mDataDefinedProperties; }
307 
314  const QgsPropertyCollection &dataDefinedProperties() const { return mDataDefinedProperties; } SIP_SKIP
315 
322  void setDataDefinedProperties( const QgsPropertyCollection &collection ) { mDataDefinedProperties = collection; }
323 
324  private:
325 
328 
330  Placement mPlacement = AroundPoint;
331 
333  LinePlacementFlags mPlacementFlags = OnLine;
334 
340  int mPriority = 5;
341 
343  double mZIndex = 0.0;
344 
346  bool mObstacle = false;
347 
349  double mDistance = 0.0;
350 
352  QgsDiagramRenderer *mRenderer = nullptr;
353 
355  bool mShowAll = true;
356 
358  QgsPropertyCollection mDataDefinedProperties;
359 
360  static void initPropertyDefinitions();
361 
363  static QgsPropertiesDefinition sPropertyDefinitions;
364 
365 };
366 
376 class CORE_EXPORT QgsDiagramSettings
377 {
378  public:
379 
381  {
383  XHeight
384  };
385 
388  {
389  Up,
392  Right
393  };
394 
396  QgsDiagramSettings() = default;
397 
398  bool enabled = true;
399  QFont font;
400  QList< QColor > categoryColors;
401  QList< QString > categoryAttributes;
403  QList< QString > categoryLabels;
404  QSizeF size; //size
405 
410 
416 
422 
428 
430  QColor penColor;
431  double penWidth = 0.0;
434  double barWidth = 5.0;
435 
437  double opacity = 1.0;
438 
439  bool scaleByArea = true;
440 
445  double rotationOffset = 270;
446 
447  bool scaleBasedVisibility = false;
448 
455  double maximumScale = 0;
456 
463  double minimumScale = 0;
464 
466  double minimumSize = 0.0;
467 
469  void readXml( const QDomElement &elem );
471  void writeXml( QDomElement &rendererElem, QDomDocument &doc ) const;
472 
478  QList< QgsLayerTreeModelLegendNode * > legendItems( QgsLayerTreeLayer *nodeLayer ) const SIP_FACTORY;
479 
480 };
481 
488 {
489  public:
490  QSizeF lowerSize;
491  QSizeF upperSize;
492  double lowerValue;
493  double upperValue;
494 
497 
500 };
501 
502 
509 class CORE_EXPORT QgsDiagramRenderer
510 {
511 
512 #ifdef SIP_RUN
514  if ( sipCpp->rendererName() == QStringLiteral( "SingleCategory" ) )
515  sipType = sipType_QgsSingleCategoryDiagramRenderer;
516  else if ( sipCpp->rendererName() == QStringLiteral( "LinearlyInterpolated" ) )
517  sipType = sipType_QgsLinearlyInterpolatedDiagramRenderer;
518  else
519  sipType = NULL;
520  SIP_END
521 #endif
522 
523  public:
524 
528  QgsDiagramRenderer() = default;
529  virtual ~QgsDiagramRenderer() = default;
530 
534  virtual QgsDiagramRenderer *clone() const = 0 SIP_FACTORY;
535 
537  virtual QSizeF sizeMapUnits( const QgsFeature &feature, const QgsRenderContext &c ) const;
538 
539  virtual QString rendererName() const = 0;
540 
542  virtual QList<QString> diagramAttributes() const = 0;
543 
549  virtual QSet< QString > referencedFields( const QgsExpressionContext &context = QgsExpressionContext() ) const;
550 
554  void renderDiagram( const QgsFeature &feature, QgsRenderContext &c, QPointF pos, const QgsPropertyCollection &properties = QgsPropertyCollection() ) const;
555 
556  void setDiagram( QgsDiagram *d SIP_TRANSFER );
557  QgsDiagram *diagram() const { return mDiagram.get(); }
558 
560  virtual QList<QgsDiagramSettings> diagramSettings() const = 0;
561 
567  virtual void readXml( const QDomElement &elem, const QgsReadWriteContext &context ) = 0;
568 
574  virtual void writeXml( QDomElement &layerElem, QDomDocument &doc, const QgsReadWriteContext &context ) const = 0;
575 
581  virtual QList< QgsLayerTreeModelLegendNode * > legendItems( QgsLayerTreeLayer *nodeLayer ) const SIP_FACTORY;
582 
588  bool attributeLegend() const { return mShowAttributeLegend; }
589 
596  void setAttributeLegend( bool enabled ) { mShowAttributeLegend = enabled; }
597 
598  protected:
599  QgsDiagramRenderer( const QgsDiagramRenderer &other );
600  QgsDiagramRenderer &operator=( const QgsDiagramRenderer &other );
601 
608  virtual bool diagramSettings( const QgsFeature &feature, const QgsRenderContext &c, QgsDiagramSettings &s ) const = 0;
609 
611  virtual QSizeF diagramSize( const QgsFeature &features, const QgsRenderContext &c ) const = 0;
612 
614  void convertSizeToMapUnits( QSizeF &size, const QgsRenderContext &context ) const;
615 
617  static int dpiPaintDevice( const QPainter * );
618 
619  //read / write diagram
620 
625  void _readXml( const QDomElement &elem, const QgsReadWriteContext &context );
626 
631  void _writeXml( QDomElement &rendererElem, QDomDocument &doc, const QgsReadWriteContext &context ) const;
632 
634  std::unique_ptr< QgsDiagram > mDiagram;
635 
637  bool mShowAttributeLegend = true;
638 };
639 
645 {
646  public:
647 
650 
651  QgsSingleCategoryDiagramRenderer *clone() const override SIP_FACTORY;
652 
653  QString rendererName() const override { return QStringLiteral( "SingleCategory" ); }
654 
655  QList<QString> diagramAttributes() const override { return mSettings.categoryAttributes; }
656 
657  void setDiagramSettings( const QgsDiagramSettings &s ) { mSettings = s; }
658 
659  QList<QgsDiagramSettings> diagramSettings() const override;
660 
661  void readXml( const QDomElement &elem, const QgsReadWriteContext &context ) override;
662  void writeXml( QDomElement &layerElem, QDomDocument &doc, const QgsReadWriteContext &context ) const override;
663 
664  QList< QgsLayerTreeModelLegendNode * > legendItems( QgsLayerTreeLayer *nodeLayer ) const override SIP_FACTORY;
665 
666  protected:
667  bool diagramSettings( const QgsFeature &feature, const QgsRenderContext &c, QgsDiagramSettings &s ) const override;
668 
669  QSizeF diagramSize( const QgsFeature &, const QgsRenderContext &c ) const override;
670 
671  private:
672  QgsDiagramSettings mSettings;
673 };
674 
680 {
681  public:
684 
685  QgsLinearlyInterpolatedDiagramRenderer *clone() const override SIP_FACTORY;
686 
688  QList<QgsDiagramSettings> diagramSettings() const override;
689 
690  void setDiagramSettings( const QgsDiagramSettings &s ) { mSettings = s; }
691 
692  QList<QString> diagramAttributes() const override;
693 
694  QSet< QString > referencedFields( const QgsExpressionContext &context = QgsExpressionContext() ) const override;
695 
696  QString rendererName() const override { return QStringLiteral( "LinearlyInterpolated" ); }
697 
698  void setLowerValue( double val ) { mInterpolationSettings.lowerValue = val; }
699  double lowerValue() const { return mInterpolationSettings.lowerValue; }
700 
701  void setUpperValue( double val ) { mInterpolationSettings.upperValue = val; }
702  double upperValue() const { return mInterpolationSettings.upperValue; }
703 
704  void setLowerSize( QSizeF s ) { mInterpolationSettings.lowerSize = s; }
705  QSizeF lowerSize() const { return mInterpolationSettings.lowerSize; }
706 
707  void setUpperSize( QSizeF s ) { mInterpolationSettings.upperSize = s; }
708  QSizeF upperSize() const { return mInterpolationSettings.upperSize; }
709 
715  QString classificationField() const { return mInterpolationSettings.classificationField; }
716 
722  void setClassificationField( const QString &field ) { mInterpolationSettings.classificationField = field; }
723 
724  QString classificationAttributeExpression() const { return mInterpolationSettings.classificationAttributeExpression; }
725  void setClassificationAttributeExpression( const QString &expression ) { mInterpolationSettings.classificationAttributeExpression = expression; }
726 
727  bool classificationAttributeIsExpression() const { return mInterpolationSettings.classificationAttributeIsExpression; }
728  void setClassificationAttributeIsExpression( bool isExpression ) { mInterpolationSettings.classificationAttributeIsExpression = isExpression; }
729 
730  void readXml( const QDomElement &elem, const QgsReadWriteContext &context ) override;
731  void writeXml( QDomElement &layerElem, QDomDocument &doc, const QgsReadWriteContext &context ) const override;
732 
733  QList< QgsLayerTreeModelLegendNode * > legendItems( QgsLayerTreeLayer *nodeLayer ) const override SIP_FACTORY;
734 
739  void setDataDefinedSizeLegend( QgsDataDefinedSizeLegend *settings SIP_TRANSFER );
740 
745  QgsDataDefinedSizeLegend *dataDefinedSizeLegend() const;
746 
747  protected:
748  bool diagramSettings( const QgsFeature &feature, const QgsRenderContext &c, QgsDiagramSettings &s ) const override;
749 
750  QSizeF diagramSize( const QgsFeature &, const QgsRenderContext &c ) const override;
751 
754 
755  private:
756  QgsDiagramSettings mSettings;
757  QgsDiagramInterpolationSettings mInterpolationSettings;
758 
760  QgsDataDefinedSizeLegend *mDataDefinedSizeLegend = nullptr;
761 };
762 
763 #endif // QGSDIAGRAMRENDERERV2_H
void setClassificationAttributeIsExpression(bool isExpression)
The class is used as a container of context for various read/write operations on other objects...
y-coordinate data defined diagram position
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...
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.
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:63
QgsPropertyCollection & dataDefinedProperties()
Returns a reference to the diagram&#39;s property collection, used for data defined overrides.
Whether the diagram should always be shown, even if it overlaps other diagrams/labels.
void setZIndex(double index)
Sets the diagram z-index.
The feature class encapsulates a single feature including its id, geometry and a list of field/values...
Definition: qgsfeature.h:62
Z-index for diagram ordering.
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:36
Diagram priority (between 0 and 10)
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:119
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:182
QgsMapUnitScale sizeScale
Diagram size unit scale.
DiagramOrientation
Orientation of histogram.
void setClassificationField(const QString &field)
Sets the field name used for interpolating the diagram size.
x-coordinate data defined diagram position
#define SIP_FACTORY
Definition: qgis_sip.h:69
Whether diagram features act as obstacles for other diagrams/labels.
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.
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.
bool attributeLegend() const
Returns true if renderer will show legend items for diagram attributes.
Distance to diagram from feature.
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:165
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 to show the diagram.
LinePlacementFlag
Line placement flags for controlling line based placements.
QgsMapUnitScale lineSizeScale
Line unit scale.
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.
RenderUnit
Rendering size units.
Definition: qgsunittypes.h:100
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.