QGIS API Documentation  2.12.0-Lyon
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"
22 #include "qgsfontutils.h"
23 
24 #include <QDomElement>
25 #include <QPainter>
26 
28  : placement( AroundPoint )
29  , placementFlags( OnLine )
30  , priority( 5 )
31  , obstacle( false )
32  , dist( 0.0 )
33  , renderer( 0 )
34  , ct( 0 )
35  , xform( 0 )
36  , xPosColumn( -1 )
37  , yPosColumn( -1 )
38  , showAll( true )
39 {
40 }
41 
43 {
44  delete renderer;
45 }
46 
48 {
49  Q_UNUSED( layer )
50 
51  placement = ( Placement )elem.attribute( "placement" ).toInt();
52  placementFlags = ( LinePlacementFlags )elem.attribute( "linePlacementFlags" ).toInt();
53  priority = elem.attribute( "priority" ).toInt();
54  obstacle = elem.attribute( "obstacle" ).toInt();
55  dist = elem.attribute( "dist" ).toDouble();
56  xPosColumn = elem.attribute( "xPosColumn" ).toInt();
57  yPosColumn = elem.attribute( "yPosColumn" ).toInt();
58  showAll = ( elem.attribute( "showAll", "0" ) != "0" );
59 }
60 
61 void QgsDiagramLayerSettings::writeXML( QDomElement& layerElem, QDomDocument& doc, const QgsVectorLayer* layer ) const
62 {
63  Q_UNUSED( layer )
64 
65  QDomElement diagramLayerElem = doc.createElement( "DiagramLayerSettings" );
66  diagramLayerElem.setAttribute( "placement", placement );
67  diagramLayerElem.setAttribute( "linePlacementFlags", placementFlags );
68  diagramLayerElem.setAttribute( "priority", priority );
69  diagramLayerElem.setAttribute( "obstacle", obstacle );
70  diagramLayerElem.setAttribute( "dist", QString::number( dist ) );
71  diagramLayerElem.setAttribute( "xPosColumn", xPosColumn );
72  diagramLayerElem.setAttribute( "yPosColumn", yPosColumn );
73  diagramLayerElem.setAttribute( "showAll", showAll );
74  layerElem.appendChild( diagramLayerElem );
75 }
76 
77 void QgsDiagramSettings::readXML( const QDomElement& elem, const QgsVectorLayer* layer )
78 {
79  Q_UNUSED( layer );
80 
81  enabled = ( elem.attribute( "enabled", "1" ) != "0" );
82  if ( !QgsFontUtils::setFromXmlChildNode( font, elem, "fontProperties" ) )
83  {
84  font.fromString( elem.attribute( "font" ) );
85  }
86  backgroundColor.setNamedColor( elem.attribute( "backgroundColor" ) );
87  backgroundColor.setAlpha( elem.attribute( "backgroundAlpha" ).toInt() );
88  size.setWidth( elem.attribute( "width" ).toDouble() );
89  size.setHeight( elem.attribute( "height" ).toDouble() );
90  transparency = elem.attribute( "transparency", "0" ).toInt();
91  penColor.setNamedColor( elem.attribute( "penColor" ) );
92  int penAlpha = elem.attribute( "penAlpha", "255" ).toInt();
93  penColor.setAlpha( penAlpha );
94  penWidth = elem.attribute( "penWidth" ).toDouble();
95 
96  minScaleDenominator = elem.attribute( "minScaleDenominator", "-1" ).toDouble();
97  maxScaleDenominator = elem.attribute( "maxScaleDenominator", "-1" ).toDouble();
98  if ( elem.hasAttribute( "scaleBasedVisibility" ) )
99  {
100  scaleBasedVisibility = ( elem.attribute( "scaleBasedVisibility", "1" ) != "0" );
101  }
102  else
103  {
105  }
106 
107  //mm vs map units
108  if ( elem.attribute( "sizeType" ) == "MM" )
109  {
110  sizeType = MM;
111  }
112  else
113  {
114  sizeType = MapUnits;
115  }
116 
117  //label placement method
118  if ( elem.attribute( "labelPlacementMethod" ) == "Height" )
119  {
121  }
122  else
123  {
125  }
126 
127  // orientation
128  if ( elem.attribute( "diagramOrientation" ) == "Left" )
129  {
131  }
132  else if ( elem.attribute( "diagramOrientation" ) == "Right" )
133  {
135  }
136  else if ( elem.attribute( "diagramOrientation" ) == "Down" )
137  {
139  }
140  else
141  {
143  }
144 
145  // scale dependency
146  if ( elem.attribute( "scaleDependency" ) == "Diameter" )
147  {
148  scaleByArea = false;
149  }
150  else
151  {
152  scaleByArea = true;
153  }
154 
155  barWidth = elem.attribute( "barWidth" ).toDouble();
156 
157  angleOffset = elem.attribute( "angleOffset" ).toInt();
158 
159  minimumSize = elem.attribute( "minimumSize" ).toDouble();
160 
161  //colors
163  QDomNodeList attributes = elem.elementsByTagName( "attribute" );
164 
165  if ( attributes.length() > 0 )
166  {
167  for ( int i = 0; i < attributes.size(); i++ )
168  {
169  QDomElement attrElem = attributes.at( i ).toElement();
170  QColor newColor( attrElem.attribute( "color" ) );
171  newColor.setAlpha( 255 - transparency );
172  categoryColors.append( newColor );
173  categoryAttributes.append( attrElem.attribute( "field" ) );
174  categoryLabels.append( attrElem.attribute( "label" ) );
175  if ( categoryLabels.back().isEmpty() )
176  {
178  }
179  }
180  }
181  else
182  {
183  // Restore old format attributes and colors
184 
185  QStringList colorList = elem.attribute( "colors" ).split( "/" );
186  QStringList::const_iterator colorIt = colorList.constBegin();
187  for ( ; colorIt != colorList.constEnd(); ++colorIt )
188  {
189  QColor newColor( *colorIt );
190  newColor.setAlpha( 255 - transparency );
191  categoryColors.append( QColor( newColor ) );
192  }
193 
194  //attribute indices
196  QStringList catList = elem.attribute( "categories" ).split( "/" );
197  QStringList::const_iterator catIt = catList.constBegin();
198  for ( ; catIt != catList.constEnd(); ++catIt )
199  {
200  categoryAttributes.append( *catIt );
201  categoryLabels.append( *catIt );
202  }
203  }
204 }
205 
206 void QgsDiagramSettings::writeXML( QDomElement& rendererElem, QDomDocument& doc, const QgsVectorLayer* layer ) const
207 {
208  Q_UNUSED( layer );
209 
210  QDomElement categoryElem = doc.createElement( "DiagramCategory" );
211  categoryElem.setAttribute( "enabled", enabled );
212  categoryElem.appendChild( QgsFontUtils::toXmlElement( font, doc, "fontProperties" ) );
213  categoryElem.setAttribute( "backgroundColor", backgroundColor.name() );
214  categoryElem.setAttribute( "backgroundAlpha", backgroundColor.alpha() );
215  categoryElem.setAttribute( "width", QString::number( size.width() ) );
216  categoryElem.setAttribute( "height", QString::number( size.height() ) );
217  categoryElem.setAttribute( "penColor", penColor.name() );
218  categoryElem.setAttribute( "penAlpha", penColor.alpha() );
219  categoryElem.setAttribute( "penWidth", QString::number( penWidth ) );
220  categoryElem.setAttribute( "scaleBasedVisibility", scaleBasedVisibility );
221  categoryElem.setAttribute( "minScaleDenominator", QString::number( minScaleDenominator ) );
222  categoryElem.setAttribute( "maxScaleDenominator", QString::number( maxScaleDenominator ) );
223  categoryElem.setAttribute( "transparency", QString::number( transparency ) );
224 
225  // site type (mm vs. map units)
226  if ( sizeType == MM )
227  {
228  categoryElem.setAttribute( "sizeType", "MM" );
229  }
230  else
231  {
232  categoryElem.setAttribute( "sizeType", "MapUnits" );
233  }
234 
235  // label placement method (text diagram)
236  if ( labelPlacementMethod == Height )
237  {
238  categoryElem.setAttribute( "labelPlacementMethod", "Height" );
239  }
240  else
241  {
242  categoryElem.setAttribute( "labelPlacementMethod", "XHeight" );
243  }
244 
245  if ( scaleByArea )
246  {
247  categoryElem.setAttribute( "scaleDependency", "Area" );
248  }
249  else
250  {
251  categoryElem.setAttribute( "scaleDependency", "Diameter" );
252  }
253 
254  // orientation (histogram)
255  switch ( diagramOrientation )
256  {
257  case Left:
258  categoryElem.setAttribute( "diagramOrientation", "Left" );
259  break;
260 
261  case Right:
262  categoryElem.setAttribute( "diagramOrientation", "Right" );
263  break;
264 
265  case Down:
266  categoryElem.setAttribute( "diagramOrientation", "Down" );
267  break;
268 
269  case Up:
270  categoryElem.setAttribute( "diagramOrientation", "Up" );
271  break;
272 
273  default:
274  categoryElem.setAttribute( "diagramOrientation", "Up" );
275  break;
276  }
277 
278  categoryElem.setAttribute( "barWidth", QString::number( barWidth ) );
279  categoryElem.setAttribute( "minimumSize", QString::number( minimumSize ) );
280  categoryElem.setAttribute( "angleOffset", QString::number( angleOffset ) );
281 
282  QString colors;
283  int nCats = qMin( categoryColors.size(), categoryAttributes.size() );
284  for ( int i = 0; i < nCats; ++i )
285  {
286  QDomElement attributeElem = doc.createElement( "attribute" );
287 
288  attributeElem.setAttribute( "field", categoryAttributes.at( i ) );
289  attributeElem.setAttribute( "color", categoryColors.at( i ).name() );
290  attributeElem.setAttribute( "label", categoryLabels.at( i ) );
291  categoryElem.appendChild( attributeElem );
292  }
293 
294  rendererElem.appendChild( categoryElem );
295 }
296 
298  : mDiagram( 0 )
299 {
300 }
301 
303 {
304  delete mDiagram;
305 }
306 
308 {
309  delete mDiagram;
310  mDiagram = d;
311 }
312 
314  : mDiagram( other.mDiagram ? other.mDiagram->clone() : 0 )
315 {
316 }
317 
319 {
320  if ( !mDiagram )
321  {
322  return;
323  }
324 
326  if ( !diagramSettings( feature, c, s ) )
327  {
328  return;
329  }
330 
331  mDiagram->renderDiagram( feature, c, s, pos );
332 }
333 
335 {
337  if ( !diagramSettings( feature, c, s ) )
338  {
339  return QSizeF();
340  }
341 
342  QSizeF size = diagramSize( feature, c );
343  if ( s.sizeType == QgsDiagramSettings::MM )
344  {
345  convertSizeToMapUnits( size, c );
346  }
347  return size;
348 }
349 
351 {
352  if ( !size.isValid() )
353  {
354  return;
355  }
356 
357  double pixelToMap = context.scaleFactor() * context.mapToPixel().mapUnitsPerPixel();
358  size.rwidth() *= pixelToMap;
359  size.rheight() *= pixelToMap;
360 }
361 
363 {
364  if ( painter )
365  {
366  QPaintDevice* device = painter->device();
367  if ( device )
368  {
369  return device->logicalDpiX();
370  }
371  }
372  return -1;
373 }
374 
376 {
377  Q_UNUSED( layer )
378 
379  delete mDiagram;
380  QString diagramType = elem.attribute( "diagramType" );
381  if ( diagramType == "Pie" )
382  {
383  mDiagram = new QgsPieDiagram();
384  }
385  else if ( diagramType == "Text" )
386  {
387  mDiagram = new QgsTextDiagram();
388  }
389  else if ( diagramType == "Histogram" )
390  {
392  }
393  else
394  {
395  mDiagram = 0;
396  }
397 }
398 
399 void QgsDiagramRendererV2::_writeXML( QDomElement& rendererElem, QDomDocument& doc, const QgsVectorLayer* layer ) const
400 {
401  Q_UNUSED( doc );
402  Q_UNUSED( layer )
403 
404  if ( mDiagram )
405  {
406  rendererElem.setAttribute( "diagramType", mDiagram->diagramName() );
407  }
408 }
409 
411 {
412 }
413 
415 {
416 }
417 
419 {
420  return new QgsSingleCategoryDiagramRenderer( *this );
421 }
422 
424 {
425  Q_UNUSED( c );
426  s = mSettings;
427  return true;
428 }
429 
431 {
432  return mDiagram->diagramSize( feature.attributes(), c, mSettings );
433 }
434 
436 {
437  QList<QgsDiagramSettings> settingsList;
438  settingsList.push_back( mSettings );
439  return settingsList;
440 }
441 
443 {
444  QDomElement categoryElem = elem.firstChildElement( "DiagramCategory" );
445  if ( categoryElem.isNull() )
446  {
447  return;
448  }
449 
450  mSettings.readXML( categoryElem, layer );
451  _readXML( elem, layer );
452 }
453 
455 {
456  QDomElement rendererElem = doc.createElement( "SingleCategoryDiagramRenderer" );
457  mSettings.writeXML( rendererElem, doc, layer );
458  _writeXML( rendererElem, doc, layer );
459  layerElem.appendChild( rendererElem );
460 }
461 
462 
464 {
465  mInterpolationSettings.classificationAttributeIsExpression = false;
466 }
467 
469 {
470 }
471 
473 {
474  return new QgsLinearlyInterpolatedDiagramRenderer( *this );
475 }
476 
478 {
479  QList<QgsDiagramSettings> settingsList;
480  settingsList.push_back( mSettings );
481  return settingsList;
482 }
483 
485 {
486  s = mSettings;
487  s.size = diagramSize( feature, c );
488  return true;
489 }
490 
492 {
493  return mSettings.categoryAttributes;
494 }
495 
497 {
498  return mDiagram->diagramSize( feature, c, mSettings, mInterpolationSettings );
499 }
500 
502 {
503  mInterpolationSettings.lowerValue = elem.attribute( "lowerValue" ).toDouble();
504  mInterpolationSettings.upperValue = elem.attribute( "upperValue" ).toDouble();
505  mInterpolationSettings.lowerSize.setWidth( elem.attribute( "lowerWidth" ).toDouble() );
506  mInterpolationSettings.lowerSize.setHeight( elem.attribute( "lowerHeight" ).toDouble() );
507  mInterpolationSettings.upperSize.setWidth( elem.attribute( "upperWidth" ).toDouble() );
508  mInterpolationSettings.upperSize.setHeight( elem.attribute( "upperHeight" ).toDouble() );
509  mInterpolationSettings.classificationAttributeIsExpression = elem.hasAttribute( "classificationAttributeExpression" );
510  if ( mInterpolationSettings.classificationAttributeIsExpression )
511  {
512  mInterpolationSettings.classificationAttributeExpression = elem.attribute( "classificationAttributeExpression" );
513  }
514  else
515  {
516  mInterpolationSettings.classificationAttribute = elem.attribute( "classificationAttribute" ).toInt();
517  }
518  QDomElement settingsElem = elem.firstChildElement( "DiagramCategory" );
519  if ( !settingsElem.isNull() )
520  {
521  mSettings.readXML( settingsElem, layer );
522  }
523  _readXML( elem, layer );
524 }
525 
527 {
528  QDomElement rendererElem = doc.createElement( "LinearlyInterpolatedDiagramRenderer" );
529  rendererElem.setAttribute( "lowerValue", QString::number( mInterpolationSettings.lowerValue ) );
530  rendererElem.setAttribute( "upperValue", QString::number( mInterpolationSettings.upperValue ) );
531  rendererElem.setAttribute( "lowerWidth", QString::number( mInterpolationSettings.lowerSize.width() ) );
532  rendererElem.setAttribute( "lowerHeight", QString::number( mInterpolationSettings.lowerSize.height() ) );
533  rendererElem.setAttribute( "upperWidth", QString::number( mInterpolationSettings.upperSize.width() ) );
534  rendererElem.setAttribute( "upperHeight", QString::number( mInterpolationSettings.upperSize.height() ) );
535  if ( mInterpolationSettings.classificationAttributeIsExpression )
536  {
537  rendererElem.setAttribute( "classificationAttributeExpression", mInterpolationSettings.classificationAttributeExpression );
538  }
539  else
540  {
541  rendererElem.setAttribute( "classificationAttribute", mInterpolationSettings.classificationAttribute );
542  }
543  mSettings.writeXML( rendererElem, doc, layer );
544  _writeXML( rendererElem, doc, layer );
545  layerElem.appendChild( rendererElem );
546 }
547 
549 {
551  list.reserve( categoryLabels.size() );
552  for ( int i = 0 ; i < categoryLabels.size(); ++i )
553  {
554  QPixmap pix( 16, 16 );
555  pix.fill( categoryColors[i] );
556  list << new QgsSimpleLegendNode( nodeLayer, categoryLabels[i], QIcon( pix ), 0, QString( "diagram_%1" ).arg( QString::number( i ) ) );
557  }
558  return list;
559 }
560 
562 {
564 }
565 
567 {
568  return mSettings.legendItems( nodeLayer );
569 }
570 
572 {
573  return mSettings.legendItems( nodeLayer );
574 }
void clear()
QDomNodeList elementsByTagName(const QString &tagname) const
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.
QDomNode appendChild(const QDomNode &newChild)
void fill(const QColor &color)
void push_back(const T &value)
QString name() const
void _readXML(const QDomElement &elem, const QgsVectorLayer *layer)
virtual QList< QgsLayerTreeModelLegendNode * > legendItems(QgsLayerTreeLayer *nodeLayer) const
Returns list of legend nodes for the diagram.
QString attribute(const QString &name, const QString &defValue) const
QList< QString > categoryAttributes
QStringList split(const QString &sep, SplitBehavior behavior, Qt::CaseSensitivity cs) const
void reserve(int alloc)
qreal & rwidth()
const T & at(int i) const
bool isValid() const
virtual QList< QgsDiagramSettings > diagramSettings() const =0
Returns list with all diagram settings in the renderer.
void setAlpha(int alpha)
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:176
virtual Q_DECL_DEPRECATED void renderDiagram(const QgsAttributes &att, QgsRenderContext &c, const QgsDiagramSettings &s, const QPointF &position)
Definition: qgsdiagram.cpp:123
void setNamedColor(const QString &name)
double toDouble(bool *ok) const
void readXML(const QDomElement &elem, const QgsVectorLayer *layer)
QList< QgsDiagramSettings > diagramSettings() const override
Returns list with all diagram settings in the renderer.
int size() const
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.
QList< QgsLayerTreeModelLegendNode * > legendItems(QgsLayerTreeLayer *nodeLayer) const override
Returns list of legend nodes for the diagram.
QList< QgsLayerTreeModelLegendNode * > legendItems(QgsLayerTreeLayer *nodeLayer) const override
Returns list of legend nodes for the diagram.
void convertSizeToMapUnits(QSizeF &size, const QgsRenderContext &context) const
Converts size from mm to map units.
QDomElement toElement() const
DiagramOrientation diagramOrientation
QString number(int n, int base)
void readXML(const QDomElement &elem, const QgsVectorLayer *layer) override
void append(const T &value)
bool fromString(const QString &descrip)
bool hasAttribute(const QString &name) const
virtual QSizeF sizeMapUnits(const QgsFeature &feature, const QgsRenderContext &c)
Returns size of the diagram for a feature in map units.
QgsAttributes attributes() const
Returns the feature's attributes.
Definition: qgsfeature.cpp:92
void readXML(const QDomElement &elem, const QgsVectorLayer *layer)
void setAttribute(const QString &name, const QString &value)
void setWidth(qreal width)
int toInt(bool *ok, int base) const
QPaintDevice * device() const
QList< QgsLayerTreeModelLegendNode * > legendItems(QgsLayerTreeLayer *nodeLayer) const
Returns list of legend nodes for the diagram.
static bool setFromXmlChildNode(QFont &font, const QDomElement &element, const QString &childNode)
Sets the properties of a font to match the properties stored in an XML child node.
double mapUnitsPerPixel() const
Return current map units per pixel.
QgsDiagramRendererV2 * renderer
Implementation of legend node interface for displaying arbitrary label with icon. ...
int alpha() 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.
int logicalDpiX() const
Base class for all diagram types.
Definition: qgsdiagram.h:35
bool isNull() const
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.
QDomElement firstChildElement(const QString &tagName) const
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
QList< QString > categoryLabels
int classificationAttribute
Index of the classification attribute.
static QDomElement toXmlElement(const QFont &font, QDomDocument &document, const QString &elementName)
Returns a DOM element containing the properties of the font.
int size() const
uint length() const
const_iterator constEnd() const
qreal & rheight()
QDomElement createElement(const QString &tagName)
const_iterator constBegin() const
qreal height() const
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.
void setHeight(qreal height)
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.
T & back()
QList< QgsDiagramSettings > diagramSettings() const override
Returns list with all diagram settings in the renderer.
qreal width() const
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.
Layer tree node points to a map layer.
QDomNode at(int index) const