QGIS API Documentation  3.0.2-Girona (307d082)
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 
589  bool attributeLegend() const { return mShowAttributeLegend; }
590 
598  void setAttributeLegend( bool enabled ) { mShowAttributeLegend = enabled; }
599 
600  protected:
601  QgsDiagramRenderer( const QgsDiagramRenderer &other );
602  QgsDiagramRenderer &operator=( const QgsDiagramRenderer &other );
603 
610  virtual bool diagramSettings( const QgsFeature &feature, const QgsRenderContext &c, QgsDiagramSettings &s ) const = 0;
611 
613  virtual QSizeF diagramSize( const QgsFeature &features, const QgsRenderContext &c ) const = 0;
614 
616  void convertSizeToMapUnits( QSizeF &size, const QgsRenderContext &context ) const;
617 
619  static int dpiPaintDevice( const QPainter * );
620 
621  //read / write diagram
622 
627  void _readXml( const QDomElement &elem, const QgsReadWriteContext &context );
628 
633  void _writeXml( QDomElement &rendererElem, QDomDocument &doc, const QgsReadWriteContext &context ) const;
634 
636  std::unique_ptr< QgsDiagram > mDiagram;
637 
639  bool mShowAttributeLegend = true;
640 };
641 
647 {
648  public:
649 
652 
653  QgsSingleCategoryDiagramRenderer *clone() const override SIP_FACTORY;
654 
655  QString rendererName() const override { return QStringLiteral( "SingleCategory" ); }
656 
657  QList<QString> diagramAttributes() const override { return mSettings.categoryAttributes; }
658 
659  void setDiagramSettings( const QgsDiagramSettings &s ) { mSettings = s; }
660 
661  QList<QgsDiagramSettings> diagramSettings() const override;
662 
663  void readXml( const QDomElement &elem, const QgsReadWriteContext &context ) override;
664  void writeXml( QDomElement &layerElem, QDomDocument &doc, const QgsReadWriteContext &context ) const override;
665 
666  QList< QgsLayerTreeModelLegendNode * > legendItems( QgsLayerTreeLayer *nodeLayer ) const override SIP_FACTORY;
667 
668  protected:
669  bool diagramSettings( const QgsFeature &feature, const QgsRenderContext &c, QgsDiagramSettings &s ) const override;
670 
671  QSizeF diagramSize( const QgsFeature &, const QgsRenderContext &c ) const override;
672 
673  private:
674  QgsDiagramSettings mSettings;
675 };
676 
682 {
683  public:
686 
687  QgsLinearlyInterpolatedDiagramRenderer *clone() const override SIP_FACTORY;
688 
690  QList<QgsDiagramSettings> diagramSettings() const override;
691 
692  void setDiagramSettings( const QgsDiagramSettings &s ) { mSettings = s; }
693 
694  QList<QString> diagramAttributes() const override;
695 
696  QSet< QString > referencedFields( const QgsExpressionContext &context = QgsExpressionContext() ) const override;
697 
698  QString rendererName() const override { return QStringLiteral( "LinearlyInterpolated" ); }
699 
700  void setLowerValue( double val ) { mInterpolationSettings.lowerValue = val; }
701  double lowerValue() const { return mInterpolationSettings.lowerValue; }
702 
703  void setUpperValue( double val ) { mInterpolationSettings.upperValue = val; }
704  double upperValue() const { return mInterpolationSettings.upperValue; }
705 
706  void setLowerSize( QSizeF s ) { mInterpolationSettings.lowerSize = s; }
707  QSizeF lowerSize() const { return mInterpolationSettings.lowerSize; }
708 
709  void setUpperSize( QSizeF s ) { mInterpolationSettings.upperSize = s; }
710  QSizeF upperSize() const { return mInterpolationSettings.upperSize; }
711 
717  QString classificationField() const { return mInterpolationSettings.classificationField; }
718 
724  void setClassificationField( const QString &field ) { mInterpolationSettings.classificationField = field; }
725 
726  QString classificationAttributeExpression() const { return mInterpolationSettings.classificationAttributeExpression; }
727  void setClassificationAttributeExpression( const QString &expression ) { mInterpolationSettings.classificationAttributeExpression = expression; }
728 
729  bool classificationAttributeIsExpression() const { return mInterpolationSettings.classificationAttributeIsExpression; }
730  void setClassificationAttributeIsExpression( bool isExpression ) { mInterpolationSettings.classificationAttributeIsExpression = isExpression; }
731 
732  void readXml( const QDomElement &elem, const QgsReadWriteContext &context ) override;
733  void writeXml( QDomElement &layerElem, QDomDocument &doc, const QgsReadWriteContext &context ) const override;
734 
735  QList< QgsLayerTreeModelLegendNode * > legendItems( QgsLayerTreeLayer *nodeLayer ) const override SIP_FACTORY;
736 
741  void setDataDefinedSizeLegend( QgsDataDefinedSizeLegend *settings SIP_TRANSFER );
742 
747  QgsDataDefinedSizeLegend *dataDefinedSizeLegend() const;
748 
749  protected:
750  bool diagramSettings( const QgsFeature &feature, const QgsRenderContext &c, QgsDiagramSettings &s ) const override;
751 
752  QSizeF diagramSize( const QgsFeature &, const QgsRenderContext &c ) const override;
753 
756 
757  private:
758  QgsDiagramSettings mSettings;
759  QgsDiagramInterpolationSettings mInterpolationSettings;
760 
762  QgsDataDefinedSizeLegend *mDataDefinedSizeLegend = nullptr;
763 };
764 
765 #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.
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:175
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.