QGIS API Documentation  2.7.0-Master
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
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 {
49  if ( !mExpressions.contains( expression ) )
50  {
51  QgsExpression* expr = new QgsExpression( expression );
52  expr->prepare( *fields );
53  mExpressions[expression] = expr;
54  }
55  return mExpressions[expression];
56 }
57 
58 void QgsDiagram::setPenWidth( QPen& pen, const QgsDiagramSettings& s, const QgsRenderContext& c )
59 {
61  {
62  pen.setWidthF( s.penWidth * c.scaleFactor() );
63  }
64  else
65  {
66  pen.setWidthF( s.penWidth / c.mapToPixel().mapUnitsPerPixel() );
67  }
68 }
69 
70 
71 QSizeF QgsDiagram::sizePainterUnits( const QSizeF& size, const QgsDiagramSettings& s, const QgsRenderContext& c )
72 {
74  {
75  return QSizeF( size.width() * c.scaleFactor(), size.height() * c.scaleFactor() );
76  }
77  else
78  {
79  return QSizeF( size.width() / c.mapToPixel().mapUnitsPerPixel(), size.height() / c.mapToPixel().mapUnitsPerPixel() );
80  }
81 }
82 
84 {
86  {
87  return l * c.scaleFactor();
88  }
89  else
90  {
91  return l / c.mapToPixel().mapUnitsPerPixel();
92  }
93 }
94 
96 {
97  QFont f = s.font;
99  {
100  f.setPixelSize( s.font.pointSizeF() * 0.376 * c.scaleFactor() );
101  }
102  else
103  {
104  f.setPixelSize( s.font.pointSizeF() / c.mapToPixel().mapUnitsPerPixel() );
105  }
106 
107  return f;
108 }
109 
110 void QgsDiagram::renderDiagram( const QgsAttributes& attributes, QgsRenderContext& c, const QgsDiagramSettings& s, const QPointF& position )
111 {
112  QgsFeature feature;
113  feature.setAttributes( attributes );
114  renderDiagram( feature, c, s, position );
115 }
116 
118 {
119  QgsFeature feature;
120  feature.setAttributes( attributes );
121  return diagramSize( feature, c, s, is );
122 }
Class for parsing and evaluation of expressions (formerly called "search strings").
Definition: qgsexpression.h:87
bool prepare(const QgsFields &fields)
Get the expression ready for evaluation - find out column indexes.
QgsExpression * getExpression(const QString &expression, const QgsFields *fields)
Definition: qgsdiagram.cpp:47
void clearCache()
Definition: qgsdiagram.cpp:36
Container of fields for a vector layer.
Definition: qgsfield.h:172
void setAttributes(const QgsAttributes &attrs)
Definition: qgsfeature.h:144
double scaleFactor() const
The feature class encapsulates a single feature including its id, geometry and a list of field/values...
Definition: qgsfeature.h:113
virtual Q_DECL_DEPRECATED void renderDiagram(const QgsAttributes &att, QgsRenderContext &c, const QgsDiagramSettings &s, const QPointF &position)
Definition: qgsdiagram.cpp:110
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:71
double mapUnitsPerPixel() const
Return current map units per pixel.
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:34
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:95
QVector< QVariant > QgsAttributes
Definition: qgsfeature.h:100
const QgsMapToPixel & mapToPixel() 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:58
double size
Definition: qgssvgcache.cpp:77