Advertisement
xunilk

where_ami.py

Aug 20th, 2015
517
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 7.50 KB | None | 0 0
  1. # -*- coding: utf-8 -*-
  2. """
  3. /***************************************************************************
  4. WhereAmI
  5.                                 A QGIS plugin
  6. Show location of a point click on the map
  7.                              -------------------
  8.        begin                : 2015-08-20
  9.        git sha              : $Format:%H$
  10.        copyright            : (C) 2015 by gsherman
  11.        email                : gsherman@geoapt.com
  12. ***************************************************************************/
  13.  
  14. /***************************************************************************
  15. *                                                                         *
  16. *   This program is free software; you can redistribute it and/or modify  *
  17. *   it under the terms of the GNU General Public License as published by  *
  18. *   the Free Software Foundation; either version 2 of the License, or     *
  19. *   (at your option) any later version.                                   *
  20. *                                                                         *
  21. ***************************************************************************/
  22. """
  23. from PyQt4.QtCore import QSettings, QTranslator, qVersion, QCoreApplication
  24. from PyQt4.QtGui import QAction, QIcon, QMessageBox
  25.  
  26. from qgis.gui import *  #(3)
  27.  
  28. # Initialize Qt resources from file resources.py
  29. import resources_rc
  30. # Import the code for the dialog
  31. from where_ami_dialog import WhereAmIDialog
  32. import os.path
  33.  
  34.  
  35. class WhereAmI:
  36.     """QGIS Plugin Implementation."""
  37.  
  38.     def __init__(self, iface):
  39.         """Constructor.
  40.  
  41.        :param iface: An interface instance that will be passed to this class
  42.            which provides the hook by which you can manipulate the QGIS
  43.            application at run time.
  44.        :type iface: QgsInterface
  45.        """
  46.         # Save reference to the QGIS interface
  47.         self.iface = iface
  48.        
  49.         # a reference to our map canvas  (1)
  50.         self.canvas = self.iface.mapCanvas()
  51.         # this QGIS tool emits as QgsPoint after each click on the map canvas (2)
  52.         self.pointTool = QgsMapToolEmitPoint(self.canvas)
  53.        
  54.         # initialize plugin directory
  55.         self.plugin_dir = os.path.dirname(__file__)
  56.         # initialize locale
  57.         locale = QSettings().value('locale/userLocale')[0:2]
  58.         locale_path = os.path.join(
  59.             self.plugin_dir,
  60.             'i18n',
  61.             'WhereAmI_{}.qm'.format(locale))
  62.  
  63.         if os.path.exists(locale_path):
  64.             self.translator = QTranslator()
  65.             self.translator.load(locale_path)
  66.  
  67.             if qVersion() > '4.3.3':
  68.                 QCoreApplication.installTranslator(self.translator)
  69.  
  70.         # Create the dialog (after translation) and keep reference
  71.         self.dlg = WhereAmIDialog()
  72.  
  73.         # Declare instance attributes
  74.         self.actions = []
  75.         self.menu = self.tr(u'&Where Am I?')
  76.         # TODO: We are going to let the user set this up in a future iteration
  77.         self.toolbar = self.iface.addToolBar(u'WhereAmI')
  78.         self.toolbar.setObjectName(u'WhereAmI')
  79.  
  80.     # noinspection PyMethodMayBeStatic
  81.     def tr(self, message):
  82.         """Get the translation for a string using Qt translation API.
  83.  
  84.        We implement this ourselves since we do not inherit QObject.
  85.  
  86.        :param message: String for translation.
  87.        :type message: str, QString
  88.  
  89.        :returns: Translated version of message.
  90.        :rtype: QString
  91.        """
  92.         # noinspection PyTypeChecker,PyArgumentList,PyCallByClass
  93.         return QCoreApplication.translate('WhereAmI', message)
  94.  
  95.  
  96.     def add_action(
  97.         self,
  98.         icon_path,
  99.         text,
  100.         callback,
  101.         enabled_flag=True,
  102.         add_to_menu=True,
  103.         add_to_toolbar=True,
  104.         status_tip=None,
  105.         whats_this=None,
  106.         parent=None):
  107.         """Add a toolbar icon to the toolbar.
  108.  
  109.        :param icon_path: Path to the icon for this action. Can be a resource
  110.            path (e.g. ':/plugins/foo/bar.png') or a normal file system path.
  111.        :type icon_path: str
  112.  
  113.        :param text: Text that should be shown in menu items for this action.
  114.        :type text: str
  115.  
  116.        :param callback: Function to be called when the action is triggered.
  117.        :type callback: function
  118.  
  119.        :param enabled_flag: A flag indicating if the action should be enabled
  120.            by default. Defaults to True.
  121.        :type enabled_flag: bool
  122.  
  123.        :param add_to_menu: Flag indicating whether the action should also
  124.            be added to the menu. Defaults to True.
  125.        :type add_to_menu: bool
  126.  
  127.        :param add_to_toolbar: Flag indicating whether the action should also
  128.            be added to the toolbar. Defaults to True.
  129.        :type add_to_toolbar: bool
  130.  
  131.        :param status_tip: Optional text to show in a popup when mouse pointer
  132.            hovers over the action.
  133.        :type status_tip: str
  134.  
  135.        :param parent: Parent widget for the new action. Defaults None.
  136.        :type parent: QWidget
  137.  
  138.        :param whats_this: Optional text to show in the status bar when the
  139.            mouse pointer hovers over the action.
  140.  
  141.        :returns: The action that was created. Note that the action is also
  142.            added to self.actions list.
  143.        :rtype: QAction
  144.        """
  145.  
  146.         icon = QIcon(icon_path)
  147.         action = QAction(icon, text, parent)
  148.         action.triggered.connect(callback)
  149.         action.setEnabled(enabled_flag)
  150.  
  151.         if status_tip is not None:
  152.             action.setStatusTip(status_tip)
  153.  
  154.         if whats_this is not None:
  155.             action.setWhatsThis(whats_this)
  156.  
  157.         if add_to_toolbar:
  158.             self.toolbar.addAction(action)
  159.  
  160.         if add_to_menu:
  161.             self.iface.addPluginToMenu(
  162.                 self.menu,
  163.                 action)
  164.  
  165.         self.actions.append(action)
  166.  
  167.         return action
  168.  
  169.     def initGui(self):
  170.         """Create the menu entries and toolbar icons inside the QGIS GUI."""
  171.  
  172.         icon_path = ':/plugins/WhereAmI/WhereAmI.png'
  173.         self.add_action(
  174.             icon_path,
  175.             text=self.tr(u'Where Am I?'),
  176.             callback=self.run,
  177.             parent=self.iface.mainWindow())
  178.  
  179.         result = self.pointTool.canvasClicked.connect(self.display_point)  #4
  180.         QMessageBox.information( self.iface.mainWindow(),"Info", "connect = %s"%str(result) )    #5
  181.  
  182.  
  183.     def unload(self):
  184.         """Removes the plugin menu item and icon from QGIS GUI."""
  185.         for action in self.actions:
  186.             self.iface.removePluginMenu(
  187.                 self.tr(u'&Where Am I?'),
  188.                 action)
  189.             self.iface.removeToolBarIcon(action)
  190.         # remove the toolbar
  191.         del self.toolbar
  192.  
  193.  
  194.     def display_point(self, point, button):   #6
  195.         # report map coordinates from a canvas click
  196.         self.dlg.hide()
  197.         coords = "{}, {}".format(point.x(), point.y())
  198.         self.dlg.lineEdit.setText(str(coords))  #it is self.dlg.lineEdit #7
  199.         self.dlg.show()
  200.  
  201.     def run(self):
  202.         """Run method that performs all the real work"""
  203.         # make our clickTool the tool that we'll use for now    #8
  204.         self.canvas.setMapTool(self.pointTool)
  205.         # show the dialog
  206.         self.dlg.show()
  207.         # Run the dialog event loop
  208.         result = self.dlg.exec_()
  209.         # See if OK was pressed
  210.         if result:
  211.             # Do something useful here - delete the line containing pass and
  212.             # substitute with your code.
  213.             pass
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement