Skip to content

Commit

Permalink
Handle moving annotation with different CRS to canvas
Browse files Browse the repository at this point in the history
  • Loading branch information
nyalldawson committed Jan 29, 2017
1 parent aa14926 commit 3d372e6
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 3 deletions.
27 changes: 24 additions & 3 deletions src/app/qgsmaptoolannotation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
#include "qgssvgannotationdialog.h"
#include "qgssvgannotation.h"
#include "qgsproject.h"
#include "qgscsexception.h"
#include <QDialog>
#include <QMouseEvent>

Expand Down Expand Up @@ -119,7 +120,8 @@ void QgsMapToolAnnotation::canvasPressEvent( QgsMapMouseEvent* e )
QgsAnnotation* annotation = createItem();
if ( annotation )
{
annotation->setMapPosition( toMapCoordinates( e->pos() ) );
QgsPoint mapPos = transformCanvasToAnnotation( toMapCoordinates( e->pos() ), annotation );
annotation->setMapPosition( mapPos );
annotation->setRelativePosition( QPointF( e->posF().x() / mCanvas->width(),
e->posF().y() / mCanvas->height() ) );
annotation->setFrameSize( QSizeF( 200, 100 ) );
Expand Down Expand Up @@ -171,7 +173,8 @@ void QgsMapToolAnnotation::canvasMoveEvent( QgsMapMouseEvent* e )
{
if ( mCurrentMoveAction == QgsMapCanvasAnnotationItem::MoveMapPosition )
{
annotation->setMapPosition( toMapCoordinates( e->pos() ) );
QgsPoint mapPos = transformCanvasToAnnotation( toMapCoordinates( e->pos() ), annotation );
annotation->setMapPosition( mapPos );
annotation->setRelativePosition( QPointF( e->posF().x() / mCanvas->width(),
e->posF().y() / mCanvas->height() ) );
item->update();
Expand All @@ -188,7 +191,8 @@ void QgsMapToolAnnotation::canvasMoveEvent( QgsMapMouseEvent* e )
}
else
{
annotation->setMapPosition( toMapCoordinates( newCanvasPos.toPoint() ) );
QgsPoint mapPos = transformCanvasToAnnotation( toMapCoordinates( newCanvasPos.toPoint() ), annotation );
annotation->setMapPosition( mapPos );
annotation->setRelativePosition( QPointF( newCanvasPos.x() / mCanvas->width(),
newCanvasPos.y() / mCanvas->height() ) );
}
Expand Down Expand Up @@ -355,3 +359,20 @@ void QgsMapToolAnnotation::toggleTextItemVisibilities()
}
}
}

QgsPoint QgsMapToolAnnotation::transformCanvasToAnnotation( QgsPoint p, QgsAnnotation* annotation ) const
{
if ( annotation->mapPositionCrs() != mCanvas->mapSettings().destinationCrs() )
{
QgsCoordinateTransform transform( mCanvas->mapSettings().destinationCrs(), annotation->mapPositionCrs() );
try
{
p = transform.transform( p );
}
catch ( const QgsCsException & )
{
// ignore
}
}
return p;
}
2 changes: 2 additions & 0 deletions src/app/qgsmaptoolannotation.h
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,8 @@ class APP_EXPORT QgsMapToolAnnotation: public QgsMapTool
//! Switches visibility states of text items
void toggleTextItemVisibilities();

QgsPoint transformCanvasToAnnotation( QgsPoint p, QgsAnnotation* annotation ) const;

QgsMapCanvasAnnotationItem::MouseMoveAction mCurrentMoveAction = QgsMapCanvasAnnotationItem::NoAction;
QPointF mLastMousePosition = QPointF( 0, 0 );
};
Expand Down

0 comments on commit 3d372e6

Please sign in to comment.