QGIS API Documentation  2.18.21-Las Palmas (9fba24a)
qgshistogramdiagram.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  qgshistogramdiagram.cpp
3  ---------------------
4  begin : August 2012
5  copyright : (C) 2012 by Matthias Kuhn
6  email : matthias at opengis 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 "qgshistogramdiagram.h"
16 #include "qgsdiagramrendererv2.h"
17 #include "qgsrendercontext.h"
18 #include "qgsexpression.h"
19 
20 #include <QPainter>
21 
23 {
24  mCategoryBrush.setStyle( Qt::SolidPattern );
25  mPen.setStyle( Qt::SolidLine );
26  mScaleFactor = 0;
27 }
28 
30 {
31 }
32 
34 {
35  return new QgsHistogramDiagram( *this );
36 }
37 
39 {
40  QSizeF size;
41  if ( feature.attributes().isEmpty() )
42  {
43  return size; //zero size if no attributes
44  }
45 
46  if ( qgsDoubleNear( is.upperValue, is.lowerValue ) )
47  return size; // invalid value range => zero size
48 
49  double maxValue = 0;
50 
51  QgsExpressionContext expressionContext = c.expressionContext();
52  expressionContext.setFeature( feature );
53  if ( feature.fields() )
54  expressionContext.setFields( *feature.fields() );
55 
56  Q_FOREACH ( const QString& cat, s.categoryAttributes )
57  {
58  QgsExpression* expression = getExpression( cat, expressionContext );
59  maxValue = qMax( expression->evaluate( &expressionContext ).toDouble(), maxValue );
60  }
61 
62  // Scale, if extension is smaller than the specified minimum
63  if ( maxValue < s.minimumSize )
64  {
65  maxValue = s.minimumSize;
66  }
67 
68  switch ( s.diagramOrientation )
69  {
72  mScaleFactor = (( is.upperSize.width() - is.lowerSize.height() ) / ( is.upperValue - is.lowerValue ) );
73  size.scale( s.barWidth * s.categoryAttributes.size(), maxValue * mScaleFactor, Qt::IgnoreAspectRatio );
74  break;
75 
78  mScaleFactor = (( is.upperSize.width() - is.lowerSize.width() ) / ( is.upperValue - is.lowerValue ) );
79  size.scale( maxValue * mScaleFactor, s.barWidth * s.categoryAttributes.size(), Qt::IgnoreAspectRatio );
80  break;
81  }
82 
83  return size;
84 }
85 
87 {
88  if ( qgsDoubleNear( is.upperValue, is.lowerValue ) )
89  return s.minimumSize; // invalid value range => zero size
90 
91  // Scale, if extension is smaller than the specified minimum
92  if ( value < s.minimumSize )
93  {
94  value = s.minimumSize;
95  }
96 
97  double scaleFactor = (( is.upperSize.width() - is.lowerSize.width() ) / ( is.upperValue - is.lowerValue ) );
98  return value * scaleFactor;
99 }
100 
102 {
103  Q_UNUSED( c );
104  QSizeF size;
105 
106  if ( attributes.isEmpty() )
107  {
108  return QSizeF(); //zero size if no attributes
109  }
110 
111  double maxValue = attributes.at( 0 ).toDouble();
112 
113  for ( int i = 0; i < attributes.count(); ++i )
114  {
115  maxValue = qMax( attributes.at( i ).toDouble(), maxValue );
116  }
117 
118  switch ( s.diagramOrientation )
119  {
122  mScaleFactor = maxValue / s.size.height();
123  size.scale( s.barWidth * s.categoryColors.size(), s.size.height(), Qt::IgnoreAspectRatio );
124  break;
125 
128  default: // just in case...
129  mScaleFactor = maxValue / s.size.width();
130  size.scale( s.size.width(), s.barWidth * s.categoryColors.size(), Qt::IgnoreAspectRatio );
131  break;
132  }
133 
134  return size;
135 }
136 
138 {
139  QPainter* p = c.painter();
140  if ( !p )
141  {
142  return;
143  }
144 
145  QList<double> values;
146  double maxValue = 0;
147 
148  QgsExpressionContext expressionContext = c.expressionContext();
149  expressionContext.setFeature( feature );
150  if ( feature.fields() )
151  expressionContext.setFields( *feature.fields() );
152 
153  Q_FOREACH ( const QString& cat, s.categoryAttributes )
154  {
155  QgsExpression* expression = getExpression( cat, expressionContext );
156  double currentVal = expression->evaluate( &expressionContext ).toDouble();
157  values.push_back( currentVal );
158  maxValue = qMax( currentVal, maxValue );
159  }
160 
161  double scaledMaxVal = sizePainterUnits( maxValue * mScaleFactor, s, c );
162 
163  double currentOffset = 0;
164  double scaledWidth = sizePainterUnits( s.barWidth, s, c );
165 
166  double baseX = position.x();
167  double baseY = position.y();
168 
169  mPen.setColor( s.penColor );
170  setPenWidth( mPen, s, c );
171  p->setPen( mPen );
172 
173  QList<double>::const_iterator valIt = values.constBegin();
175  for ( ; valIt != values.constEnd(); ++valIt, ++colIt )
176  {
177  double length = sizePainterUnits( *valIt * mScaleFactor, s, c );
178 
179  mCategoryBrush.setColor( *colIt );
180  p->setBrush( mCategoryBrush );
181 
182  switch ( s.diagramOrientation )
183  {
185  p->drawRect( baseX + currentOffset, baseY, scaledWidth, length * -1 );
186  break;
187 
189  p->drawRect( baseX + currentOffset, baseY - scaledMaxVal, scaledWidth, length );
190  break;
191 
193  p->drawRect( baseX, baseY - currentOffset, length, scaledWidth * -1 );
194  break;
195 
197  p->drawRect( baseX + scaledMaxVal, baseY - currentOffset, 0 - length, scaledWidth * -1 );
198  break;
199  }
200 
201  currentOffset += scaledWidth;
202  }
203 }
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 renderDiagram(const QgsFeature &feature, QgsRenderContext &c, const QgsDiagramSettings &s, QPointF position) override
Draws the diagram at the given position (in pixel coordinates)
QSizeF diagramSize(const QgsAttributes &attributes, const QgsRenderContext &c, const QgsDiagramSettings &s) override
Returns the size in map units the diagram will use to render.
double minimumSize
Scale diagrams smaller than mMinimumSize to mMinimumSize.
void setStyle(Qt::PenStyle style)
QgsAttributes attributes() const
Returns the feature&#39;s attributes.
Definition: qgsfeature.cpp:110
void scale(qreal width, qreal height, Qt::AspectRatioMode mode)
Q_DECL_DEPRECATED QVariant evaluate(const QgsFeature *f)
Evaluate the feature and return the result.
virtual QgsHistogramDiagram * clone() const override
Returns an instance that is equivalent to this one.
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.
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.
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)
bool qgsDoubleNear(double a, double b, double epsilon=4 *DBL_EPSILON)
Compare two doubles (but allow some difference)
Definition: qgis.h:353
int size() const
void drawRect(const QRectF &rectangle)
DiagramOrientation diagramOrientation
qreal x() const
qreal y() const
const QgsFields * fields() const
Returns the field map associated with the feature.
Definition: qgsfeature.cpp:188
void setPen(const QColor &color)
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.
QgsExpressionContext & expressionContext()
Gets the expression context.
const T & at(int i) const
Additional diagram settings for interpolated size rendering.
Contains information about the context of a rendering operation.
QPainter * painter()
bool isEmpty() const
int count(const T &value) const
double toDouble(bool *ok) const
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.