@@ -75,11 +75,17 @@ void QgsPointDisplacementRenderer::drawGroup( QPointF centerPoint, QgsRenderCont
75
75
QList<QPointF> symbolPositions;
76
76
QList<QPointF> labelPositions;
77
77
double circleRadius = -1.0 ;
78
- calculateSymbolAndLabelPositions ( symbolContext, centerPoint, group.size (), diagonal, symbolPositions, labelPositions, circleRadius );
78
+ double gridRadius;
79
+ int gridSize;
80
+
81
+ calculateSymbolAndLabelPositions ( symbolContext, centerPoint, group.size (), diagonal, symbolPositions, labelPositions, circleRadius, gridRadius, gridSize );
79
82
80
83
// draw circle
81
84
if ( circleRadius > 0 )
82
85
drawCircle ( circleRadius, symbolContext, centerPoint, group.size () );
86
+ // draw grid
87
+ else
88
+ drawGrid ( gridSize, symbolContext, symbolPositions, group.size () );
83
89
84
90
if ( group.size () > 1 )
85
91
{
@@ -221,7 +227,8 @@ void QgsPointDisplacementRenderer::setCenterSymbol( QgsMarkerSymbol *symbol )
221
227
}
222
228
223
229
void QgsPointDisplacementRenderer::calculateSymbolAndLabelPositions ( QgsSymbolRenderContext &symbolContext, QPointF centerPoint, int nPosition,
224
- double symbolDiagonal, QList<QPointF> &symbolPositions, QList<QPointF> &labelShifts, double &circleRadius ) const
230
+ double symbolDiagonal, QList<QPointF> &symbolPositions, QList<QPointF> &labelShifts, double &circleRadius, double &gridRadius,
231
+ int &gridSize ) const
225
232
{
226
233
symbolPositions.clear ();
227
234
labelShifts.clear ();
@@ -294,6 +301,75 @@ void QgsPointDisplacementRenderer::calculateSymbolAndLabelPositions( QgsSymbolRe
294
301
}
295
302
break ;
296
303
}
304
+ case Grid:
305
+ {
306
+ double centerDiagonal = symbolContext.renderContext ().convertToPainterUnits ( M_SQRT2 * mCenterSymbol ->size (),
307
+ mCenterSymbol ->sizeUnit (), mCenterSymbol ->sizeMapUnitScale () );
308
+ int pointsRemaining = nPosition;
309
+ gridSize = ceil ( sqrt ( pointsRemaining ) );
310
+ if ( pointsRemaining - pow ( gridSize - 1 , 2 ) < gridSize )
311
+ gridSize -= 1 ;
312
+ double originalPointRadius = ( ( centerDiagonal / 2.0 + symbolDiagonal / 2.0 ) + symbolDiagonal ) / 2 ;
313
+ double userPointRadius = originalPointRadius + circleAdditionPainterUnits;
314
+
315
+ int yIndex = 0 ;
316
+ while ( pointsRemaining > 0 )
317
+ {
318
+ for ( int xIndex = 0 ; xIndex < gridSize && pointsRemaining > 0 ; ++xIndex )
319
+ {
320
+ QPointF positionShift ( userPointRadius * xIndex, userPointRadius * yIndex );
321
+ QPointF labelShift ( ( userPointRadius + symbolDiagonal / 2 ) * xIndex, ( userPointRadius + symbolDiagonal / 2 ) * yIndex );
322
+ symbolPositions.append ( centerPoint + positionShift );
323
+ labelShifts.append ( labelShift );
324
+ pointsRemaining--;
325
+ }
326
+ yIndex++;
327
+ }
328
+
329
+ centralizeGrid ( symbolPositions, userPointRadius, gridSize );
330
+ centralizeGrid ( labelShifts, userPointRadius, gridSize );
331
+ gridRadius = userPointRadius;
332
+ break ;
333
+ }
334
+ }
335
+ }
336
+
337
+ void QgsPointDisplacementRenderer::centralizeGrid ( QList<QPointF> &pointSymbolPositions, double radius, int size ) const
338
+ {
339
+ double shiftAmount = -radius * ( size - 1 ) / 2 ;
340
+ QPointF centralShift ( shiftAmount, shiftAmount );
341
+ for ( int i = 0 ; i < pointSymbolPositions.size (); ++i )
342
+ {
343
+ pointSymbolPositions[i] += centralShift;
344
+ }
345
+ }
346
+
347
+ void QgsPointDisplacementRenderer::drawGrid ( int gridSizeUnits, QgsSymbolRenderContext &context,
348
+ QList<QPointF> pointSymbolPositions, int nSymbols )
349
+ {
350
+ QPainter *p = context.renderContext ().painter ();
351
+ if ( nSymbols < 2 || !p ) // draw grid only if multiple features
352
+ {
353
+ return ;
354
+ }
355
+
356
+ QPen gridPen ( mCircleColor );
357
+ gridPen.setWidthF ( context.outputLineWidth ( mCircleWidth ) );
358
+ p->setPen ( gridPen );
359
+
360
+ for ( int i = 0 ; i < pointSymbolPositions.size (); ++i )
361
+ {
362
+ if ( i + 1 < pointSymbolPositions.size () && 0 != ( i + 1 ) % gridSizeUnits )
363
+ {
364
+ QLineF gridLineRow ( pointSymbolPositions[i], pointSymbolPositions[i + 1 ] );
365
+ p->drawLine ( gridLineRow );
366
+ }
367
+
368
+ if ( i + gridSizeUnits < pointSymbolPositions.size () )
369
+ {
370
+ QLineF gridLineColumn ( pointSymbolPositions[i], pointSymbolPositions[i + gridSizeUnits] );
371
+ p->drawLine ( gridLineColumn );
372
+ }
297
373
}
298
374
}
299
375
0 commit comments