QGIS API Documentation  3.0.2-Girona (307d082)
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 
24 {
25  Q_UNUSED( other );
26  // do not copy the cached expression map - the expressions need to be created and prepared with getExpression(...) call
27 }
28 
29 
31 {
32  QMapIterator<QString, QgsExpression *> i( mExpressions );
33  while ( i.hasNext() )
34  {
35  i.next();
36  delete i.value();
37  }
38  mExpressions.clear();
39 }
40 
41 QgsExpression *QgsDiagram::getExpression( const QString &expression, const QgsExpressionContext &context )
42 {
43  if ( !mExpressions.contains( expression ) )
44  {
45  QgsExpression *expr = new QgsExpression( expression );
46  expr->prepare( &context );
47  mExpressions[expression] = expr;
48  }
49  return mExpressions[expression];
50 }
51 
52 void QgsDiagram::setPenWidth( QPen &pen, const QgsDiagramSettings &s, const QgsRenderContext &c )
53 {
54  pen.setWidthF( c.convertToPainterUnits( s.penWidth, s.lineSizeUnit, s.lineSizeScale ) );
55 }
56 
57 
58 QSizeF QgsDiagram::sizePainterUnits( QSizeF size, const QgsDiagramSettings &s, const QgsRenderContext &c )
59 {
60  return QSizeF( c.convertToPainterUnits( size.width(), s.sizeType, s.sizeScale ), c.convertToPainterUnits( size.height(), s.sizeType, s.sizeScale ) );
61 }
62 
64 {
65  return c.convertToPainterUnits( l, s.sizeType, s.sizeScale );
66 }
67 
69 {
70  QFont f = s.font;
72  {
73  int pixelsize = s.font.pointSizeF() / c.mapToPixel().mapUnitsPerPixel();
74  f.setPixelSize( pixelsize > 0 ? pixelsize : 1 );
75  }
76  else
77  {
78  f.setPixelSize( s.font.pointSizeF() * 0.376 * c.scaleFactor() );
79  }
80 
81  return f;
82 }
83 
84 QSizeF QgsDiagram::sizeForValue( double value, const QgsDiagramSettings &s, const QgsDiagramInterpolationSettings &is ) const
85 {
86  double scaledValue = value;
87  double scaledLowerValue = is.lowerValue;
88  double scaledUpperValue = is.upperValue;
89 
90  // interpolate the squared value if scale by area
91  if ( s.scaleByArea )
92  {
93  scaledValue = std::sqrt( scaledValue );
94  scaledLowerValue = std::sqrt( scaledLowerValue );
95  scaledUpperValue = std::sqrt( scaledUpperValue );
96  }
97 
98  //interpolate size
99  double scaledRatio = ( scaledValue - scaledLowerValue ) / ( scaledUpperValue - scaledLowerValue );
100 
101  QSizeF size = QSizeF( is.upperSize.width() * scaledRatio + is.lowerSize.width() * ( 1 - scaledRatio ),
102  is.upperSize.height() * scaledRatio + is.lowerSize.height() * ( 1 - scaledRatio ) );
103 
104  // Scale, if extension is smaller than the specified minimum
105  if ( size.width() <= s.minimumSize && size.height() <= s.minimumSize )
106  {
107  bool p = false; // preserve height == width
108  if ( qgsDoubleNear( size.width(), size.height() ) )
109  p = true;
110 
111  size.scale( s.minimumSize, s.minimumSize, Qt::KeepAspectRatio );
112 
113  // If height == width, recover here (overwrite floating point errors)
114  if ( p )
115  size.setWidth( size.height() );
116  }
117 
118  return size;
119 }
QSizeF sizePainterUnits(QSizeF size, const QgsDiagramSettings &s, const QgsRenderContext &c)
Calculates a size to match the current settings and rendering context.
Definition: qgsdiagram.cpp:58
QgsUnitTypes::RenderUnit lineSizeUnit
Line unit index.
double minimumSize
Scale diagrams smaller than mMinimumSize to mMinimumSize.
void clearCache()
Definition: qgsdiagram.cpp:30
bool qgsDoubleNear(double a, double b, double epsilon=4 *DBL_EPSILON)
Compare two doubles (but allow some difference)
Definition: qgis.h:251
QgsDiagram()=default
Constructor for QgsDiagram.
QgsExpression * getExpression(const QString &expression, const QgsExpressionContext &context)
Returns a prepared expression for the specified context.
Definition: qgsdiagram.cpp:41
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:38
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:84
Additional diagram settings for interpolated size rendering.
Contains information about the context of a rendering operation.
double convertToPainterUnits(double size, QgsUnitTypes::RenderUnit unit, const QgsMapUnitScale &scale=QgsMapUnitScale()) const
Converts a size from the specified units to painter units (pixels).
QFont scaledFont(const QgsDiagramSettings &s, const QgsRenderContext &c)
Calculates a size to match the current settings and rendering context.
Definition: qgsdiagram.cpp:68
const QgsMapToPixel & mapToPixel() const
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:52
double scaleFactor() const
Returns the scaling factor for the render to convert painter units to physical sizes.
Stores the settings for rendering a single diagram.