Skip to content

Commit 66a83bd

Browse files
committedDec 12, 2015
make annotations aware of crs (fixes #3618)
1 parent 62f90d0 commit 66a83bd

File tree

5 files changed

+41
-1
lines changed

5 files changed

+41
-1
lines changed
 

‎python/gui/qgsannotationitem.sip

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,12 @@ class QgsAnnotationItem: QgsMapCanvasItem
4444
virtual void setMapPosition( const QgsPoint& pos );
4545
QgsPoint mapPosition() const;
4646

47+
/** Sets the CRS of the map position.
48+
@param crs the CRS to set */
49+
virtual void setMapPositionCrs( const QgsCoordinateReferenceSystem& crs );
50+
/** Returns the CRS of the map position.*/
51+
QgsCoordinateReferenceSystem mapPositionCrs() const;
52+
4753
void setFrameSize( const QSizeF& size );
4854
QSizeF frameSize() const;
4955

‎src/app/qgisapp.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2316,6 +2316,9 @@ void QgisApp::setupConnections()
23162316
connect( mRenderSuppressionCBox, SIGNAL( toggled( bool ) ),
23172317
mMapCanvas, SLOT( setRenderFlag( bool ) ) );
23182318

2319+
connect( mMapCanvas, SIGNAL( destinationCrsChanged() ),
2320+
this, SLOT( reprojectAnnotations() ) );
2321+
23192322
// connect MapCanvas keyPress event so we can check if selected feature collection must be deleted
23202323
connect( mMapCanvas, SIGNAL( keyPressed( QKeyEvent * ) ),
23212324
this, SLOT( mapCanvas_keyPressed( QKeyEvent * ) ) );
@@ -5161,6 +5164,14 @@ void QgisApp::modifyAnnotation()
51615164
mMapCanvas->setMapTool( mMapTools.mAnnotation );
51625165
}
51635166

