Skip to content

Commit 31a9021

Browse files
committed
Improve coordinate display
1 parent b758329 commit 31a9021

File tree

1 file changed

+51
-5
lines changed

1 file changed

+51
-5
lines changed

src/gui/qgscomposerruler.cpp

+51-5
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,21 @@ void QgsComposerRuler::paintEvent( QPaintEvent* event )
3131

3232
QTransform t = mTransform.inverted();
3333

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+
3449
if ( mDirection == Horizontal )
3550
{
3651
if ( doubleNear( width(), 0 ) )
@@ -42,7 +57,7 @@ void QgsComposerRuler::paintEvent( QPaintEvent* event )
4257
double startX = t.map( QPointF( 0, 0 ) ).x();
4358
double endX = t.map( QPointF( width(), 0 ) ).x();
4459

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
4661
while ( markerPos <= endX )
4762
{
4863
if ( markerPos >= 0 && markerPos <= mComposition->paperWidth() ) //todo: need to know paper size
@@ -51,7 +66,7 @@ void QgsComposerRuler::paintEvent( QPaintEvent* event )
5166
p.drawLine( pixelCoord, 0, pixelCoord, RULER_MIN_SIZE );
5267
p.drawText( QPointF( pixelCoord + 2, RULER_MIN_SIZE / 2.0 ), QString::number(( int )( markerPos ) ) );
5368
}
54-
markerPos += 10.0;
69+
markerPos += mmDisplay;
5570
}
5671

5772
p.setPen( QColor( Qt::red ) );
@@ -66,8 +81,38 @@ void QgsComposerRuler::paintEvent( QPaintEvent* event )
6681

6782
double startY = t.map( QPointF( 0, 0 ) ).y(); //start position in mm (total including space between pages)
6883
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+
}
69113

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
71116
while ( markerPos <= endY )
72117
{
73118
int page = ( int )( markerPos / ( mComposition->paperHeight() + mComposition->spaceBetweenPages() ) );
@@ -79,14 +124,15 @@ void QgsComposerRuler::paintEvent( QPaintEvent* event )
79124

80125
if ( pageCoord < 0 || pageCoord > mComposition->paperHeight() ) //marker is in a page gap
81126
{
82-
markerPos += 10.0;
127+
markerPos += mmDisplay;
83128
continue;
84129
}
85130
double pixelCoord = mTransform.map( QPointF( 0, markerPos ) ).y();
86131
p.drawLine( 0, pixelCoord, RULER_MIN_SIZE, pixelCoord );
87132
p.drawText( QPointF( 0, pixelCoord - 2.0 ), QString::number( pageCoord ) );
88-
markerPos += 10.0;
133+
markerPos += mmDisplay;
89134
}
135+
#endif //0
90136

91137
p.setPen( QColor( Qt::red ) );
92138
p.drawLine( 0, mMarkerPos.y(), RULER_MIN_SIZE, mMarkerPos.y() );

0 commit comments

Comments
 (0)