Skip to content

Commit

Permalink
[composer] Fix incorrect calculation of scale for tick scale bars
Browse files Browse the repository at this point in the history
QPainter::drawLine(x1,y1,x2,y2) takes int values, so coordinates
were being rounded to the nearest mm. Consequently scale was way
off. Also fixes horizontal line for tick styles being drawn
incorrectly with multiple overlapping segments. (fix #10685)
  • Loading branch information
nyalldawson committed Oct 27, 2014
1 parent 04ed47e commit d2c9ffd
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 16 deletions.
5 changes: 3 additions & 2 deletions src/core/composer/qgscomposerscalebar.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -434,10 +434,11 @@ void QgsComposerScaleBar::segmentPositions( QList<QPair<double, double> >& posWi
double mCurrentXCoord = mPen.widthF() + mBoxContentSpace;

//left segments
double leftSegmentSize = mSegmentMillimeters / mNumSegmentsLeft;
for ( int i = 0; i < mNumSegmentsLeft; ++i )
{
posWidthList.push_back( qMakePair( mCurrentXCoord, mSegmentMillimeters / mNumSegmentsLeft ) );
mCurrentXCoord += mSegmentMillimeters / mNumSegmentsLeft;
posWidthList.push_back( qMakePair( mCurrentXCoord, leftSegmentSize ) );
mCurrentXCoord += leftSegmentSize;
}

//right segments
Expand Down
2 changes: 0 additions & 2 deletions src/core/composer/qgsdoubleboxscalebarstyle.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,6 @@ void QgsDoubleBoxScaleBarStyle::draw( QPainter* p, double xOffset ) const

bool useColor = true; //alternate brush color/white



QList<QPair<double, double> >::const_iterator segmentIt = segmentInfo.constBegin();
for ( ; segmentIt != segmentInfo.constEnd(); ++segmentIt )
{
Expand Down
1 change: 0 additions & 1 deletion src/core/composer/qgssingleboxscalebarstyle.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,6 @@ void QgsSingleBoxScaleBarStyle::draw( QPainter* p, double xOffset ) const
p->setRenderHint( QPainter::Antialiasing, true );
p->setPen( mScaleBar->pen() );


QList<QPair<double, double> > segmentInfo;
mScaleBar->segmentPositions( segmentInfo );

Expand Down
26 changes: 15 additions & 11 deletions src/core/composer/qgsticksscalebarstyle.cpp
100755 → 100644
Original file line number Diff line number Diff line change
Expand Up @@ -69,26 +69,30 @@ void QgsTicksScaleBarStyle::draw( QPainter* p, double xOffset ) const
QList<QPair<double, double> >::const_iterator segmentIt = segmentInfo.constBegin();
for ( ; segmentIt != segmentInfo.constEnd(); ++segmentIt )
{
p->drawLine( segmentIt->first + xOffset, barTopPosition, segmentIt->first + xOffset, barTopPosition + mScaleBar->height() );
p->drawLine( QLineF( segmentIt->first + xOffset, barTopPosition, segmentIt->first + xOffset, barTopPosition + mScaleBar->height() ) );
}

//draw last tick and horizontal line
if ( !segmentInfo.isEmpty() )
{
double lastTickPositionX = segmentInfo.last().first + mScaleBar->segmentMillimeters() + xOffset;
double verticalPos;
switch ( mTickPosition )
{
case TicksDown:
p->drawLine( xOffset + segmentIt->first, barTopPosition, xOffset + segmentIt->first + mScaleBar->segmentMillimeters(), barTopPosition );
verticalPos = barTopPosition;
break;
case TicksMiddle:
p->drawLine( xOffset + segmentIt->first, middlePosition, xOffset + segmentIt->first + mScaleBar->segmentMillimeters(), middlePosition );
verticalPos = middlePosition;
break;
case TicksUp:
p->drawLine( xOffset + segmentIt->first, bottomPosition, xOffset + segmentIt->first + mScaleBar->segmentMillimeters(), bottomPosition );
verticalPos = bottomPosition;
break;
}
}

//draw last tick
if ( !segmentInfo.isEmpty() )
{
double lastTickPositionX = segmentInfo.last().first + mScaleBar->segmentMillimeters();
p->drawLine( lastTickPositionX + xOffset, barTopPosition, lastTickPositionX + xOffset, barTopPosition + mScaleBar->height() );
//horizontal line
p->drawLine( QLineF( xOffset + segmentInfo.at( 0 ).first, verticalPos, lastTickPositionX, verticalPos ) );
//last vertical line
p->drawLine( QLineF( lastTickPositionX, barTopPosition, lastTickPositionX, barTopPosition + mScaleBar->height() ) );
}

p->restore();
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit d2c9ffd

Please sign in to comment.