5167+
void QgisApp::reprojectAnnotations()
5168+
{
5169+
Q_FOREACH ( QgsAnnotationItem * annotation, annotationItems() )
5170+
{
5171+
annotation->updatePosition();
5172+
}
5173+
}
5174+
51645175
void QgisApp::labelingFontNotFound( QgsVectorLayer* vlayer, const QString& fontfamily )
51655176
{
51665177
// TODO: update when pref for how to resolve missing family (use matching algorithm or just default font) is implemented

‎src/app/qgisapp.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1128,6 +1128,7 @@ class APP_EXPORT QgisApp : public QMainWindow, private Ui::MainWindow
11281128
void addHtmlAnnotation();
11291129
void addSvgAnnotation();
11301130
void modifyAnnotation();
1131+
void reprojectAnnotations();
11311132

11321133
/** Alerts user when labeling font for layer has not been found on system */
11331134
void labelingFontNotFound( QgsVectorLayer *vlayer, const QString& fontfamily );

‎src/gui/qgsannotationitem.cpp

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
QgsAnnotationItem::QgsAnnotationItem( QgsMapCanvas* mapCanvas )
2727
: QgsMapCanvasItem( mapCanvas )
2828
, mMapPositionFixed( true )
29+
, mMapPositionCrs( QgsCoordinateReferenceSystem() )
2930
, mOffsetFromReferencePoint( QPointF( 50, -50 ) )
3031
, mBalloonSegment( -1 )
3132
{
@@ -53,6 +54,12 @@ void QgsAnnotationItem::setMapPosition( const QgsPoint& pos )
5354
{
5455
mMapPosition = pos;
5556
setPos( toCanvasCoordinates( mMapPosition ) );
57+
mMapPositionCrs = mMapCanvas->mapSettings().destinationCrs();
58+
}
59+
60+
void QgsAnnotationItem::setMapPositionCrs( const QgsCoordinateReferenceSystem& crs )
61+
{
62+
mMapPositionCrs = crs;
5663
}
5764

5865
void QgsAnnotationItem::setOffsetFromReferencePoint( const QPointF& offset )
@@ -85,7 +92,8 @@ void QgsAnnotationItem::updatePosition()
8592
{
8693
if ( mMapPositionFixed )
8794
{
88-
setPos( toCanvasCoordinates( mMapPosition ) );
95+
QgsCoordinateTransform t( mMapPositionCrs, mMapCanvas->mapSettings().destinationCrs() );
96+
setPos( toCanvasCoordinates( t.transform( mMapPosition ) ) );
8997
}
9098
else
9199
{
@@ -392,6 +400,8 @@ void QgsAnnotationItem::_writeXML( QDomDocument& doc, QDomElement& itemElem ) co
392400
annotationElem.setAttribute( "mapPositionFixed", mMapPositionFixed );
393401
annotationElem.setAttribute( "mapPosX", qgsDoubleToString( mMapPosition.x() ) );
394402
annotationElem.setAttribute( "mapPosY", qgsDoubleToString( mMapPosition.y() ) );
403+
if ( mMapPositionCrs.isValid() )
404+
mMapPositionCrs.writeXML( annotationElem, doc );
395405
annotationElem.setAttribute( "offsetX", qgsDoubleToString( mOffsetFromReferencePoint.x() ) );
396406
annotationElem.setAttribute( "offsetY", qgsDoubleToString( mOffsetFromReferencePoint.y() ) );
397407
annotationElem.setAttribute( "frameWidth", QString::number( mFrameSize.width() ) );
@@ -431,6 +441,8 @@ void QgsAnnotationItem::_readXML( const QDomDocument& doc, const QDomElement& an
431441
mapPos.setX( annotationElem.attribute( "mapPosX", "0" ).toDouble() );
432442
mapPos.setY( annotationElem.attribute( "mapPosY", "0" ).toDouble() );
433443
mMapPosition = mapPos;
444+
if ( !mMapPositionCrs.readXML( annotationElem ) )
445+
mMapPositionCrs = mMapCanvas->mapSettings().destinationCrs();
434446
mFrameBorderWidth = annotationElem.attribute( "frameBorderWidth", "0.5" ).toDouble();
435447
mFrameColor.setNamedColor( annotationElem.attribute( "frameColor", "#000000" ) );
436448
mFrameColor.setAlpha( annotationElem.attribute( "frameColorAlpha", "255" ).toInt() );

‎src/gui/qgsannotationitem.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
#define QGSANNOTATIONITEM_H
2020

2121
#include "qgsmapcanvasitem.h"
22+
#include "qgscoordinatereferencesystem.h"
2223

2324
class QDomDocument;
2425
class QDomElement;
@@ -68,6 +69,12 @@ class GUI_EXPORT QgsAnnotationItem: public QgsMapCanvasItem
6869
virtual void setMapPosition( const QgsPoint& pos );
6970
QgsPoint mapPosition() const { return mMapPosition; }
7071

72+
/** Sets the CRS of the map position.
73+
@param crs the CRS to set */
74+
virtual void setMapPositionCrs( const QgsCoordinateReferenceSystem& crs );
75+
/** Returns the CRS of the map position.*/
76+
QgsCoordinateReferenceSystem mapPositionCrs() const { return mMapPositionCrs; }
77+
7178
void setFrameSize( const QSizeF& size );
7279
QSizeF frameSize() const { return mFrameSize; }
7380

@@ -98,6 +105,9 @@ class GUI_EXPORT QgsAnnotationItem: public QgsMapCanvasItem
98105
bool mMapPositionFixed;
99106
/** Map position (in case mMapPositionFixed is true)*/
100107
QgsPoint mMapPosition;
108+
/** CRS of the map position */
109+
QgsCoordinateReferenceSystem mMapPositionCrs;
110+
101111
/** Describes the shift of the item content box to the reference point*/
102112
QPointF mOffsetFromReferencePoint;
103113

0 commit comments

Comments
 (0)
Please sign in to comment.