QGIS API Documentation
4.3.0-Master (16eddb2670e)
Toggle main menu visibility
Loading...
Searching...
No Matches
src
core
diagram
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
22
QgsDiagram::QgsDiagram
(
const
QgsDiagram
& )
23
: mExpressions {}
24
{
25
// do not copy the cached expression map - the expressions need to be created and prepared with getExpression(...) call
26
}
27
28
29
void
QgsDiagram::clearCache
()
30
{
31
QMapIterator<QString, QgsExpression *> i( mExpressions );
32
while
( i.hasNext() )
33
{
34
i.next();
35
delete
i.value();
36
}
37
mExpressions.clear();
38
}
39
40
QgsExpression
*
QgsDiagram::getExpression
(
const
QString &expression,
const
QgsExpressionContext
&context )
41
{
42
if
( !mExpressions.contains( expression ) )
43
{
44
QgsExpression
*expr =
new
QgsExpression
( expression );
45
expr->
prepare
( &context );
46
mExpressions[expression] = expr;
47
}
48
return
mExpressions[expression];
49
}
50
51
void
QgsDiagram::setPenWidth
( QPen &pen,
const
QgsDiagramSettings
&s,
const
QgsRenderContext
&
c
)
52
{
53
pen.setWidthF(
c
.convertToPainterUnits( s.
penWidth
, s.
lineSizeUnit
, s.
lineSizeScale
) );
54
}
55
56
57
QSizeF
QgsDiagram::sizePainterUnits
( QSizeF size,
const
QgsDiagramSettings
&s,
const
QgsRenderContext
&
c
)
58
{
59
return
QSizeF(
c
.convertToPainterUnits( size.width(), s.
sizeType
, s.
sizeScale
),
c
.convertToPainterUnits( size.height(), s.
sizeType
, s.
sizeScale
) );
60
}
61
62
double
QgsDiagram::sizePainterUnits
(
double
l,
const
QgsDiagramSettings
&s,
const
QgsRenderContext
&
c
)
63
{
64
return
c
.convertToPainterUnits( l, s.
sizeType
, s.
sizeScale
);
65
}
66
67
QFont
QgsDiagram::scaledFont
(
const
QgsDiagramSettings
&s,
const
QgsRenderContext
&
c
)
68
{
69
QFont f = s.
font
;
70
if
( s.
sizeType
==
Qgis::RenderUnit::MapUnits
)
71
{
72
const
int
pixelsize = s.
font
.pointSizeF() /
c
.mapToPixel().mapUnitsPerPixel();
73
f.setPixelSize( pixelsize > 0 ? pixelsize : 1 );
74
}
75
else
76
{
77
f.setPixelSize( s.
font
.pointSizeF() * 0.376 *
c
.scaleFactor() );
78
}
79
80
return
f;
81
}
82
83
QSizeF
QgsDiagram::sizeForValue
(
double
value,
const
QgsDiagramSettings
&s,
const
QgsDiagramInterpolationSettings
&is )
const
84
{
85
double
scaledValue = value;
86
double
scaledLowerValue = is.
lowerValue
;
87
double
scaledUpperValue = is.
upperValue
;
88
89
// interpolate the squared value if scale by area
90
if
( s.
scaleByArea
)
91
{
92
scaledValue = std::sqrt( scaledValue );
93
scaledLowerValue = std::sqrt( scaledLowerValue );
94
scaledUpperValue = std::sqrt( scaledUpperValue );
95
}
96
97
//interpolate size
98
const
double
scaledRatio = ( scaledValue - scaledLowerValue ) / ( scaledUpperValue - scaledLowerValue );
99
100
QSizeF size = QSizeF( is.
upperSize
.width() * scaledRatio + is.
lowerSize
.width() * ( 1 - scaledRatio ), 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
}
Qgis::RenderUnit::MapUnits
@ MapUnits
Map units.
Definition
qgis.h:5760
QgsDiagramInterpolationSettings
Additional diagram settings for interpolated size rendering.
Definition
qgsdiagramrenderer.h:718
QgsDiagramInterpolationSettings::upperSize
QSizeF upperSize
Definition
qgsdiagramrenderer.h:721
QgsDiagramInterpolationSettings::lowerSize
QSizeF lowerSize
Definition
qgsdiagramrenderer.h:720
QgsDiagramInterpolationSettings::lowerValue
double lowerValue
Definition
qgsdiagramrenderer.h:722
QgsDiagramInterpolationSettings::upperValue
double upperValue
Definition
qgsdiagramrenderer.h:723
QgsDiagramSettings
Stores the settings for rendering a single diagram.
Definition
qgsdiagramrenderer.h:367
QgsDiagramSettings::scaleByArea
bool scaleByArea
Definition
qgsdiagramrenderer.h:448
QgsDiagramSettings::sizeType
Qgis::RenderUnit sizeType
Diagram size unit.
Definition
qgsdiagramrenderer.h:420
QgsDiagramSettings::font
QFont font
Definition
qgsdiagramrenderer.h:411
QgsDiagramSettings::lineSizeScale
QgsMapUnitScale lineSizeScale
Line unit scale.
Definition
qgsdiagramrenderer.h:435
QgsDiagramSettings::sizeScale
QgsMapUnitScale sizeScale
Diagram size unit scale.
Definition
qgsdiagramrenderer.h:425
QgsDiagramSettings::lineSizeUnit
Qgis::RenderUnit lineSizeUnit
Line unit index.
Definition
qgsdiagramrenderer.h:430
QgsDiagramSettings::minimumSize
double minimumSize
Scale diagrams smaller than mMinimumSize to mMinimumSize.
Definition
qgsdiagramrenderer.h:474
QgsDiagramSettings::penWidth
double penWidth
Definition
qgsdiagramrenderer.h:439
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:51
QgsDiagram::clearCache
void clearCache()
Definition
qgsdiagram.cpp:29
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:57
QgsDiagram::QgsDiagram
QgsDiagram()=default
QgsDiagram::getExpression
QgsExpression * getExpression(const QString &expression, const QgsExpressionContext &context)
Returns a prepared expression for the specified context.
Definition
qgsdiagram.cpp:40
QgsDiagram::sizeForValue
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...
Definition
qgsdiagram.cpp:83
QgsDiagram::scaledFont
QFont scaledFont(const QgsDiagramSettings &s, const QgsRenderContext &c)
Calculates a size to match the current settings and rendering context.
Definition
qgsdiagram.cpp:67
QgsExpressionContext
Expression contexts are used to encapsulate the parameters around which a QgsExpression should be eva...
Definition
qgsexpressioncontext.h:491
QgsExpression
Handles parsing and evaluation of expressions (formerly called "search strings").
Definition
qgsexpression.h:197
QgsExpression::prepare
bool prepare(const QgsExpressionContext *context)
Gets the expression ready for evaluation - find out column indexes.
Definition
qgsexpression.cpp:373
QgsRenderContext
Contains information about the context of a rendering operation.
Definition
qgsrendercontext.h:63
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
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:7743
qgsdiagram.h
qgsdiagramrenderer.h
qgsrendercontext.h
Generated on
for QGIS API Documentation by
1.17.0