#---------------------------------------------------------------------
# 
# Raster Info - A QGIS plugin to allow for getting file info from a
#               loaded raster by clicking on the raster in the map
#               window
#
# Copyright (C) 2009  Aaron Racicot
#
# EMAIL: aaronr (at) z-pulley.com
# WEB  : www.reprojected.com
#
#---------------------------------------------------------------------
# 
# 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, write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
# 
#---------------------------------------------------------------------

from PyQt4.QtCore import *
from PyQt4.QtGui import *
from qgis.core import *
from qgis.gui import *

from rasterfileinfotool import RasterFileInfoTool
import resources

import os
import tempfile

class RasterFileInfoPlugin(object):
  def __init__(self, iface):
    # Save a reference to the QGIS iface
    self.iface = iface
    self.canvas = iface.mapCanvas()

  def initGui(self):
    # Create action
    self.action = QAction(QIcon(":/rasterinfo.png"),
                          "Get Raster File Info", self.iface.mainWindow())
    self.action.setWhatsThis("Raster File Info Plugin")
    QObject.connect(self.action, SIGNAL("activated()"), self.run)
    self.helpaction = QAction(QIcon(":/rasterhelp.png"),
                              "About", self.iface.mainWindow())
    self.helpaction.setWhatsThis("Raster File Info Plugin Help")
    QObject.connect(self.helpaction, SIGNAL("activated()"), self.helprun)

    # Add to the main toolbar
    self.iface.addToolBarIcon(self.action)
    self.iface.addPluginToMenu("&RasterFileInfo", self.action)
    self.iface.addPluginToMenu("&RasterFileInfo", self.helpaction)

    self.tool = RasterFileInfoTool(self.canvas)

  def unload(self):
    # Remove the plugin
    self.iface.removePluginMenu("&RasterFileInfo",self.action)
    self.iface.removePluginMenu("&RasterFileInfo",self.helpaction)
    self.iface.removeToolBarIcon(self.action)

  def helprun(self):
    # print "Help pressed..."
    infoString = QString("Written by Aaron Racicot\naaronr@z-pulley.com\n")
    infoString = infoString.append("Company - http://www.z-pulley.com\n\n")
    infoString = infoString.append("Blog - http://www.reprojected.com\n\n")
    infoString = infoString.append("Source: http://svn.reprojected.com/")
    infoString = infoString.append("qgisplugins/trunk/rasterinfo\n")
    infoString = infoString.append("TRAC: http://trac.reprojected.com/")
    infoString = infoString.append("qgisplugins\n")
    QMessageBox.information(self.iface.mainWindow(),
                            "Raster File Info Plugin About",infoString)

  def run(self):
    # Change the tool to the rasterinfo tool
    QObject.connect(self.tool, SIGNAL("finished(PyQt_PyObject)"),
                    self.finished)
    self.saveTool = self.canvas.mapTool()
    self.canvas.setMapTool(self.tool)

  def finished(self,xy):
    # Need to find the layer that intersects the press
    transform = self.canvas.getCoordinateTransform()
    pt = transform.toMapCoordinates(xy['x'],xy['y'])
    infoString = None    
    # Now loop through all layers and look for intersects
    nLayers=self.canvas.layerCount()
    for l in range(nLayers):
      layer = self.canvas.layer(l)
      if layer and layer.type() == QgsMapLayer.RasterLayer:
        extent = layer.extent()
        if pt.x() > extent.xMinimum() and pt.x() < extent.xMaximum() and \
           pt.y() > extent.yMinimum() and pt.y() < extent.yMaximum():
          if infoString == None:
            infoString = QString("The following raster layers intersect::\n")
          infoString.append(layer.source()).append(QString("\n"))
    if infoString != None:
      QMessageBox.information(self.iface.mainWindow(),
                              "Raster File Info",infoString)
    else:
      QMessageBox.information(self.iface.mainWindow(),
                              "Raster File Info","No Raster Layers Intersect")
    self.canvas.setMapTool(self.saveTool)
    QObject.disconnect(self.tool, SIGNAL("finished(PyQt_PyObject)"),
                       self.finished)
   
