28
28
#include < QPainter>
29
29
#include < QPen>
30
30
31
+ #define MAX_GRID_LINES 1000 // maximum number of horizontal or vertical grid lines to draw
32
+
31
33
QgsComposerMapGrid::QgsComposerMapGrid ( const QString& name, QgsComposerMap* map ):
32
34
mComposerMap( map ),
33
35
mName( name ),
@@ -370,13 +372,13 @@ void QgsComposerMapGrid::drawGrid( QPainter* p ) const
370
372
void QgsComposerMapGrid::drawGridNoTransform ( QgsRenderContext &context, double dotsPerMM, QList< QPair< double , QLineF > > &horizontalLines,
371
373
QList< QPair< double , QLineF > > &verticalLines ) const
372
374
{
373
- // get line positions
375
+ // get line positions
374
376
yGridLines ( verticalLines );
375
377
QList< QPair< double , QLineF > >::const_iterator vIt = verticalLines.constBegin ();
376
378
xGridLines ( horizontalLines );
377
379
QList< QPair< double , QLineF > >::const_iterator hIt = horizontalLines.constBegin ();
378
380
379
- // simple approach: draw vertical lines first, then horizontal ones
381
+ // simple approach: draw vertical lines first, then horizontal ones
380
382
if ( mGridStyle == QgsComposerMap::Solid )
381
383
{
382
384
// we need to scale line coordinates to dots, rather than mm, since the painter has already been scaled to dots
@@ -815,16 +817,18 @@ int QgsComposerMapGrid::xGridLines( QList< QPair< double, QLineF > >& lines ) co
815
817
double roundCorrection = mapBoundingRect.top () > 0 ? 1.0 : 0.0 ;
816
818
double currentLevel = ( int )(( mapBoundingRect.top () - gridOffsetY ) / gridIntervalY + roundCorrection ) * gridIntervalY + gridOffsetY;
817
819
820
+ int gridLineCount = 0 ;
818
821
if ( qgsDoubleNear ( mComposerMap ->mapRotation (), 0.0 ) || mGridUnit != MapUnit )
819
822
{
820
823
// no rotation. Do it 'the easy way'
821
824
822
825
double yCanvasCoord;
823
- while ( currentLevel <= mapBoundingRect.bottom () )
826
+ while ( currentLevel <= mapBoundingRect.bottom () && gridLineCount < MAX_GRID_LINES )
824
827
{
825
828
yCanvasCoord = mComposerMap ->rect ().height () * ( 1 - ( currentLevel - mapBoundingRect.top () ) / mapBoundingRect.height () );
826
829
lines.push_back ( qMakePair ( currentLevel * annotationScale, QLineF ( 0 , yCanvasCoord, mComposerMap ->rect ().width (), yCanvasCoord ) ) );
827
830
currentLevel += gridIntervalY;
831
+ gridLineCount++;
828
832
}
829
833
return 0 ;
830
834
}
@@ -838,7 +842,7 @@ int QgsComposerMapGrid::xGridLines( QList< QPair< double, QLineF > >& lines ) co
838
842
839
843
QList<QPointF> intersectionList; // intersects between border lines and grid lines
840
844
841
- while ( currentLevel <= mapBoundingRect.bottom () )
845
+ while ( currentLevel <= mapBoundingRect.bottom () && gridLineCount < MAX_GRID_LINES )
842
846
{
843
847
intersectionList.clear ();
844
848
QLineF gridLine ( mapBoundingRect.left (), currentLevel, mapBoundingRect.right (), currentLevel );
@@ -860,6 +864,7 @@ int QgsComposerMapGrid::xGridLines( QList< QPair< double, QLineF > >& lines ) co
860
864
if ( intersectionList.size () >= 2 )
861
865
{
862
866
lines.push_back ( qMakePair ( currentLevel, QLineF ( mComposerMap ->mapToItemCoords ( intersectionList.at ( 0 ) ), mComposerMap ->mapToItemCoords ( intersectionList.at ( 1 ) ) ) ) );
867
+ gridLineCount++;
863
868
}
864
869
currentLevel += gridIntervalY;
865
870
}
@@ -896,16 +901,17 @@ int QgsComposerMapGrid::yGridLines( QList< QPair< double, QLineF > >& lines ) co
896
901
double roundCorrection = mapBoundingRect.left () > 0 ? 1.0 : 0.0 ;
897
902
double currentLevel = ( int )(( mapBoundingRect.left () - gridOffsetX ) / gridIntervalX + roundCorrection ) * gridIntervalX + gridOffsetX;
898
903
904
+ int gridLineCount = 0 ;
899
905
if ( qgsDoubleNear ( mComposerMap ->mapRotation (), 0.0 ) || mGridUnit != MapUnit )
900
906
{
901
907
// no rotation. Do it 'the easy way'
902
908
double xCanvasCoord;
903
-
904
- while ( currentLevel <= mapBoundingRect.right () )
909
+ while ( currentLevel <= mapBoundingRect.right () && gridLineCount < MAX_GRID_LINES )
905
910
{
906
911
xCanvasCoord = mComposerMap ->rect ().width () * ( currentLevel - mapBoundingRect.left () ) / mapBoundingRect.width ();
907
912
lines.push_back ( qMakePair ( currentLevel * annotationScale, QLineF ( xCanvasCoord, 0 , xCanvasCoord, mComposerMap ->rect ().height () ) ) );
908
913
currentLevel += gridIntervalX;
914
+ gridLineCount++;
909
915
}
910
916
return 0 ;
911
917
}
@@ -919,7 +925,7 @@ int QgsComposerMapGrid::yGridLines( QList< QPair< double, QLineF > >& lines ) co
919
925
920
926
QList<QPointF> intersectionList; // intersects between border lines and grid lines
921
927
922
- while ( currentLevel <= mapBoundingRect.right () )
928
+ while ( currentLevel <= mapBoundingRect.right () && gridLineCount < MAX_GRID_LINES )
923
929
{
924
930
intersectionList.clear ();
925
931
QLineF gridLine ( currentLevel, mapBoundingRect.bottom (), currentLevel, mapBoundingRect.top () );
@@ -941,6 +947,7 @@ int QgsComposerMapGrid::yGridLines( QList< QPair< double, QLineF > >& lines ) co
941
947
if ( intersectionList.size () >= 2 )
942
948
{
943
949
lines.push_back ( qMakePair ( currentLevel, QLineF ( mComposerMap ->mapToItemCoords ( intersectionList.at ( 0 ) ), mComposerMap ->mapToItemCoords ( intersectionList.at ( 1 ) ) ) ) );
950
+ gridLineCount++;
944
951
}
945
952
currentLevel += gridIntervalX;
946
953
}
0 commit comments