QGIS API Documentation  2.18.21-Las Palmas (9fba24a)
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 #include "qgssymbollayerv2utils.h"
24 #include "qgssymbolv2.h"
25 
26 #include <QDomElement>
27 #include <QPainter>
28 
29 Q_NOWARN_DEPRECATED_PUSH // because of deprecated xform member
31  : placement( AroundPoint )
32  , placementFlags( OnLine )
33  , priority( 5 )
34  , zIndex( 0.0 )
35  , obstacle( false )
36  , dist( 0.0 )
37  , renderer( nullptr )
38  , ct( nullptr )
39  , xform( nullptr )
40  , xPosColumn( -1 )
41  , yPosColumn( -1 )
42  , showColumn( -1 )
43  , showAll( true )
44 {
45 }
47 
48 Q_NOWARN_DEPRECATED_PUSH // because of deprecated xform member
50  : placement( rh.placement )
52  , priority( rh.priority )
53  , zIndex( rh.zIndex )
54  , obstacle( rh.obstacle )
55  , dist( rh.dist )
56  , renderer( rh.renderer ? rh.renderer->clone() : nullptr )
57  , ct( rh.ct ? rh.ct->clone() : nullptr )
58  , xform( rh.xform )
59  , fields( rh.fields )
60  , xPosColumn( rh.xPosColumn )
61  , yPosColumn( rh.yPosColumn )
62  , showColumn( rh.showColumn )
63  , showAll( rh.showAll )
64 {
65 }
67 
68 Q_NOWARN_DEPRECATED_PUSH // because of deprecated xform member
70 {
71  placement = rh.placement;
73  priority = rh.priority;
74  zIndex = rh.zIndex;
75  obstacle = rh.obstacle;
76  dist = rh.dist;
77  renderer = rh.renderer ? rh.renderer->clone() : nullptr;
78  ct = rh.ct ? rh.ct->clone() : nullptr;
79  xform = rh.xform;
80  fields = rh.fields;
84  showAll = rh.showAll;
85  return *this;
86 }
88 
89 Q_NOWARN_DEPRECATED_PUSH // because of deprecated fields member
91 {
92  delete renderer;
93  delete ct;
94 }
96 
98 {
99  if ( diagramRenderer == renderer )
100  return;
101 
102  delete renderer;
103  renderer = diagramRenderer;
104 }
105 
107 {
108  delete ct;
109  ct = transform;
110 }
111 
113 {
114  Q_UNUSED( layer )
115 
116  placement = static_cast< Placement >( elem.attribute( "placement" ).toInt() );
117  placementFlags = static_cast< LinePlacementFlags >( elem.attribute( "linePlacementFlags" ).toInt() );
118  priority = elem.attribute( "priority" ).toInt();
119  zIndex = elem.attribute( "zIndex" ).toDouble();
120  obstacle = elem.attribute( "obstacle" ).toInt();
121  dist = elem.attribute( "dist" ).toDouble();
122  xPosColumn = elem.attribute( "xPosColumn" ).toInt();
123  yPosColumn = elem.attribute( "yPosColumn" ).toInt();
124  showColumn = elem.attribute( "showColumn" ).toInt();
125  showAll = ( elem.attribute( "showAll", "0" ) != "0" );
126 }
127 
128 void QgsDiagramLayerSettings::writeXML( QDomElement& layerElem, QDomDocument& doc, const QgsVectorLayer* layer ) const
129 {
130  Q_UNUSED( layer )
131 
132  QDomElement diagramLayerElem = doc.createElement( "DiagramLayerSettings" );
133  diagramLayerElem.setAttribute( "placement", placement );
134  diagramLayerElem.setAttribute( "linePlacementFlags", placementFlags );
135  diagramLayerElem.setAttribute( "priority", priority );
136  diagramLayerElem.setAttribute( "zIndex", zIndex );
137  diagramLayerElem.setAttribute( "obstacle", obstacle );
138  diagramLayerElem.setAttribute( "dist", QString::number( dist ) );
139  diagramLayerElem.setAttribute( "xPosColumn", xPosColumn );
140  diagramLayerElem.setAttribute( "yPosColumn", yPosColumn );
141  diagramLayerElem.setAttribute( "showColumn", showColumn );
142  diagramLayerElem.setAttribute( "showAll", showAll );
143  layerElem.appendChild( diagramLayerElem );
144 }
145 
147 {
148  QSet< QString > referenced;
149  if ( renderer )
150  referenced = renderer->referencedFields( context, fieldsParameter );
151 
152  //and the ones needed for data defined diagram positions
153  if ( xPosColumn >= 0 && xPosColumn < fieldsParameter.count() )
154  referenced << fieldsParameter.at( xPosColumn ).name();
155  if ( yPosColumn >= 0 && yPosColumn < fieldsParameter.count() )
156  referenced << fieldsParameter.at( yPosColumn ).name();
157 
158  // and the ones needed for data defined diagram visibility
159  if ( showColumn >= 0 && showColumn < fieldsParameter.count() )
160  referenced << fieldsParameter.at( showColumn ).name();
161 
162  return referenced;
163 }
164 
165 void QgsDiagramSettings::readXML( const QDomElement& elem, const QgsVectorLayer* layer )
166 {
167  Q_UNUSED( layer );
168 
169  enabled = ( elem.attribute( "enabled", "1" ) != "0" );
170  if ( !QgsFontUtils::setFromXmlChildNode( font, elem, "fontProperties" ) )
171  {
172  font.fromString( elem.attribute( "font" ) );
173  }
174  backgroundColor.setNamedColor( elem.attribute( "backgroundColor" ) );
175  backgroundColor.setAlpha( elem.attribute( "backgroundAlpha" ).toInt() );
176  size.setWidth( elem.attribute( "width" ).toDouble() );
177  size.setHeight( elem.attribute( "height" ).toDouble() );
178  transparency = elem.attribute( "transparency", "0" ).toInt();
179  penColor.setNamedColor( elem.attribute( "penColor" ) );
180  int penAlpha = elem.attribute( "penAlpha", "255" ).toInt();
181  penColor.setAlpha( penAlpha );
182  penWidth = elem.attribute( "penWidth" ).toDouble();
183 
184  minScaleDenominator = elem.attribute( "minScaleDenominator", "-1" ).toDouble();
185  maxScaleDenominator = elem.attribute( "maxScaleDenominator", "-1" ).toDouble();
186  if ( elem.hasAttribute( "scaleBasedVisibility" ) )
187  {
188  scaleBasedVisibility = ( elem.attribute( "scaleBasedVisibility", "1" ) != "0" );
189  }
190  else
191  {
192  scaleBasedVisibility = minScaleDenominator >= 0 && maxScaleDenominator >= 0;
193  }
194 
195  //diagram size unit type and scale
196  if ( elem.attribute( "sizeType" ) == "MapUnits" )
197  {
198  //compatibility with pre-2.16 project files
199  sizeType = QgsSymbolV2::MapUnit;
200  }
201  else
202  {
203  sizeType = QgsSymbolLayerV2Utils::decodeOutputUnit( elem.attribute( "sizeType" ) );
204  }
205  sizeScale = QgsSymbolLayerV2Utils::decodeMapUnitScale( elem.attribute( "sizeScale" ) );
206 
207  //line width unit type and scale
208  lineSizeUnit = QgsSymbolLayerV2Utils::decodeOutputUnit( elem.attribute( "lineSizeType" ) );
209  lineSizeScale = QgsSymbolLayerV2Utils::decodeMapUnitScale( elem.attribute( "lineSizeScale" ) );
210 
211  //label placement method
212  if ( elem.attribute( "labelPlacementMethod" ) == "Height" )
213  {
214  labelPlacementMethod = Height;
215  }
216  else
217  {
218  labelPlacementMethod = XHeight;
219  }
220 
221  // orientation
222  if ( elem.attribute( "diagramOrientation" ) == "Left" )
223  {
224  diagramOrientation = Left;
225  }
226  else if ( elem.attribute( "diagramOrientation" ) == "Right" )
227  {
228  diagramOrientation = Right;
229  }
230  else if ( elem.attribute( "diagramOrientation" ) == "Down" )
231  {
232  diagramOrientation = Down;
233  }
234  else
235  {
236  diagramOrientation = Up;
237  }
238 
239  // scale dependency
240  if ( elem.attribute( "scaleDependency" ) == "Diameter" )
241  {
242  scaleByArea = false;
243  }
244  else
245  {
246  scaleByArea = true;
247  }
248 
249  barWidth = elem.attribute( "barWidth" ).toDouble();
250 
251  angleOffset = elem.attribute( "angleOffset" ).toInt();
252 
253  minimumSize = elem.attribute( "minimumSize" ).toDouble();
254 
255  //colors
256  categoryColors.clear();
257  QDomNodeList attributes = elem.elementsByTagName( "attribute" );
258 
259  if ( attributes.length() > 0 )
260  {
261  for ( int i = 0; i < attributes.size(); i++ )
262  {
263  QDomElement attrElem = attributes.at( i ).toElement();
264  QColor newColor( attrElem.attribute( "color" ) );
265  newColor.setAlpha( 255 - transparency );
266  categoryColors.append( newColor );
267  categoryAttributes.append( attrElem.attribute( "field" ) );
268  categoryLabels.append( attrElem.attribute( "label" ) );
269  if ( categoryLabels.back().isEmpty() )
270  {
271  categoryLabels.back() = categoryAttributes.back();
272  }
273  }
274  }
275  else
276  {
277  // Restore old format attributes and colors
278 
279  QStringList colorList = elem.attribute( "colors" ).split( '/' );
280  QStringList::const_iterator colorIt = colorList.constBegin();
281  for ( ; colorIt != colorList.constEnd(); ++colorIt )
282  {
283  QColor newColor( *colorIt );
284  newColor.setAlpha( 255 - transparency );
285  categoryColors.append( QColor( newColor ) );
286  }
287 
288  //attribute indices
289  categoryAttributes.clear();
290  QStringList catList = elem.attribute( "categories" ).split( '/' );
291  QStringList::const_iterator catIt = catList.constBegin();
292  for ( ; catIt != catList.constEnd(); ++catIt )
293  {
294  categoryAttributes.append( *catIt );
295  categoryLabels.append( *catIt );
296  }
297  }
298 }
299 
300 void QgsDiagramSettings::writeXML( QDomElement& rendererElem, QDomDocument& doc, const QgsVectorLayer* layer ) const
301 {
302  Q_UNUSED( layer );
303 
304  QDomElement categoryElem = doc.createElement( "DiagramCategory" );
305  categoryElem.setAttribute( "enabled", enabled );
306  categoryElem.appendChild( QgsFontUtils::toXmlElement( font, doc, "fontProperties" ) );
307  categoryElem.setAttribute( "backgroundColor", backgroundColor.name() );
308  categoryElem.setAttribute( "backgroundAlpha", backgroundColor.alpha() );
309  categoryElem.setAttribute( "width", QString::number( size.width() ) );
310  categoryElem.setAttribute( "height", QString::number( size.height() ) );
311  categoryElem.setAttribute( "penColor", penColor.name() );
312  categoryElem.setAttribute( "penAlpha", penColor.alpha() );
313  categoryElem.setAttribute( "penWidth", QString::number( penWidth ) );
314  categoryElem.setAttribute( "scaleBasedVisibility", scaleBasedVisibility );
315  categoryElem.setAttribute( "minScaleDenominator", QString::number( minScaleDenominator ) );
316  categoryElem.setAttribute( "maxScaleDenominator", QString::number( maxScaleDenominator ) );
317  categoryElem.setAttribute( "transparency", QString::number( transparency ) );
318 
319  //diagram size unit type and scale
320  categoryElem.setAttribute( "sizeType", QgsSymbolLayerV2Utils::encodeOutputUnit( sizeType ) );
321  categoryElem.setAttribute( "sizeScale", QgsSymbolLayerV2Utils::encodeMapUnitScale( sizeScale ) );
322 
323  //line width unit type and scale
324  categoryElem.setAttribute( "lineSizeType", QgsSymbolLayerV2Utils::encodeOutputUnit( lineSizeUnit ) );
325  categoryElem.setAttribute( "lineSizeScale", QgsSymbolLayerV2Utils::encodeMapUnitScale( lineSizeScale ) );
326 
327  // label placement method (text diagram)
328  if ( labelPlacementMethod == Height )
329  {
330  categoryElem.setAttribute( "labelPlacementMethod", "Height" );
331  }
332  else
333  {
334  categoryElem.setAttribute( "labelPlacementMethod", "XHeight" );
335  }
336 
337  if ( scaleByArea )
338  {
339  categoryElem.setAttribute( "scaleDependency", "Area" );
340  }
341  else
342  {
343  categoryElem.setAttribute( "scaleDependency", "Diameter" );
344  }
345 
346  // orientation (histogram)
347  switch ( diagramOrientation )
348  {
349  case Left:
350  categoryElem.setAttribute( "diagramOrientation", "Left" );
351  break;
352 
353  case Right:
354  categoryElem.setAttribute( "diagramOrientation", "Right" );
355  break;
356 
357  case Down:
358  categoryElem.setAttribute( "diagramOrientation", "Down" );
359  break;
360 
361  case Up:
362  categoryElem.setAttribute( "diagramOrientation", "Up" );
363  break;
364 
365  default:
366  categoryElem.setAttribute( "diagramOrientation", "Up" );
367  break;
368  }
369 
370  categoryElem.setAttribute( "barWidth", QString::number( barWidth ) );
371  categoryElem.setAttribute( "minimumSize", QString::number( minimumSize ) );
372  categoryElem.setAttribute( "angleOffset", QString::number( angleOffset ) );
373 
374  int nCats = qMin( categoryColors.size(), categoryAttributes.size() );
375  for ( int i = 0; i < nCats; ++i )
376  {
377  QDomElement attributeElem = doc.createElement( "attribute" );
378 
379  attributeElem.setAttribute( "field", categoryAttributes.at( i ) );
380  attributeElem.setAttribute( "color", categoryColors.at( i ).name() );
381  attributeElem.setAttribute( "label", categoryLabels.at( i ) );
382  categoryElem.appendChild( attributeElem );
383  }
384 
385  rendererElem.appendChild( categoryElem );
386 }
387 
389  : mDiagram( nullptr )
390  , mShowAttributeLegend( true )
391  , mShowSizeLegend( false )
392  , mSizeLegendSymbol( QgsMarkerSymbolV2::createSimple( QgsStringMap() ) )
393 {
394 }
395 
397 {
398  delete mDiagram;
399 }
400 
402 {
403  delete mDiagram;
404  mDiagram = d;
405 }
406 
408  : mDiagram( other.mDiagram ? other.mDiagram->clone() : nullptr )
411  , mSizeLegendSymbol( other.mSizeLegendSymbol.data() ? other.mSizeLegendSymbol->clone() : nullptr )
412 {
413 }
414 
416 {
417  mDiagram = other.mDiagram ? other.mDiagram->clone() : nullptr;
420  mSizeLegendSymbol.reset( other.mSizeLegendSymbol.data() ? other.mSizeLegendSymbol->clone() : nullptr );
421  return *this;
422 }
423 
425 {
426  if ( !mDiagram )
427  {
428  return;
429  }
430 
432  if ( !diagramSettings( feature, c, s ) )
433  {
434  return;
435  }
436 
437  mDiagram->renderDiagram( feature, c, s, pos );
438 }
439 
441 {
443  if ( !diagramSettings( feature, c, s ) )
444  {
445  return QSizeF();
446  }
447 
448  QSizeF size = diagramSize( feature, c );
449  if ( size.isValid() )
450  {
451  double width = QgsSymbolLayerV2Utils::convertToMapUnits( c, size.width(), s.sizeType, s.sizeScale );
452  size.rheight() *= width / size.width();
453  size.setWidth( width );
454  }
455  return size;
456 }
457 
459 {
460  Q_UNUSED( fields );
461 
462  QSet< QString > referenced;
463 
464  if ( !mDiagram )
465  return referenced;
466 
467  Q_FOREACH ( const QString& att, diagramAttributes() )
468  {
469  QgsExpression* expression = mDiagram->getExpression( att, context );
470  Q_FOREACH ( const QString& field, expression->referencedColumns() )
471  {
472  referenced << field;
473  }
474  }
475  return referenced;
476 }
477 
479 {
480  if ( !size.isValid() )
481  {
482  return;
483  }
484 
485  double pixelToMap = context.scaleFactor() * context.mapToPixel().mapUnitsPerPixel();
486  size.rwidth() *= pixelToMap;
487  size.rheight() *= pixelToMap;
488 }
489 
491 {
492  if ( painter )
493  {
494  QPaintDevice* device = painter->device();
495  if ( device )
496  {
497  return device->logicalDpiX();
498  }
499  }
500  return -1;
501 }
502 
504 {
505  Q_UNUSED( layer )
506 
507  delete mDiagram;
508  QString diagramType = elem.attribute( "diagramType" );
509  if ( diagramType == "Pie" )
510  {
511  mDiagram = new QgsPieDiagram();
512  }
513  else if ( diagramType == "Text" )
514  {
515  mDiagram = new QgsTextDiagram();
516  }
517  else if ( diagramType == "Histogram" )
518  {
520  }
521  else
522  {
523  mDiagram = nullptr;
524  }
525  mShowAttributeLegend = ( elem.attribute( "attributeLegend", "1" ) != "0" );
526  mShowSizeLegend = ( elem.attribute( "sizeLegend", "0" ) != "0" );
527  QDomElement sizeLegendSymbolElem = elem.firstChildElement( "symbol" );
528  if ( !sizeLegendSymbolElem.isNull() && sizeLegendSymbolElem.attribute( "name" ) == "sizeSymbol" )
529  {
530  mSizeLegendSymbol.reset( QgsSymbolLayerV2Utils::loadSymbol<QgsMarkerSymbolV2>( sizeLegendSymbolElem ) );
531  }
532 }
533 
534 void QgsDiagramRendererV2::_writeXML( QDomElement& rendererElem, QDomDocument& doc, const QgsVectorLayer* layer ) const
535 {
536  Q_UNUSED( doc );
537  Q_UNUSED( layer )
538 
539  if ( mDiagram )
540  {
541  rendererElem.setAttribute( "diagramType", mDiagram->diagramName() );
542  }
543  rendererElem.setAttribute( "attributeLegend", mShowAttributeLegend );
544  rendererElem.setAttribute( "sizeLegend", mShowSizeLegend );
545  QDomElement sizeLegendSymbolElem = QgsSymbolLayerV2Utils::saveSymbol( "sizeSymbol", mSizeLegendSymbol.data(), doc );
546  rendererElem.appendChild( sizeLegendSymbolElem );
547 }
548 
550 {
551 }
552 
554 {
555 }
556 
558 {
559  return new QgsSingleCategoryDiagramRenderer( *this );
560 }
561 
563 {
564  Q_UNUSED( c );
565  s = mSettings;
566  return true;
567 }
568 
570 {
571  return mDiagram->diagramSize( feature.attributes(), c, mSettings );
572 }
573 
575 {
576  QList<QgsDiagramSettings> settingsList;
577  settingsList.push_back( mSettings );
578  return settingsList;
579 }
580 
582 {
583  QDomElement categoryElem = elem.firstChildElement( "DiagramCategory" );
584  if ( categoryElem.isNull() )
585  {
586  return;
587  }
588 
589  mSettings.readXML( categoryElem, layer );
590  _readXML( elem, layer );
591 }
592 
594 {
595  QDomElement rendererElem = doc.createElement( "SingleCategoryDiagramRenderer" );
596  mSettings.writeXML( rendererElem, doc, layer );
597  _writeXML( rendererElem, doc, layer );
598  layerElem.appendChild( rendererElem );
599 }
600 
601 
603 {
604  mInterpolationSettings.classificationAttributeIsExpression = false;
605 }
606 
608 {
609 }
610 
612 {
613  return new QgsLinearlyInterpolatedDiagramRenderer( *this );
614 }
615 
617 {
618  QList<QgsDiagramSettings> settingsList;
619  settingsList.push_back( mSettings );
620  return settingsList;
621 }
622 
624 {
625  s = mSettings;
626  s.size = diagramSize( feature, c );
627  return true;
628 }
629 
631 {
632  return mSettings.categoryAttributes;
633 }
634 
636 {
637  QSet< QString > referenced = QgsDiagramRendererV2::referencedFields( context, fields );
638  if ( mInterpolationSettings.classificationAttributeIsExpression )
639  {
640  QgsExpression* expression = mDiagram->getExpression( mInterpolationSettings.classificationAttributeExpression, context );
641  Q_FOREACH ( const QString& field, expression->referencedColumns() )
642  {
643  referenced << field;
644  }
645  }
646  else
647  {
648  referenced << fields.at( mInterpolationSettings.classificationAttribute ).name();
649  }
650  return referenced;
651 }
652 
654 {
655  return mDiagram->diagramSize( feature, c, mSettings, mInterpolationSettings );
656 }
657 
659 {
660  mInterpolationSettings.lowerValue = elem.attribute( "lowerValue" ).toDouble();
661  mInterpolationSettings.upperValue = elem.attribute( "upperValue" ).toDouble();
662  mInterpolationSettings.lowerSize.setWidth( elem.attribute( "lowerWidth" ).toDouble() );
663  mInterpolationSettings.lowerSize.setHeight( elem.attribute( "lowerHeight" ).toDouble() );
664  mInterpolationSettings.upperSize.setWidth( elem.attribute( "upperWidth" ).toDouble() );
665  mInterpolationSettings.upperSize.setHeight( elem.attribute( "upperHeight" ).toDouble() );
666  mInterpolationSettings.classificationAttributeIsExpression = elem.hasAttribute( "classificationAttributeExpression" );
667  if ( mInterpolationSettings.classificationAttributeIsExpression )
668  {
669  mInterpolationSettings.classificationAttributeExpression = elem.attribute( "classificationAttributeExpression" );
670  }
671  else
672  {
673  mInterpolationSettings.classificationAttribute = elem.attribute( "classificationAttribute" ).toInt();
674  }
675  QDomElement settingsElem = elem.firstChildElement( "DiagramCategory" );
676  if ( !settingsElem.isNull() )
677  {
678  mSettings.readXML( settingsElem, layer );
679  }
680  _readXML( elem, layer );
681 }
682 
684 {
685  QDomElement rendererElem = doc.createElement( "LinearlyInterpolatedDiagramRenderer" );
686  rendererElem.setAttribute( "lowerValue", QString::number( mInterpolationSettings.lowerValue ) );
687  rendererElem.setAttribute( "upperValue", QString::number( mInterpolationSettings.upperValue ) );
688  rendererElem.setAttribute( "lowerWidth", QString::number( mInterpolationSettings.lowerSize.width() ) );
689  rendererElem.setAttribute( "lowerHeight", QString::number( mInterpolationSettings.lowerSize.height() ) );
690  rendererElem.setAttribute( "upperWidth", QString::number( mInterpolationSettings.upperSize.width() ) );
691  rendererElem.setAttribute( "upperHeight", QString::number( mInterpolationSettings.upperSize.height() ) );
692  if ( mInterpolationSettings.classificationAttributeIsExpression )
693  {
694  rendererElem.setAttribute( "classificationAttributeExpression", mInterpolationSettings.classificationAttributeExpression );
695  }
696  else
697  {
698  rendererElem.setAttribute( "classificationAttribute", mInterpolationSettings.classificationAttribute );
699  }
700  mSettings.writeXML( rendererElem, doc, layer );
701  _writeXML( rendererElem, doc, layer );
702  layerElem.appendChild( rendererElem );
703 }
704 
706 {
708  list.reserve( categoryLabels.size() );
709  for ( int i = 0 ; i < categoryLabels.size(); ++i )
710  {
711  QPixmap pix( 16, 16 );
712  pix.fill( categoryColors[i] );
713  list << new QgsSimpleLegendNode( nodeLayer, categoryLabels[i], QIcon( pix ), nullptr, QString( "diagram_%1" ).arg( QString::number( i ) ) );
714  }
715  return list;
716 }
717 
719 {
721 }
722 
724 {
726  if ( mShowAttributeLegend )
727  nodes = mSettings.legendItems( nodeLayer );
728 
729  return nodes;
730 }
731 
733 {
735  if ( mShowAttributeLegend )
736  nodes = mSettings.legendItems( nodeLayer );
737 
739  {
740  // add size legend
741  Q_FOREACH ( double v, QgsSymbolLayerV2Utils::prettyBreaks( mInterpolationSettings.lowerValue, mInterpolationSettings.upperValue, 4 ) )
742  {
743  double size = mDiagram->legendSize( v, mSettings, mInterpolationSettings );
745  QgsMarkerSymbolV2 * s = static_cast<QgsMarkerSymbolV2 *>( si.symbol() );
746  s->setSize( size );
747  s->setSizeUnit( mSettings.sizeType );
748  s->setSizeMapUnitScale( mSettings.sizeScale );
749  nodes << new QgsSymbolV2LegendNode( nodeLayer, si );
750  }
751  }
752 
753  return nodes;
754 }
static QString encodeOutputUnit(QgsSymbolV2::OutputUnit unit)
double zIndex
Z-index of diagrams, where diagrams with a higher z-index are drawn on top of diagrams with a lower z...
Class for parsing and evaluation of expressions (formerly called "search strings").
QDomNodeList elementsByTagName(const QString &tagname) const
void writeXML(QDomElement &layerElem, QDomDocument &doc, const QgsVectorLayer *layer) const override
virtual QSizeF sizeMapUnits(const QgsFeature &feature, const QgsRenderContext &c) const
Returns size of the diagram for a feature in map units.
QgsAttributes attributes() const
Returns the feature&#39;s attributes.
Definition: qgsfeature.cpp:110
static QList< double > prettyBreaks(double minimum, double maximum, int classes)
Computes a sequence of about &#39;classes&#39; equally spaced round values which cover the range of values fr...
bool showAll
Whether to show all diagrams, including overlapping diagrams.
QString name
Definition: qgsfield.h:52
QgsSingleCategoryDiagramRenderer * 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)
void _readXML(const QDomElement &elem, const QgsVectorLayer *layer)
Renders the diagrams for all features with the same settings.
QString attribute(const QString &name, const QString &defValue) const
QList< QString > categoryAttributes
virtual QList< QString > diagramAttributes() const =0
Returns attribute indices needed for diagram rendering.
Q_DECL_DEPRECATED QgsExpression * getExpression(const QString &expression, const QgsFields *fields)
Definition: qgsdiagram.cpp:47
int yPosColumn
Attribute index for y coordinate (or -1 if position not data defined)
virtual QList< QgsLayerTreeModelLegendNode *> legendItems(QgsLayerTreeLayer *nodeLayer) const
Returns list of legend nodes for the diagram.
QStringList split(const QString &sep, SplitBehavior behavior, Qt::CaseSensitivity cs) const
void reserve(int alloc)
QStringList referencedColumns() const
Get list of columns referenced by the expression.
unsigned int placementFlags
Diagram placement flags.
qreal & rwidth()
bool isValid() const
#define Q_NOWARN_DEPRECATED_PUSH
Definition: qgis.h:515
virtual QList< QgsDiagramSettings > diagramSettings() const =0
Returns list with all diagram settings in the renderer.
static QDomElement saveSymbol(const QString &symbolName, QgsSymbolV2 *symbol, QDomDocument &doc)
Container of fields for a vector layer.
Definition: qgsfield.h:252
void setAlpha(int alpha)
bool mShowAttributeLegend
Whether to show an attribute legend for the diagrams.
static int dpiPaintDevice(const QPainter *)
Returns the paint device dpi (or -1 in case of error.
static QString encodeMapUnitScale(const QgsMapUnitScale &mapUnitScale)
The feature class encapsulates a single feature including its id, geometry and a list of field/values...
Definition: qgsfeature.h:187
bool mShowSizeLegend
Whether to show a size legend for the diagrams.
double toDouble(bool *ok) const
QgsCoordinateTransform * ct
Associated coordinate transform. Owned by this object.
QSizeF diagramSize(const QgsFeature &, const QgsRenderContext &c) const override
Returns size of the diagram (in painter units) or an invalid size in case of error.
int count() const
Return number of items.
Definition: qgsfield.cpp:402
void readXML(const QDomElement &elem, const QgsVectorLayer *layer)
QList< QgsDiagramSettings > diagramSettings() const override
Returns list with all diagram settings in the renderer.
const QgsField & at(int i) const
Get field at particular index (must be in range 0..N-1)
Definition: qgsfield.cpp:422
Evaluates and returns the diagram settings relating to a diagram for a specific feature.
void reset(T *other)
static QgsSymbolV2::OutputUnit decodeOutputUnit(const QString &str)
QDomElement toElement() const
QString number(int n, int base)
void readXML(const QDomElement &elem, const QgsVectorLayer *layer) override
QgsDiagramLayerSettings & operator=(const QgsDiagramLayerSettings &rh)
int xPosColumn
Attribute index for x coordinate (or -1 if position not data defined)
QgsSymbolV2::OutputUnit sizeType
Diagram size unit.
bool hasAttribute(const QString &name) const
bool obstacle
Whether associated feature acts as an obstacle for other labels or diagrams.
virtual double legendSize(double value, const QgsDiagramSettings &s, const QgsDiagramInterpolationSettings &is) const =0
Returns the size of the legend item for the diagram corresponding to a specified value.
void readXML(const QDomElement &elem, const QgsVectorLayer *layer)
virtual QSizeF diagramSize(const QgsFeature &features, const QgsRenderContext &c) const =0
Returns size of the diagram (in painter units) or an invalid size in case of error.
void setAttribute(const QString &name, const QString &value)
QScopedPointer< QgsMarkerSymbolV2 > mSizeLegendSymbol
Marker symbol to use in size legends.
void setWidth(qreal width)
int toInt(bool *ok, int base) const
Expression contexts are used to encapsulate the parameters around which a QgsExpression should be eva...
The output shall be in map unitx.
Definition: qgssymbolv2.h:68
QPaintDevice * device() const
LinePlacementFlags
Line placement flags for controlling line based placements.
void setSize(double size)
Sets the size for the whole symbol.
QgsMapUnitScale sizeScale
Diagram size unit scale.
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.
Q_DECL_DEPRECATED QgsFields fields
QgsDiagramRendererV2 * renderer
Associated diagram renderer. Owned by this object.
Implementation of legend node interface for displaying arbitrary label with icon. ...
Placement placement
Diagram placement.
double mapUnitsPerPixel() const
Return current map units per pixel.
static double convertToMapUnits(const QgsRenderContext &c, double size, QgsSymbolV2::OutputUnit unit, const QgsMapUnitScale &scale=QgsMapUnitScale())
Converts a size from the specied units to map units.
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.
QList< QgsLayerTreeModelLegendNode *> legendItems(QgsLayerTreeLayer *nodeLayer) const
Returns list of legend nodes for the diagram.
void convertSizeToMapUnits(QSizeF &size, const QgsRenderContext &context) const
Converts size from mm to map units.
int logicalDpiX() const
T * data() const
Base class for all diagram types.
Definition: qgsdiagram.h:36
QgsLinearlyInterpolatedDiagramRenderer * clone() const override
Returns new instance that is equivalent to this one.
double dist
Distance between diagram and the feature (in mm)
Stores the settings for rendering of all diagrams for a layer.
bool isNull() const
virtual QString diagramName() const =0
void setCoordinateTransform(QgsCoordinateTransform *transform)
Sets the coordinate transform associated with the layer.
void _writeXML(QDomElement &rendererElem, QDomDocument &doc, const QgsVectorLayer *layer) const
void writeXML(QDomElement &layerElem, QDomDocument &doc, const QgsVectorLayer *layer) const override
virtual QgsDiagram * clone() const =0
Returns an instance that is equivalent to this one.
#define Q_NOWARN_DEPRECATED_POP
Definition: qgis.h:516
void writeXML(QDomElement &rendererElem, QDomDocument &doc, const QgsVectorLayer *layer) const
void readXML(const QDomElement &elem, const QgsVectorLayer *layer) override
void renderDiagram(const QgsFeature &feature, QgsRenderContext &c, QPointF pos) const
Contains information about the context of a rendering operation.
void setRenderer(QgsDiagramRendererV2 *diagramRenderer)
Sets the diagram renderer associated with the layer.
const QgsMapToPixel & mapToPixel() const
QList< QgsLayerTreeModelLegendNode *> legendItems(QgsLayerTreeLayer *nodeLayer) const override
Returns list of legend nodes for the diagram.
virtual Q_DECL_DEPRECATED void renderDiagram(const QgsAttributes &att, QgsRenderContext &c, const QgsDiagramSettings &s, QPointF position)
Definition: qgsdiagram.cpp:148
QList< QString > diagramAttributes() const override
Returns attribute indices needed for diagram rendering.
QSet< QString > referencedFields(const QgsExpressionContext &context=QgsExpressionContext(), const QgsFields &fields=QgsFields()) const
Returns the set of any fields referenced by the layer&#39;s diagrams.
QDomElement firstChildElement(const QString &tagName) const
QgsCoordinateTransform * clone() const
int showColumn
Attribute index for visibility (or -1 if visibility not data defined)
Class for doing transforms between two map coordinate systems.
virtual QgsDiagramRendererV2 * clone() const =0
Returns new instance that is equivalent to this one.
int priority
Placement priority, where 0 = low and 10 = high.
Implementation of legend node interface for displaying preview of vector symbols and their labels and...
virtual QSet< QString > referencedFields(const QgsExpressionContext &context=QgsExpressionContext(), const QgsFields &fields=QgsFields()) const override
Returns the set of any fields required for diagram rendering.
int classificationAttribute
Index of the classification attribute.
Q_DECL_DEPRECATED const QgsMapToPixel * xform
static QDomElement toXmlElement(const QFont &font, QDomDocument &document, const QString &elementName)
Returns a DOM element containing the properties of the font.
int size() const
static QgsMapUnitScale decodeMapUnitScale(const QString &str)
uint length() const
const_iterator constEnd() const
qreal & rheight()
QDomElement createElement(const QString &tagName)
const_iterator constBegin() const
qreal height() const
QList< QgsLayerTreeModelLegendNode *> legendItems(QgsLayerTreeLayer *nodeLayer) const override
Returns list of legend nodes for the diagram.
void setDiagram(QgsDiagram *d)
double scaleFactor() const
Represents a vector layer which manages a vector based data sets.
void setHeight(qreal height)
The class stores information about one class/rule of a vector layer renderer in a unified way that ca...
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.
qreal width() const
void writeXML(QDomElement &layerElem, QDomDocument &doc, const QgsVectorLayer *layer) const
Stores the settings for rendering a single diagram.
virtual QSet< QString > referencedFields(const QgsExpressionContext &context=QgsExpressionContext(), const QgsFields &fields=QgsFields()) const
Returns the set of any fields required for diagram rendering.
QgsDiagramRendererV2 & operator=(const QgsDiagramRendererV2 &other)
Layer tree node points to a map layer.
QDomNode at(int index) const
QSizeF diagramSize(const QgsFeature &, const QgsRenderContext &c) const override
Returns size of the diagram (in painter units) or an invalid size in case of error.