QGIS API Documentation  2.11.0-Master
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 "qgsdiagramrendererv2.h"
17 #include "qgsrendercontext.h"
18 #include "qgsexpression.h"
19 
20 #include <QPainter>
21 
22 
23 
25 {
26 
27 }
28 
30 {
31  Q_UNUSED( other );
32  // do not copy the cached expression map - the expressions need to be created and prepared with getExpression(...) call
33 }
34 
35 
37 {
38  QMapIterator<QString, QgsExpression*> i( mExpressions );
39  while ( i.hasNext() )
40  {
41  i.next();
42  delete i.value();
43  }
44  mExpressions.clear();
45 }
46 
47 QgsExpression* QgsDiagram::getExpression( const QString& expression, const QgsFields* fields )
48 {
50  if ( !mExpressions.contains( expression ) )
51  {
52  QgsExpression* expr = new QgsExpression( expression );
53  expr->prepare( *fields );
54  mExpressions[expression] = expr;
55  }
56  return mExpressions[expression];
58 }
59 
61 {
62  if ( !mExpressions.contains( expression ) )
63  {
64  QgsExpression* expr = new QgsExpression( expression );
65  expr->prepare( &context );
66  mExpressions[expression] = expr;
67  }
68  return mExpressions[expression];
69 }
70 
72 {
74  {
75  pen.setWidthF( s.penWidth * c.scaleFactor() );
76  }
77  else
78  {
80  }
81 }
82 
83 
85 {
87  {
88  return QSizeF( size.width() * c.scaleFactor(), size.height() * c.scaleFactor() );
89  }
90  else
91  {
92  return QSizeF( size.width() / c.mapToPixel().mapUnitsPerPixel(), size.height() / c.mapToPixel().mapUnitsPerPixel() );
93  }
94 }
95 
97 {
99  {
100  return l * c.scaleFactor();
101  }
102  else
103  {
104  return l / c.mapToPixel().mapUnitsPerPixel();
105  }
106 }
107 
109 {
110  QFont f = s.font;
111  if ( s.sizeType == QgsDiagramSettings::MM )
112  {
113  f.setPixelSize( s.font.pointSizeF() * 0.376 * c.scaleFactor() );
114  }
115  else
116  {
118  }
119 
120  return f;
121 }
122 
123 void QgsDiagram::renderDiagram( const QgsAttributes& attributes, QgsRenderContext& c, const QgsDiagramSettings& s, const QPointF& position )
124 {
125  QgsFeature feature;
126  feature.setAttributes( attributes );
127  renderDiagram( feature, c, s, position );
128 }
129 
131 {
132  QgsFeature feature;
133  feature.setAttributes( attributes );
134  return diagramSize( feature, c, s, is );
135 }
Class for parsing and evaluation of expressions (formerly called "search strings").
Definition: qgsexpression.h:88
bool contains(const Key &key) const
Q_DECL_DEPRECATED bool prepare(const QgsFields &fields)
Get the expression ready for evaluation - find out column indexes.
qreal pointSizeF() const
Q_DECL_DEPRECATED QgsExpression * getExpression(const QString &expression, const QgsFields *fields)
Definition: qgsdiagram.cpp:47
void clearCache()
Definition: qgsdiagram.cpp:36
#define Q_NOWARN_DEPRECATED_PUSH
Definition: qgis.h:464
Container of fields for a vector layer.
Definition: qgsfield.h:177
void setAttributes(const QgsAttributes &attrs)
Sets the feature's attributes.
Definition: qgsfeature.cpp:95
double scaleFactor() const
The feature class encapsulates a single feature including its id, geometry and a list of field/values...
Definition: qgsfeature.h:162
virtual Q_DECL_DEPRECATED void renderDiagram(const QgsAttributes &att, QgsRenderContext &c, const QgsDiagramSettings &s, const QPointF &position)
Definition: qgsdiagram.cpp:123
void clear()
QSizeF sizePainterUnits(const QSizeF &size, const QgsDiagramSettings &s, const QgsRenderContext &c)
Calculates a size to match the current settings and rendering context.
Definition: qgsdiagram.cpp:84
void setPixelSize(int pixelSize)
Expression contexts are used to encapsulate the parameters around which a QgsExpression should be eva...
void setWidthF(qreal width)
double mapUnitsPerPixel() const
Return current map units per pixel.
const T & value() const
virtual QSizeF diagramSize(const QgsAttributes &attributes, const QgsRenderContext &c, const QgsDiagramSettings &s)=0
Returns the size in map units the diagram will use to render.
Base class for all diagram types.
Definition: qgsdiagram.h:35
#define Q_NOWARN_DEPRECATED_POP
Definition: qgis.h:465
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:108
const QgsMapToPixel & mapToPixel() const
qreal height() const
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:71
A vector of attributes.
Definition: qgsfeature.h:109
qreal width() const
bool hasNext() const