@@ -31,6 +31,21 @@ void QgsComposerRuler::paintEvent( QPaintEvent* event )
31
31
32
32
QTransform t = mTransform .inverted ();
33
33
34
+ // find optimal ruler display scale (steps of 1, 10 or 50)
35
+ double pixelDiff1 = mTransform .map ( QPointF ( 1 , 0 ) ).x () - mTransform .map ( QPointF ( 0 , 0 ) ).x ();
36
+ double pixelDiff10 = mTransform .map ( QPointF ( 10 , 0 ) ).x () - mTransform .map ( QPointF ( 0 , 0 ) ).x ();
37
+ // double pixelDiff50 = mTransform.map( QPointF( 50, 0 ) ).x() - mTransform.map( QPointF( 5, 0 ) ).x();
38
+
39
+ double mmDisplay = 50.0 ;
40
+ if ( pixelDiff1 > 25 )
41
+ {
42
+ mmDisplay = 1.0 ;
43
+ }
44
+ else if ( pixelDiff10 > 25 )
45
+ {
46
+ mmDisplay = 10.0 ;
47
+ }
48
+
34
49
if ( mDirection == Horizontal )
35
50
{
36
51
if ( doubleNear ( width (), 0 ) )
@@ -42,7 +57,7 @@ void QgsComposerRuler::paintEvent( QPaintEvent* event )
42
57
double startX = t.map ( QPointF ( 0 , 0 ) ).x ();
43
58
double endX = t.map ( QPointF ( width (), 0 ) ).x ();
44
59
45
- double markerPos = ( floor ( startX / 10.0 ) + 1 ) * 10.0 ; // marker position in mm
60
+ double markerPos = ( floor ( startX / mmDisplay ) + 1 ) * mmDisplay ; // marker position in mm
46
61
while ( markerPos <= endX )
47
62
{
48
63
if ( markerPos >= 0 && markerPos <= mComposition ->paperWidth () ) // todo: need to know paper size
@@ -51,7 +66,7 @@ void QgsComposerRuler::paintEvent( QPaintEvent* event )
51
66
p.drawLine ( pixelCoord, 0 , pixelCoord, RULER_MIN_SIZE );
52
67
p.drawText ( QPointF ( pixelCoord + 2 , RULER_MIN_SIZE / 2.0 ), QString::number (( int )( markerPos ) ) );
53
68
}
54
- markerPos += 10.0 ;
69
+ markerPos += mmDisplay ;
55
70
}
56
71
57
72
p.setPen ( QColor ( Qt::red ) );
@@ -66,8 +81,38 @@ void QgsComposerRuler::paintEvent( QPaintEvent* event )
66
81
67
82
double startY = t.map ( QPointF ( 0 , 0 ) ).y (); // start position in mm (total including space between pages)
68
83
double endY = t.map ( QPointF ( 0 , height () ) ).y (); // stop position in mm (total including space between pages)
84
+ int startPage = ( int )( startY / ( mComposition ->paperHeight () + mComposition ->spaceBetweenPages () ) );
85
+ if ( startPage < 0 )
86
+ {
87
+ startPage = 0 ;
88
+ }
89
+ int endPage = ( int )( endY / ( mComposition ->paperHeight () + mComposition ->spaceBetweenPages () ) );
90
+ if ( endPage > ( mComposition ->numPages () - 1 ) )
91
+ {
92
+ endPage = mComposition ->numPages () - 1 ;
93
+ }
94
+
95
+ for ( int i = startPage; i <= endPage; ++i )
96
+ {
97
+ double pageCoord = 0 ; // page coordinate in mm
98
+ // total (composition) coordinate in mm, including space between pages
99
+ double totalCoord = i * ( mComposition ->paperHeight () + mComposition ->spaceBetweenPages () );
100
+ while ( pageCoord < mComposition ->paperHeight () )
101
+ {
102
+ if ( totalCoord > endY )
103
+ {
104
+ break ;
105
+ }
106
+ double pixelCoord = mTransform .map ( QPointF ( 0 , totalCoord ) ).y ();
107
+ p.drawLine ( 0 , pixelCoord, RULER_MIN_SIZE, pixelCoord );
108
+ p.drawText ( QPointF ( 0 , pixelCoord - 2.0 ), QString::number ( pageCoord ) );
109
+ pageCoord += mmDisplay;
110
+ totalCoord += mmDisplay;
111
+ }
112
+ }
69
113
70
- double markerPos = ( floor ( startY / 10.0 ) + 1 ) * 10.0 ; // marker position in mm
114
+ #if 0
115
+ double markerPos = ( floor( startY / mmDisplay ) + 1 ) * mmDisplay; //marker position in mm
71
116
while ( markerPos <= endY )
72
117
{
73
118
int page = ( int )( markerPos / ( mComposition->paperHeight() + mComposition->spaceBetweenPages() ) );
@@ -79,14 +124,15 @@ void QgsComposerRuler::paintEvent( QPaintEvent* event )
79
124
80
125
if ( pageCoord < 0 || pageCoord > mComposition->paperHeight() ) //marker is in a page gap
81
126
{
82
- markerPos += 10.0 ;
127
+ markerPos += mmDisplay ;
83
128
continue;
84
129
}
85
130
double pixelCoord = mTransform.map( QPointF( 0, markerPos ) ).y();
86
131
p.drawLine( 0, pixelCoord, RULER_MIN_SIZE, pixelCoord );
87
132
p.drawText( QPointF( 0, pixelCoord - 2.0 ), QString::number( pageCoord ) );
88
- markerPos += 10.0 ;
133
+ markerPos += mmDisplay ;
89
134
}
135
+ #endif // 0
90
136
91
137
p.setPen ( QColor ( Qt::red ) );
92
138
p.drawLine ( 0 , mMarkerPos .y (), RULER_MIN_SIZE, mMarkerPos .y () );
0 commit comments