QGIS API Documentation  2.12.0-Lyon
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 
63 
65 
66  //pal placement properties
68  unsigned int placementFlags;
69  int priority; // 0 = low, 10 = high
70  bool obstacle; // whether it's an obstacle
71  double dist; // distance from the feature (in mm)
72  QgsDiagramRendererV2* renderer; // if any renderer is assigned, it is owned by this class
73 
74  //assigned when layer gets prepared
78 
79  int xPosColumn; //attribute index for x coordinate (or -1 if position not data defined)
80  int yPosColumn;//attribute index for y coordinate (or -1 if position not data defined)
81  bool showAll;
82 
83  void readXML( const QDomElement& elem, const QgsVectorLayer* layer );
84  void writeXML( QDomElement& layerElem, QDomDocument& doc, const QgsVectorLayer* layer ) const;
85 };
86 
87 //diagram settings for rendering
88 class CORE_EXPORT QgsDiagramSettings
89 {
90  public:
91  enum SizeType
92  {
93  MM,
94  MapUnits
95  };
96 
98  {
100  XHeight
101  };
102 
105  {
106  Up,
109  Right
110  };
111 
113  : enabled( true )
114  , sizeType( MM )
115  , penWidth( 0.0 )
116  , labelPlacementMethod( QgsDiagramSettings::Height )
117  , diagramOrientation( QgsDiagramSettings::Up )
118  , barWidth( 5.0 )
119  , transparency( 0 )
120  , scaleByArea( true )
121  , angleOffset( 90 * 16 ) //top
122  , scaleBasedVisibility( false )
123  , minScaleDenominator( -1 )
124  , maxScaleDenominator( -1 )
125  , minimumSize( 0.0 )
126  {}
127  bool enabled;
133  QSizeF size; //size
134  SizeType sizeType; //mm or map units
137  double penWidth;
140  double barWidth;
141  int transparency; // 0 - 100
144 
146  //scale range (-1 if no lower / upper bound )
149 
151  double minimumSize;
152 
153  void readXML( const QDomElement& elem, const QgsVectorLayer* layer );
154  void writeXML( QDomElement& rendererElem, QDomDocument& doc, const QgsVectorLayer* layer ) const;
155 
160  QList< QgsLayerTreeModelLegendNode* > legendItems( QgsLayerTreeLayer* nodeLayer ) const;
161 
162 };
163 
164 //additional diagram settings for interpolated size rendering
166 {
167  public:
170  double lowerValue;
171  double upperValue;
176 };
177 
179 class CORE_EXPORT QgsDiagramRendererV2
180 {
181  public:
182 
184  virtual ~QgsDiagramRendererV2();
185 
188  virtual QgsDiagramRendererV2* clone() const = 0;
189 
191  virtual QSizeF sizeMapUnits( const QgsFeature& feature, const QgsRenderContext& c );
192 
193  virtual QString rendererName() const = 0;
194 
196  virtual QList<QString> diagramAttributes() const = 0;
197 
198  void renderDiagram( const QgsFeature& feature, QgsRenderContext& c, const QPointF& pos );
199 
200  void setDiagram( QgsDiagram* d );
201  QgsDiagram* diagram() const { return mDiagram; }
202 
204  virtual QList<QgsDiagramSettings> diagramSettings() const = 0;
205 
206  virtual void readXML( const QDomElement& elem, const QgsVectorLayer* layer ) = 0;
207  virtual void writeXML( QDomElement& layerElem, QDomDocument& doc, const QgsVectorLayer* layer ) const = 0;
208 
213  virtual QList< QgsLayerTreeModelLegendNode* > legendItems( QgsLayerTreeLayer* nodeLayer ) const;
214 
215  protected:
217 
223  virtual bool diagramSettings( const QgsFeature &feature, const QgsRenderContext& c, QgsDiagramSettings& s ) = 0;
224 
226  virtual QSizeF diagramSize( const QgsFeature& features, const QgsRenderContext& c ) = 0;
227 
229  void convertSizeToMapUnits( QSizeF& size, const QgsRenderContext& context ) const;
230 
232  static int dpiPaintDevice( const QPainter* );
233 
234  //read / write diagram
235  void _readXML( const QDomElement& elem, const QgsVectorLayer* layer );
236  void _writeXML( QDomElement& rendererElem, QDomDocument& doc, const QgsVectorLayer* layer ) const;
237 
240 };
241 
244 {
245  public:
248 
249  QgsDiagramRendererV2* clone() const override;
250 
251  QString rendererName() const override { return "SingleCategory"; }
252 
253  QList<QString> diagramAttributes() const override { return mSettings.categoryAttributes; }
254 
255  void setDiagramSettings( const QgsDiagramSettings& s ) { mSettings = s; }
256 
258 
259  void readXML( const QDomElement& elem, const QgsVectorLayer* layer ) override;
260  void writeXML( QDomElement& layerElem, QDomDocument& doc, const QgsVectorLayer* layer ) const override;
261 
263 
264  protected:
265  bool diagramSettings( const QgsFeature &feature, const QgsRenderContext& c, QgsDiagramSettings& s ) override;
266 
267  QSizeF diagramSize( const QgsFeature&, const QgsRenderContext& c ) override;
268 
269  private:
270  QgsDiagramSettings mSettings;
271 };
272 
274 {
275  public:
278 
279  QgsDiagramRendererV2* clone() const override;
280 
283 
284  void setDiagramSettings( const QgsDiagramSettings& s ) { mSettings = s; }
285 
286  QList<QString> diagramAttributes() const override;
287 
288  QString rendererName() const override { return "LinearlyInterpolated"; }
289 
290  void setLowerValue( double val ) { mInterpolationSettings.lowerValue = val; }
291  double lowerValue() const { return mInterpolationSettings.lowerValue; }
292 
293  void setUpperValue( double val ) { mInterpolationSettings.upperValue = val; }
294  double upperValue() const { return mInterpolationSettings.upperValue; }
295 
296  void setLowerSize( QSizeF s ) { mInterpolationSettings.lowerSize = s; }
297  QSizeF lowerSize() const { return mInterpolationSettings.lowerSize; }
298 
299  void setUpperSize( QSizeF s ) { mInterpolationSettings.upperSize = s; }
300  QSizeF upperSize() const { return mInterpolationSettings.upperSize; }
301 
302  int classificationAttribute() const { return mInterpolationSettings.classificationAttribute; }
303  void setClassificationAttribute( int index ) { mInterpolationSettings.classificationAttribute = index; }
304 
305  QString classificationAttributeExpression() const { return mInterpolationSettings.classificationAttributeExpression; }
306  void setClassificationAttributeExpression( const QString& expression ) { mInterpolationSettings.classificationAttributeExpression = expression; }
307 
308  bool classificationAttributeIsExpression() const { return mInterpolationSettings.classificationAttributeIsExpression; }
309  void setClassificationAttributeIsExpression( bool isExpression ) { mInterpolationSettings.classificationAttributeIsExpression = isExpression; }
310 
311  void readXML( const QDomElement& elem, const QgsVectorLayer* layer ) override;
312  void writeXML( QDomElement& layerElem, QDomDocument& doc, const QgsVectorLayer* layer ) const override;
313 
315 
316  protected:
317  bool diagramSettings( const QgsFeature &feature, const QgsRenderContext& c, QgsDiagramSettings& s ) override;
318 
319  QSizeF diagramSize( const QgsFeature&, const QgsRenderContext& c ) override;
320 
321  private:
322  QgsDiagramSettings mSettings;
323  QgsDiagramInterpolationSettings mInterpolationSettings;
324 };
325 
326 #endif // QGSDIAGRAMRENDERERV2_H
void setClassificationAttributeIsExpression(bool isExpression)
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 layer of spacial entites.
Definition: layer.h:57
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:177
The feature class encapsulates a single feature including its id, geometry and a list of field/values...
Definition: qgsfeature.h:176
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.