QGIS API Documentation  2.17.0-Master (f49f7ce)
qgsdiagram.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  qgsdiagram.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 "qgsdiagram.h"
16 #include "qgsdiagramrendererv2.h"
17 #include "qgsrendercontext.h"
18 #include "qgsexpression.h"
19 
20 #include <QPainter>
21 
22 
23 
25 {
26 
27 }
28 
30 {
31  Q_UNUSED( other );
32  // do not copy the cached expression map - the expressions need to be created and prepared with getExpression(...) call
33 }
34 
35 
37 {
38  QMapIterator<QString, QgsExpression*> i( mExpressions );
39  while ( i.hasNext() )
40  {
41  i.next();
42  delete i.value();
43  }
44  mExpressions.clear();
45 }
46 
47 QgsExpression* QgsDiagram::getExpression( const QString& expression, const QgsFields* fields )
48 {
50  if ( !mExpressions.contains( expression ) )
51  {
52  QgsExpression* expr = new QgsExpression( expression );
53  expr->prepare( *fields );
54  mExpressions[expression] = expr;
55  }
56  return mExpressions[expression];
58 }
59 
61 {
62  if ( !mExpressions.contains( expression ) )
63  {
64  QgsExpression* expr = new QgsExpression( expression );
65  expr->prepare( &context );
66  mExpressions[expression] = expr;
67  }
68  return mExpressions[expression];
69 }
70 
72 {
74 }
75 
76 
78 {
80 }
81 
83 {
85 }
86 
88 {
89  QFont f = s.font;
90  if ( s.sizeType == QgsSymbolV2::MapUnit )
91  {
92  int pixelsize = s.font.pointSizeF() / c.mapToPixel().mapUnitsPerPixel();
93  f.setPixelSize( pixelsize > 0 ? pixelsize : 1 );
94  }
95  else
96  {
97  f.setPixelSize( s.font.pointSizeF() * 0.376 * c.scaleFactor() );
98  }
99 
100  return f;
101 }
102 
104 {
105  double scaledValue = value;
106  double scaledLowerValue = is.lowerValue;
107  double scaledUpperValue = is.upperValue;
108  double scaledLowerSizeWidth = is.lowerSize.width();
109  double scaledLowerSizeHeight = is.lowerSize.height();
110  double scaledUpperSizeWidth = is.upperSize.width();
111  double scaledUpperSizeHeight = is.upperSize.height();
112 
113  // interpolate the squared value if scale by area
114  if ( s.scaleByArea )
115  {
116  scaledValue = sqrt( scaledValue );
117  scaledLowerValue = sqrt( scaledLowerValue );
118  scaledUpperValue = sqrt( scaledUpperValue );
119  scaledLowerSizeWidth = sqrt( scaledLowerSizeWidth );
120  scaledLowerSizeHeight = sqrt( scaledLowerSizeHeight );
121  scaledUpperSizeWidth = sqrt( scaledUpperSizeWidth );
122  scaledUpperSizeHeight = sqrt( scaledUpperSizeHeight );
123  }
124 
125  //interpolate size
126  double scaledRatio = ( scaledValue - scaledLowerValue ) / ( scaledUpperValue - scaledLowerValue );
127 
128  QSizeF size = QSizeF( is.upperSize.width() * scaledRatio + is.lowerSize.width() * ( 1 - scaledRatio ),
129  is.upperSize.height() * scaledRatio + is.lowerSize.height() * ( 1 - scaledRatio ) );
130 
131  // Scale, if extension is smaller than the specified minimum
132  if ( size.width() <= s.minimumSize && size.height() <= s.minimumSize )
133  {
134  bool p = false; // preserve height == width
135  if ( qgsDoubleNear( size.width(), size.height() ) )
136  p = true;
137 
138  size.scale( s.minimumSize, s.minimumSize, Qt::KeepAspectRatio );
139 
140  // If height == width, recover here (overwrite floating point errors)
141  if ( p )
142  size.setWidth( size.height() );
143  }
144 
145  return size;
146 }
147 
148 void QgsDiagram::renderDiagram( const QgsAttributes& attributes, QgsRenderContext& c, const QgsDiagramSettings& s, QPointF position )
149 {
150  QgsFeature feature;
151  feature.setAttributes( attributes );
152  renderDiagram( feature, c, s, position );
153 }
154 
156 {
157  QgsFeature feature;
158  feature.setAttributes( attributes );
159  return diagramSize( feature, c, s, is );
160 }
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").
double minimumSize
Scale diagrams smaller than mMinimumSize to mMinimumSize.
void scale(qreal width, qreal height, Qt::AspectRatioMode mode)
bool contains(const Key &key) const
Q_DECL_DEPRECATED bool prepare(const QgsFields &fields)
Get the expression ready for evaluation - find out column indexes.
qreal pointSizeF() const
Q_DECL_DEPRECATED QgsExpression * getExpression(const QString &expression, const QgsFields *fields)
Definition: qgsdiagram.cpp:47
void clearCache()
Definition: qgsdiagram.cpp:36
#define Q_NOWARN_DEPRECATED_PUSH
Definition: qgis.h:515
Container of fields for a vector layer.
Definition: qgsfield.h:252
void setAttributes(const QgsAttributes &attrs)
Sets the feature&#39;s attributes.
Definition: qgsfeature.cpp:115
double scaleFactor() const
The feature class encapsulates a single feature including its id, geometry and a list of field/values...
Definition: qgsfeature.h:187
void clear()
bool qgsDoubleNear(double a, double b, double epsilon=4 *DBL_EPSILON)
Compare two doubles (but allow some difference)
Definition: qgis.h:353
void setPixelSize(int pixelSize)
QgsSymbolV2::OutputUnit sizeType
Diagram size unit.
static double convertToPainterUnits(const QgsRenderContext &c, double size, QgsSymbolV2::OutputUnit unit, const QgsMapUnitScale &scale=QgsMapUnitScale())
Converts a size from the specied units to painter units.
void setWidth(qreal width)
Expression contexts are used to encapsulate the parameters around which a QgsExpression should be eva...
The output shall be in map unitx.
Definition: qgssymbolv2.h:68
void setWidthF(qreal width)
QgsMapUnitScale sizeScale
Diagram size unit scale.
double mapUnitsPerPixel() const
Return current map units per pixel.
const T & value() const
virtual QSizeF diagramSize(const QgsAttributes &attributes, const QgsRenderContext &c, const QgsDiagramSettings &s)=0
Returns the size in map units the diagram will use to render.
Base class for all diagram types.
Definition: qgsdiagram.h:36
#define Q_NOWARN_DEPRECATED_POP
Definition: qgis.h:516
Additional diagram settings for interpolated size rendering.
Contains information about the context of a rendering operation.
QFont scaledFont(const QgsDiagramSettings &s, const QgsRenderContext &c)
Calculates a size to match the current settings and rendering context.
Definition: qgsdiagram.cpp:87
virtual Q_DECL_DEPRECATED void renderDiagram(const QgsAttributes &att, QgsRenderContext &c, const QgsDiagramSettings &s, QPointF position)
Definition: qgsdiagram.cpp:148
const QgsMapToPixel & mapToPixel() const
QgsMapUnitScale lineSizeScale
Line unit scale.
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
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
qreal width() const
Stores the settings for rendering a single diagram.
bool hasNext() const
QgsSymbolV2::OutputUnit lineSizeUnit
Line unit index.