diff --git a/src/gui/qgsmaptool.cpp b/src/gui/qgsmaptool.cpp index 8234e5eddc07..f114ba6e5392 100644 --- a/src/gui/qgsmaptool.cpp +++ b/src/gui/qgsmaptool.cpp @@ -15,8 +15,10 @@ /* $Id$ */ #include "qgsmaptool.h" +#include "qgsmaplayer.h" #include "qgsmapcanvas.h" #include "qgsmaptopixel.h" +#include "qgsproject.h" #include QgsMapTool::QgsMapTool(QgsMapCanvas* canvas) @@ -36,6 +38,30 @@ QgsPoint QgsMapTool::toMapCoords(const QPoint& point) } +QgsPoint QgsMapTool::toLayerCoords(QgsMapLayer* layer, const QPoint& point) +{ + // FIXME: this information should be accessible elsewhere without accessing QgsProject! + bool projectionsEnabled = (QgsProject::instance()->readNumEntry("SpatialRefSys","/ProjectionsEnabled",0)!=0); + + if (projectionsEnabled) + { + + if (!layer || !layer->coordinateTransform()) + return QgsPoint(0,0); + + // first transform from canvas coords to map coordinates + QgsPoint pnt = toMapCoords(point); + + // then convert from + return layer->coordinateTransform()->transform(pnt, QgsCoordinateTransform::INVERSE); + } + else + { + return toMapCoords(point); + } +} + + QPoint QgsMapTool::toCanvasCoords(const QgsPoint& point) { double x = point.x(), y = point.y(); diff --git a/src/gui/qgsmaptool.h b/src/gui/qgsmaptool.h index aa73f5581208..45bef7e12e87 100644 --- a/src/gui/qgsmaptool.h +++ b/src/gui/qgsmaptool.h @@ -20,6 +20,7 @@ #include #include +class QgsMapLayer; class QgsMapCanvas; class QMouseEvent; class QgsPoint; @@ -74,6 +75,9 @@ class QgsMapTool //! transformation from screen coordinates to map coordinates QgsPoint toMapCoords(const QPoint& point); + //! transformation from screen coordinates to layer's coordinates + QgsPoint toLayerCoords(QgsMapLayer* layer, const QPoint& point); + //! transformation from map coordinates to screen coordinates QPoint toCanvasCoords(const QgsPoint& point);