'''
Created on 10 apr 2014

@author: nke
'''
# Import the PyQt and QGIS libraries
from qgis.PyQt.QtCore import pyqtSignal
from qgis.core import QgsGeometry
from qgis.gui import QgsMapToolEmitPoint


class FeatureTool(QgsMapToolEmitPoint):

    Feature = []
    selectedFeature = pyqtSignal()

    def __init__(self, canvas):
        
        #Create a reference to the map canvas
        self.canvas = canvas
        QgsMapToolEmitPoint.__init__(self, self.canvas)

    def canvasPressEvent(self, e):
        currentLayer = self.canvas.currentLayer()
        point = self.toMapCoordinates(e.pos())
        pntGeom = QgsGeometry.fromPointXY(point)
        pntbuf = pntGeom.buffer((self.canvas.mapUnitsPerPixel() * 2),0)
        rect = pntbuf.boundingBox()
        cLayer = self.canvas.currentLayer()
        selectList = None
        if cLayer:
               # selectList = cLayer.select(rect,True)
               selectList = cLayer.selectByRect()
               if cLayer.selectedFeatureCount() is not 0:
                   self.selectedFeature.emit()
               
              # while provider.nextFeature(feat):
                    #   if feat.geometry().intersects(pntGeom):
                         #      selectList.append(feat.id())

              # cLayer.setSelectedFeatures(selectList)
                
        #point2 = QgsPoint(point.x(), point.y())
        #rect = QgsRectangle(point, point2)
        #self.Feature = QgsFeature()
        #provider = currentLayer.dataProvider()
        #currentLayer.select(rect, True)
        #self.Feature = currentLayer.selectedFeatures()
        #self.selectedFeature.emit(self.Feature)