QGIS API Documentation
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  QString colors;
375  int nCats = qMin( categoryColors.size(), categoryAttributes.size() );
376  for ( int i = 0; i < nCats; ++i )
377  {
378  QDomElement attributeElem = doc.createElement( "attribute" );
379 
380  attributeElem.setAttribute( "field", categoryAttributes.at( i ) );
381  attributeElem.setAttribute( "color", categoryColors.at( i ).name() );
382  attributeElem.setAttribute( "label", categoryLabels.at( i ) );
383  categoryElem.appendChild( attributeElem );
384  }
385 
386  rendererElem.appendChild( categoryElem );
387 }
388 
390  : mDiagram( nullptr )
391  , mShowAttributeLegend( true )
392  , mShowSizeLegend( false )
393  , mSizeLegendSymbol( QgsMarkerSymbolV2::createSimple( QgsStringMap() ) )
394 {
395 }
396 
398 {
399  delete mDiagram;
400 }
401 
403 {
404  delete mDiagram;
405  mDiagram = d;
406 }
407 
409  : mDiagram( other.mDiagram ? other.mDiagram->clone() : nullptr )
412  , mSizeLegendSymbol( other.mSizeLegendSymbol.data() ? other.mSizeLegendSymbol->clone() : nullptr )
413 {
414 }
415 
417 {
418  mDiagram = other.mDiagram ? other.mDiagram->clone() : nullptr;
421  mSizeLegendSymbol.reset( other.mSizeLegendSymbol.data() ? other.mSizeLegendSymbol->clone() : nullptr );
422  return *this;
423 }
424 
426 {
427  if ( !mDiagram )
428  {
429  return;
430  }
431 
433  if ( !diagramSettings( feature, c, s ) )
434  {
435  return;
436  }
437 
438  mDiagram->renderDiagram( feature, c, s, pos );
439 }
440 
442 {
444  if ( !diagramSettings( feature, c, s ) )
445  {
446  return QSizeF();
447  }
448 
449  QSizeF size = diagramSize( feature, c );
450  if ( size.isValid() )
451  {
452  double width = QgsSymbolLayerV2Utils::convertToMapUnits( c, size.width(), s.sizeType, s.sizeScale );
453  size.rheight() *= width / size.width();
454  size.setWidth( width );
455  }
456  return size;
457 }
458 
460 {
461  Q_UNUSED( fields );
462 
463  QSet< QString > referenced;
464 
465  if ( !mDiagram )
466  return referenced;
467 
468  Q_FOREACH ( const QString& att, diagramAttributes() )
469  {
470  QgsExpression* expression = mDiagram->getExpression( att, context );
471  Q_FOREACH ( const QString& field, expression->referencedColumns() )
472  {
473  referenced << field;
474  }
475  }
476  return referenced;
477 }
478 
480 {
481  if ( !size.isValid() )
482  {
483  return;
484  }
485 
486  double pixelToMap = context.scaleFactor() * context.mapToPixel().mapUnitsPerPixel();
487  size.rwidth() *= pixelToMap;
488  size.rheight() *= pixelToMap;
489 }
490 
492 {
493  if ( painter )
494  {
495  QPaintDevice* device = painter->device();
496  if ( device )
497  {
498  return device->logicalDpiX();
499  }
500  }
501  return -1;
502 }
503 
505 {
506  Q_UNUSED( layer )
507 
508  delete mDiagram;
509  QString diagramType = elem.attribute( "diagramType" );
510  if ( diagramType == "Pie" )
511  {
512  mDiagram = new QgsPieDiagram();
513  }
514  else if ( diagramType == "Text" )
515  {
516  mDiagram = new QgsTextDiagram();
517  }
518  else if ( diagramType == "Histogram" )
519  {
521  }
522  else
523  {
524  mDiagram = nullptr;
525  }
526  mShowAttributeLegend = ( elem.attribute( "attributeLegend", "1" ) != "0" );
527  mShowSizeLegend = ( elem.attribute( "sizeLegend", "0" ) != "0" );
528  QDomElement sizeLegendSymbolElem = elem.firstChildElement( "symbol" );
529  if ( !sizeLegendSymbolElem.isNull() && sizeLegendSymbolElem.attribute( "name" ) == "sizeSymbol" )
530  {
531  mSizeLegendSymbol.reset( QgsSymbolLayerV2Utils::loadSymbol<QgsMarkerSymbolV2>( sizeLegendSymbolElem ) );
532  }
533 }
534 
535 void QgsDiagramRendererV2::_writeXML( QDomElement& rendererElem, QDomDocument& doc, const QgsVectorLayer* layer ) const
536 {
537  Q_UNUSED( doc );
538  Q_UNUSED( layer )
539 
540  if ( mDiagram )
541  {
542  rendererElem.setAttribute( "diagramType", mDiagram->diagramName() );
543  }
544  rendererElem.setAttribute( "attributeLegend", mShowAttributeLegend );
545  rendererElem.setAttribute( "sizeLegend", mShowSizeLegend );
546  QDomElement sizeLegendSymbolElem = QgsSymbolLayerV2Utils::saveSymbol( "sizeSymbol", mSizeLegendSymbol.data(), doc );
547  rendererElem.appendChild( sizeLegendSymbolElem );
548 }
549 
551 {
552 }
553 
555 {
556 }
557 
559 {
560  return new QgsSingleCategoryDiagramRenderer( *this );
561 }
562 
564 {
565  Q_UNUSED( c );
566  s = mSettings;
567  return true;
568 }
569 
571 {
572  return mDiagram->diagramSize( feature.attributes(), c, mSettings );
573 }
574 
576 {
577  QList<QgsDiagramSettings> settingsList;
578  settingsList.push_back( mSettings );
579  return settingsList;
580 }
581 
583 {
584  QDomElement categoryElem = elem.firstChildElement( "DiagramCategory" );
585  if ( categoryElem.isNull() )
586  {
587  return;
588  }
589 
590  mSettings.readXML( categoryElem, layer );
591  _readXML( elem, layer );
592 }
593 
595 {
596  QDomElement rendererElem = doc.createElement( "SingleCategoryDiagramRenderer" );
597  mSettings.writeXML( rendererElem, doc, layer );
598  _writeXML( rendererElem, doc, layer );
599  layerElem.appendChild( rendererElem );
600 }
601 
602 
604 {
605  mInterpolationSettings.classificationAttributeIsExpression = false;
606 }
607 
609 {
610 }
611 
613 {
614  return new QgsLinearlyInterpolatedDiagramRenderer( *this );
615 }
616 
618 {
619  QList<QgsDiagramSettings> settingsList;
620  settingsList.push_back( mSettings );
621  return settingsList;
622 }
623 
625 {
626  s = mSettings;
627  s.size = diagramSize( feature, c );
628  return true;
629 }
630 
632 {
633  return mSettings.categoryAttributes;
634 }
635 
637 {
638  QSet< QString > referenced = QgsDiagramRendererV2::referencedFields( context, fields );
639  if ( mInterpolationSettings.classificationAttributeIsExpression )
640  {
641  QgsExpression* expression = mDiagram->getExpression( mInterpolationSettings.classificationAttributeExpression, context );
642  Q_FOREACH ( const QString& field, expression->referencedColumns() )
643  {
644  referenced << field;
645  }
646  }
647  else
648  {
649  referenced << fields.at( mInterpolationSettings.classificationAttribute ).name();
650  }
651  return referenced;
652 }
653 
655 {
656  return mDiagram->diagramSize( feature, c, mSettings, mInterpolationSettings );
657 }
658 
660 {
661  mInterpolationSettings.lowerValue = elem.attribute( "lowerValue" ).toDouble();
662  mInterpolationSettings.upperValue = elem.attribute( "upperValue" ).toDouble();
663  mInterpolationSettings.lowerSize.setWidth( elem.attribute( "lowerWidth" ).toDouble() );
664  mInterpolationSettings.lowerSize.setHeight( elem.attribute( "lowerHeight" ).toDouble() );
665  mInterpolationSettings.upperSize.setWidth( elem.attribute( "upperWidth" ).toDouble() );
666  mInterpolationSettings.upperSize.setHeight( elem.attribute( "upperHeight" ).toDouble() );
667  mInterpolationSettings.classificationAttributeIsExpression = elem.hasAttribute( "classificationAttributeExpression" );
668  if ( mInterpolationSettings.classificationAttributeIsExpression )
669  {
670  mInterpolationSettings.classificationAttributeExpression = elem.attribute( "classificationAttributeExpression" );
671  }
672  else
673  {
674  mInterpolationSettings.classificationAttribute = elem.attribute( "classificationAttribute" ).toInt();
675  }
676  QDomElement settingsElem = elem.firstChildElement( "DiagramCategory" );
677  if ( !settingsElem.isNull() )
678  {
679  mSettings.readXML( settingsElem, layer );
680  }
681  _readXML( elem, layer );
682 }
683 
685 {
686  QDomElement rendererElem = doc.createElement( "LinearlyInterpolatedDiagramRenderer" );
687  rendererElem.setAttribute( "lowerValue", QString::number( mInterpolationSettings.lowerValue ) );
688  rendererElem.setAttribute( "upperValue", QString::number( mInterpolationSettings.upperValue ) );
689  rendererElem.setAttribute( "lowerWidth", QString::number( mInterpolationSettings.lowerSize.width() ) );
690  rendererElem.setAttribute( "lowerHeight", QString::number( mInterpolationSettings.lowerSize.height() ) );
691  rendererElem.setAttribute( "upperWidth", QString::number( mInterpolationSettings.upperSize.width() ) );
692  rendererElem.setAttribute( "upperHeight", QString::number( mInterpolationSettings.upperSize.height() ) );
693  if ( mInterpolationSettings.classificationAttributeIsExpression )
694  {
695  rendererElem.setAttribute( "classificationAttributeExpression", mInterpolationSettings.classificationAttributeExpression );
696  }
697  else
698  {
699  rendererElem.setAttribute( "classificationAttribute", mInterpolationSettings.classificationAttribute );
700  }
701  mSettings.writeXML( rendererElem, doc, layer );
702  _writeXML( rendererElem, doc, layer );
703  layerElem.appendChild( rendererElem );
704 }
705 
707 {
709  list.reserve( categoryLabels.size() );
710  for ( int i = 0 ; i < categoryLabels.size(); ++i )
711  {
712  QPixmap pix( 16, 16 );
713  pix.fill( categoryColors[i] );
714  list << new QgsSimpleLegendNode( nodeLayer, categoryLabels[i], QIcon( pix ), nullptr, QString( "diagram_%1" ).arg( QString::number( i ) ) );
715  }
716  return list;
717 }
718 
720 {
722 }
723 
725 {
727  if ( mShowAttributeLegend )
728  nodes = mSettings.legendItems( nodeLayer );
729 
730  return nodes;
731 }
732 
734 {
736  if ( mShowAttributeLegend )
737  nodes = mSettings.legendItems( nodeLayer );
738 
740  {
741  // add size legend
742  Q_FOREACH ( double v, QgsSymbolLayerV2Utils::prettyBreaks( mInterpolationSettings.lowerValue, mInterpolationSettings.upperValue, 4 ) )
743  {
744  double size = mDiagram->legendSize( v, mSettings, mInterpolationSettings );
746  QgsMarkerSymbolV2 * s = static_cast<QgsMarkerSymbolV2 *>( si.symbol() );
747  s->setSize( size );
748  s->setSizeUnit( mSettings.sizeType );
749  s->setSizeMapUnitScale( mSettings.sizeScale );
750  nodes << new QgsSymbolV2LegendNode( nodeLayer, si );
751  }
752  }
753 
754  return nodes;
755 }
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
QStringList referencedColumns() const
Get list of columns referenced by the expression.
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.
QgsSingleCategoryDiagramRenderer * clone() const override
Returns new instance that is equivalent to this one.
QDomNode appendChild(const QDomNode &newChild)
virtual QSet< QString > referencedFields(const QgsExpressionContext &context=QgsExpressionContext(), const QgsFields &fields=QgsFields()) const
Returns the set of any fields required for diagram rendering.
void fill(const QColor &color)
void push_back(const T &value)
void _readXML(const QDomElement &elem, const QgsVectorLayer *layer)
virtual QList< QgsLayerTreeModelLegendNode * > legendItems(QgsLayerTreeLayer *nodeLayer) const
Returns list of legend nodes for the diagram.
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)
QStringList split(const QString &sep, SplitBehavior behavior, Qt::CaseSensitivity cs) const
void reserve(int alloc)
unsigned int placementFlags
Diagram placement flags.
qreal & rwidth()
bool isValid() const
#define Q_NOWARN_DEPRECATED_PUSH
Definition: qgis.h:474
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:187
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)
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.
double scaleFactor() const
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.
void readXML(const QDomElement &elem, const QgsVectorLayer *layer)
QList< QgsDiagramSettings > diagramSettings() const override
Returns list with all diagram settings in the renderer.
Evaluates and returns the diagram settings relating to a diagram for a specific feature.
QList< QgsLayerTreeModelLegendNode * > legendItems(QgsLayerTreeLayer *nodeLayer) const override
Returns list of legend nodes for the diagram.
void reset(T *other)
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.
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.
QgsAttributes attributes() const
Returns the feature&#39;s attributes.
Definition: qgsfeature.cpp:110
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)
QString name() const
Gets the name of the field.
Definition: qgsfield.cpp:84
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:65
QPaintDevice * device() const
QList< QgsLayerTreeModelLegendNode * > legendItems(QgsLayerTreeLayer *nodeLayer) const
Returns list of legend nodes for the diagram.
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.
int count() const
Return number of items.
Definition: qgsfield.cpp:365
Q_DECL_DEPRECATED QgsFields fields
double mapUnitsPerPixel() const
Return current map units per pixel.
QgsDiagramRendererV2 * renderer
Associated diagram renderer. Owned by this object.
Implementation of legend node interface for displaying arbitrary label with icon. ...
Placement placement
Diagram placement.
const QgsField & at(int i) const
Get field at particular index (must be in range 0..N-1)
Definition: qgsfield.cpp:385
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.
int logicalDpiX() const
T * data() const
Base class for all diagram types.
Definition: qgsdiagram.h:35
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 &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:475
void readXML(const QDomElement &elem, const QgsVectorLayer *layer) override
Contains information about the context of a rendering operation.
void setRenderer(QgsDiagramRendererV2 *diagramRenderer)
Sets the diagram renderer associated with the layer.
void renderDiagram(const QgsFeature &feature, QgsRenderContext &c, QPointF pos) const
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.
QDomElement firstChildElement(const QString &tagName) const
void writeXML(QDomElement &layerElem, QDomDocument &doc, const QgsVectorLayer *layer) 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.
const QgsMapToPixel & mapToPixel() const
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
void setDiagram(QgsDiagram *d)
Represents a vector layer which manages a vector based data sets.
void setHeight(qreal height)
void writeXML(QDomElement &rendererElem, QDomDocument &doc, const QgsVectorLayer *layer) const
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 &rendererElem, QDomDocument &doc, const QgsVectorLayer *layer) const
Stores the settings for rendering a single diagram.
virtual QSizeF sizeMapUnits(const QgsFeature &feature, const QgsRenderContext &c) const
Returns size of the diagram for a feature in map units.
QgsDiagramRendererV2 & operator=(const QgsDiagramRendererV2 &other)
Layer tree node points to a map layer.
QDomNode at(int index) const
QgsCoordinateTransform * clone() 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.