QGIS API Documentation  2.12.0-Lyon
qgspiediagram.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  qgspiediagram.cpp
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 #include "qgspiediagram.h"
16 #include "qgsdiagramrendererv2.h"
17 #include "qgsrendercontext.h"
18 #include "qgsexpression.h"
19 
20 #include <QPainter>
21 
22 
24 {
25  mCategoryBrush.setStyle( Qt::SolidPattern );
26  mPen.setStyle( Qt::SolidLine );
27 }
28 
30 {
31 }
32 
34 {
35  return new QgsPieDiagram( *this );
36 }
37 
39 {
40  Q_UNUSED( c );
41 
42  QVariant attrVal;
44  {
45  QgsExpressionContext expressionContext = c.expressionContext();
46  if ( feature.fields() )
47  expressionContext.setFields( *feature.fields() );
48  expressionContext.setFeature( feature );
49 
50  QgsExpression* expression = getExpression( is.classificationAttributeExpression, expressionContext );
51  attrVal = expression->evaluate( &expressionContext );
52  }
53  else
54  {
55  attrVal = feature.attributes().at( is.classificationAttribute );
56  }
57 
58  if ( !attrVal.isValid() )
59  {
60  return QSizeF(); //zero size if attribute is missing
61  }
62 
63  double scaledValue = attrVal.toDouble();
64  double scaledLowerValue = is.lowerValue;
65  double scaledUpperValue = is.upperValue;
66  double scaledLowerSizeWidth = is.lowerSize.width();
67  double scaledLowerSizeHeight = is.lowerSize.height();
68  double scaledUpperSizeWidth = is.upperSize.width();
69  double scaledUpperSizeHeight = is.upperSize.height();
70 
71  // interpolate the squared value if scale by area
72  if ( s.scaleByArea )
73  {
74  scaledValue = sqrt( scaledValue );
75  scaledLowerValue = sqrt( scaledLowerValue );
76  scaledUpperValue = sqrt( scaledUpperValue );
77  scaledLowerSizeWidth = sqrt( scaledLowerSizeWidth );
78  scaledLowerSizeHeight = sqrt( scaledLowerSizeHeight );
79  scaledUpperSizeWidth = sqrt( scaledUpperSizeWidth );
80  scaledUpperSizeHeight = sqrt( scaledUpperSizeHeight );
81  }
82 
83  //interpolate size
84  double scaledRatio = ( scaledValue - scaledLowerValue ) / ( scaledUpperValue - scaledLowerValue );
85 
86  QSizeF size = QSizeF( is.upperSize.width() * scaledRatio + is.lowerSize.width() * ( 1 - scaledRatio ),
87  is.upperSize.height() * scaledRatio + is.lowerSize.height() * ( 1 - scaledRatio ) );
88 
89  // Scale, if extension is smaller than the specified minimum
90  if ( size.width() <= s.minimumSize && size.height() <= s.minimumSize )
91  {
92  bool p = false; // preserve height == width
93  if ( size.width() == size.height() )
94  p = true;
95 
96  size.scale( s.minimumSize, s.minimumSize, Qt::KeepAspectRatio );
97 
98  // If height == width, recover here (overwrite floating point errors)
99  if ( p )
100  size.setWidth( size.height() );
101  }
102 
103  return size;
104 }
105 
107 {
108  Q_UNUSED( c );
109  Q_UNUSED( attributes );
110  return s.size;
111 }
112 
113 int QgsPieDiagram::sCount = 0;
114 
115 void QgsPieDiagram::renderDiagram( const QgsFeature& feature, QgsRenderContext& c, const QgsDiagramSettings& s, const QPointF& position )
116 {
117  QPainter* p = c.painter();
118  if ( !p )
119  {
120  return;
121  }
122 
123  //get sum of values
124  QList<double> values;
125  double currentVal = 0;
126  double valSum = 0;
127  int valCount = 0;
128 
129  QgsExpressionContext expressionContext = c.expressionContext();
130  expressionContext.setFeature( feature );
131  if ( feature.fields() )
132  expressionContext.setFields( *feature.fields() );
133 
135  for ( ; catIt != s.categoryAttributes.constEnd(); ++catIt )
136  {
137  QgsExpression* expression = getExpression( *catIt, expressionContext );
138  currentVal = expression->evaluate( &expressionContext ).toDouble();
139  values.push_back( currentVal );
140  valSum += currentVal;
141  if ( currentVal ) valCount++;
142  }
143 
144  //draw the slices
145  double totalAngle = 0;
146  double currentAngle;
147 
148  //convert from mm / map units to painter units
149  QSizeF spu = sizePainterUnits( s.size, s, c );
150  double w = spu.width();
151  double h = spu.height();
152 
153  double baseX = position.x();
154  double baseY = position.y() - h;
155 
156  mPen.setColor( s.penColor );
157  setPenWidth( mPen, s, c );
158  p->setPen( mPen );
159 
160  // there are some values > 0 available
161  if ( valSum > 0 )
162  {
163  QList<double>::const_iterator valIt = values.constBegin();
165  for ( ; valIt != values.constEnd(); ++valIt, ++colIt )
166  {
167  if ( *valIt )
168  {
169  currentAngle = *valIt / valSum * 360 * 16;
170  mCategoryBrush.setColor( *colIt );
171  p->setBrush( mCategoryBrush );
172  // if only 1 value is > 0, draw a circle
173  if ( valCount == 1 )
174  {
175  p->drawEllipse( baseX, baseY, w, h );
176  }
177  else
178  {
179  p->drawPie( baseX, baseY, w, h, totalAngle + s.angleOffset, currentAngle );
180  }
181  totalAngle += currentAngle;
182  }
183  }
184  }
185  else // valSum > 0
186  {
187  // draw empty circle if no values are defined at all
188  mCategoryBrush.setColor( Qt::transparent );
189  p->setBrush( mCategoryBrush );
190  p->drawEllipse( baseX, baseY, w, h );
191  }
192 }
Class for parsing and evaluation of expressions (formerly called "search strings").
Definition: qgsexpression.h:92
double minimumSize
Scale diagrams smaller than mMinimumSize to mMinimumSize.
void setStyle(Qt::PenStyle style)
void scale(qreal width, qreal height, Qt::AspectRatioMode mode)
Q_DECL_DEPRECATED QVariant evaluate(const QgsFeature *f)
Evaluate the feature and return the result.
void push_back(const T &value)
QList< QString > categoryAttributes
Q_DECL_DEPRECATED QgsExpression * getExpression(const QString &expression, const QgsFields *fields)
Definition: qgsdiagram.cpp:47
void setFeature(const QgsFeature &feature)
Convenience function for setting a feature for the context.
The feature class encapsulates a single feature including its id, geometry and a list of field/values...
Definition: qgsfeature.h:176
void setStyle(Qt::BrushStyle style)
QSizeF sizePainterUnits(const QSizeF &size, const QgsDiagramSettings &s, const QgsRenderContext &c)
Calculates a size to match the current settings and rendering context.
Definition: qgsdiagram.cpp:84
qreal x() const
qreal y() const
void renderDiagram(const QgsFeature &feature, QgsRenderContext &c, const QgsDiagramSettings &s, const QPointF &position) override
Draws the diagram at the given position (in pixel coordinates)
QgsAttributes attributes() const
Returns the feature's attributes.
Definition: qgsfeature.cpp:92
void setPen(const QColor &color)
void drawEllipse(const QRectF &rectangle)
void setWidth(qreal width)
const QgsFields * fields() const
Returns the field map associated with the feature.
Definition: qgsfeature.cpp:164
Expression contexts are used to encapsulate the parameters around which a QgsExpression should be eva...
virtual QgsDiagram * clone() const override
Returns an instance that is equivalent to this one.
void setBrush(const QBrush &brush)
void setColor(const QColor &color)
void setFields(const QgsFields &fields)
Convenience function for setting a fields for the context.
Base class for all diagram types.
Definition: qgsdiagram.h:35
QgsExpressionContext & expressionContext()
Gets the expression context.
void drawPie(const QRectF &rectangle, int startAngle, int spanAngle)
const T & at(int i) const
Contains information about the context of a rendering operation.
QPainter * painter()
bool isValid() const
double toDouble(bool *ok) const
int classificationAttribute
Index of the classification attribute.
const_iterator constEnd() const
const_iterator constBegin() const
qreal height() const
void setPenWidth(QPen &pen, const QgsDiagramSettings &s, const QgsRenderContext &c)
Changes the pen width to match the current settings and rendering context.
Definition: qgsdiagram.cpp:71
A vector of attributes.
Definition: qgsfeature.h:109
QList< QColor > categoryColors
void setColor(const QColor &color)
qreal width() const
QSizeF diagramSize(const QgsAttributes &attributes, const QgsRenderContext &c, const QgsDiagramSettings &s) override
Returns the size in map units the diagram will use to render.