Skip to content
Permalink
Browse files

Handle moving annotation with different CRS to canvas

  • Loading branch information
nyalldawson committed Jan 29, 2017
1 parent aa14926 commit 3d372e615c46c9b4bb05497467ae1a66e3df83b7
Showing with 26 additions and 3 deletions.
  1. +24 −3 src/app/qgsmaptoolannotation.cpp
  2. +2 −0 src/app/qgsmaptoolannotation.h
@@ -28,6 +28,7 @@
#include "qgssvgannotationdialog.h"
#include "qgssvgannotation.h"
#include "qgsproject.h"
#include "qgscsexception.h"
#include <QDialog>
#include <QMouseEvent>

@@ -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 ) );
@@ -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();
@@ -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() ) );
}
@@ -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;
}
@@ -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 );
};

0 comments on commit 3d372e6

Please sign in to comment.
You can’t perform that action at this time.