QGIS API Documentation  2.99.0-Master (c42dad3)
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 "qgsdiagramrenderer.h"
17 #include "qgsrendercontext.h"
18 #include "qgsexpression.h"
19 #include "qgssymbollayerutils.h"
20 
21 #include <QPainter>
22 
23 
24 
26 {
27 
28 }
29 
31 {
32  Q_UNUSED( other );
33  // do not copy the cached expression map - the expressions need to be created and prepared with getExpression(...) call
34 }
35 
36 
38 {
39  QMapIterator<QString, QgsExpression*> i( mExpressions );
40  while ( i.hasNext() )
41  {
42  i.next();
43  delete i.value();
44  }
45  mExpressions.clear();
46 }
47 
48 QgsExpression *QgsDiagram::getExpression( const QString &expression, const QgsExpressionContext &context )
49 {
50  if ( !mExpressions.contains( expression ) )
51  {
52  QgsExpression* expr = new QgsExpression( expression );
53  expr->prepare( &context );
54  mExpressions[expression] = expr;
55  }
56  return mExpressions[expression];
57 }
58 
59 void QgsDiagram::setPenWidth( QPen& pen, const QgsDiagramSettings& s, const QgsRenderContext& c )
60 {
62 }
63 
64 
65 QSizeF QgsDiagram::sizePainterUnits( QSizeF size, const QgsDiagramSettings& s, const QgsRenderContext& c )
66 {
67  return QSizeF( QgsSymbolLayerUtils::convertToPainterUnits( c, size.width(), s.sizeType, s.sizeScale ), QgsSymbolLayerUtils::convertToPainterUnits( c, size.height(), s.sizeType, s.sizeScale ) );
68 }
69 
71 {
73 }
74 
76 {
77  QFont f = s.font;
79  {
80  int pixelsize = s.font.pointSizeF() / c.mapToPixel().mapUnitsPerPixel();
81  f.setPixelSize( pixelsize > 0 ? pixelsize : 1 );
82  }
83  else
84  {
85  f.setPixelSize( s.font.pointSizeF() * 0.376 * c.scaleFactor() );
86  }
87 
88  return f;
89 }
90 
91 QSizeF QgsDiagram::sizeForValue( double value, const QgsDiagramSettings &s, const QgsDiagramInterpolationSettings &is ) const
92 {
93  double scaledValue = value;
94  double scaledLowerValue = is.lowerValue;
95  double scaledUpperValue = is.upperValue;
96  double scaledLowerSizeWidth = is.lowerSize.width();
97  double scaledLowerSizeHeight = is.lowerSize.height();
98  double scaledUpperSizeWidth = is.upperSize.width();
99  double scaledUpperSizeHeight = is.upperSize.height();
100 
101  // interpolate the squared value if scale by area
102  if ( s.scaleByArea )
103  {
104  scaledValue = sqrt( scaledValue );
105  scaledLowerValue = sqrt( scaledLowerValue );
106  scaledUpperValue = sqrt( scaledUpperValue );
107  scaledLowerSizeWidth = sqrt( scaledLowerSizeWidth );
108  scaledLowerSizeHeight = sqrt( scaledLowerSizeHeight );
109  scaledUpperSizeWidth = sqrt( scaledUpperSizeWidth );
110  scaledUpperSizeHeight = sqrt( scaledUpperSizeHeight );
111  }
112 
113  //interpolate size
114  double scaledRatio = ( scaledValue - scaledLowerValue ) / ( scaledUpperValue - scaledLowerValue );
115 
116  QSizeF size = QSizeF( is.upperSize.width() * scaledRatio + is.lowerSize.width() * ( 1 - scaledRatio ),
117  is.upperSize.height() * scaledRatio + is.lowerSize.height() * ( 1 - scaledRatio ) );
118 
119  // Scale, if extension is smaller than the specified minimum
120  if ( size.width() <= s.minimumSize && size.height() <= s.minimumSize )
121  {
122  bool p = false; // preserve height == width
123  if ( qgsDoubleNear( size.width(), size.height() ) )
124  p = true;
125 
126  size.scale( s.minimumSize, s.minimumSize, Qt::KeepAspectRatio );
127 
128  // If height == width, recover here (overwrite floating point errors)
129  if ( p )
130  size.setWidth( size.height() );
131  }
132 
133  return size;
134 }
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").
QgsUnitTypes::RenderUnit lineSizeUnit
Line unit index.
double minimumSize
Scale diagrams smaller than mMinimumSize to mMinimumSize.
void clearCache()
Definition: qgsdiagram.cpp:37
bool qgsDoubleNear(double a, double b, double epsilon=4 *DBL_EPSILON)
Compare two doubles (but allow some difference)
Definition: qgis.h:196
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...
QgsMapUnitScale sizeScale
Diagram size unit scale.
double mapUnitsPerPixel() const
Return current map units per pixel.
Base class for all diagram types.
Definition: qgsdiagram.h:35
QgsUnitTypes::RenderUnit sizeType
Diagram size unit.
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
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:75
const QgsMapToPixel & mapToPixel() const
bool prepare(const QgsExpressionContext *context)
Get the expression ready for evaluation - find out column indexes.
QgsMapUnitScale lineSizeScale
Line unit scale.
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
double scaleFactor() const
static double convertToPainterUnits(const QgsRenderContext &c, double size, QgsUnitTypes::RenderUnit unit, const QgsMapUnitScale &scale=QgsMapUnitScale())
Converts a size from the specied units to painter units.
Stores the settings for rendering a single diagram.