QGIS API Documentation  2.9.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;
32 class QgsPalGeometry;
34 class QgsMapToPixel;
35 class QgsVectorLayer;
37 class QgsLayerTreeLayer;
38 
39 namespace pal { class Layer; }
40 
41 class CORE_EXPORT QgsDiagramLayerSettings
42 {
43  public:
44  //avoid inclusion of QgsPalLabeling
45  enum Placement
46  {
47  AroundPoint = 0, // Point / Polygon
48  OverPoint, // Point / Polygon
49  Line, // Line / Polygon
50  Curved, // Line
51  Horizontal, // Polygon
52  Free // Polygon
53  };
54 
56  {
57  OnLine = 1,
58  AboveLine = 2,
59  BelowLine = 4,
60  MapOrientation = 8
61  };
62 
64 
66 
67  //pal placement properties
69  unsigned int placementFlags;
70  int priority; // 0 = low, 10 = high
71  bool obstacle; // whether it's an obstacle
72  double dist; // distance from the feature (in mm)
73  QgsDiagramRendererV2* renderer; // if any renderer is assigned, it is owned by this class
74 
75  //assigned when layer gets prepared
79  QList<QgsPalGeometry*> geometries;
81 
82  int xPosColumn; //attribute index for x coordinate (or -1 if position not data defined)
83  int yPosColumn;//attribute index for y coordinate (or -1 if position not data defined)
84  bool showAll;
85 
86  void readXML( const QDomElement& elem, const QgsVectorLayer* layer );
87  void writeXML( QDomElement& layerElem, QDomDocument& doc, const QgsVectorLayer* layer ) const;
88 };
89 
90 //diagram settings for rendering
91 class CORE_EXPORT QgsDiagramSettings
92 {
93  public:
94  enum SizeType
95  {
96  MM,
97  MapUnits
98  };
99 
101  {
103  XHeight
104  };
105 
108  {
109  Up,
112  Right
113  };
114 
116  : enabled( true )
117  , sizeType( MM )
118  , penWidth( 0.0 )
119  , labelPlacementMethod( QgsDiagramSettings::Height )
120  , diagramOrientation( QgsDiagramSettings::Up )
121  , barWidth( 5.0 )
122  , transparency( 0 )
123  , scaleByArea( true )
124  , angleOffset( 90 * 16 ) //top
125  , scaleBasedVisibility( false )
126  , minScaleDenominator( -1 )
127  , maxScaleDenominator( -1 )
128  , minimumSize( 0.0 )
129  {}
130  bool enabled;
131  QFont font;
132  QList< QColor > categoryColors;
133  QList< QString > categoryAttributes;
135  QList< QString > categoryLabels;
136  QSizeF size; //size
137  SizeType sizeType; //mm or map units
139  QColor penColor;
140  double penWidth;
143  double barWidth;
144  int transparency; // 0 - 100
147 
149  //scale range (-1 if no lower / upper bound )
152 
154  double minimumSize;
155 
156  void readXML( const QDomElement& elem, const QgsVectorLayer* layer );
157  void writeXML( QDomElement& rendererElem, QDomDocument& doc, const QgsVectorLayer* layer ) const;
158 
163  QList< QgsLayerTreeModelLegendNode* > legendItems( QgsLayerTreeLayer* nodeLayer ) const;
164 
165 };
166 
167 //additional diagram settings for interpolated size rendering
169 {
170  public:
171  QSizeF lowerSize;
172  QSizeF upperSize;
173  double lowerValue;
174  double upperValue;
179 };
180 
182 class CORE_EXPORT QgsDiagramRendererV2
183 {
184  public:
185 
187  virtual ~QgsDiagramRendererV2();
188 
191  virtual QgsDiagramRendererV2* clone() const = 0;
192 
194  virtual QSizeF sizeMapUnits( const QgsFeature& feature, const QgsRenderContext& c );
195 
196  virtual QString rendererName() const = 0;
197 
199  virtual QList<QString> diagramAttributes() const = 0;
200 
201  void renderDiagram( const QgsFeature& feature, QgsRenderContext& c, const QPointF& pos );
202 
203  void setDiagram( QgsDiagram* d );
204  QgsDiagram* diagram() const { return mDiagram; }
205 
207  virtual QList<QgsDiagramSettings> diagramSettings() const = 0;
208 
209  virtual void readXML( const QDomElement& elem, const QgsVectorLayer* layer ) = 0;
210  virtual void writeXML( QDomElement& layerElem, QDomDocument& doc, const QgsVectorLayer* layer ) const = 0;
211 
216  virtual QList< QgsLayerTreeModelLegendNode* > legendItems( QgsLayerTreeLayer* nodeLayer ) const;
217 
218  protected:
220 
226  virtual bool diagramSettings( const QgsFeature &feature, const QgsRenderContext& c, QgsDiagramSettings& s ) = 0;
227 
229  virtual QSizeF diagramSize( const QgsFeature& features, const QgsRenderContext& c ) = 0;
230 
232  void convertSizeToMapUnits( QSizeF& size, const QgsRenderContext& context ) const;
233 
235  static int dpiPaintDevice( const QPainter* );
236 
237  //read / write diagram
238  void _readXML( const QDomElement& elem, const QgsVectorLayer* layer );
239  void _writeXML( QDomElement& rendererElem, QDomDocument& doc, const QgsVectorLayer* layer ) const;
240 
243 };
244 
247 {
248  public:
251 
252  QgsDiagramRendererV2* clone() const override;
253 
254  QString rendererName() const override { return "SingleCategory"; }
255 
256  QList<QString> diagramAttributes() const override { return mSettings.categoryAttributes; }
257 
258  void setDiagramSettings( const QgsDiagramSettings& s ) { mSettings = s; }
259 
260  QList<QgsDiagramSettings> diagramSettings() const override;
261 
262  void readXML( const QDomElement& elem, const QgsVectorLayer* layer ) override;
263  void writeXML( QDomElement& layerElem, QDomDocument& doc, const QgsVectorLayer* layer ) const override;
264 
265  QList< QgsLayerTreeModelLegendNode* > legendItems( QgsLayerTreeLayer* nodeLayer ) const override;
266 
267  protected:
268  bool diagramSettings( const QgsFeature &feature, const QgsRenderContext& c, QgsDiagramSettings& s ) override;
269 
270  QSizeF diagramSize( const QgsFeature&, const QgsRenderContext& c ) override;
271 
272  private:
273  QgsDiagramSettings mSettings;
274 };
275 
277 {
278  public:
281 
282  QgsDiagramRendererV2* clone() const override;
283 
285  QList<QgsDiagramSettings> diagramSettings() const override;
286 
287  void setDiagramSettings( const QgsDiagramSettings& s ) { mSettings = s; }
288 
289  QList<QString> diagramAttributes() const override;
290 
291  QString rendererName() const override { return "LinearlyInterpolated"; }
292 
293  void setLowerValue( double val ) { mInterpolationSettings.lowerValue = val; }
294  double lowerValue() const { return mInterpolationSettings.lowerValue; }
295 
296  void setUpperValue( double val ) { mInterpolationSettings.upperValue = val; }
297  double upperValue() const { return mInterpolationSettings.upperValue; }
298 
299  void setLowerSize( QSizeF s ) { mInterpolationSettings.lowerSize = s; }
300  QSizeF lowerSize() const { return mInterpolationSettings.lowerSize; }
301 
302  void setUpperSize( QSizeF s ) { mInterpolationSettings.upperSize = s; }
303  QSizeF upperSize() const { return mInterpolationSettings.upperSize; }
304 
305  int classificationAttribute() const { return mInterpolationSettings.classificationAttribute; }
306  void setClassificationAttribute( int index ) { mInterpolationSettings.classificationAttribute = index; }
307 
308  QString classificationAttributeExpression() const { return mInterpolationSettings.classificationAttributeExpression; }
309  void setClassificationAttributeExpression( QString expression ) { mInterpolationSettings.classificationAttributeExpression = expression; }
310 
311  bool classificationAttributeIsExpression() const { return mInterpolationSettings.classificationAttributeIsExpression; }
312  void setClassificationAttributeIsExpression( bool isExpression ) { mInterpolationSettings.classificationAttributeIsExpression = isExpression; }
313 
314  void readXML( const QDomElement& elem, const QgsVectorLayer* layer ) override;
315  void writeXML( QDomElement& layerElem, QDomDocument& doc, const QgsVectorLayer* layer ) const override;
316 
317  QList< QgsLayerTreeModelLegendNode* > legendItems( QgsLayerTreeLayer* nodeLayer ) const override;
318 
319  protected:
320  bool diagramSettings( const QgsFeature &feature, const QgsRenderContext& c, QgsDiagramSettings& s ) override;
321 
322  QSizeF diagramSize( const QgsFeature&, const QgsRenderContext& c ) override;
323 
324  private:
325  QgsDiagramSettings mSettings;
326  QgsDiagramInterpolationSettings mInterpolationSettings;
327 };
328 
329 #endif // QGSDIAGRAMRENDERERV2_H
void setClassificationAttributeIsExpression(bool isExpression)
double minimumSize
Scale diagrams smaller than mMinimumSize to mMinimumSize.
static unsigned index
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 layer of spacial entites.
Definition: layer.h:65
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:173
The feature class encapsulates a single feature including its id, geometry and a list of field/values...
Definition: qgsfeature.h:119
void setClassificationAttributeExpression(QString expression)
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:34
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.
QList< QgsPalGeometry * > geometries
void setDiagramSettings(const QgsDiagramSettings &s)
Represents a vector layer which manages a vector based data sets.
double size
Definition: qgssvgcache.cpp:77
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.