Skip to content

Commit d0b4fc1

Browse files
committedAug 9, 2012
[FEATURE]: possibility to show composer map grid coordinates in degree/minute/seconds
1 parent 487879d commit d0b4fc1

File tree

2 files changed

+81
-9
lines changed

2 files changed

+81
-9
lines changed
 

‎src/core/composer/qgscomposermap.cpp

Lines changed: 64 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,8 @@ QgsComposerMap::QgsComposerMap( QgsComposition *composition, int x, int y, int w
7474
setSceneRect( QRectF( x, y, width, height ) );
7575
setToolTip( tr( "Map %1" ).arg( mId ) );
7676
mGridPen.setCapStyle( Qt::FlatCap );
77+
78+
initGridAnnotationFormatFromProject();
7779
}
7880

7981
QgsComposerMap::QgsComposerMap( QgsComposition *composition )
@@ -101,6 +103,8 @@ QgsComposerMap::QgsComposerMap( QgsComposition *composition )
101103

102104
setToolTip( tr( "Map %1" ).arg( mId ) );
103105
mGridPen.setCapStyle( Qt::FlatCap );
106+
107+
initGridAnnotationFormatFromProject();
104108
}
105109

106110
QgsComposerMap::~QgsComposerMap()
@@ -1071,15 +1075,15 @@ void QgsComposerMap::drawCoordinateAnnotations( QPainter* p, const QList< QPair<
10711075
QList< QPair< double, QLineF > >::const_iterator it = hLines.constBegin();
10721076
for ( ; it != hLines.constEnd(); ++it )
10731077
{
1074-
currentAnnotationString = QString::number( it->first, 'f', mGridAnnotationPrecision );
1078+
currentAnnotationString = gridAnnotationString( it->first, Latitude );
10751079
drawCoordinateAnnotation( p, it->second.p1(), currentAnnotationString );
10761080
drawCoordinateAnnotation( p, it->second.p2(), currentAnnotationString );
10771081
}
10781082

10791083
it = vLines.constBegin();
10801084
for ( ; it != vLines.constEnd(); ++it )
10811085
{
1082-
currentAnnotationString = QString::number( it->first, 'f', mGridAnnotationPrecision );
1086+
currentAnnotationString = gridAnnotationString( it->first, Longitude );
10831087
drawCoordinateAnnotation( p, it->second.p1(), currentAnnotationString );
10841088
drawCoordinateAnnotation( p, it->second.p2(), currentAnnotationString );
10851089
}
@@ -1253,6 +1257,42 @@ void QgsComposerMap::drawAnnotation( QPainter* p, const QPointF& pos, int rotati
12531257
p->restore();
12541258
}
12551259

1260+
QString QgsComposerMap::gridAnnotationString( double value, AnnotationCoordinate coord ) const
1261+
{
1262+
if ( mGridAnnotationFormat == Decimal )
1263+
{
1264+
return QString::number( value, 'f', mGridAnnotationPrecision );
1265+
}
1266+
1267+
QgsPoint p;
1268+
p.setX( coord == Longitude ? value : 0 );
1269+
p.setY( coord == Longitude ? 0 : value );
1270+
1271+
QString annotationString;
1272+
if ( mGridAnnotationFormat == DegreeMinute )
1273+
{
1274+
annotationString = p.toDegreesMinutes( mGridAnnotationPrecision );
1275+
}
1276+
else //DegreeMinuteSecond
1277+
{
1278+
annotationString = p.toDegreesMinutesSeconds( mGridAnnotationPrecision );
1279+
}
1280+
1281+
QStringList split = annotationString.split( "," );
1282+
if ( coord == Longitude )
1283+
{
1284+
return split.at( 0 );
1285+
}
1286+
else
1287+
{
1288+
if ( split.size() < 2 )
1289+
{
1290+
return "";
1291+
}
1292+
return split.at( 1 );
1293+
}
1294+
}
1295+
12561296
int QgsComposerMap::xGridLines( QList< QPair< double, QLineF > >& lines ) const
12571297
{
12581298
lines.clear();
@@ -1454,12 +1494,10 @@ double QgsComposerMap::maxExtension() const
14541494
QList< QPair< double, QLineF > > xLines;
14551495
QList< QPair< double, QLineF > > yLines;
14561496

1457-
if ( xGridLines( xLines ) != 0 )
1458-
{
1459-
return 0;
1460-
}
1497+
int xGridReturn = xGridLines( xLines );
1498+
int yGridReturn = yGridLines( yLines );
14611499

1462-
if ( yGridLines( yLines ) != 0 )
1500+
if ( xGridReturn != 0 && yGridReturn != 0 )
14631501
{
14641502
return 0;
14651503
}
@@ -1471,15 +1509,15 @@ double QgsComposerMap::maxExtension() const
14711509
QList< QPair< double, QLineF > >::const_iterator it = xLines.constBegin();
14721510
for ( ; it != xLines.constEnd(); ++it )
14731511
{
1474-
currentAnnotationString = QString::number( it->first, 'f', mGridAnnotationPrecision );
1512+
currentAnnotationString = gridAnnotationString( it->first, Latitude );
14751513
currentExtension = qMax( textWidthMillimeters( mGridAnnotationFont, currentAnnotationString ), fontAscentMillimeters( mGridAnnotationFont ) );
14761514
maxExtension = qMax( maxExtension, currentExtension );
14771515
}
14781516

14791517
it = yLines.constBegin();
14801518
for ( ; it != yLines.constEnd(); ++it )
14811519
{
1482-
currentAnnotationString = QString::number( it->first, 'f', mGridAnnotationPrecision );
1520+
currentAnnotationString = gridAnnotationString( it->first, Longitude );
14831521
currentExtension = qMax( textWidthMillimeters( mGridAnnotationFont, currentAnnotationString ), fontAscentMillimeters( mGridAnnotationFont ) );
14841522
maxExtension = qMax( maxExtension, currentExtension );
14851523
}
@@ -1948,6 +1986,23 @@ void QgsComposerMap::createDefaultOverviewFrameSymbol()
19481986
mOverviewFrameMapSymbol->setAlpha( 0.3 );
19491987
}
19501988

1989+
void QgsComposerMap::initGridAnnotationFormatFromProject()
1990+
{
1991+
QString format = QgsProject::instance()->readEntry( "PositionPrecision", "/DegreeFormat", "D" );
1992+
if ( format == "DM" )
1993+
{
1994+
mGridAnnotationFormat = DegreeMinute;
1995+
}
1996+
else if ( format == "DMS" )
1997+
{
1998+
mGridAnnotationFormat = DegreeMinuteSecond;
1999+
}
2000+
else
2001+
{
2002+
mGridAnnotationFormat = Decimal;
2003+
}
2004+
}
2005+
19512006
void QgsComposerMap::assignFreeId()
19522007
{
19532008
if ( !mComposition )

‎src/core/composer/qgscomposermap.h

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,13 @@ class CORE_EXPORT QgsComposerMap : public QgsComposerItem
7979
BoundaryDirection
8080
};
8181

82+
enum GridAnnotationFormat
83+
{
84+
Decimal = 0,
85+
DegreeMinute,
86+
DegreeMinuteSecond
87+
};
88+
8289
enum GridFrameStyle
8390
{
8491
NoGridFrame = 0,
@@ -316,6 +323,12 @@ class CORE_EXPORT QgsComposerMap : public QgsComposerItem
316323

317324
private:
318325

326+
enum AnnotationCoordinate
327+
{
328+
Longitude = 0,
329+
Latitude
330+
};
331+
319332
// Pointer to map renderer of the QGIS main map. Note that QgsComposerMap uses a different map renderer,
320333
//it just copies some properties from the main map renderer.
321334
QgsMapRenderer *mMapRenderer;
@@ -407,6 +420,8 @@ class CORE_EXPORT QgsComposerMap : public QgsComposerItem
407420
/**Annotation direction on bottom side ( horizontal or vertical )*/
408421
GridAnnotationDirection mBottomGridAnnotationDirection;
409422

423+
GridAnnotationFormat mGridAnnotationFormat;
424+
410425
GridFrameStyle mGridFrameStyle;
411426
double mGridFrameWidth;
412427

@@ -433,6 +448,7 @@ class CORE_EXPORT QgsComposerMap : public QgsComposerItem
433448
@param rotation text rotation
434449
@param annotationText the text to draw*/
435450
void drawAnnotation( QPainter* p, const QPointF& pos, int rotation, const QString& annotationText );
451+
QString gridAnnotationString( double value, AnnotationCoordinate coord ) const;
436452
/**Returns the grid lines with associated coordinate value
437453
@return 0 in case of success*/
438454
int xGridLines( QList< QPair< double, QLineF > >& lines ) const;
@@ -467,6 +483,7 @@ class CORE_EXPORT QgsComposerMap : public QgsComposerItem
467483
void drawGridFrameBorder( QPainter* p, const QMap< double, double >& borderPos, Border border );
468484
void drawOverviewMapExtent( QPainter* p );
469485
void createDefaultOverviewFrameSymbol();
486+
void initGridAnnotationFormatFromProject();
470487
};
471488

472489
#endif

0 commit comments

Comments
 (0)
Please sign in to comment.