qgis_interface.py 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. # coding=utf-8
  2. """QGIS plugin implementation.
  3. .. note:: This program is free software; you can redistribute it and/or modify
  4. it under the terms of the GNU General Public License as published by
  5. the Free Software Foundation; either version 2 of the License, or
  6. (at your option) any later version.
  7. .. note:: This source code was copied from the 'postgis viewer' application
  8. with original authors:
  9. Copyright (c) 2010 by Ivan Mincik, ivan.mincik@gista.sk
  10. Copyright (c) 2011 German Carrillo, geotux_tuxman@linuxmail.org
  11. Copyright (c) 2014 Tim Sutton, tim@linfiniti.com
  12. """
  13. __author__ = 'tim@linfiniti.com'
  14. __revision__ = '$Format:%H$'
  15. __date__ = '10/01/2011'
  16. __copyright__ = (
  17. 'Copyright (c) 2010 by Ivan Mincik, ivan.mincik@gista.sk and '
  18. 'Copyright (c) 2011 German Carrillo, geotux_tuxman@linuxmail.org'
  19. 'Copyright (c) 2014 Tim Sutton, tim@linfiniti.com'
  20. )
  21. import logging
  22. from PyQt5.QtCore import QObject, pyqtSlot, pyqtSignal
  23. from qgis.core import QgsMapLayerRegistry
  24. from qgis.gui import QgsMapCanvasLayer
  25. LOGGER = logging.getLogger('QGIS')
  26. #noinspection PyMethodMayBeStatic,PyPep8Naming
  27. class QgisInterface(QObject):
  28. """Class to expose QGIS objects and functions to plugins.
  29. This class is here for enabling us to run unit tests only,
  30. so most methods are simply stubs.
  31. """
  32. currentLayerChanged = pyqtSignal(QgsMapCanvasLayer)
  33. def __init__(self, canvas):
  34. """Constructor
  35. :param canvas:
  36. """
  37. QObject.__init__(self)
  38. self.canvas = canvas
  39. # Set up slots so we can mimic the behaviour of QGIS when layers
  40. # are added.
  41. LOGGER.debug('Initialising canvas...')
  42. # noinspection PyArgumentList
  43. QgsMapLayerRegistry.instance().layersAdded.connect(self.addLayers)
  44. # noinspection PyArgumentList
  45. QgsMapLayerRegistry.instance().layerWasAdded.connect(self.addLayer)
  46. # noinspection PyArgumentList
  47. QgsMapLayerRegistry.instance().removeAll.connect(self.removeAllLayers)
  48. # For processing module
  49. self.destCrs = None
  50. @pyqtSlot('QStringList')
  51. def addLayers(self, layers):
  52. """Handle layers being added to the registry so they show up in canvas.
  53. :param layers: list<QgsMapLayer> list of map layers that were added
  54. .. note:: The QgsInterface api does not include this method,
  55. it is added here as a helper to facilitate testing.
  56. """
  57. #LOGGER.debug('addLayers called on qgis_interface')
  58. #LOGGER.debug('Number of layers being added: %s' % len(layers))
  59. #LOGGER.debug('Layer Count Before: %s' % len(self.canvas.layers()))
  60. current_layers = self.canvas.layers()
  61. final_layers = []
  62. for layer in current_layers:
  63. final_layers.append(QgsMapCanvasLayer(layer))
  64. for layer in layers:
  65. final_layers.append(QgsMapCanvasLayer(layer))
  66. self.canvas.setLayerSet(final_layers)
  67. #LOGGER.debug('Layer Count After: %s' % len(self.canvas.layers()))
  68. @pyqtSlot('QgsMapLayer')
  69. def addLayer(self, layer):
  70. """Handle a layer being added to the registry so it shows up in canvas.
  71. :param layer: list<QgsMapLayer> list of map layers that were added
  72. .. note: The QgsInterface api does not include this method, it is added
  73. here as a helper to facilitate testing.
  74. .. note: The addLayer method was deprecated in QGIS 1.8 so you should
  75. not need this method much.
  76. """
  77. pass
  78. @pyqtSlot()
  79. def removeAllLayers(self):
  80. """Remove layers from the canvas before they get deleted."""
  81. self.canvas.setLayerSet([])
  82. def newProject(self):
  83. """Create new project."""
  84. # noinspection PyArgumentList
  85. QgsMapLayerRegistry.instance().removeAllMapLayers()
  86. # ---------------- API Mock for QgsInterface follows -------------------
  87. def zoomFull(self):
  88. """Zoom to the map full extent."""
  89. pass
  90. def zoomToPrevious(self):
  91. """Zoom to previous view extent."""
  92. pass
  93. def zoomToNext(self):
  94. """Zoom to next view extent."""
  95. pass
  96. def zoomToActiveLayer(self):
  97. """Zoom to extent of active layer."""
  98. pass
  99. def addVectorLayer(self, path, base_name, provider_key):
  100. """Add a vector layer.
  101. :param path: Path to layer.
  102. :type path: str
  103. :param base_name: Base name for layer.
  104. :type base_name: str
  105. :param provider_key: Provider key e.g. 'ogr'
  106. :type provider_key: str
  107. """
  108. pass
  109. def addRasterLayer(self, path, base_name):
  110. """Add a raster layer given a raster layer file name
  111. :param path: Path to layer.
  112. :type path: str
  113. :param base_name: Base name for layer.
  114. :type base_name: str
  115. """
  116. pass
  117. def activeLayer(self):
  118. """Get pointer to the active layer (layer selected in the legend)."""
  119. # noinspection PyArgumentList
  120. layers = QgsMapLayerRegistry.instance().mapLayers()
  121. for item in layers:
  122. return layers[item]
  123. def addToolBarIcon(self, action):
  124. """Add an icon to the plugins toolbar.
  125. :param action: Action to add to the toolbar.
  126. :type action: QAction
  127. """
  128. pass
  129. def removeToolBarIcon(self, action):
  130. """Remove an action (icon) from the plugin toolbar.
  131. :param action: Action to add to the toolbar.
  132. :type action: QAction
  133. """
  134. pass
  135. def addToolBar(self, name):
  136. """Add toolbar with specified name.
  137. :param name: Name for the toolbar.
  138. :type name: str
  139. """
  140. pass
  141. def mapCanvas(self):
  142. """Return a pointer to the map canvas."""
  143. return self.canvas
  144. def mainWindow(self):
  145. """Return a pointer to the main window.
  146. In case of QGIS it returns an instance of QgisApp.
  147. """
  148. pass
  149. def addDockWidget(self, area, dock_widget):
  150. """Add a dock widget to the main window.
  151. :param area: Where in the ui the dock should be placed.
  152. :type area:
  153. :param dock_widget: A dock widget to add to the UI.
  154. :type dock_widget: QDockWidget
  155. """
  156. pass
  157. def legendInterface(self):
  158. """Get the legend."""
  159. return self.canvas