Skip to content

Commit fadf182

Browse files
committedOct 19, 2014
[composer] Fix placement of coordinates in map corners (fix #8827)
1 parent 422ea56 commit fadf182

File tree

2 files changed

+89
-25
lines changed

2 files changed

+89
-25
lines changed
 

‎src/core/composer/qgscomposermapgrid.cpp

Lines changed: 70 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -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
16101610
void 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;

‎src/core/composer/qgscomposermapgrid.h

100755100644
Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -808,6 +808,14 @@ class CORE_EXPORT QgsComposerMapGrid : public QgsComposerMapItem
808808
QRectF mPrevPaintRect;
809809
QPolygonF mPrevMapPolygon;
810810

811+
class QgsMapAnnotation
812+
{
813+
public:
814+
double coordinate;
815+
QPointF itemPosition;
816+
QgsComposerMapGrid::AnnotationCoordinate coordinateType;
817+
};
818+
811819
/**Draws the map grid*/
812820
void drawGridFrame( QPainter* p, const QList< QPair< double, QLineF > >& hLines, const QList< QPair< double, QLineF > >& vLines ) const;
813821

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

820-
void drawCoordinateAnnotation( QPainter* p, const QPointF& pos, QString annotationString ) const;
828+
void drawCoordinateAnnotation( QPainter* p, const QPointF& pos, QString annotationString, const AnnotationCoordinate coordinateType ) const;
821829

822830
/**Draws a single annotation
823-
@param p drawing painter
824-
@param pos item coordinates where to draw
825-
@param rotation text rotation
826-
@param annotationText the text to draw*/
831+
* @param p drawing painter
832+
* @param pos item coordinates where to draw
833+
* @param rotation text rotation
834+
* @param annotationText the text to draw
835+
*/
827836
void drawAnnotation( QPainter* p, const QPointF& pos, int rotation, const QString& annotationText ) const;
828837

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

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

852-
/**Returns the item border of a point (in item coordinates)*/
853-
BorderSide borderForLineCoord( const QPointF& p ) const;
861+
/**Returns the item border of a point (in item coordinates)
862+
* @param p point
863+
* @param coordinateType coordinate type
864+
*/
865+
BorderSide borderForLineCoord( const QPointF& p, const AnnotationCoordinate coordinateType ) const;
854866

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

0 commit comments

Comments
 (0)
Please sign in to comment.