QGIS API Documentation  2.9.0-Master
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
qgsdiagramrendererv2.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  qgsdiagramrendererv2.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 "qgsdiagramrendererv2.h"
16 #include "qgsvectorlayer.h"
17 #include "diagram/qgstextdiagram.h"
18 #include "diagram/qgspiediagram.h"
20 #include "qgsrendercontext.h"
21 
22 #include <QDomElement>
23 #include <QPainter>
24 
26  : placement( AroundPoint )
27  , placementFlags( OnLine )
28  , priority( 5 )
29  , obstacle( false )
30  , dist( 0.0 )
31  , renderer( 0 )
32  , palLayer( 0 )
33  , ct( 0 )
34  , xform( 0 )
35  , xPosColumn( -1 )
36  , yPosColumn( -1 )
37  , showAll( true )
38 {
39 }
40 
42 {
43  delete renderer;
44 }
45 
46 void QgsDiagramLayerSettings::readXML( const QDomElement& elem, const QgsVectorLayer* layer )
47 {
48  Q_UNUSED( layer )
49 
50  placement = ( Placement )elem.attribute( "placement" ).toInt();
51  placementFlags = ( LinePlacementFlags )elem.attribute( "linePlacementFlags" ).toInt();
52  priority = elem.attribute( "priority" ).toInt();
53  obstacle = elem.attribute( "obstacle" ).toInt();
54  dist = elem.attribute( "dist" ).toDouble();
55  xPosColumn = elem.attribute( "xPosColumn" ).toInt();
56  yPosColumn = elem.attribute( "yPosColumn" ).toInt();
57  showAll = ( elem.attribute( "showAll", "0" ) != "0" );
58 }
59 
60 void QgsDiagramLayerSettings::writeXML( QDomElement& layerElem, QDomDocument& doc, const QgsVectorLayer* layer ) const
61 {
62  Q_UNUSED( layer )
63 
64  QDomElement diagramLayerElem = doc.createElement( "DiagramLayerSettings" );
65  diagramLayerElem.setAttribute( "placement", placement );
66  diagramLayerElem.setAttribute( "linePlacementFlags", placementFlags );
67  diagramLayerElem.setAttribute( "priority", priority );
68  diagramLayerElem.setAttribute( "obstacle", obstacle );
69  diagramLayerElem.setAttribute( "dist", QString::number( dist ) );
70  diagramLayerElem.setAttribute( "xPosColumn", xPosColumn );
71  diagramLayerElem.setAttribute( "yPosColumn", yPosColumn );
72  diagramLayerElem.setAttribute( "showAll", showAll );
73  layerElem.appendChild( diagramLayerElem );
74 }
75 
76 void QgsDiagramSettings::readXML( const QDomElement& elem, const QgsVectorLayer* layer )
77 {
78  Q_UNUSED( layer );
79 
80  enabled = ( elem.attribute( "enabled", "1" ) != "0" );
81  font.fromString( elem.attribute( "font" ) );
82  backgroundColor.setNamedColor( elem.attribute( "backgroundColor" ) );
83  backgroundColor.setAlpha( elem.attribute( "backgroundAlpha" ).toInt() );
84  size.setWidth( elem.attribute( "width" ).toDouble() );
85  size.setHeight( elem.attribute( "height" ).toDouble() );
86  transparency = elem.attribute( "transparency", "0" ).toInt();
87  penColor.setNamedColor( elem.attribute( "penColor" ) );
88  int penAlpha = elem.attribute( "penAlpha", "255" ).toInt();
89  penColor.setAlpha( penAlpha );
90  penWidth = elem.attribute( "penWidth" ).toDouble();
91 
92  minScaleDenominator = elem.attribute( "minScaleDenominator", "-1" ).toDouble();
93  maxScaleDenominator = elem.attribute( "maxScaleDenominator", "-1" ).toDouble();
94 
95  //mm vs map units
96  if ( elem.attribute( "sizeType" ) == "MM" )
97  {
98  sizeType = MM;
99  }
100  else
101  {
102  sizeType = MapUnits;
103  }
104 
105  //label placement method
106  if ( elem.attribute( "labelPlacementMethod" ) == "Height" )
107  {
109  }
110  else
111  {
113  }
114 
115  // orientation
116  if ( elem.attribute( "diagramOrientation" ) == "Left" )
117  {
119  }
120  else if ( elem.attribute( "diagramOrientation" ) == "Right" )
121  {
123  }
124  else if ( elem.attribute( "diagramOrientation" ) == "Down" )
125  {
127  }
128  else
129  {
131  }
132 
133  // scale dependency
134  if ( elem.attribute( "scaleDependency" ) == "Diameter" )
135  {
136  scaleByArea = false;
137  }
138  else
139  {
140  scaleByArea = true;
141  }
142 
143  barWidth = elem.attribute( "barWidth" ).toDouble();
144 
145  angleOffset = elem.attribute( "angleOffset" ).toInt();
146 
147  minimumSize = elem.attribute( "minimumSize" ).toDouble();
148 
149  //colors
150  categoryColors.clear();
151  QDomNodeList attributes = elem.elementsByTagName( "attribute" );
152 
153  if ( attributes.length() > 0 )
154  {
155  for ( uint i = 0; i < attributes.length(); i++ )
156  {
157  QDomElement attrElem = attributes.at( i ).toElement();
158  QColor newColor( attrElem.attribute( "color" ) );
159  newColor.setAlpha( 255 - transparency );
160  categoryColors.append( newColor );
161  categoryAttributes.append( attrElem.attribute( "field" ) );
162  }
163  }
164  else
165  {
166  // Restore old format attributes and colors
167 
168  QStringList colorList = elem.attribute( "colors" ).split( "/" );
169  QStringList::const_iterator colorIt = colorList.constBegin();
170  for ( ; colorIt != colorList.constEnd(); ++colorIt )
171  {
172  QColor newColor( *colorIt );
173  newColor.setAlpha( 255 - transparency );
174  categoryColors.append( QColor( newColor ) );
175  }
176 
177  //attribute indices
178  categoryAttributes.clear();
179  QStringList catList = elem.attribute( "categories" ).split( "/" );
180  QStringList::const_iterator catIt = catList.constBegin();
181  for ( ; catIt != catList.constEnd(); ++catIt )
182  {
183  categoryAttributes.append( *catIt );
184  }
185  }
186 }
187 
188 void QgsDiagramSettings::writeXML( QDomElement& rendererElem, QDomDocument& doc, const QgsVectorLayer* layer ) const
189 {
190  Q_UNUSED( layer );
191 
192  QDomElement categoryElem = doc.createElement( "DiagramCategory" );
193  categoryElem.setAttribute( "enabled", enabled );
194  categoryElem.setAttribute( "font", font.toString() );
195  categoryElem.setAttribute( "backgroundColor", backgroundColor.name() );
196  categoryElem.setAttribute( "backgroundAlpha", backgroundColor.alpha() );
197  categoryElem.setAttribute( "width", QString::number( size.width() ) );
198  categoryElem.setAttribute( "height", QString::number( size.height() ) );
199  categoryElem.setAttribute( "penColor", penColor.name() );
200  categoryElem.setAttribute( "penAlpha", penColor.alpha() );
201  categoryElem.setAttribute( "penWidth", QString::number( penWidth ) );
202  categoryElem.setAttribute( "minScaleDenominator", QString::number( minScaleDenominator ) );
203  categoryElem.setAttribute( "maxScaleDenominator", QString::number( maxScaleDenominator ) );
204  categoryElem.setAttribute( "transparency", QString::number( transparency ) );
205 
206  // site type (mm vs. map units)
207  if ( sizeType == MM )
208  {
209  categoryElem.setAttribute( "sizeType", "MM" );
210  }
211  else
212  {
213  categoryElem.setAttribute( "sizeType", "MapUnits" );
214  }
215 
216  // label placement method (text diagram)
217  if ( labelPlacementMethod == Height )
218  {
219  categoryElem.setAttribute( "labelPlacementMethod", "Height" );
220  }
221  else
222  {
223  categoryElem.setAttribute( "labelPlacementMethod", "XHeight" );
224  }
225 
226  if ( scaleByArea )
227  {
228  categoryElem.setAttribute( "scaleDependency", "Area" );
229  }
230  else
231  {
232  categoryElem.setAttribute( "scaleDependency", "Diameter" );
233  }
234 
235  // orientation (histogram)
236  switch ( diagramOrientation )
237  {
238  case Left:
239  categoryElem.setAttribute( "diagramOrientation", "Left" );
240  break;
241 
242  case Right:
243  categoryElem.setAttribute( "diagramOrientation", "Right" );
244  break;
245 
246  case Down:
247  categoryElem.setAttribute( "diagramOrientation", "Down" );
248  break;
249 
250  case Up:
251  categoryElem.setAttribute( "diagramOrientation", "Up" );
252  break;
253 
254  default:
255  categoryElem.setAttribute( "diagramOrientation", "Up" );
256  break;
257  }
258 
259  categoryElem.setAttribute( "barWidth", QString::number( barWidth ) );
260  categoryElem.setAttribute( "minimumSize", QString::number( minimumSize ) );
261  categoryElem.setAttribute( "angleOffset", QString::number( angleOffset ) );
262 
263  QString colors;
264  int nCats = qMin( categoryColors.size(), categoryAttributes.size() );
265  for ( int i = 0; i < nCats; ++i )
266  {
267  QDomElement attributeElem = doc.createElement( "attribute" );
268 
269  attributeElem.setAttribute( "field", categoryAttributes.at( i ) );
270  attributeElem.setAttribute( "color", categoryColors.at( i ).name() );
271  categoryElem.appendChild( attributeElem );
272  }
273 
274  rendererElem.appendChild( categoryElem );
275 }
276 
278  : mDiagram( 0 )
279 {
280 }
281 
283 {
284  delete mDiagram;
285 }
286 
288 {
289  delete mDiagram;
290  mDiagram = d;
291 }
292 
294  : mDiagram( other.mDiagram ? other.mDiagram->clone() : 0 )
295 {
296 }
297 
298 void QgsDiagramRendererV2::renderDiagram( const QgsFeature& feature, QgsRenderContext& c, const QPointF& pos )
299 {
300  if ( !mDiagram )
301  {
302  return;
303  }
304 
306  if ( !diagramSettings( feature, c, s ) )
307  {
308  return;
309  }
310 
311  mDiagram->renderDiagram( feature, c, s, pos );
312 }
313 
315 {
317  if ( !diagramSettings( feature, c, s ) )
318  {
319  return QSizeF();
320  }
321 
322  QSizeF size = diagramSize( feature, c );
323  if ( s.sizeType == QgsDiagramSettings::MM )
324  {
325  convertSizeToMapUnits( size, c );
326  }
327  return size;
328 }
329 
331 {
332  if ( !size.isValid() )
333  {
334  return;
335  }
336 
337  double pixelToMap = context.scaleFactor() * context.mapToPixel().mapUnitsPerPixel();
338  size.rwidth() *= pixelToMap;
339  size.rheight() *= pixelToMap;
340 }
341 
342 int QgsDiagramRendererV2::dpiPaintDevice( const QPainter* painter )
343 {
344  if ( painter )
345  {
346  QPaintDevice* device = painter->device();
347  if ( device )
348  {
349  return device->logicalDpiX();
350  }
351  }
352  return -1;
353 }
354 
355 void QgsDiagramRendererV2::_readXML( const QDomElement& elem, const QgsVectorLayer* layer )
356 {
357  Q_UNUSED( layer )
358 
359  delete mDiagram;
360  QString diagramType = elem.attribute( "diagramType" );
361  if ( diagramType == "Pie" )
362  {
363  mDiagram = new QgsPieDiagram();
364  }
365  else if ( diagramType == "Text" )
366  {
367  mDiagram = new QgsTextDiagram();
368  }
369  else if ( diagramType == "Histogram" )
370  {
372  }
373  else
374  {
375  mDiagram = 0;
376  }
377 }
378 
379 void QgsDiagramRendererV2::_writeXML( QDomElement& rendererElem, QDomDocument& doc, const QgsVectorLayer* layer ) const
380 {
381  Q_UNUSED( doc );
382  Q_UNUSED( layer )
383 
384  if ( mDiagram )
385  {
386  rendererElem.setAttribute( "diagramType", mDiagram->diagramName() );
387  }
388 }
389 
391 {
392 }
393 
395 {
396 }
397 
399 {
400  return new QgsSingleCategoryDiagramRenderer( *this );
401 }
402 
404 {
405  Q_UNUSED( c );
406  s = mSettings;
407  return true;
408 }
409 
411 {
412  return mDiagram->diagramSize( feature.attributes(), c, mSettings );
413 }
414 
415 QList<QgsDiagramSettings> QgsSingleCategoryDiagramRenderer::diagramSettings() const
416 {
417  QList<QgsDiagramSettings> settingsList;
418  settingsList.push_back( mSettings );
419  return settingsList;
420 }
421 
422 void QgsSingleCategoryDiagramRenderer::readXML( const QDomElement& elem, const QgsVectorLayer* layer )
423 {
424  QDomElement categoryElem = elem.firstChildElement( "DiagramCategory" );
425  if ( categoryElem.isNull() )
426  {
427  return;
428  }
429 
430  mSettings.readXML( categoryElem, layer );
431  _readXML( elem, layer );
432 }
433 
434 void QgsSingleCategoryDiagramRenderer::writeXML( QDomElement& layerElem, QDomDocument& doc, const QgsVectorLayer* layer ) const
435 {
436  QDomElement rendererElem = doc.createElement( "SingleCategoryDiagramRenderer" );
437  mSettings.writeXML( rendererElem, doc, layer );
438  _writeXML( rendererElem, doc, layer );
439  layerElem.appendChild( rendererElem );
440 }
441 
442 
444 {
445  mInterpolationSettings.classificationAttributeIsExpression = false;
446 }
447 
449 {
450 }
451 
453 {
454  return new QgsLinearlyInterpolatedDiagramRenderer( *this );
455 }
456 
458 {
459  QList<QgsDiagramSettings> settingsList;
460  settingsList.push_back( mSettings );
461  return settingsList;
462 }
463 
465 {
466  s = mSettings;
467  s.size = diagramSize( feature, c );
468  return true;
469 }
470 
472 {
473  return mSettings.categoryAttributes;
474 }
475 
477 {
478  return mDiagram->diagramSize( feature, c, mSettings, mInterpolationSettings );
479 }
480 
481 void QgsLinearlyInterpolatedDiagramRenderer::readXML( const QDomElement& elem, const QgsVectorLayer* layer )
482 {
483  mInterpolationSettings.lowerValue = elem.attribute( "lowerValue" ).toDouble();
484  mInterpolationSettings.upperValue = elem.attribute( "upperValue" ).toDouble();
485  mInterpolationSettings.lowerSize.setWidth( elem.attribute( "lowerWidth" ).toDouble() );
486  mInterpolationSettings.lowerSize.setHeight( elem.attribute( "lowerHeight" ).toDouble() );
487  mInterpolationSettings.upperSize.setWidth( elem.attribute( "upperWidth" ).toDouble() );
488  mInterpolationSettings.upperSize.setHeight( elem.attribute( "upperHeight" ).toDouble() );
489  mInterpolationSettings.classificationAttributeIsExpression = elem.hasAttribute( "classificationAttributeExpression" );
490  if ( mInterpolationSettings.classificationAttributeIsExpression )
491  {
492  mInterpolationSettings.classificationAttributeExpression = elem.attribute( "classificationAttributeExpression" );
493  }
494  else
495  {
496  mInterpolationSettings.classificationAttribute = elem.attribute( "classificationAttribute" ).toInt();
497  }
498  QDomElement settingsElem = elem.firstChildElement( "DiagramCategory" );
499  if ( !settingsElem.isNull() )
500  {
501  mSettings.readXML( settingsElem, layer );
502  }
503  _readXML( elem, layer );
504 }
505 
506 void QgsLinearlyInterpolatedDiagramRenderer::writeXML( QDomElement& layerElem, QDomDocument& doc, const QgsVectorLayer* layer ) const
507 {
508  QDomElement rendererElem = doc.createElement( "LinearlyInterpolatedDiagramRenderer" );
509  rendererElem.setAttribute( "lowerValue", QString::number( mInterpolationSettings.lowerValue ) );
510  rendererElem.setAttribute( "upperValue", QString::number( mInterpolationSettings.upperValue ) );
511  rendererElem.setAttribute( "lowerWidth", QString::number( mInterpolationSettings.lowerSize.width() ) );
512  rendererElem.setAttribute( "lowerHeight", QString::number( mInterpolationSettings.lowerSize.height() ) );
513  rendererElem.setAttribute( "upperWidth", QString::number( mInterpolationSettings.upperSize.width() ) );
514  rendererElem.setAttribute( "upperHeight", QString::number( mInterpolationSettings.upperSize.height() ) );
515  if ( mInterpolationSettings.classificationAttributeIsExpression )
516  {
517  rendererElem.setAttribute( "classificationAttributeExpression", mInterpolationSettings.classificationAttributeExpression );
518  }
519  else
520  {
521  rendererElem.setAttribute( "classificationAttribute", mInterpolationSettings.classificationAttribute );
522  }
523  mSettings.writeXML( rendererElem, doc, layer );
524  _writeXML( rendererElem, doc, layer );
525  layerElem.appendChild( rendererElem );
526 }
double minimumSize
Scale diagrams smaller than mMinimumSize to mMinimumSize.
void writeXML(QDomElement &layerElem, QDomDocument &doc, const QgsVectorLayer *layer) const override
QgsDiagramRendererV2 * clone() const override
Returns new instance that is equivalent to this one.
void _readXML(const QDomElement &elem, const QgsVectorLayer *layer)
QList< QString > categoryAttributes
virtual QList< QgsDiagramSettings > diagramSettings() const =0
Returns list with all diagram settings in the renderer.
static int dpiPaintDevice(const QPainter *)
Returns the paint device dpi (or -1 in case of error.
void renderDiagram(const QgsFeature &feature, QgsRenderContext &c, const QPointF &pos)
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
void readXML(const QDomElement &elem, const QgsVectorLayer *layer)
QList< QgsDiagramSettings > diagramSettings() const override
Returns list with all diagram settings in the renderer.
Returns diagram settings for a feature.
virtual QSizeF diagramSize(const QgsFeature &features, const QgsRenderContext &c)=0
Returns size of the diagram (in painter units) or an invalid size in case of error.
void convertSizeToMapUnits(QSizeF &size, const QgsRenderContext &context) const
Converts size from mm to map units.
DiagramOrientation diagramOrientation
void readXML(const QDomElement &elem, const QgsVectorLayer *layer) override
virtual QSizeF sizeMapUnits(const QgsFeature &feature, const QgsRenderContext &c)
Returns size of the diagram for a feature in map units.
void readXML(const QDomElement &elem, const QgsVectorLayer *layer)
const QgsAttributes & attributes() const
Definition: qgsfeature.h:142
double mapUnitsPerPixel() const
Return current map units per pixel.
QgsDiagramRendererV2 * renderer
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
virtual QString diagramName() const =0
void writeXML(QDomElement &layerElem, QDomDocument &doc, const QgsVectorLayer *layer) const override
void readXML(const QDomElement &elem, const QgsVectorLayer *layer) override
Contains information about the context of a rendering operation.
QList< QString > diagramAttributes() const override
Returns attribute indices needed for diagram rendering.
void writeXML(QDomElement &layerElem, QDomDocument &doc, const QgsVectorLayer *layer) const
QgsDiagramRendererV2 * clone() const override
Returns new instance that is equivalent to this one.
const QgsMapToPixel & mapToPixel() const
int classificationAttribute
Index of the classification attribute.
void setDiagram(QgsDiagram *d)
QSizeF diagramSize(const QgsFeature &, const QgsRenderContext &c) override
Returns size of the diagram (in painter units) or an invalid size in case of error.
Represents a vector layer which manages a vector based data sets.
double size
Definition: qgssvgcache.cpp:77
QList< QColor > categoryColors
void writeXML(QDomElement &rendererElem, QDomDocument &doc, const QgsVectorLayer *layer) const
QgsDiagram * mDiagram
Reference to the object that does the real diagram rendering.
QList< QgsDiagramSettings > diagramSettings() const override
Returns list with all diagram settings in the renderer.
void _writeXML(QDomElement &rendererElem, QDomDocument &doc, const QgsVectorLayer *layer) const
LabelPlacementMethod labelPlacementMethod
QSizeF diagramSize(const QgsFeature &, const QgsRenderContext &c) override
Returns size of the diagram (in painter units) or an invalid size in case of error.
LinePlacementFlags placementFlags