QGIS API Documentation 3.99.0-Master (21b3aa880ba)
Loading...
Searching...
No Matches
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
17#include "qgsdiagramrenderer.h"
18#include "qgsrendercontext.h"
19
20#include <QPainter>
21
22QgsDiagram::QgsDiagram( const QgsDiagram & ): mExpressions{}
23{
24 // do not copy the cached expression map - the expressions need to be created and prepared with getExpression(...) call
25}
26
27
29{
30 QMapIterator<QString, QgsExpression *> i( mExpressions );
31 while ( i.hasNext() )
32 {
33 i.next();
34 delete i.value();
35 }
36 mExpressions.clear();
37}
38
39QgsExpression *QgsDiagram::getExpression( const QString &expression, const QgsExpressionContext &context )
40{
41 if ( !mExpressions.contains( expression ) )
42 {
43 QgsExpression *expr = new QgsExpression( expression );
44 expr->prepare( &context );
45 mExpressions[expression] = expr;
46 }
47 return mExpressions[expression];
48}
49
51{
52 pen.setWidthF( c.convertToPainterUnits( s.penWidth, s.lineSizeUnit, s.lineSizeScale ) );
53}
54
55
57{
58 return QSizeF( c.convertToPainterUnits( size.width(), s.sizeType, s.sizeScale ), c.convertToPainterUnits( size.height(), s.sizeType, s.sizeScale ) );
59}
60
62{
63 return c.convertToPainterUnits( l, s.sizeType, s.sizeScale );
64}
65
67{
68 QFont f = s.font;
70 {
71 const int pixelsize = s.font.pointSizeF() / c.mapToPixel().mapUnitsPerPixel();
72 f.setPixelSize( pixelsize > 0 ? pixelsize : 1 );
73 }
74 else
75 {
76 f.setPixelSize( s.font.pointSizeF() * 0.376 * c.scaleFactor() );
77 }
78
79 return f;
80}
81
82QSizeF QgsDiagram::sizeForValue( double value, const QgsDiagramSettings &s, const QgsDiagramInterpolationSettings &is ) const
83{
84 double scaledValue = value;
85 double scaledLowerValue = is.lowerValue;
86 double scaledUpperValue = is.upperValue;
87
88 // interpolate the squared value if scale by area
89 if ( s.scaleByArea )
90 {
91 scaledValue = std::sqrt( scaledValue );
92 scaledLowerValue = std::sqrt( scaledLowerValue );
93 scaledUpperValue = std::sqrt( scaledUpperValue );
94 }
95
96 //interpolate size
97 const double scaledRatio = ( scaledValue - scaledLowerValue ) / ( scaledUpperValue - scaledLowerValue );
98
99 QSizeF size = QSizeF( is.upperSize.width() * scaledRatio + is.lowerSize.width() * ( 1 - scaledRatio ),
100 is.upperSize.height() * scaledRatio + is.lowerSize.height() * ( 1 - scaledRatio ) );
101
102 // Scale, if extension is smaller than the specified minimum
103 if ( size.width() <= s.minimumSize && size.height() <= s.minimumSize )
104 {
105 bool p = false; // preserve height == width
106 if ( qgsDoubleNear( size.width(), size.height() ) )
107 p = true;
108
109 size.scale( s.minimumSize, s.minimumSize, Qt::KeepAspectRatio );
110
111 // If height == width, recover here (overwrite floating point errors)
112 if ( p )
113 size.setWidth( size.height() );
114 }
115
116 return size;
117}
@ MapUnits
Map units.
Definition qgis.h:5185
Additional diagram settings for interpolated size rendering.
Stores the settings for rendering a single diagram.
Qgis::RenderUnit sizeType
Diagram size unit.
QgsMapUnitScale lineSizeScale
Line unit scale.
QgsMapUnitScale sizeScale
Diagram size unit scale.
Qgis::RenderUnit lineSizeUnit
Line unit index.
double minimumSize
Scale diagrams smaller than mMinimumSize to mMinimumSize.
void setPenWidth(QPen &pen, const QgsDiagramSettings &s, const QgsRenderContext &c)
Changes the pen width to match the current settings and rendering context.
void clearCache()
QSizeF sizePainterUnits(QSizeF size, const QgsDiagramSettings &s, const QgsRenderContext &c)
Calculates a size to match the current settings and rendering context.
QgsDiagram()=default
QgsExpression * getExpression(const QString &expression, const QgsExpressionContext &context)
Returns a prepared expression for the specified context.
QSizeF sizeForValue(double value, const QgsDiagramSettings &s, const QgsDiagramInterpolationSettings &interpolationSettings) const
Returns the scaled size of a diagram for a value, respecting the specified diagram interpolation sett...
QFont scaledFont(const QgsDiagramSettings &s, const QgsRenderContext &c)
Calculates a size to match the current settings and rendering context.
Expression contexts are used to encapsulate the parameters around which a QgsExpression should be eva...
Handles parsing and evaluation of expressions (formerly called "search strings").
bool prepare(const QgsExpressionContext *context)
Gets the expression ready for evaluation - find out column indexes.
Contains information about the context of a rendering operation.
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
bool qgsDoubleNear(double a, double b, double epsilon=4 *std::numeric_limits< double >::epsilon())
Compare two doubles (but allow some difference).
Definition qgis.h:6607