@@ -979,26 +979,26 @@ void QgsComposerMapGrid::drawCoordinateAnnotations( QPainter* p, const QList< QP
979979 for ( ; it != hLines.constEnd (); ++it )
980980 {
981981 currentAnnotationString = gridAnnotationString ( it->first , QgsComposerMapGrid::Latitude );
982- drawCoordinateAnnotation ( p, it->second .p1 (), currentAnnotationString );
983- drawCoordinateAnnotation ( p, it->second .p2 (), currentAnnotationString );
982+ drawCoordinateAnnotation ( p, it->second .p1 (), currentAnnotationString, QgsComposerMapGrid::Latitude );
983+ drawCoordinateAnnotation ( p, it->second .p2 (), currentAnnotationString, QgsComposerMapGrid::Latitude );
984984 }
985985
986986 it = vLines.constBegin ();
987987 for ( ; it != vLines.constEnd (); ++it )
988988 {
989989 currentAnnotationString = gridAnnotationString ( it->first , QgsComposerMapGrid::Longitude );
990- drawCoordinateAnnotation ( p, it->second .p1 (), currentAnnotationString );
991- drawCoordinateAnnotation ( p, it->second .p2 (), currentAnnotationString );
990+ drawCoordinateAnnotation ( p, it->second .p1 (), currentAnnotationString, QgsComposerMapGrid::Longitude );
991+ drawCoordinateAnnotation ( p, it->second .p2 (), currentAnnotationString, QgsComposerMapGrid::Longitude );
992992 }
993993}
994994
995- void QgsComposerMapGrid::drawCoordinateAnnotation ( QPainter* p, const QPointF& pos, QString annotationString ) const
995+ void QgsComposerMapGrid::drawCoordinateAnnotation ( QPainter* p, const QPointF& pos, QString annotationString, const AnnotationCoordinate coordinateType ) const
996996{
997997 if ( !mComposerMap )
998998 {
999999 return ;
10001000 }
1001- QgsComposerMapGrid::BorderSide frameBorder = borderForLineCoord ( pos );
1001+ QgsComposerMapGrid::BorderSide frameBorder = borderForLineCoord ( pos, coordinateType );
10021002 double textWidth = QgsComposerUtils::textWidthMM ( mGridAnnotationFont , annotationString );
10031003 // relevant for annotations is the height of digits
10041004 double textHeight = QgsComposerUtils::fontHeightCharacterMM ( mGridAnnotationFont , QChar ( ' 0' ) );
@@ -1610,51 +1610,103 @@ int QgsComposerMapGrid::yGridLinesCRSTransform( const QgsRectangle& bbox, const
16101610void QgsComposerMapGrid::sortGridLinesOnBorders ( const QList< QPair< double , QLineF > >& hLines, const QList< QPair< double , QLineF > >& vLines, QMap< double , double >& leftFrameEntries,
16111611 QMap< double , double >& rightFrameEntries, QMap< double , double >& topFrameEntries, QMap< double , double >& bottomFrameEntries ) const
16121612{
1613- QList< QPair< double , QPointF > > borderPositions;
1613+ QList< QgsMapAnnotation > borderPositions;
16141614 QList< QPair< double , QLineF > >::const_iterator it = hLines.constBegin ();
16151615 for ( ; it != hLines.constEnd (); ++it )
16161616 {
1617- borderPositions << qMakePair ( it->first , it->second .p1 () );
1618- borderPositions << qMakePair ( it->first , it->second .p2 () );
1617+ QgsMapAnnotation p1;
1618+ p1.coordinate = it->first ;
1619+ p1.itemPosition = it->second .p1 ();
1620+ p1.coordinateType = QgsComposerMapGrid::Latitude;
1621+ borderPositions << p1;
1622+
1623+ QgsMapAnnotation p2;
1624+ p2.coordinate = it->first ;
1625+ p2.itemPosition = it->second .p2 ();
1626+ p2.coordinateType = QgsComposerMapGrid::Latitude;
1627+ borderPositions << p2;
16191628 }
16201629 it = vLines.constBegin ();
16211630 for ( ; it != vLines.constEnd (); ++it )
16221631 {
1623- borderPositions << qMakePair ( it->first , it->second .p1 () );
1624- borderPositions << qMakePair ( it->first , it->second .p2 () );
1632+ QgsMapAnnotation p1;
1633+ p1.coordinate = it->first ;
1634+ p1.itemPosition = it->second .p1 ();
1635+ p1.coordinateType = QgsComposerMapGrid::Longitude;
1636+ borderPositions << p1;
1637+
1638+ QgsMapAnnotation p2;
1639+ p2.coordinate = it->first ;
1640+ p2.itemPosition = it->second .p2 ();
1641+ p2.coordinateType = QgsComposerMapGrid::Longitude;
1642+ borderPositions << p2;
16251643 }
16261644
1627- QList< QPair< double , QPointF > >::const_iterator bIt = borderPositions.constBegin ();
1645+ QList< QgsMapAnnotation >::const_iterator bIt = borderPositions.constBegin ();
16281646 for ( ; bIt != borderPositions.constEnd (); ++bIt )
16291647 {
1630- QgsComposerMapGrid::BorderSide frameBorder = borderForLineCoord ( bIt->second );
1648+ QgsComposerMapGrid::BorderSide frameBorder = borderForLineCoord ( bIt->itemPosition , bIt-> coordinateType );
16311649 if ( frameBorder == QgsComposerMapGrid::Left )
16321650 {
1633- leftFrameEntries.insert ( bIt->second .y (), bIt->first );
1651+ leftFrameEntries.insert ( bIt->itemPosition .y (), bIt->coordinate );
16341652 }
16351653 else if ( frameBorder == QgsComposerMapGrid::Right )
16361654 {
1637- rightFrameEntries.insert ( bIt->second .y (), bIt->first );
1655+ rightFrameEntries.insert ( bIt->itemPosition .y (), bIt->coordinate );
16381656 }
16391657 else if ( frameBorder == QgsComposerMapGrid::Top )
16401658 {
1641- topFrameEntries.insert ( bIt->second .x (), bIt->first );
1659+ topFrameEntries.insert ( bIt->itemPosition .x (), bIt->coordinate );
16421660 }
16431661 else // Bottom
16441662 {
1645- bottomFrameEntries.insert ( bIt->second .x (), bIt->first );
1663+ bottomFrameEntries.insert ( bIt->itemPosition .x (), bIt->coordinate );
16461664 }
16471665 }
16481666}
16491667
1650- QgsComposerMapGrid::BorderSide QgsComposerMapGrid::borderForLineCoord ( const QPointF& p ) const
1668+ QgsComposerMapGrid::BorderSide QgsComposerMapGrid::borderForLineCoord ( const QPointF& p , const AnnotationCoordinate coordinateType ) const
16511669{
16521670 if ( !mComposerMap )
16531671 {
16541672 return QgsComposerMapGrid::Left;
16551673 }
16561674
16571675 double framePenWidth = mComposerMap ->hasFrame () ? mComposerMap ->pen ().widthF () : 0.000000001 ;
1676+
1677+ // check for corner coordinates
1678+ if (( p.y () <= framePenWidth && p.x () <= framePenWidth ) // top left
1679+ || ( p.y () <= framePenWidth && p.x () >= ( mComposerMap ->rect ().width () - framePenWidth ) ) // top right
1680+ || ( p.y () >= ( mComposerMap ->rect ().height () - framePenWidth ) && p.x () <= framePenWidth ) // bottom left
1681+ || ( p.y () >= ( mComposerMap ->rect ().height () - framePenWidth ) && p.x () >= ( mComposerMap ->rect ().width () - framePenWidth ) ) // bottom right
1682+ )
1683+ {
1684+ // coordinate is in corner - fall back to preferred side for coordinate type
1685+ if ( coordinateType == QgsComposerMapGrid::Latitude )
1686+ {
1687+ if ( p.x () <= framePenWidth )
1688+ {
1689+ return QgsComposerMapGrid::Left;
1690+ }
1691+ else
1692+ {
1693+ return QgsComposerMapGrid::Right;
1694+ }
1695+ }
1696+ else
1697+ {
1698+ if ( p.y () <= framePenWidth )
1699+ {
1700+ return QgsComposerMapGrid::Top;
1701+ }
1702+ else
1703+ {
1704+ return QgsComposerMapGrid::Bottom;
1705+ }
1706+ }
1707+ }
1708+
1709+ // otherwise, guess side based on point
16581710 if ( p.y () <= framePenWidth )
16591711 {
16601712 return QgsComposerMapGrid::Top;
0 commit comments