QGIS API Documentation
qgspointdisplacementrenderer.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  qgspointdisplacementrenderer.cpp
3  --------------------------------
4  begin : January 26, 2010
5  copyright : (C) 2010 by Marco Hugentobler
6  email : marco at hugis dot net
7  ***************************************************************************/
8 
9 /***************************************************************************
10  * *
11  * This program is free software; you can redistribute it and/or modify *
12  * it under the terms of the GNU General Public License as published by *
13  * the Free Software Foundation; either version 2 of the License, or *
14  * (at your option) any later version. *
15  * *
16  ***************************************************************************/
17 
19 #include "qgsgeometry.h"
20 #include "qgslogger.h"
21 #include "qgsspatialindex.h"
22 #include "qgssymbolv2.h"
23 #include "qgssymbollayerv2utils.h"
24 #include "qgsvectorlayer.h"
26 #include "qgspainteffect.h"
27 #include "qgspainteffectregistry.h"
28 #include "qgsfontutils.h"
29 #include "qgsmultipointv2.h"
30 #include "qgspointv2.h"
31 #include "qgsunittypes.h"
32 #include "qgswkbptr.h"
33 
34 #include <QDomElement>
35 #include <QPainter>
36 
37 #include <cmath>
38 
39 #ifndef M_SQRT2
40 #define M_SQRT2 1.41421356237309504880
41 #endif
42 
44  : QgsFeatureRendererV2( "pointDisplacement" )
45  , mLabelAttributeName( labelAttributeName )
46  , mLabelIndex( -1 )
47  , mTolerance( 3 )
48  , mToleranceUnit( QgsSymbolV2::MM )
49  , mPlacement( Ring )
50  , mCircleWidth( 0.4 )
51  , mCircleColor( QColor( 125, 125, 125 ) )
52  , mCircleRadiusAddition( 0 )
53  , mMaxLabelScaleDenominator( -1 )
54  , mSpatialIndex( nullptr )
55 {
57  mCenterSymbol = new QgsMarkerSymbolV2(); //the symbol for the center of a displacement group
58  mDrawLabels = true;
59 }
60 
62 {
63  delete mCenterSymbol;
64  delete mRenderer;
65 }
66 
68 {
69  QgsPointDisplacementRenderer* r = new QgsPointDisplacementRenderer( mLabelAttributeName );
70  r->setEmbeddedRenderer( mRenderer->clone() );
71  r->setCircleWidth( mCircleWidth );
72  r->setCircleColor( mCircleColor );
73  r->setLabelFont( mLabelFont );
74  r->setLabelColor( mLabelColor );
75  r->setPlacement( mPlacement );
76  r->setCircleRadiusAddition( mCircleRadiusAddition );
77  r->setMaxLabelScaleDenominator( mMaxLabelScaleDenominator );
78  r->setTolerance( mTolerance );
79  r->setToleranceUnit( mToleranceUnit );
80  r->setToleranceMapUnitScale( mToleranceMapUnitScale );
81  if ( mCenterSymbol )
82  {
83  r->setCenterSymbol( mCenterSymbol->clone() );
84  }
85  copyRendererData( r );
86  return r;
87 }
88 
90 {
91  mRenderer->toSld( doc, element );
92 }
93 
94 
95 bool QgsPointDisplacementRenderer::renderFeature( QgsFeature& feature, QgsRenderContext& context, int layer, bool selected, bool drawVertexMarker )
96 {
97  Q_UNUSED( drawVertexMarker );
98  Q_UNUSED( context );
99  Q_UNUSED( layer );
100 
101  //check, if there is already a point at that position
102  if ( !feature.constGeometry() )
103  return false;
104 
105  QgsSymbolV2* symbol = firstSymbolForFeature( mRenderer, feature, context );
106 
107  //if the feature has no symbol (eg, no matching rule in a rule-based renderer), skip it
108  if ( !symbol )
109  return false;
110 
111  //point position in screen coords
112  const QgsGeometry* geom = feature.constGeometry();
113  QGis::WkbType geomType = geom->wkbType();
114  if ( geomType != QGis::WKBPoint && geomType != QGis::WKBPoint25D )
115  {
116  //can only render point type
117  return false;
118  }
119 
120  if ( selected )
121  mSelectedFeatures.insert( feature.id() );
122 
123  double searchDistance = mTolerance * QgsSymbolLayerV2Utils::mapUnitScaleFactor( context, mToleranceUnit, mToleranceMapUnitScale );
124  QList<QgsFeatureId> intersectList = mSpatialIndex->intersects( searchRect( feature.constGeometry()->asPoint(), searchDistance ) );
125  if ( intersectList.empty() )
126  {
127  mSpatialIndex->insertFeature( feature );
128  // create new group
129  DisplacementGroup newGroup;
130  newGroup.insert( feature.id(), qMakePair( feature, symbol ) );
131  mDisplacementGroups.push_back( newGroup );
132  // add to group index
133  mGroupIndex.insert( feature.id(), mDisplacementGroups.count() - 1 );
134  return true;
135  }
136 
137  //go through all the displacement group maps and search an entry where the id equals the result of the spatial search
138  QgsFeatureId existingEntry = intersectList.at( 0 );
139 
140  int groupIdx = mGroupIndex[ existingEntry ];
141  DisplacementGroup& group = mDisplacementGroups[groupIdx];
142 
143  // add to a group
144  group.insert( feature.id(), qMakePair( feature, symbol ) );
145  // add to group index
146  mGroupIndex.insert( feature.id(), groupIdx );
147  return true;
148 }
149 
150 void QgsPointDisplacementRenderer::drawGroup( const DisplacementGroup& group, QgsRenderContext& context )
151 {
152  const QgsFeature& feature = group.begin().value().first;
153  bool selected = mSelectedFeatures.contains( feature.id() ); // maybe we should highlight individual features instead of the whole group?
154 
155 
156 
157  //get list of labels and symbols
158  QStringList labelAttributeList;
159  QList<QgsMarkerSymbolV2*> symbolList;
160 
161  QgsMultiPointV2* groupMultiPoint = new QgsMultiPointV2();
162  for ( DisplacementGroup::const_iterator attIt = group.constBegin(); attIt != group.constEnd(); ++attIt )
163  {
164  labelAttributeList << ( mDrawLabels ? getLabel( attIt.value().first ) : QString() );
165  symbolList << dynamic_cast<QgsMarkerSymbolV2*>( attIt.value().second );
166  groupMultiPoint->addGeometry( attIt.value().first.constGeometry()->geometry()->clone() );
167  }
168 
169  //calculate centroid of all points, this will be center of group
170  QgsGeometry groupGeom( groupMultiPoint );
171  QgsGeometry* centroid = groupGeom.centroid();
172  QPointF pt;
173  _getPoint( pt, context, QgsConstWkbPtr( centroid->asWkb(), centroid->wkbSize() ) );
174  delete centroid;
175 
176  //calculate max diagonal size from all symbols in group
177  double diagonal = 0;
178  Q_FOREACH ( QgsMarkerSymbolV2* symbol, symbolList )
179  {
180  if ( symbol )
181  {
182  diagonal = qMax( diagonal, QgsSymbolLayerV2Utils::convertToPainterUnits( context,
183  M_SQRT2 * symbol->size(),
184  symbol->outputUnit(), symbol->mapUnitScale() ) );
185  }
186  }
187 
188  QgsSymbolV2RenderContext symbolContext( context, QgsSymbolV2::MM, 1.0, selected );
189 
190  QList<QPointF> symbolPositions;
191  QList<QPointF> labelPositions;
192  double circleRadius = -1.0;
193  calculateSymbolAndLabelPositions( symbolContext, pt, symbolList.size(), diagonal, symbolPositions, labelPositions, circleRadius );
194 
195  //draw Circle
196  if ( circleRadius > 0 )
197  drawCircle( circleRadius, symbolContext, pt, symbolList.size() );
198 
199  //draw mid point
200  if ( labelAttributeList.size() > 1 )
201  {
202  if ( mCenterSymbol )
203  {
204  mCenterSymbol->renderPoint( pt, &feature, context, -1, selected );
205  }
206  else
207  {
208  context.painter()->drawRect( QRectF( pt.x() - symbolContext.outputLineWidth( 1 ), pt.y() - symbolContext.outputLineWidth( 1 ), symbolContext.outputLineWidth( 2 ), symbolContext.outputLineWidth( 2 ) ) );
209  }
210  }
211 
212  //draw symbols on the circle
213  drawSymbols( feature, context, symbolList, symbolPositions, selected );
214  //and also the labels
215  drawLabels( pt, symbolContext, labelPositions, labelAttributeList );
216 }
217 
219 {
220  delete mRenderer;
221  mRenderer = r;
222 }
223 
225 {
226  QList<QString> attributeList;
227  if ( !mLabelAttributeName.isEmpty() )
228  {
229  attributeList.push_back( mLabelAttributeName );
230  }
231  if ( mRenderer )
232  {
233  attributeList += mRenderer->usedAttributes();
234  }
235  return attributeList;
236 }
237 
239 {
240  if ( !mRenderer )
241  {
242  return 0;
243  }
244  return mRenderer->capabilities();
245 }
246 
248 {
249  if ( !mRenderer )
250  {
251  return QgsSymbolV2List();
252  }
253  return mRenderer->symbols( context );
254 }
255 
257 {
258  if ( !mRenderer )
259  {
260  return nullptr;
261  }
262  return mRenderer->symbolForFeature( feature, context );
263 }
264 
266 {
267  if ( !mRenderer )
268  return nullptr;
269  return mRenderer->originalSymbolForFeature( feat, context );
270 }
271 
273 {
274  if ( !mRenderer )
275  {
276  return QgsSymbolV2List();
277  }
278  return mRenderer->symbolsForFeature( feature, context );
279 }
280 
282 {
283  if ( !mRenderer )
284  return QgsSymbolV2List();
285  return mRenderer->originalSymbolsForFeature( feat, context );
286 }
287 
289 {
290  if ( !mRenderer )
291  {
292  return false;
293  }
294  return mRenderer->willRenderFeature( feat, context );
295 }
296 
297 
299 {
300  mRenderer->startRender( context, fields );
301 
302  mDisplacementGroups.clear();
303  mGroupIndex.clear();
304  mSpatialIndex = new QgsSpatialIndex;
305  mSelectedFeatures.clear();
306 
307  if ( mLabelAttributeName.isEmpty() )
308  {
309  mLabelIndex = -1;
310  }
311  else
312  {
313  mLabelIndex = fields.fieldNameIndex( mLabelAttributeName );
314  }
315 
316  if ( mMaxLabelScaleDenominator > 0 && context.rendererScale() > mMaxLabelScaleDenominator )
317  {
318  mDrawLabels = false;
319  }
320  else
321  {
322  mDrawLabels = true;
323  }
324 
325  if ( mCenterSymbol )
326  {
327  mCenterSymbol->startRender( context, &fields );
328  }
329  return;
330 }
331 
333 {
334  QgsDebugMsg( "QgsPointDisplacementRenderer::stopRender" );
335 
336  //printInfoDisplacementGroups(); //just for debugging
337 
338  Q_FOREACH ( const DisplacementGroup& group, mDisplacementGroups )
339  {
340  drawGroup( group, context );
341  }
342 
343  mDisplacementGroups.clear();
344  mGroupIndex.clear();
345  delete mSpatialIndex;
346  mSpatialIndex = nullptr;
347  mSelectedFeatures.clear();
348 
349  mRenderer->stopRender( context );
350  if ( mCenterSymbol )
351  {
352  mCenterSymbol->stopRender( context );
353  }
354 }
355 
357 {
359  r->setLabelAttributeName( symbologyElem.attribute( "labelAttributeName" ) );
361  if ( !QgsFontUtils::setFromXmlChildNode( labelFont, symbologyElem, "labelFontProperties" ) )
362  {
363  labelFont.fromString( symbologyElem.attribute( "labelFont", "" ) );
364  }
365  r->setLabelFont( labelFont );
366  r->setPlacement( static_cast< Placement >( symbologyElem.attribute( "placement", "0" ).toInt() ) );
367  r->setCircleWidth( symbologyElem.attribute( "circleWidth", "0.4" ).toDouble() );
368  r->setCircleColor( QgsSymbolLayerV2Utils::decodeColor( symbologyElem.attribute( "circleColor", "" ) ) );
369  r->setLabelColor( QgsSymbolLayerV2Utils::decodeColor( symbologyElem.attribute( "labelColor", "" ) ) );
370  r->setCircleRadiusAddition( symbologyElem.attribute( "circleRadiusAddition", "0.0" ).toDouble() );
371  r->setMaxLabelScaleDenominator( symbologyElem.attribute( "maxLabelScaleDenominator", "-1" ).toDouble() );
372  r->setTolerance( symbologyElem.attribute( "tolerance", "0.00001" ).toDouble() );
373  r->setToleranceUnit( QgsSymbolLayerV2Utils::decodeOutputUnit( symbologyElem.attribute( "toleranceUnit", "MapUnit" ) ) );
374  r->setToleranceMapUnitScale( QgsSymbolLayerV2Utils::decodeMapUnitScale( symbologyElem.attribute( "toleranceUnitScale" ) ) );
375 
376  //look for an embedded renderer <renderer-v2>
377  QDomElement embeddedRendererElem = symbologyElem.firstChildElement( "renderer-v2" );
378  if ( !embeddedRendererElem.isNull() )
379  {
380  r->setEmbeddedRenderer( QgsFeatureRendererV2::load( embeddedRendererElem ) );
381  }
382 
383  //center symbol
384  QDomElement centerSymbolElem = symbologyElem.firstChildElement( "symbol" );
385  if ( !centerSymbolElem.isNull() )
386  {
387  r->setCenterSymbol( QgsSymbolLayerV2Utils::loadSymbol<QgsMarkerSymbolV2>( centerSymbolElem ) );
388  }
389  return r;
390 }
391 
393 {
394  QDomElement rendererElement = doc.createElement( RENDERER_TAG_NAME );
395  rendererElement.setAttribute( "forceraster", ( mForceRaster ? "1" : "0" ) );
396  rendererElement.setAttribute( "type", "pointDisplacement" );
397  rendererElement.setAttribute( "labelAttributeName", mLabelAttributeName );
398  rendererElement.appendChild( QgsFontUtils::toXmlElement( mLabelFont, doc, "labelFontProperties" ) );
399  rendererElement.setAttribute( "circleWidth", QString::number( mCircleWidth ) );
400  rendererElement.setAttribute( "circleColor", QgsSymbolLayerV2Utils::encodeColor( mCircleColor ) );
401  rendererElement.setAttribute( "labelColor", QgsSymbolLayerV2Utils::encodeColor( mLabelColor ) );
402  rendererElement.setAttribute( "circleRadiusAddition", QString::number( mCircleRadiusAddition ) );
403  rendererElement.setAttribute( "placement", static_cast< int >( mPlacement ) );
404  rendererElement.setAttribute( "maxLabelScaleDenominator", QString::number( mMaxLabelScaleDenominator ) );
405  rendererElement.setAttribute( "tolerance", QString::number( mTolerance ) );
406  rendererElement.setAttribute( "toleranceUnit", QgsSymbolLayerV2Utils::encodeOutputUnit( mToleranceUnit ) );
407  rendererElement.setAttribute( "toleranceUnitScale", QgsSymbolLayerV2Utils::encodeMapUnitScale( mToleranceMapUnitScale ) );
408 
409  if ( mRenderer )
410  {
411  QDomElement embeddedRendererElem = mRenderer->save( doc );
412  rendererElement.appendChild( embeddedRendererElem );
413  }
414  if ( mCenterSymbol )
415  {
416  QDomElement centerSymbolElem = QgsSymbolLayerV2Utils::saveSymbol( "centerSymbol", mCenterSymbol, doc );
417  rendererElement.appendChild( centerSymbolElem );
418  }
419 
421  mPaintEffect->saveProperties( doc, rendererElement );
422 
423  if ( !mOrderBy.isEmpty() )
424  {
425  QDomElement orderBy = doc.createElement( "orderby" );
426  mOrderBy.save( orderBy );
427  rendererElement.appendChild( orderBy );
428  }
429  rendererElement.setAttribute( "enableorderby", ( mOrderByEnabled ? "1" : "0" ) );
430 
431  return rendererElement;
432 }
433 
435 {
436  if ( mRenderer )
437  {
438  return mRenderer->legendSymbologyItems( iconSize );
439  }
440  return QgsLegendSymbologyList();
441 }
442 
444 {
445  if ( mRenderer )
446  {
447  return mRenderer->legendSymbolItems( scaleDenominator, rule );
448  }
449  return QgsLegendSymbolList();
450 }
451 
452 
453 QgsRectangle QgsPointDisplacementRenderer::searchRect( const QgsPoint& p, double distance ) const
454 {
455  return QgsRectangle( p.x() - distance, p.y() - distance, p.x() + distance, p.y() + distance );
456 }
457 
458 void QgsPointDisplacementRenderer::printInfoDisplacementGroups()
459 {
460  int nGroups = mDisplacementGroups.size();
461  QgsDebugMsg( "number of displacement groups:" + QString::number( nGroups ) );
462  for ( int i = 0; i < nGroups; ++i )
463  {
464  QgsDebugMsg( "***************displacement group " + QString::number( i ) );
465  DisplacementGroup::const_iterator it = mDisplacementGroups.at( i ).constBegin();
466  for ( ; it != mDisplacementGroups.at( i ).constEnd(); ++it )
467  {
468  QgsDebugMsg( FID_TO_STRING( it.key() ) );
469  }
470  }
471 }
472 
473 QString QgsPointDisplacementRenderer::getLabel( const QgsFeature& f )
474 {
475  QString attribute;
476  QgsAttributes attrs = f.attributes();
477  if ( mLabelIndex >= 0 && mLabelIndex < attrs.count() )
478  {
479  attribute = attrs.at( mLabelIndex ).toString();
480  }
481  return attribute;
482 }
483 
485 {
486  delete mCenterSymbol;
487  mCenterSymbol = symbol;
488 }
489 
490 
491 
492 void QgsPointDisplacementRenderer::calculateSymbolAndLabelPositions( QgsSymbolV2RenderContext& symbolContext, QPointF centerPoint, int nPosition,
493  double symbolDiagonal, QList<QPointF>& symbolPositions, QList<QPointF>& labelShifts, double& circleRadius ) const
494 {
495  symbolPositions.clear();
496  labelShifts.clear();
497 
498  if ( nPosition < 1 )
499  {
500  return;
501  }
502  else if ( nPosition == 1 ) //If there is only one feature, draw it exactly at the center position
503  {
504  symbolPositions.append( centerPoint );
505  labelShifts.append( QPointF( symbolDiagonal / 2.0, -symbolDiagonal / 2.0 ) );
506  return;
507  }
508 
509  double circleAdditionPainterUnits = symbolContext.outputLineWidth( mCircleRadiusAddition );
510 
511  switch ( mPlacement )
512  {
513  case Ring:
514  {
515  double minDiameterToFitSymbols = nPosition * symbolDiagonal / ( 2.0 * M_PI );
516  double radius = qMax( symbolDiagonal / 2, minDiameterToFitSymbols ) + circleAdditionPainterUnits;
517 
518  double fullPerimeter = 2 * M_PI;
519  double angleStep = fullPerimeter / nPosition;
520  for ( double currentAngle = 0.0; currentAngle < fullPerimeter; currentAngle += angleStep )
521  {
522  double sinusCurrentAngle = sin( currentAngle );
523  double cosinusCurrentAngle = cos( currentAngle );
524  QPointF positionShift( radius * sinusCurrentAngle, radius * cosinusCurrentAngle );
525  QPointF labelShift(( radius + symbolDiagonal / 2 ) * sinusCurrentAngle, ( radius + symbolDiagonal / 2 ) * cosinusCurrentAngle );
526  symbolPositions.append( centerPoint + positionShift );
527  labelShifts.append( labelShift );
528  }
529 
530  circleRadius = radius;
531  break;
532  }
533  case ConcentricRings:
534  {
535  double centerDiagonal = QgsSymbolLayerV2Utils::convertToPainterUnits( symbolContext.renderContext(),
536  M_SQRT2 * mCenterSymbol->size(),
537  mCenterSymbol->outputUnit(), mCenterSymbol->mapUnitScale() );
538 
539  int pointsRemaining = nPosition;
540  int ringNumber = 1;
541  double firstRingRadius = centerDiagonal / 2.0 + symbolDiagonal / 2.0;
542  while ( pointsRemaining > 0 )
543  {
544  double radiusCurrentRing = qMax( firstRingRadius + ( ringNumber - 1 ) * symbolDiagonal + ringNumber * circleAdditionPainterUnits, 0.0 );
545  int maxPointsCurrentRing = qMax( floor( 2 * M_PI * radiusCurrentRing / symbolDiagonal ), 1.0 );
546  int actualPointsCurrentRing = qMin( maxPointsCurrentRing, pointsRemaining );
547 
548  double angleStep = 2 * M_PI / actualPointsCurrentRing;
549  double currentAngle = 0.0;
550  for ( int i = 0; i < actualPointsCurrentRing; ++i )
551  {
552  double sinusCurrentAngle = sin( currentAngle );
553  double cosinusCurrentAngle = cos( currentAngle );
554  QPointF positionShift( radiusCurrentRing * sinusCurrentAngle, radiusCurrentRing * cosinusCurrentAngle );
555  QPointF labelShift(( radiusCurrentRing + symbolDiagonal / 2 ) * sinusCurrentAngle, ( radiusCurrentRing + symbolDiagonal / 2 ) * cosinusCurrentAngle );
556  symbolPositions.append( centerPoint + positionShift );
557  labelShifts.append( labelShift );
558  currentAngle += angleStep;
559  }
560 
561  pointsRemaining -= actualPointsCurrentRing;
562  ringNumber++;
563  circleRadius = radiusCurrentRing;
564  }
565  break;
566  }
567  }
568 }
569 
570 void QgsPointDisplacementRenderer::drawCircle( double radiusPainterUnits, QgsSymbolV2RenderContext& context, QPointF centerPoint, int nSymbols )
571 {
572  QPainter* p = context.renderContext().painter();
573  if ( nSymbols < 2 || !p ) //draw circle only if multiple features
574  {
575  return;
576  }
577 
578  //draw Circle
579  QPen circlePen( mCircleColor );
580  circlePen.setWidthF( context.outputLineWidth( mCircleWidth ) );
581  p->setPen( circlePen );
582  p->drawArc( QRectF( centerPoint.x() - radiusPainterUnits, centerPoint.y() - radiusPainterUnits, 2 * radiusPainterUnits, 2 * radiusPainterUnits ), 0, 5760 );
583 }
584 
585 void QgsPointDisplacementRenderer::drawSymbols( const QgsFeature& f, QgsRenderContext& context, const QList<QgsMarkerSymbolV2*>& symbolList, const QList<QPointF>& symbolPositions, bool selected )
586 {
587  QList<QPointF>::const_iterator symbolPosIt = symbolPositions.constBegin();
589  for ( ; symbolPosIt != symbolPositions.constEnd() && symbolIt != symbolList.constEnd(); ++symbolPosIt, ++symbolIt )
590  {
591  if ( *symbolIt )
592  {
593  ( *symbolIt )->renderPoint( *symbolPosIt, &f, context, -1, selected );
594  }
595  }
596 }
597 
598 void QgsPointDisplacementRenderer::drawLabels( QPointF centerPoint, QgsSymbolV2RenderContext& context, const QList<QPointF>& labelShifts, const QStringList& labelList )
599 {
600  QPainter* p = context.renderContext().painter();
601  if ( !p )
602  {
603  return;
604  }
605 
606  QPen labelPen( mLabelColor );
607  p->setPen( labelPen );
608 
609  //scale font (for printing)
610  QFont pixelSizeFont = mLabelFont;
611  pixelSizeFont.setPixelSize( context.outputLineWidth( mLabelFont.pointSizeF() * 0.3527 ) );
612  QFont scaledFont = pixelSizeFont;
613  scaledFont.setPixelSize( pixelSizeFont.pixelSize() * context.renderContext().rasterScaleFactor() );
614  p->setFont( scaledFont );
615 
616  QFontMetricsF fontMetrics( pixelSizeFont );
617  QPointF currentLabelShift; //considers the signs to determine the label position
618 
619  QList<QPointF>::const_iterator labelPosIt = labelShifts.constBegin();
620  QStringList::const_iterator text_it = labelList.constBegin();
621 
622  for ( ; labelPosIt != labelShifts.constEnd() && text_it != labelList.constEnd(); ++labelPosIt, ++text_it )
623  {
624  currentLabelShift = *labelPosIt;
625  if ( currentLabelShift.x() < 0 )
626  {
627  currentLabelShift.setX( currentLabelShift.x() - fontMetrics.width( *text_it ) );
628  }
629  if ( currentLabelShift.y() > 0 )
630  {
631  currentLabelShift.setY( currentLabelShift.y() + fontMetrics.ascent() );
632  }
633 
634  QPointF drawingPoint( centerPoint + currentLabelShift );
635  p->save();
636  p->translate( drawingPoint.x(), drawingPoint.y() );
637  p->scale( 1.0 / context.renderContext().rasterScaleFactor(), 1.0 / context.renderContext().rasterScaleFactor() );
638  p->drawText( QPointF( 0, 0 ), *text_it );
639  p->restore();
640  }
641 }
642 
643 QgsSymbolV2* QgsPointDisplacementRenderer::firstSymbolForFeature( QgsFeatureRendererV2* r, QgsFeature& f, QgsRenderContext &context )
644 {
645  if ( !r )
646  {
647  return nullptr;
648  }
649 
650  QgsSymbolV2List symbolList = r->symbolsForFeature( f, context );
651  if ( symbolList.size() < 1 )
652  {
653  return nullptr;
654  }
655 
656  return symbolList.at( 0 );
657 }
658 
660 {
661  if ( renderer->type() == "pointDisplacement" )
662  {
663  return dynamic_cast<QgsPointDisplacementRenderer*>( renderer->clone() );
664  }
665 
666  if ( renderer->type() == "singleSymbol" ||
667  renderer->type() == "categorizedSymbol" ||
668  renderer->type() == "graduatedSymbol" ||
669  renderer->type() == "RuleRenderer" )
670  {
672  pointRenderer->setEmbeddedRenderer( renderer->clone() );
673  return pointRenderer;
674  }
675  return nullptr;
676 }
QgsFeatureId id() const
Get the feature ID for this feature.
Definition: qgsfeature.cpp:65
static QString encodeOutputUnit(QgsSymbolV2::OutputUnit unit)
virtual bool addGeometry(QgsAbstractGeometryV2 *g) override
Adds a geometry and takes ownership.
void clear()
#define RENDERER_TAG_NAME
Definition: qgsrendererv2.h:49
void CORE_EXPORT save(QDomElement &elem) const
Serialize to XML.
A rectangle specified with double values.
Definition: qgsrectangle.h:35
QgsPointDisplacementRenderer * clone() const override
int pixelSize() const
QList< QgsFeatureId > intersects(const QgsRectangle &rect) const
Returns features that intersect the specified rectangle.
QgsFeatureRequest::OrderBy orderBy() const
Get the order in which features shall be processed by this renderer.
QList< QgsSymbolV2 * > QgsSymbolV2List
Definition: qgsrendererv2.h:40
QDomNode appendChild(const QDomNode &newChild)
void push_back(const T &value)
QString attribute(const QString &name, const QString &defValue) const
static QgsFeatureRendererV2 * create(QDomElement &symbologyElem)
create a renderer from XML element
static double mapUnitScaleFactor(const QgsRenderContext &c, QgsSymbolV2::OutputUnit u, const QgsMapUnitScale &scale=QgsMapUnitScale())
Returns scale factor painter units -> map units.
qreal pointSizeF() const
#define QgsDebugMsg(str)
Definition: qgslogger.h:33
virtual Q_DECL_DEPRECATED QgsSymbolV2 * originalSymbolForFeature(QgsFeature &feature)
Return symbol for feature.
void setLabelAttributeName(const QString &name)
static QString encodeColor(const QColor &color)
int fieldNameIndex(const QString &fieldName) const
Look up field&#39;s index from name also looks up case-insensitive if there is no match otherwise...
Definition: qgsfield.cpp:534
double size() const
Returns the size for the whole symbol, which is the maximum size of all marker symbol layers in the s...
double rendererScale() const
virtual QDomElement save(QDomDocument &doc)
store renderer info to XML element
void setCenterSymbol(QgsMarkerSymbolV2 *symbol)
Sets the center symbol (takes ownership)
void scale(qreal sx, qreal sy)
const_iterator constBegin() const
const T & at(int i) const
static bool isDefaultStack(QgsPaintEffect *effect)
Tests whether a paint effect matches the default effects stack.
QgsLegendSymbologyList legendSymbologyItems(QSize iconSize) override
return a list of symbology items for the legend
#define FID_TO_STRING(fid)
Definition: qgsfeature.h:89
static QDomElement saveSymbol(const QString &symbolName, QgsSymbolV2 *symbol, QDomDocument &doc)
void save()
QgsMapUnitScale mapUnitScale() const
Container of fields for a vector layer.
Definition: qgsfield.h:187
A geometry is the spatial representation of a feature.
Definition: qgsgeometry.h:76
static QgsPointDisplacementRenderer * convertFromRenderer(const QgsFeatureRendererV2 *renderer)
creates a QgsPointDisplacementRenderer from an existing renderer.
WkbType
Used for symbology operations.
Definition: qgis.h:57
QDomElement save(QDomDocument &doc) override
store renderer info to XML element
virtual QgsSymbolV2List symbolsForFeature(QgsFeature &feat, QgsRenderContext &context) override
Proxy that will call this method on the embedded renderer.
static QString encodeMapUnitScale(const QgsMapUnitScale &mapUnitScale)
virtual QList< QString > usedAttributes()=0
Returns a set of attributes required for this renderer.
Multi point geometry collection.
QgsPointDisplacementRenderer(const QString &labelAttributeName="")
const_iterator insert(const T &value)
The feature class encapsulates a single feature including its id, geometry and a list of field/values...
Definition: qgsfeature.h:187
void clear()
double toDouble(bool *ok) const
virtual void startRender(QgsRenderContext &context, const QgsFields &fields) override
Needs to be called when a new render cycle is started.
virtual Q_DECL_DEPRECATED bool willRenderFeature(QgsFeature &feat)
Returns whether the renderer will render a feature or not.
qreal width(const QString &text) const
QgsPaintEffect * mPaintEffect
double x() const
Get the x value of the point.
Definition: qgspoint.h:180
virtual QgsLegendSymbologyList legendSymbologyItems(QSize iconSize)
return a list of symbology items for the legend
int size() const
virtual void startRender(QgsRenderContext &context, const QgsFields &fields)=0
Needs to be called when a new render cycle is started.
void setToleranceUnit(QgsSymbolV2::OutputUnit unit)
Sets the units for the tolerance distance.
virtual void stopRender(QgsRenderContext &context)=0
Needs to be called when a render cycle has finished to clean up.
static QgsSymbolV2::OutputUnit decodeOutputUnit(const QString &str)
QString type() const
Definition: qgsrendererv2.h:83
virtual Q_DECL_DEPRECATED QgsSymbolV2List originalSymbolsForFeature(QgsFeature &feat)
Equivalent of originalSymbolsForFeature() call extended to support renderers that may use more symbol...
void drawRect(const QRectF &rectangle)
void setPixelSize(int pixelSize)
virtual QgsFeatureRendererV2 * clone() const =0
void setFont(const QFont &font)
The output shall be in millimeters.
Definition: qgssymbolv2.h:64
QString number(int n, int base)
virtual Q_DECL_DEPRECATED QgsSymbolV2 * symbolForFeature(QgsFeature &feature)
To be overridden.
qreal x() const
qreal y() const
void append(const T &value)
virtual QgsSymbolV2List originalSymbolsForFeature(QgsFeature &feat, QgsRenderContext &context) override
Proxy that will call this method on the embedded renderer.
bool empty() const
void startRender(QgsRenderContext &context, const QgsFields *fields=nullptr)
bool fromString(const QString &descrip)
void setEmbeddedRenderer(QgsFeatureRendererV2 *r)
Sets embedded renderer (takes ownership)
const Key & key() const
void drawArc(const QRectF &rectangle, int startAngle, int spanAngle)
static double convertToPainterUnits(const QgsRenderContext &c, double size, QgsSymbolV2::OutputUnit unit, const QgsMapUnitScale &scale=QgsMapUnitScale())
Converts a size from the specied units to painter units.
virtual QgsLegendSymbolList legendSymbolItems(double scaleDenominator=-1, const QString &rule="")
return a list of item text / symbol
QgsAttributes attributes() const
Returns the feature&#39;s attributes.
Definition: qgsfeature.cpp:110
void setPen(const QColor &color)
void setAttribute(const QString &name, const QString &value)
int toInt(bool *ok, int base) const
bool isEmpty() const
bool isEmpty() const
const_iterator constEnd() const
#define M_PI
virtual void toSld(QDomDocument &doc, QDomElement &element) const override
used from subclasses to create SLD Rule elements following SLD v1.1 specs
void setWidthF(qreal width)
virtual QgsSymbolV2 * originalSymbolForFeature(QgsFeature &feat, QgsRenderContext &context) override
Proxy that will call this method on the embedded renderer.
void drawText(const QPointF &position, const QString &text)
double rasterScaleFactor() const
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.
QGis::WkbType wkbType() const
Returns type of the geometry as a WKB type (point / linestring / polygon etc.)
void setPlacement(Placement placement)
Sets the placement method used for dispersing the points.
QgsGeometry * centroid() const
Returns the center of mass of a geometry.
static QgsFeatureRendererV2 * defaultRenderer(QGis::GeometryType geomType)
return a new renderer - used by default in vector layers
virtual Q_DECL_DEPRECATED QgsSymbolV2List symbols()
For symbol levels.
T & value() const
A class to represent a point.
Definition: qgspoint.h:117
iterator begin()
bool renderFeature(QgsFeature &feature, QgsRenderContext &context, int layer=-1, bool selected=false, bool drawVertexMarker=false) override
Reimplemented from QgsFeatureRendererV2.
virtual bool willRenderFeature(QgsFeature &feat, QgsRenderContext &context) override
Proxy that will call this method on the embedded renderer.
QList< QPair< QString, QPixmap > > QgsLegendSymbologyList
void stopRender(QgsRenderContext &context) override
Needs to be called when a render cycle has finished to clean up.
void copyRendererData(QgsFeatureRendererV2 *destRenderer) const
Clones generic renderer data to another renderer.
virtual void toSld(QDomDocument &doc, QDomElement &element) const
used from subclasses to create SLD Rule elements following SLD v1.1 specs
A renderer that automatically displaces points with the same position.
virtual QgsSymbolV2 * symbolForFeature(QgsFeature &feature, QgsRenderContext &context) override
Proxy that will call this method on the embedded renderer.
bool contains(const T &value) const
bool isNull() const
void restore()
const T & at(int i) const
virtual bool saveProperties(QDomDocument &doc, QDomElement &element) const
Saves the current state of the effect to a DOM element.
Contains information about the context of a rendering operation.
bool insertFeature(const QgsFeature &f)
Add feature to index.
QPainter * painter()
void stopRender(QgsRenderContext &context)
int wkbSize() const
Returns the size of the WKB in asWkb().
QgsLegendSymbolList legendSymbolItems(double scaleDenominator=-1, const QString &rule="") override
qreal ascent() const
virtual Q_DECL_DEPRECATED QgsSymbolV2List symbolsForFeature(QgsFeature &feat)
Returns list of symbols used for rendering the feature.
static QgsFeatureRendererV2 * load(QDomElement &symbologyElem)
create a renderer from XML element
QgsFeatureRequest::OrderBy mOrderBy
QgsRenderContext & renderContext()
Definition: qgssymbolv2.h:345
void setX(qreal x)
void setY(qreal y)
QDomElement firstChildElement(const QString &tagName) const
int count(const T &value) const
QgsSymbolV2::OutputUnit outputUnit() const
const QgsGeometry * constGeometry() const
Gets a const pointer to the geometry object associated with this feature.
Definition: qgsfeature.cpp:82
void renderPoint(QPointF point, const QgsFeature *f, QgsRenderContext &context, int layer=-1, bool selected=false)
void translate(const QPointF &offset)
qint64 QgsFeatureId
Definition: qgsfeature.h:31
double y() const
Get the y value of the point.
Definition: qgspoint.h:188
virtual int capabilities() override
Proxy that will call this method on the embedded renderer.
static QColor decodeColor(const QString &str)
iterator insert(const Key &key, const T &value)
void setToleranceMapUnitScale(const QgsMapUnitScale &scale)
Sets the map unit scale object for the distance tolerance.
static QDomElement toXmlElement(const QFont &font, QDomDocument &document, const QString &elementName)
Returns a DOM element containing the properties of the font.
double outputLineWidth(double width) const
static QgsMapUnitScale decodeMapUnitScale(const QString &str)
static QgsConstWkbPtr _getPoint(QPointF &pt, QgsRenderContext &context, QgsConstWkbPtr wkb)
const_iterator constEnd() const
virtual int capabilities()
returns bitwise OR-ed capabilities of the renderer
QDomElement createElement(const QString &tagName)
void clear()
const_iterator constBegin() const
QgsPoint asPoint() const
Return contents of the geometry as a point if wkbType is WKBPoint, otherwise returns [0...
A vector of attributes.
Definition: qgsfeature.h:115
virtual QList< QString > usedAttributes() override
Partial proxy that will call this method on the embedded renderer.
QList< QPair< QString, QgsSymbolV2 * > > QgsLegendSymbolList
Definition: qgsrendererv2.h:44
const unsigned char * asWkb() const
Returns the buffer containing this geometry in WKB format.
virtual QgsMarkerSymbolV2 * clone() const override
void setTolerance(double t)
Sets the tolerance distance for grouping points.