QGIS API Documentation  2.13.0-Master
qgsdiagramrendererv2.h
Go to the documentation of this file.
1 /***************************************************************************
2  qgsdiagramrendererv2.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 <QColor>
19 #include <QFont>
20 #include <QList>
21 #include <QPointF>
22 #include <QSizeF>
23 #include <QDomDocument>
24 
25 #include "qgsfeature.h"
26 
27 class QgsDiagram;
29 class QgsFeature;
30 class QgsRenderContext;
31 class QDomElement;
33 class QgsMapToPixel;
34 class QgsVectorLayer;
36 class QgsLayerTreeLayer;
37 
38 namespace pal { class Layer; }
39 
40 class CORE_EXPORT QgsDiagramLayerSettings
41 {
42  public:
43  //avoid inclusion of QgsPalLabeling
44  enum Placement
45  {
46  AroundPoint = 0, // Point / Polygon
47  OverPoint, // Point / Polygon
48  Line, // Line / Polygon
49  Curved, // Line
50  Horizontal, // Polygon
51  Free // Polygon
52  };
53 
55  {
56  OnLine = 1,
57  AboveLine = 2,
58  BelowLine = 4,
59  MapOrientation = 8
60  };
61 
64  QgsDiagramLayerSettings& operator=( const QgsDiagramLayerSettings& rh );
65 
67 
68  //pal placement properties
70  unsigned int placementFlags;
71  int priority; // 0 = low, 10 = high
72 
74  double zIndex;
75 
76  bool obstacle; // whether it's an obstacle
77  double dist; // distance from the feature (in mm)
78  QgsDiagramRendererV2* renderer; // if any renderer is assigned, it is owned by this class
79 
80  //assigned when layer gets prepared
84 
85  int xPosColumn; //attribute index for x coordinate (or -1 if position not data defined)
86  int yPosColumn;//attribute index for y coordinate (or -1 if position not data defined)
87  bool showAll;
88 
89  void readXML( const QDomElement& elem, const QgsVectorLayer* layer );
90  void writeXML( QDomElement& layerElem, QDomDocument& doc, const QgsVectorLayer* layer ) const;
91 
92 };
93 
94 //diagram settings for rendering
95 class CORE_EXPORT QgsDiagramSettings
96 {
97  public:
98  enum SizeType
99  {
100  MM,
101  MapUnits
102  };
103 
105  {
107  XHeight
108  };
109 
112  {
113  Up,
116  Right
117  };
118 
120  : enabled( true )
121  , sizeType( MM )
122  , penWidth( 0.0 )
123  , labelPlacementMethod( QgsDiagramSettings::Height )
124  , diagramOrientation( QgsDiagramSettings::Up )
125  , barWidth( 5.0 )
126  , transparency( 0 )
127  , scaleByArea( true )
128  , angleOffset( 90 * 16 ) //top
129  , scaleBasedVisibility( false )
130  , minScaleDenominator( -1 )
131  , maxScaleDenominator( -1 )
132  , minimumSize( 0.0 )
133  {}
134  bool enabled;
140  QSizeF size; //size
141  SizeType sizeType; //mm or map units
144  double penWidth;
147  double barWidth;
148  int transparency; // 0 - 100
151 
153  //scale range (-1 if no lower / upper bound )
156 
158  double minimumSize;
159 
160  void readXML( const QDomElement& elem, const QgsVectorLayer* layer );
161  void writeXML( QDomElement& rendererElem, QDomDocument& doc, const QgsVectorLayer* layer ) const;
162 
167  QList< QgsLayerTreeModelLegendNode* > legendItems( QgsLayerTreeLayer* nodeLayer ) const;
168 
169 };
170 
171 //additional diagram settings for interpolated size rendering
173 {
174  public:
177  double lowerValue;
178  double upperValue;
183 };
184 
186 class CORE_EXPORT QgsDiagramRendererV2
187 {
188  public:
189 
191  virtual ~QgsDiagramRendererV2();
192 
195  virtual QgsDiagramRendererV2* clone() const = 0;
196 
198  virtual QSizeF sizeMapUnits( const QgsFeature& feature, const QgsRenderContext& c );
199 
200  virtual QString rendererName() const = 0;
201 
203  virtual QList<QString> diagramAttributes() const = 0;
204 
205  void renderDiagram( const QgsFeature& feature, QgsRenderContext& c, QPointF pos );
206 
207  void setDiagram( QgsDiagram* d );
208  QgsDiagram* diagram() const { return mDiagram; }
209 
211  virtual QList<QgsDiagramSettings> diagramSettings() const = 0;
212 
213  virtual void readXML( const QDomElement& elem, const QgsVectorLayer* layer ) = 0;
214  virtual void writeXML( QDomElement& layerElem, QDomDocument& doc, const QgsVectorLayer* layer ) const = 0;
215 
220  virtual QList< QgsLayerTreeModelLegendNode* > legendItems( QgsLayerTreeLayer* nodeLayer ) const;
221 
222  protected:
224 
230  virtual bool diagramSettings( const QgsFeature &feature, const QgsRenderContext& c, QgsDiagramSettings& s ) = 0;
231 
233  virtual QSizeF diagramSize( const QgsFeature& features, const QgsRenderContext& c ) = 0;
234 
236  void convertSizeToMapUnits( QSizeF& size, const QgsRenderContext& context ) const;
237 
239  static int dpiPaintDevice( const QPainter* );
240 
241  //read / write diagram
242  void _readXML( const QDomElement& elem, const QgsVectorLayer* layer );
243  void _writeXML( QDomElement& rendererElem, QDomDocument& doc, const QgsVectorLayer* layer ) const;
244 
247 };
248 
251 {
252  public:
255 
256  QgsSingleCategoryDiagramRenderer* clone() const override;
257 
258  QString rendererName() const override { return "SingleCategory"; }
259 
260  QList<QString> diagramAttributes() const override { return mSettings.categoryAttributes; }
261 
262  void setDiagramSettings( const QgsDiagramSettings& s ) { mSettings = s; }
263 
265 
266  void readXML( const QDomElement& elem, const QgsVectorLayer* layer ) override;
267  void writeXML( QDomElement& layerElem, QDomDocument& doc, const QgsVectorLayer* layer ) const override;
268 
270 
271  protected:
272  bool diagramSettings( const QgsFeature &feature, const QgsRenderContext& c, QgsDiagramSettings& s ) override;
273 
274  QSizeF diagramSize( const QgsFeature&, const QgsRenderContext& c ) override;
275 
276  private:
277  QgsDiagramSettings mSettings;
278 };
279 
281 {
282  public:
285 
287 
290 
291  void setDiagramSettings( const QgsDiagramSettings& s ) { mSettings = s; }
292 
293  QList<QString> diagramAttributes() const override;
294 
295  QString rendererName() const override { return "LinearlyInterpolated"; }
296 
297  void setLowerValue( double val ) { mInterpolationSettings.lowerValue = val; }
298  double lowerValue() const { return mInterpolationSettings.lowerValue; }
299 
300  void setUpperValue( double val ) { mInterpolationSettings.upperValue = val; }
301  double upperValue() const { return mInterpolationSettings.upperValue; }
302 
303  void setLowerSize( QSizeF s ) { mInterpolationSettings.lowerSize = s; }
304  QSizeF lowerSize() const { return mInterpolationSettings.lowerSize; }
305 
306  void setUpperSize( QSizeF s ) { mInterpolationSettings.upperSize = s; }
307  QSizeF upperSize() const { return mInterpolationSettings.upperSize; }
308 
309  int classificationAttribute() const { return mInterpolationSettings.classificationAttribute; }
310  void setClassificationAttribute( int index ) { mInterpolationSettings.classificationAttribute = index; }
311 
312  QString classificationAttributeExpression() const { return mInterpolationSettings.classificationAttributeExpression; }
313  void setClassificationAttributeExpression( const QString& expression ) { mInterpolationSettings.classificationAttributeExpression = expression; }
314 
315  bool classificationAttributeIsExpression() const { return mInterpolationSettings.classificationAttributeIsExpression; }
316  void setClassificationAttributeIsExpression( bool isExpression ) { mInterpolationSettings.classificationAttributeIsExpression = isExpression; }
317 
318  void readXML( const QDomElement& elem, const QgsVectorLayer* layer ) override;
319  void writeXML( QDomElement& layerElem, QDomDocument& doc, const QgsVectorLayer* layer ) const override;
320 
322 
323  protected:
324  bool diagramSettings( const QgsFeature &feature, const QgsRenderContext& c, QgsDiagramSettings& s ) override;
325 
326  QSizeF diagramSize( const QgsFeature&, const QgsRenderContext& c ) override;
327 
328  private:
329  QgsDiagramSettings mSettings;
330  QgsDiagramInterpolationSettings mInterpolationSettings;
331 };
332 
333 #endif // QGSDIAGRAMRENDERERV2_H
void setClassificationAttributeIsExpression(bool isExpression)
double zIndex
Z-index of diagrams, where diagrams with a higher z-index are drawn on top of diagrams with a lower z...
double minimumSize
Scale diagrams smaller than mMinimumSize to mMinimumSize.
static unsigned index
void setClassificationAttributeExpression(const QString &expression)
virtual QList< QgsLayerTreeModelLegendNode * > legendItems(QgsLayerTreeLayer *nodeLayer) const
Returns list of legend nodes for the diagram.
Renders the diagrams for all features with the same settings.
QList< QString > categoryAttributes
virtual QList< QString > diagramAttributes() const =0
Returns attribute indices needed for diagram rendering.
QgsDiagram * diagram() const
A set of features which influence the labelling process.
Definition: layer.h:55
virtual QList< QgsDiagramSettings > diagramSettings() const =0
Returns list with all diagram settings in the renderer.
Container of fields for a vector layer.
Definition: qgsfield.h:189
The feature class encapsulates a single feature including its id, geometry and a list of field/values...
Definition: qgsfeature.h:187
Returns diagram settings for a feature.
virtual QSizeF diagramSize(const QgsFeature &features, const QgsRenderContext &c)=0
Returns size of the diagram (in painter units) or an invalid size in case of error.
Perform transforms between map coordinates and device coordinates.
Definition: qgsmaptopixel.h:34
virtual void writeXML(QDomElement &layerElem, QDomDocument &doc, const QgsVectorLayer *layer) const =0
DiagramOrientation diagramOrientation
const QgsMapToPixel * xform
QList< QString > diagramAttributes() const override
Returns attribute indices needed for diagram rendering.
void setDiagramSettings(const QgsDiagramSettings &s)
DiagramOrientation
Orientation of histogram.
QgsDiagramRendererV2 * renderer
QString rendererName() const override
Base class for all diagram types.
Definition: qgsdiagram.h:35
Contains information about the context of a rendering operation.
virtual void readXML(const QDomElement &elem, const QgsVectorLayer *layer)=0
virtual QgsDiagramRendererV2 * clone() const =0
Returns new instance that is equivalent to this one.
Class for doing transforms between two map coordinate systems.
The QgsLegendRendererItem class is abstract interface for legend items returned from QgsMapLayerLegen...
QList< QString > categoryLabels
int classificationAttribute
Index of the classification attribute.
void setDiagramSettings(const QgsDiagramSettings &s)
Represents a vector layer which manages a vector based data sets.
QList< QColor > categoryColors
QgsDiagram * mDiagram
Reference to the object that does the real diagram rendering.
const QgsCoordinateTransform * ct
LabelPlacementMethod labelPlacementMethod
Layer tree node points to a map layer.