
from qgis.gui import *

from qgis.PyQt.QtCore import *
from qgis.PyQt.QtGui import *


class MapDigitizeTool(QgsMapTool):

    moved = pyqtSignal(dict)
    leftClicked = pyqtSignal(dict)
    rightClicked = pyqtSignal(dict)

    def __init__(self, canvas):

        QgsMapTool.__init__(self, canvas)
        self.canvas = canvas
        #self.cursor = QCursor(Qt.CrossCursor)

    def canvasMoveEvent(self, event):

        self.moved.emit({'x': event.pos().x(), 'y': event.pos().y()})

    def canvasReleaseEvent(self, event):

        if event.button() == Qt.MouseButton.RightButton:
            self.rightClicked.emit({'x': event.pos().x(), 'y': event.pos().y()})
        elif event.button() == Qt.MouseButton.LeftButton:
            self.leftClicked.emit({'x': event.pos().x(), 'y': event.pos().y()})
        else:
            return

    def activate(self):

        QgsMapTool.activate(self)
        #self.canvas.setCursor(self.cursor)

    def deactivate(self):

        QgsMapTool.deactivate(self)

    def isZoomTool(self):

        return False

    def setCursor(self, cursor):

        self.cursor = QCursor(cursor)

"""
Modified from: profiletool, script: tools/ptmaptool.py

#-----------------------------------------------------------
# 
# Profile
# Copyright (C) 2008  Borys Jurgiel
# Copyright (C) 2012  Patrice Verchere
#-----------------------------------------------------------
# 
# licensed under the terms of GNU GPL 2
# 
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
# 
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
# 
# You should have received a copy of the GNU General Public License along
# with this program; if not, print to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
#
#---------------------------------------------------------------------
"""


class EmitPointFromCanvas(QgsMapToolEmitPoint):

    def __init__(self, canvas, button):
        super().__init__(canvas)
        self.canvas = canvas
        self.button = button
        super().setCursor(QCursor(Qt.CursorShape.CrossCursor))  # applica davvero

    def setCursor(self, cursor):

        # applica davvero sul tool (non solo salva in un attributo)
        super().setCursor(QCursor(cursor))


def xy_from_canvas(
        canvas,
        position
):
    """
    Convert canvas pixel coordinates to map coordinates.
    Uses QGIS 3.x modern API instead of deprecated getCoordinateTransform().
    """
    # Use mapSettings() to convert pixel coordinates to map coordinates
    map_settings = canvas.mapSettings()
    # mapToPixel() provides the transform, toMapCoordinates() converts pixel to map coords
    map_point = map_settings.mapToPixel().toMapCoordinates(position["x"], position["y"])
    
    return map_point.x(), map_point.y()


