QGIS API Documentation  2.18.21-Las Palmas (9fba24a)
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  bool ok = false;
59  double value = attrVal.toDouble( &ok );
60  if ( !ok )
61  {
62  return QSizeF(); //zero size if attribute is missing
63  }
64 
65  return sizeForValue( value, s, is );
66 }
67 
68 double QgsPieDiagram::legendSize( double value, const QgsDiagramSettings &s, const QgsDiagramInterpolationSettings &is ) const
69 {
70  QSizeF size = sizeForValue( value, s, is );
71  return qMax( size.width(), size.height() );
72 }
73 
75 {
76  Q_UNUSED( c );
77  Q_UNUSED( attributes );
78  return s.size;
79 }
80 
82 {
83  QPainter* p = c.painter();
84  if ( !p )
85  {
86  return;
87  }
88 
89  //get sum of values
90  QList<double> values;
91  double currentVal = 0;
92  double valSum = 0;
93  int valCount = 0;
94 
95  QgsExpressionContext expressionContext = c.expressionContext();
96  expressionContext.setFeature( feature );
97  if ( feature.fields() )
98  expressionContext.setFields( *feature.fields() );
99 
101  for ( ; catIt != s.categoryAttributes.constEnd(); ++catIt )
102  {
103  QgsExpression* expression = getExpression( *catIt, expressionContext );
104  currentVal = expression->evaluate( &expressionContext ).toDouble();
105  values.push_back( currentVal );
106  valSum += currentVal;
107  if ( currentVal ) valCount++;
108  }
109 
110  //draw the slices
111  double totalAngle = 0;
112  double currentAngle;
113 
114  //convert from mm / map units to painter units
115  QSizeF spu = sizePainterUnits( s.size, s, c );
116  double w = spu.width();
117  double h = spu.height();
118 
119  double baseX = position.x();
120  double baseY = position.y() - h;
121 
122  mPen.setColor( s.penColor );
123  setPenWidth( mPen, s, c );
124  p->setPen( mPen );
125 
126  // there are some values > 0 available
127  if ( valSum > 0 )
128  {
129  QList<double>::const_iterator valIt = values.constBegin();
131  for ( ; valIt != values.constEnd(); ++valIt, ++colIt )
132  {
133  if ( *valIt )
134  {
135  currentAngle = *valIt / valSum * 360 * 16;
136  mCategoryBrush.setColor( *colIt );
137  p->setBrush( mCategoryBrush );
138  // if only 1 value is > 0, draw a circle
139  if ( valCount == 1 )
140  {
141  p->drawEllipse( baseX, baseY, w, h );
142  }
143  else
144  {
145  p->drawPie( baseX, baseY, w, h, totalAngle + s.angleOffset, currentAngle );
146  }
147  totalAngle += currentAngle;
148  }
149  }
150  }
151  else // valSum > 0
152  {
153  // draw empty circle if no values are defined at all
154  mCategoryBrush.setColor( Qt::transparent );
155  p->setBrush( mCategoryBrush );
156  p->drawEllipse( baseX, baseY, w, h );
157  }
158 }
QSizeF sizePainterUnits(QSizeF size, const QgsDiagramSettings &s, const QgsRenderContext &c)
Calculates a size to match the current settings and rendering context.
Definition: qgsdiagram.cpp:77
Class for parsing and evaluation of expressions (formerly called "search strings").
void setStyle(Qt::PenStyle style)
QgsAttributes attributes() const
Returns the feature&#39;s attributes.
Definition: qgsfeature.cpp:110
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:187
void setStyle(Qt::BrushStyle style)
virtual QgsPieDiagram * clone() const override
Returns an instance that is equivalent to this one.
qreal x() const
qreal y() const
double legendSize(double value, const QgsDiagramSettings &s, const QgsDiagramInterpolationSettings &is) const override
Returns the size of the legend item for the diagram corresponding to a specified value.
const QgsFields * fields() const
Returns the field map associated with the feature.
Definition: qgsfeature.cpp:188
void setPen(const QColor &color)
void drawEllipse(const QRectF &rectangle)
Expression contexts are used to encapsulate the parameters around which a QgsExpression should be eva...
void setBrush(const QBrush &brush)
void setColor(const QColor &color)
void setFields(const QgsFields &fields)
Convenience function for setting a fields for the context.
QSizeF sizeForValue(double value, const QgsDiagramSettings &s, const QgsDiagramInterpolationSettings &is) const
Returns the scaled size of a diagram for a value, respecting the specified diagram interpolation sett...
Definition: qgsdiagram.cpp:103
QgsExpressionContext & expressionContext()
Gets the expression context.
void drawPie(const QRectF &rectangle, int startAngle, int spanAngle)
const T & at(int i) const
Additional diagram settings for interpolated size rendering.
Contains information about the context of a rendering operation.
QPainter * painter()
void renderDiagram(const QgsFeature &feature, QgsRenderContext &c, const QgsDiagramSettings &s, QPointF position) override
Draws the diagram at the given position (in pixel coordinates)
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:115
QList< QColor > categoryColors
void setColor(const QColor &color)
qreal width() const
Stores the settings for rendering a single diagram.
QSizeF diagramSize(const QgsAttributes &attributes, const QgsRenderContext &c, const QgsDiagramSettings &s) override
Returns the size in map units the diagram will use to render.