Skip to content

Commit

Permalink
[composer] Fix placement of coordinates in map corners (fix #8827)
Browse files Browse the repository at this point in the history
  • Loading branch information
nyalldawson committed Oct 19, 2014
1 parent 422ea56 commit fadf182
Show file tree
Hide file tree
Showing 2 changed files with 89 additions and 25 deletions.
88 changes: 70 additions & 18 deletions src/core/composer/qgscomposermapgrid.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -979,26 +979,26 @@ void QgsComposerMapGrid::drawCoordinateAnnotations( QPainter* p, const QList< QP
for ( ; it != hLines.constEnd(); ++it )
{
currentAnnotationString = gridAnnotationString( it->first, QgsComposerMapGrid::Latitude );
drawCoordinateAnnotation( p, it->second.p1(), currentAnnotationString );
drawCoordinateAnnotation( p, it->second.p2(), currentAnnotationString );
drawCoordinateAnnotation( p, it->second.p1(), currentAnnotationString, QgsComposerMapGrid::Latitude );
drawCoordinateAnnotation( p, it->second.p2(), currentAnnotationString, QgsComposerMapGrid::Latitude );
}

it = vLines.constBegin();
for ( ; it != vLines.constEnd(); ++it )
{
currentAnnotationString = gridAnnotationString( it->first, QgsComposerMapGrid::Longitude );
drawCoordinateAnnotation( p, it->second.p1(), currentAnnotationString );
drawCoordinateAnnotation( p, it->second.p2(), currentAnnotationString );
drawCoordinateAnnotation( p, it->second.p1(), currentAnnotationString, QgsComposerMapGrid::Longitude );
drawCoordinateAnnotation( p, it->second.p2(), currentAnnotationString, QgsComposerMapGrid::Longitude );
}
}

void QgsComposerMapGrid::drawCoordinateAnnotation( QPainter* p, const QPointF& pos, QString annotationString ) const
void QgsComposerMapGrid::drawCoordinateAnnotation( QPainter* p, const QPointF& pos, QString annotationString, const AnnotationCoordinate coordinateType ) const
{
if ( !mComposerMap )
{
return;
}
QgsComposerMapGrid::BorderSide frameBorder = borderForLineCoord( pos );
QgsComposerMapGrid::BorderSide frameBorder = borderForLineCoord( pos, coordinateType );
double textWidth = QgsComposerUtils::textWidthMM( mGridAnnotationFont, annotationString );
//relevant for annotations is the height of digits
double textHeight = QgsComposerUtils::fontHeightCharacterMM( mGridAnnotationFont, QChar( '0' ) );
Expand Down Expand Up @@ -1610,51 +1610,103 @@ int QgsComposerMapGrid::yGridLinesCRSTransform( const QgsRectangle& bbox, const
void QgsComposerMapGrid::sortGridLinesOnBorders( const QList< QPair< double, QLineF > >& hLines, const QList< QPair< double, QLineF > >& vLines, QMap< double, double >& leftFrameEntries,
QMap< double, double >& rightFrameEntries, QMap< double, double >& topFrameEntries, QMap< double, double >& bottomFrameEntries ) const
{
QList< QPair< double, QPointF > > borderPositions;
QList< QgsMapAnnotation > borderPositions;
QList< QPair< double, QLineF > >::const_iterator it = hLines.constBegin();
for ( ; it != hLines.constEnd(); ++it )
{
borderPositions << qMakePair( it->first, it->second.p1() );
borderPositions << qMakePair( it->first, it->second.p2() );
QgsMapAnnotation p1;
p1.coordinate = it->first;
p1.itemPosition = it->second.p1();
p1.coordinateType = QgsComposerMapGrid::Latitude;
borderPositions << p1;

QgsMapAnnotation p2;
p2.coordinate = it->first;
p2.itemPosition = it->second.p2();
p2.coordinateType = QgsComposerMapGrid::Latitude;
borderPositions << p2;
}
it = vLines.constBegin();
for ( ; it != vLines.constEnd(); ++it )
{
borderPositions << qMakePair( it->first, it->second.p1() );
borderPositions << qMakePair( it->first, it->second.p2() );
QgsMapAnnotation p1;
p1.coordinate = it->first;
p1.itemPosition = it->second.p1();
p1.coordinateType = QgsComposerMapGrid::Longitude;
borderPositions << p1;

QgsMapAnnotation p2;
p2.coordinate = it->first;
p2.itemPosition = it->second.p2();
p2.coordinateType = QgsComposerMapGrid::Longitude;
borderPositions << p2;
}

QList< QPair< double, QPointF > >::const_iterator bIt = borderPositions.constBegin();
QList< QgsMapAnnotation >::const_iterator bIt = borderPositions.constBegin();
for ( ; bIt != borderPositions.constEnd(); ++bIt )
{
QgsComposerMapGrid::BorderSide frameBorder = borderForLineCoord( bIt->second );
QgsComposerMapGrid::BorderSide frameBorder = borderForLineCoord( bIt->itemPosition, bIt->coordinateType );
if ( frameBorder == QgsComposerMapGrid::Left )
{
leftFrameEntries.insert( bIt->second.y(), bIt->first );
leftFrameEntries.insert( bIt->itemPosition.y(), bIt->coordinate );
}
else if ( frameBorder == QgsComposerMapGrid::Right )
{
rightFrameEntries.insert( bIt->second.y(), bIt->first );
rightFrameEntries.insert( bIt->itemPosition.y(), bIt->coordinate );
}
else if ( frameBorder == QgsComposerMapGrid::Top )
{
topFrameEntries.insert( bIt->second.x(), bIt->first );
topFrameEntries.insert( bIt->itemPosition.x(), bIt->coordinate );
}
else //Bottom
{
bottomFrameEntries.insert( bIt->second.x(), bIt->first );
bottomFrameEntries.insert( bIt->itemPosition.x(), bIt->coordinate );
}
}
}

QgsComposerMapGrid::BorderSide QgsComposerMapGrid::borderForLineCoord( const QPointF& p ) const
QgsComposerMapGrid::BorderSide QgsComposerMapGrid::borderForLineCoord( const QPointF& p , const AnnotationCoordinate coordinateType ) const
{
if ( !mComposerMap )
{
return QgsComposerMapGrid::Left;
}

double framePenWidth = mComposerMap->hasFrame() ? mComposerMap->pen().widthF() : 0.000000001;

//check for corner coordinates
if (( p.y() <= framePenWidth && p.x() <= framePenWidth ) // top left
|| ( p.y() <= framePenWidth && p.x() >= ( mComposerMap->rect().width() - framePenWidth ) ) //top right
|| ( p.y() >= ( mComposerMap->rect().height() - framePenWidth ) && p.x() <= framePenWidth ) //bottom left
|| ( p.y() >= ( mComposerMap->rect().height() - framePenWidth ) && p.x() >= ( mComposerMap->rect().width() - framePenWidth ) ) //bottom right
)
{
//coordinate is in corner - fall back to preferred side for coordinate type
if ( coordinateType == QgsComposerMapGrid::Latitude )
{
if ( p.x() <= framePenWidth )
{
return QgsComposerMapGrid::Left;
}
else
{
return QgsComposerMapGrid::Right;
}
}
else
{
if ( p.y() <= framePenWidth )
{
return QgsComposerMapGrid::Top;
}
else
{
return QgsComposerMapGrid::Bottom;
}
}
}

//otherwise, guess side based on point
if ( p.y() <= framePenWidth )
{
return QgsComposerMapGrid::Top;
Expand Down
26 changes: 19 additions & 7 deletions src/core/composer/qgscomposermapgrid.h
100755 → 100644
Original file line number Diff line number Diff line change
Expand Up @@ -808,6 +808,14 @@ class CORE_EXPORT QgsComposerMapGrid : public QgsComposerMapItem
QRectF mPrevPaintRect;
QPolygonF mPrevMapPolygon;

class QgsMapAnnotation
{
public:
double coordinate;
QPointF itemPosition;
QgsComposerMapGrid::AnnotationCoordinate coordinateType;
};

/**Draws the map grid*/
void drawGridFrame( QPainter* p, const QList< QPair< double, QLineF > >& hLines, const QList< QPair< double, QLineF > >& vLines ) const;

Expand All @@ -817,13 +825,14 @@ class CORE_EXPORT QgsComposerMapGrid : public QgsComposerMapItem
@param vLines vertical coordinate lines in item coordinates*/
void drawCoordinateAnnotations( QPainter* p, const QList< QPair< double, QLineF > >& hLines, const QList< QPair< double, QLineF > >& vLines ) const;

void drawCoordinateAnnotation( QPainter* p, const QPointF& pos, QString annotationString ) const;
void drawCoordinateAnnotation( QPainter* p, const QPointF& pos, QString annotationString, const AnnotationCoordinate coordinateType ) const;

/**Draws a single annotation
@param p drawing painter
@param pos item coordinates where to draw
@param rotation text rotation
@param annotationText the text to draw*/
* @param p drawing painter
* @param pos item coordinates where to draw
* @param rotation text rotation
* @param annotationText the text to draw
*/
void drawAnnotation( QPainter* p, const QPointF& pos, int rotation, const QString& annotationText ) const;

QString gridAnnotationString( double value, AnnotationCoordinate coord ) const;
Expand All @@ -849,8 +858,11 @@ class CORE_EXPORT QgsComposerMapGrid : public QgsComposerMapItem

void drawGridFrameBorder( QPainter* p, const QMap< double, double >& borderPos, BorderSide border ) const;

/**Returns the item border of a point (in item coordinates)*/
BorderSide borderForLineCoord( const QPointF& p ) const;
/**Returns the item border of a point (in item coordinates)
* @param p point
* @param coordinateType coordinate type
*/
BorderSide borderForLineCoord( const QPointF& p, const AnnotationCoordinate coordinateType ) const;

/**Get parameters for drawing grid in CRS different to map CRS*/
int crsGridParams( QgsRectangle& crsRect, QgsCoordinateTransform& inverseTransform ) const;
Expand Down

0 comments on commit fadf182

Please sign in to comment.