Skip to content

Commit

Permalink
[FEATURE]: possibility to show composer map grid coordinates in degre…
Browse files Browse the repository at this point in the history
…e/minute/seconds
  • Loading branch information
mhugent committed Aug 9, 2012
1 parent 487879d commit d0b4fc1
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 9 deletions.
73 changes: 64 additions & 9 deletions src/core/composer/qgscomposermap.cpp
Expand Up @@ -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 )
Expand Down Expand Up @@ -101,6 +103,8 @@ QgsComposerMap::QgsComposerMap( QgsComposition *composition )

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

initGridAnnotationFormatFromProject();
}

QgsComposerMap::~QgsComposerMap()
Expand Down Expand Up @@ -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 );
}
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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;
}
Expand All @@ -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 );
}
Expand Down Expand Up @@ -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 )
Expand Down
17 changes: 17 additions & 0 deletions src/core/composer/qgscomposermap.h
Expand Up @@ -79,6 +79,13 @@ class CORE_EXPORT QgsComposerMap : public QgsComposerItem
BoundaryDirection
};

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

enum GridFrameStyle
{
NoGridFrame = 0,
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;

Expand All @@ -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;
Expand Down Expand Up @@ -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.