Skip to content

Commit

Permalink
Merge pull request #2537 from SebDieBln/ReprojectAnnotations
Browse files Browse the repository at this point in the history
Make annotations aware of CRS (fixes #3618)
  • Loading branch information
nyalldawson committed Dec 13, 2015
2 parents 666eda6 + 66a83bd commit 2982082
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 1 deletion.
6 changes: 6 additions & 0 deletions python/gui/qgsannotationitem.sip
Expand Up @@ -44,6 +44,12 @@ class QgsAnnotationItem: QgsMapCanvasItem
virtual void setMapPosition( const QgsPoint& pos );
QgsPoint mapPosition() const;

/** Sets the CRS of the map position.
@param crs the CRS to set */
virtual void setMapPositionCrs( const QgsCoordinateReferenceSystem& crs );
/** Returns the CRS of the map position.*/
QgsCoordinateReferenceSystem mapPositionCrs() const;

void setFrameSize( const QSizeF& size );
QSizeF frameSize() const;

Expand Down
11 changes: 11 additions & 0 deletions src/app/qgisapp.cpp
Expand Up @@ -2316,6 +2316,9 @@ void QgisApp::setupConnections()
connect( mRenderSuppressionCBox, SIGNAL( toggled( bool ) ),
mMapCanvas, SLOT( setRenderFlag( bool ) ) );

connect( mMapCanvas, SIGNAL( destinationCrsChanged() ),
this, SLOT( reprojectAnnotations() ) );

// connect MapCanvas keyPress event so we can check if selected feature collection must be deleted
connect( mMapCanvas, SIGNAL( keyPressed( QKeyEvent * ) ),
this, SLOT( mapCanvas_keyPressed( QKeyEvent * ) ) );
Expand Down Expand Up @@ -5161,6 +5164,14 @@ void QgisApp::modifyAnnotation()
mMapCanvas->setMapTool( mMapTools.mAnnotation );
}

void QgisApp::reprojectAnnotations()
{
Q_FOREACH ( QgsAnnotationItem * annotation, annotationItems() )
{
annotation->updatePosition();
}
}

void QgisApp::labelingFontNotFound( QgsVectorLayer* vlayer, const QString& fontfamily )
{
// TODO: update when pref for how to resolve missing family (use matching algorithm or just default font) is implemented
Expand Down
1 change: 1 addition & 0 deletions src/app/qgisapp.h
Expand Up @@ -1128,6 +1128,7 @@ class APP_EXPORT QgisApp : public QMainWindow, private Ui::MainWindow
void addHtmlAnnotation();
void addSvgAnnotation();
void modifyAnnotation();
void reprojectAnnotations();

/** Alerts user when labeling font for layer has not been found on system */
void labelingFontNotFound( QgsVectorLayer *vlayer, const QString& fontfamily );
Expand Down
14 changes: 13 additions & 1 deletion src/gui/qgsannotationitem.cpp
Expand Up @@ -26,6 +26,7 @@
QgsAnnotationItem::QgsAnnotationItem( QgsMapCanvas* mapCanvas )
: QgsMapCanvasItem( mapCanvas )
, mMapPositionFixed( true )
, mMapPositionCrs( QgsCoordinateReferenceSystem() )
, mOffsetFromReferencePoint( QPointF( 50, -50 ) )
, mBalloonSegment( -1 )
{
Expand Down Expand Up @@ -53,6 +54,12 @@ void QgsAnnotationItem::setMapPosition( const QgsPoint& pos )
{
mMapPosition = pos;
setPos( toCanvasCoordinates( mMapPosition ) );
mMapPositionCrs = mMapCanvas->mapSettings().destinationCrs();
}

void QgsAnnotationItem::setMapPositionCrs( const QgsCoordinateReferenceSystem& crs )
{
mMapPositionCrs = crs;
}

void QgsAnnotationItem::setOffsetFromReferencePoint( const QPointF& offset )
Expand Down Expand Up @@ -85,7 +92,8 @@ void QgsAnnotationItem::updatePosition()
{
if ( mMapPositionFixed )
{
setPos( toCanvasCoordinates( mMapPosition ) );
QgsCoordinateTransform t( mMapPositionCrs, mMapCanvas->mapSettings().destinationCrs() );
setPos( toCanvasCoordinates( t.transform( mMapPosition ) ) );
}
else
{
Expand Down Expand Up @@ -392,6 +400,8 @@ void QgsAnnotationItem::_writeXML( QDomDocument& doc, QDomElement& itemElem ) co
annotationElem.setAttribute( "mapPositionFixed", mMapPositionFixed );
annotationElem.setAttribute( "mapPosX", qgsDoubleToString( mMapPosition.x() ) );
annotationElem.setAttribute( "mapPosY", qgsDoubleToString( mMapPosition.y() ) );
if ( mMapPositionCrs.isValid() )
mMapPositionCrs.writeXML( annotationElem, doc );
annotationElem.setAttribute( "offsetX", qgsDoubleToString( mOffsetFromReferencePoint.x() ) );
annotationElem.setAttribute( "offsetY", qgsDoubleToString( mOffsetFromReferencePoint.y() ) );
annotationElem.setAttribute( "frameWidth", QString::number( mFrameSize.width() ) );
Expand Down Expand Up @@ -431,6 +441,8 @@ void QgsAnnotationItem::_readXML( const QDomDocument& doc, const QDomElement& an
mapPos.setX( annotationElem.attribute( "mapPosX", "0" ).toDouble() );
mapPos.setY( annotationElem.attribute( "mapPosY", "0" ).toDouble() );
mMapPosition = mapPos;
if ( !mMapPositionCrs.readXML( annotationElem ) )
mMapPositionCrs = mMapCanvas->mapSettings().destinationCrs();
mFrameBorderWidth = annotationElem.attribute( "frameBorderWidth", "0.5" ).toDouble();
mFrameColor.setNamedColor( annotationElem.attribute( "frameColor", "#000000" ) );
mFrameColor.setAlpha( annotationElem.attribute( "frameColorAlpha", "255" ).toInt() );
Expand Down
10 changes: 10 additions & 0 deletions src/gui/qgsannotationitem.h
Expand Up @@ -19,6 +19,7 @@
#define QGSANNOTATIONITEM_H

#include "qgsmapcanvasitem.h"
#include "qgscoordinatereferencesystem.h"

class QDomDocument;
class QDomElement;
Expand Down Expand Up @@ -68,6 +69,12 @@ class GUI_EXPORT QgsAnnotationItem: public QgsMapCanvasItem
virtual void setMapPosition( const QgsPoint& pos );
QgsPoint mapPosition() const { return mMapPosition; }

/** Sets the CRS of the map position.
@param crs the CRS to set */
virtual void setMapPositionCrs( const QgsCoordinateReferenceSystem& crs );
/** Returns the CRS of the map position.*/
QgsCoordinateReferenceSystem mapPositionCrs() const { return mMapPositionCrs; }

void setFrameSize( const QSizeF& size );
QSizeF frameSize() const { return mFrameSize; }

Expand Down Expand Up @@ -98,6 +105,9 @@ class GUI_EXPORT QgsAnnotationItem: public QgsMapCanvasItem
bool mMapPositionFixed;
/** Map position (in case mMapPositionFixed is true)*/
QgsPoint mMapPosition;
/** CRS of the map position */
QgsCoordinateReferenceSystem mMapPositionCrs;

/** Describes the shift of the item content box to the reference point*/
QPointF mOffsetFromReferencePoint;

Expand Down

0 comments on commit 2982082

Please sign in to comment.