23 changes: 23 additions & 0 deletions src/app/composer/qgscomposermapwidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,10 @@ QgsComposerMapWidget::QgsComposerMapWidget( QgsComposerMap* composerMap ): QWidg
mGridTypeComboBox->insertItem( 0, tr( "Solid" ) );
mGridTypeComboBox->insertItem( 1, tr( "Cross" ) );

mAnnotationFormatComboBox->insertItem( 0, tr( "Decimal" ) );
mAnnotationFormatComboBox->insertItem( 1, tr( "DegreeMinute" ) );
mAnnotationFormatComboBox->insertItem( 2, tr( "DegreeMinuteSecond" ) );

insertAnnotationPositionEntries( mAnnotationPositionLeftComboBox );
insertAnnotationPositionEntries( mAnnotationPositionRightComboBox );
insertAnnotationPositionEntries( mAnnotationPositionTopComboBox );
Expand Down Expand Up @@ -372,6 +376,10 @@ void QgsComposerMapWidget::updateGuiElements()
mFrameStyleComboBox->setCurrentIndex( mFrameStyleComboBox->findText( tr( "No frame" ) ) );
}

//grid annotation format
QgsComposerMap::GridAnnotationFormat gf = mComposerMap->gridAnnotationFormat();
mAnnotationFormatComboBox->setCurrentIndex(( int )gf );

//grid annotation position
initAnnotationPositionBox( mAnnotationPositionLeftComboBox, mComposerMap->gridAnnotationPosition( QgsComposerMap::Left ) );
initAnnotationPositionBox( mAnnotationPositionRightComboBox, mComposerMap->gridAnnotationPosition( QgsComposerMap::Right ) );
Expand Down Expand Up @@ -457,6 +465,7 @@ void QgsComposerMapWidget::blockAllSignals( bool b )
mLineColorButton->blockSignals( b );
mDrawAnnotationCheckBox->blockSignals( b );
mAnnotationFontButton->blockSignals( b );
mAnnotationFormatComboBox->blockSignals( b );
mAnnotationPositionLeftComboBox->blockSignals( b );
mAnnotationPositionRightComboBox->blockSignals( b );
mAnnotationPositionTopComboBox->blockSignals( b );
Expand Down Expand Up @@ -767,6 +776,20 @@ void QgsComposerMapWidget::on_mDistanceToMapFrameSpinBox_valueChanged( double d
mComposerMap->endCommand();
}

void QgsComposerMapWidget::on_mAnnotationFormatComboBox_currentIndexChanged( int index )
{
if ( !mComposerMap )
{
return;
}

mComposerMap->beginCommand( tr( "Annotation format changed" ) );
mComposerMap->setGridAnnotationFormat(( QgsComposerMap::GridAnnotationFormat )index );
mComposerMap->updateBoundingRect();
mComposerMap->update();
mComposerMap->endCommand();
}

void QgsComposerMapWidget::on_mAnnotationPositionLeftComboBox_currentIndexChanged( const QString& text )
{
handleChangedAnnotationPosition( QgsComposerMap::Left, text );
Expand Down
2 changes: 2 additions & 0 deletions src/app/composer/qgscomposermapwidget.h
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,8 @@ class QgsComposerMapWidget: public QWidget, private Ui::QgsComposerMapWidgetBase
void on_mAnnotationFontButton_clicked();
void on_mDistanceToMapFrameSpinBox_valueChanged( double d );

void on_mAnnotationFormatComboBox_currentIndexChanged( int index );

//annotation position
void on_mAnnotationPositionLeftComboBox_currentIndexChanged( const QString& text );
void on_mAnnotationPositionRightComboBox_currentIndexChanged( const QString& text );
Expand Down
82 changes: 73 additions & 9 deletions src/core/composer/qgscomposermap.cpp
Original file line number Diff line number Diff line change
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 @@ -707,6 +711,7 @@ bool QgsComposerMap::writeXML( QDomElement& elem, QDomDocument & doc ) const

//grid annotation
QDomElement annotationElem = doc.createElement( "Annotation" );
annotationElem.setAttribute( "format", mGridAnnotationFormat );
annotationElem.setAttribute( "show", mShowGridAnnotation );
annotationElem.setAttribute( "leftPosition", mLeftGridAnnotationPosition );
annotationElem.setAttribute( "rightPosition", mRightGridAnnotationPosition );
Expand Down Expand Up @@ -845,6 +850,7 @@ bool QgsComposerMap::readXML( const QDomElement& itemElem, const QDomDocument& d
{
QDomElement annotationElem = annotationNodeList.at( 0 ).toElement();
mShowGridAnnotation = ( annotationElem.attribute( "show", "0" ) != "0" );
mGridAnnotationFormat = QgsComposerMap::GridAnnotationFormat( annotationElem.attribute( "format", "0" ).toInt() );
mLeftGridAnnotationPosition = QgsComposerMap::GridAnnotationPosition( annotationElem.attribute( "leftPosition", "0" ).toInt() );
mRightGridAnnotationPosition = QgsComposerMap::GridAnnotationPosition( annotationElem.attribute( "rightPosition", "0" ).toInt() );
mTopGridAnnotationPosition = QgsComposerMap::GridAnnotationPosition( annotationElem.attribute( "topPosition", "0" ).toInt() );
Expand Down Expand Up @@ -1071,15 +1077,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 +1259,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 +1496,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 +1511,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 +1988,30 @@ void QgsComposerMap::createDefaultOverviewFrameSymbol()
mOverviewFrameMapSymbol->setAlpha( 0.3 );
}

void QgsComposerMap::initGridAnnotationFormatFromProject()
{
QString format = QgsProject::instance()->readEntry( "PositionPrecision", "/DegreeFormat", "D" );

bool degreeUnits = true;
if ( mMapRenderer )
{
degreeUnits = ( mMapRenderer->mapUnits() == QGis::Degrees );
}

if ( format == "DM" && degreeUnits )
{
mGridAnnotationFormat = DegreeMinute;
}
else if ( format == "DMS" && degreeUnits )
{
mGridAnnotationFormat = DegreeMinuteSecond;
}
else
{
mGridAnnotationFormat = Decimal;
}
}

void QgsComposerMap::assignFreeId()
{
if ( !mComposition )
Expand Down
20 changes: 20 additions & 0 deletions src/core/composer/qgscomposermap.h
Original file line number Diff line number Diff line change
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 @@ -255,6 +262,9 @@ class CORE_EXPORT QgsComposerMap : public QgsComposerItem
void setGridAnnotationDirection( GridAnnotationDirection d, QgsComposerMap::Border border );
GridAnnotationDirection gridAnnotationDirection( QgsComposerMap::Border border ) const;

void setGridAnnotationFormat( GridAnnotationFormat f ) { mGridAnnotationFormat = f; }
GridAnnotationFormat gridAnnotationFormat() const { return mGridAnnotationFormat; }

/**Set grid frame style (NoGridFrame or Zebra)
@note: this function was added in version 1.9*/
void setGridFrameStyle( GridFrameStyle style ) { mGridFrameStyle = style; }
Expand Down Expand Up @@ -316,6 +326,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 +423,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 +451,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 +486,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
Loading