Skip to content
Permalink
Browse files

[FEATURE]: possibility to show composer map grid coordinates in degre…

…e/minute/seconds
  • Loading branch information
mhugent committed Aug 9, 2012
1 parent 487879d commit d0b4fc17ea5d8a4a8006e2bc2bc6bd222a505f1d
Showing with 81 additions and 9 deletions.
  1. +64 −9 src/core/composer/qgscomposermap.cpp
  2. +17 −0 src/core/composer/qgscomposermap.h
@@ -74,6 +74,8 @@ QgsComposerMap::QgsComposerMap( QgsComposition *composition, int x, int y, int w
setSceneRect( QRectF( x, y, width, height ) );
setToolTip( tr( "Map %1" ).arg( mId ) );
mGridPen.setCapStyle( Qt::FlatCap );

initGridAnnotationFormatFromProject();
}

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

setToolTip( tr( "Map %1" ).arg( mId ) );
mGridPen.setCapStyle( Qt::FlatCap );

initGridAnnotationFormatFromProject();
}

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

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

QString QgsComposerMap::gridAnnotationString( double value, AnnotationCoordinate coord ) const
{
if ( mGridAnnotationFormat == Decimal )
{
return QString::number( value, 'f', mGridAnnotationPrecision );
}

QgsPoint p;
p.setX( coord == Longitude ? value : 0 );
p.setY( coord == Longitude ? 0 : value );

QString annotationString;
if ( mGridAnnotationFormat == DegreeMinute )
{
annotationString = p.toDegreesMinutes( mGridAnnotationPrecision );
}
else //DegreeMinuteSecond
{
annotationString = p.toDegreesMinutesSeconds( mGridAnnotationPrecision );
}

QStringList split = annotationString.split( "," );
if ( coord == Longitude )
{
return split.at( 0 );
}
else
{
if ( split.size() < 2 )
{
return "";
}
return split.at( 1 );
}
}

int QgsComposerMap::xGridLines( QList< QPair< double, QLineF > >& lines ) const
{
lines.clear();
@@ -1454,12 +1494,10 @@ double QgsComposerMap::maxExtension() const
QList< QPair< double, QLineF > > xLines;
QList< QPair< double, QLineF > > yLines;

if ( xGridLines( xLines ) != 0 )
{
return 0;
}
int xGridReturn = xGridLines( xLines );
int yGridReturn = yGridLines( yLines );

if ( yGridLines( yLines ) != 0 )
if ( xGridReturn != 0 && yGridReturn != 0 )
{
return 0;
}
@@ -1471,15 +1509,15 @@ double QgsComposerMap::maxExtension() const
QList< QPair< double, QLineF > >::const_iterator it = xLines.constBegin();
for ( ; it != xLines.constEnd(); ++it )
{
currentAnnotationString = QString::number( it->first, 'f', mGridAnnotationPrecision );
currentAnnotationString = gridAnnotationString( it->first, Latitude );
currentExtension = qMax( textWidthMillimeters( mGridAnnotationFont, currentAnnotationString ), fontAscentMillimeters( mGridAnnotationFont ) );
maxExtension = qMax( maxExtension, currentExtension );
}

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

void QgsComposerMap::initGridAnnotationFormatFromProject()
{
QString format = QgsProject::instance()->readEntry( "PositionPrecision", "/DegreeFormat", "D" );
if ( format == "DM" )
{
mGridAnnotationFormat = DegreeMinute;
}
else if ( format == "DMS" )
{
mGridAnnotationFormat = DegreeMinuteSecond;
}
else
{
mGridAnnotationFormat = Decimal;
}
}

void QgsComposerMap::assignFreeId()
{
if ( !mComposition )
@@ -79,6 +79,13 @@ class CORE_EXPORT QgsComposerMap : public QgsComposerItem
BoundaryDirection
};

enum GridAnnotationFormat
{
Decimal = 0,
DegreeMinute,
DegreeMinuteSecond
};

enum GridFrameStyle
{
NoGridFrame = 0,
@@ -316,6 +323,12 @@ class CORE_EXPORT QgsComposerMap : public QgsComposerItem

private:

enum AnnotationCoordinate
{
Longitude = 0,
Latitude
};

// Pointer to map renderer of the QGIS main map. Note that QgsComposerMap uses a different map renderer,
//it just copies some properties from the main map renderer.
QgsMapRenderer *mMapRenderer;
@@ -407,6 +420,8 @@ class CORE_EXPORT QgsComposerMap : public QgsComposerItem
/**Annotation direction on bottom side ( horizontal or vertical )*/
GridAnnotationDirection mBottomGridAnnotationDirection;

GridAnnotationFormat mGridAnnotationFormat;

GridFrameStyle mGridFrameStyle;
double mGridFrameWidth;

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

#endif

0 comments on commit d0b4fc1

Please sign in to comment.
You can’t perform that action at this time.