QGIS API Documentation  2.99.0-Master (40f86b2)
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 "qgsdiagramrenderer.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  return new QgsPieDiagram( *this );
32 }
33 
35 {
36  Q_UNUSED( c );
37 
38  QVariant attrVal;
40  {
41  QgsExpressionContext expressionContext = c.expressionContext();
42  if ( !feature.fields().isEmpty() )
43  expressionContext.setFields( feature.fields() );
44  expressionContext.setFeature( feature );
45 
46  QgsExpression *expression = getExpression( is.classificationAttributeExpression, expressionContext );
47  attrVal = expression->evaluate( &expressionContext );
48  }
49  else
50  {
51  attrVal = feature.attribute( is.classificationField );
52  }
53 
54  bool ok = false;
55  double value = attrVal.toDouble( &ok );
56  if ( !ok )
57  {
58  return QSizeF(); //zero size if attribute is missing
59  }
60 
61  return sizeForValue( value, s, is );
62 }
63 
64 double QgsPieDiagram::legendSize( double value, const QgsDiagramSettings &s, const QgsDiagramInterpolationSettings &is ) const
65 {
66  QSizeF size = sizeForValue( value, s, is );
67  return qMax( size.width(), size.height() );
68 }
69 
70 QSizeF QgsPieDiagram::diagramSize( const QgsAttributes &attributes, const QgsRenderContext &c, const QgsDiagramSettings &s )
71 {
72  Q_UNUSED( c );
73  Q_UNUSED( attributes );
74  return s.size;
75 }
76 
77 void QgsPieDiagram::renderDiagram( const QgsFeature &feature, QgsRenderContext &c, const QgsDiagramSettings &s, QPointF position )
78 {
79  QPainter *p = c.painter();
80  if ( !p )
81  {
82  return;
83  }
84 
85  //get sum of values
86  QList<double> values;
87  double currentVal = 0;
88  double valSum = 0;
89  int valCount = 0;
90 
91  QgsExpressionContext expressionContext = c.expressionContext();
92  expressionContext.setFeature( feature );
93  if ( !feature.fields().isEmpty() )
94  expressionContext.setFields( feature.fields() );
95 
96  QList<QString>::const_iterator catIt = s.categoryAttributes.constBegin();
97  for ( ; catIt != s.categoryAttributes.constEnd(); ++catIt )
98  {
99  QgsExpression *expression = getExpression( *catIt, expressionContext );
100  currentVal = expression->evaluate( &expressionContext ).toDouble();
101  values.push_back( currentVal );
102  valSum += currentVal;
103  if ( currentVal ) valCount++;
104  }
105 
106  //draw the slices
107  double totalAngle = 0;
108  double currentAngle;
109 
110  //convert from mm / map units to painter units
111  QSizeF spu = sizePainterUnits( s.size, s, c );
112  double w = spu.width();
113  double h = spu.height();
114 
115  double baseX = position.x();
116  double baseY = position.y() - h;
117 
118  mPen.setColor( s.penColor );
119  setPenWidth( mPen, s, c );
120  p->setPen( mPen );
121 
122  // there are some values > 0 available
123  if ( valSum > 0 )
124  {
125  QList<double>::const_iterator valIt = values.constBegin();
126  QList< QColor >::const_iterator colIt = s.categoryColors.constBegin();
127  for ( ; valIt != values.constEnd(); ++valIt, ++colIt )
128  {
129  if ( *valIt )
130  {
131  currentAngle = *valIt / valSum * 360 * 16;
132  mCategoryBrush.setColor( *colIt );
133  p->setBrush( mCategoryBrush );
134  // if only 1 value is > 0, draw a circle
135  if ( valCount == 1 )
136  {
137  p->drawEllipse( baseX, baseY, w, h );
138  }
139  else
140  {
141  p->drawPie( baseX, baseY, w, h, totalAngle + s.angleOffset, currentAngle );
142  }
143  totalAngle += currentAngle;
144  }
145  }
146  }
147  else // valSum > 0
148  {
149  // draw empty circle if no values are defined at all
150  mCategoryBrush.setColor( Qt::transparent );
151  p->setBrush( mCategoryBrush );
152  p->drawEllipse( baseX, baseY, w, h );
153  }
154 }
QSizeF sizePainterUnits(QSizeF size, const QgsDiagramSettings &s, const QgsRenderContext &c)
Calculates a size to match the current settings and rendering context.
Definition: qgsdiagram.cpp:65
Class for parsing and evaluation of expressions (formerly called "search strings").
QList< QString > categoryAttributes
void setFeature(const QgsFeature &feature)
Convenience function for setting a feature for the context.
QVariant evaluate()
Evaluate the feature and return the result.
The feature class encapsulates a single feature including its id, geometry and a list of field/values...
Definition: qgsfeature.h:136
QgsFields fields
Definition: qgsfeature.h:142
virtual QgsPieDiagram * clone() const override
Returns an instance that is equivalent to this one.
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.
QgsExpression * getExpression(const QString &expression, const QgsExpressionContext &context)
Returns a prepared expression for the specified context.
Definition: qgsdiagram.cpp:48
Expression contexts are used to encapsulate the parameters around which a QgsExpression should be eva...
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:91
QgsExpressionContext & expressionContext()
Gets the expression context.
Additional diagram settings for interpolated size rendering.
Contains information about the context of a rendering operation.
QPainter * painter()
Returns the destination QPainter for the render operation.
QString classificationField
Name of the field for classification.
bool isEmpty() const
Check whether the container is empty.
Definition: qgsfields.cpp:110
void renderDiagram(const QgsFeature &feature, QgsRenderContext &c, const QgsDiagramSettings &s, QPointF position) override
Draws the diagram at the given position (in pixel coordinates)
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:59
A vector of attributes.
Definition: qgsfeature.h:56
QVariant attribute(const QString &name) const
Lookup attribute value from attribute name.
Definition: qgsfeature.cpp:267
QList< QColor > categoryColors
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.