QGIS API Documentation  3.16.0-Hannover (43b64b13f3)
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 
18 #include <QPainter>
19 
20 QgsDiagram::QgsDiagram( const QgsDiagram & ): mExpressions{}
21 {
22  // do not copy the cached expression map - the expressions need to be created and prepared with getExpression(...) call
23 }
24 
25 
27 {
28  QMapIterator<QString, QgsExpression *> i( mExpressions );
29  while ( i.hasNext() )
30  {
31  i.next();
32  delete i.value();
33  }
34  mExpressions.clear();
35 }
36 
37 QgsExpression *QgsDiagram::getExpression( const QString &expression, const QgsExpressionContext &context )
38 {
39  if ( !mExpressions.contains( expression ) )
40  {
41  QgsExpression *expr = new QgsExpression( expression );
42  expr->prepare( &context );
43  mExpressions[expression] = expr;
44  }
45  return mExpressions[expression];
46 }
47 
48 void QgsDiagram::setPenWidth( QPen &pen, const QgsDiagramSettings &s, const QgsRenderContext &c )
49 {
50  pen.setWidthF( c.convertToPainterUnits( s.penWidth, s.lineSizeUnit, s.lineSizeScale ) );
51 }
52 
53 
54 QSizeF QgsDiagram::sizePainterUnits( QSizeF size, const QgsDiagramSettings &s, const QgsRenderContext &c )
55 {
56  return QSizeF( c.convertToPainterUnits( size.width(), s.sizeType, s.sizeScale ), c.convertToPainterUnits( size.height(), s.sizeType, s.sizeScale ) );
57 }
58 
60 {
61  return c.convertToPainterUnits( l, s.sizeType, s.sizeScale );
62 }
63 
65 {
66  QFont f = s.font;
68  {
69  int pixelsize = s.font.pointSizeF() / c.mapToPixel().mapUnitsPerPixel();
70  f.setPixelSize( pixelsize > 0 ? pixelsize : 1 );
71  }
72  else
73  {
74  f.setPixelSize( s.font.pointSizeF() * 0.376 * c.scaleFactor() );
75  }
76 
77  return f;
78 }
79 
80 QSizeF QgsDiagram::sizeForValue( double value, const QgsDiagramSettings &s, const QgsDiagramInterpolationSettings &is ) const
81 {
82  double scaledValue = value;
83  double scaledLowerValue = is.lowerValue;
84  double scaledUpperValue = is.upperValue;
85 
86  // interpolate the squared value if scale by area
87  if ( s.scaleByArea )
88  {
89  scaledValue = std::sqrt( scaledValue );
90  scaledLowerValue = std::sqrt( scaledLowerValue );
91  scaledUpperValue = std::sqrt( scaledUpperValue );
92  }
93 
94  //interpolate size
95  double scaledRatio = ( scaledValue - scaledLowerValue ) / ( scaledUpperValue - scaledLowerValue );
96 
97  QSizeF size = QSizeF( is.upperSize.width() * scaledRatio + is.lowerSize.width() * ( 1 - scaledRatio ),
98  is.upperSize.height() * scaledRatio + is.lowerSize.height() * ( 1 - scaledRatio ) );
99 
100  // Scale, if extension is smaller than the specified minimum
101  if ( size.width() <= s.minimumSize && size.height() <= s.minimumSize )
102  {
103  bool p = false; // preserve height == width
104  if ( qgsDoubleNear( size.width(), size.height() ) )
105  p = true;
106 
107  size.scale( s.minimumSize, s.minimumSize, Qt::KeepAspectRatio );
108 
109  // If height == width, recover here (overwrite floating point errors)
110  if ( p )
111  size.setWidth( size.height() );
112  }
113 
114  return size;
115 }
QgsDiagram::getExpression
QgsExpression * getExpression(const QString &expression, const QgsExpressionContext &context)
Returns a prepared expression for the specified context.
Definition: qgsdiagram.cpp:37
QgsExpressionContext
Expression contexts are used to encapsulate the parameters around which a QgsExpression should be eva...
Definition: qgsexpressioncontext.h:370
QgsDiagramInterpolationSettings::upperSize
QSizeF upperSize
Definition: qgsdiagramrenderer.h:665
QgsDiagram
Base class for all diagram types.
Definition: qgsdiagram.h:40
QgsDiagram::sizeForValue
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:80
QgsDiagramInterpolationSettings::lowerValue
double lowerValue
Definition: qgsdiagramrenderer.h:666
QgsDiagramSettings::minimumSize
double minimumSize
Scale diagrams smaller than mMinimumSize to mMinimumSize.
Definition: qgsdiagramrenderer.h:486
QgsRenderContext
Contains information about the context of a rendering operation.
Definition: qgsrendercontext.h:58
qgsdiagram.h
QgsDiagram::scaledFont
QFont scaledFont(const QgsDiagramSettings &s, const QgsRenderContext &c)
Calculates a size to match the current settings and rendering context.
Definition: qgsdiagram.cpp:64
QgsDiagramInterpolationSettings::upperValue
double upperValue
Definition: qgsdiagramrenderer.h:667
QgsDiagramSettings::scaleByArea
bool scaleByArea
Definition: qgsdiagramrenderer.h:459
QgsDiagramInterpolationSettings::lowerSize
QSizeF lowerSize
Definition: qgsdiagramrenderer.h:664
QgsDiagram::setPenWidth
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:48
QgsDiagram::sizePainterUnits
QSizeF sizePainterUnits(QSizeF size, const QgsDiagramSettings &s, const QgsRenderContext &c)
Calculates a size to match the current settings and rendering context.
Definition: qgsdiagram.cpp:54
qgsDoubleNear
bool qgsDoubleNear(double a, double b, double epsilon=4 *std::numeric_limits< double >::epsilon())
Compare two doubles (but allow some difference)
Definition: qgis.h:315
qgsdiagramrenderer.h
QgsExpression::prepare
bool prepare(const QgsExpressionContext *context)
Gets the expression ready for evaluation - find out column indexes.
Definition: qgsexpression.cpp:323
QgsDiagramInterpolationSettings
Additional diagram settings for interpolated size rendering.
Definition: qgsdiagramrenderer.h:662
QgsDiagramSettings::lineSizeScale
QgsMapUnitScale lineSizeScale
Line unit scale.
Definition: qgsdiagramrenderer.h:447
QgsDiagram::clearCache
void clearCache()
Definition: qgsdiagram.cpp:26
QgsDiagramSettings::font
QFont font
Definition: qgsdiagramrenderer.h:419
c
As part of the API refactoring and improvements which landed in the Processing API was substantially reworked from the x version This was done in order to allow much of the underlying Processing framework to be ported into c
Definition: porting_processing.dox:1
QgsDiagramSettings::sizeScale
QgsMapUnitScale sizeScale
Diagram size unit scale.
Definition: qgsdiagramrenderer.h:435
QgsDiagramSettings::penWidth
double penWidth
Definition: qgsdiagramrenderer.h:451
QgsDiagramSettings::sizeType
QgsUnitTypes::RenderUnit sizeType
Diagram size unit.
Definition: qgsdiagramrenderer.h:429
QgsDiagram::QgsDiagram
QgsDiagram()=default
Constructor for QgsDiagram.
QgsDiagramSettings::lineSizeUnit
QgsUnitTypes::RenderUnit lineSizeUnit
Line unit index.
Definition: qgsdiagramrenderer.h:441
QgsExpression
Class for parsing and evaluation of expressions (formerly called "search strings").
Definition: qgsexpression.h:105
QgsUnitTypes::RenderMapUnits
@ RenderMapUnits
Map units.
Definition: qgsunittypes.h:169
QgsDiagramSettings
Stores the settings for rendering a single diagram.
Definition: qgsdiagramrenderer.h:381