giface.py 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341
  1. """
  2. @package core.giface
  3. @brief GRASS interface for standalone application (without layer manager)
  4. Classes:
  5. - giface::StandaloneGrassInterface
  6. (C) 2012-2014 by the GRASS Development Team
  7. This program is free software under the GNU General Public License
  8. (>=v2). Read the file COPYING that comes with GRASS for details.
  9. @author Anna Kratochvilova <kratochanna gmail.com>
  10. @author Vaclav Petras <wenzeslaus gmail.com>
  11. """
  12. import os
  13. import grass.script as grass
  14. from grass.pydispatch.signal import Signal
  15. # to disable Abstract class not referenced
  16. # pylint: disable=R0921
  17. class Notification:
  18. """Enum class for notifications suggestions.
  19. Can be used for log messages, commands, warnings, errors.
  20. The value is the suggestion how user should be notified
  21. about the new message.
  22. """
  23. NO_NOTIFICATION = 0
  24. HIGHLIGHT = 1
  25. MAKE_VISIBLE = 2
  26. RAISE_WINDOW = 3
  27. class Layer(object):
  28. """Layer is generally usable layer object.
  29. .. note::
  30. Currently without specifying the interface.
  31. Current implementations only provides all attributes of existing
  32. layer as used in lmgr.
  33. """
  34. pass
  35. class LayerList(object):
  36. def GetSelectedLayers(self, checkedOnly=True):
  37. """Returns list of selected layers.
  38. .. note::
  39. Usage of checked and selected is still subject to change.
  40. Checked layers should be showed. Selected are for analyses.
  41. However, this may be the same for some implementations
  42. (e.g. it d.mon has all layers checked and selected).
  43. """
  44. raise NotImplementedError()
  45. def GetSelectedLayer(self, checkedOnly=False):
  46. """Returns selected layer or None when there is no selected layer.
  47. .. note::
  48. Parameter checkedOnly is here False by default. This might
  49. change if we find the right way of handling unchecked layers.
  50. """
  51. raise NotImplementedError()
  52. def AddLayer(self, ltype, name=None, checked=None, opacity=1.0, cmd=None):
  53. """Adds a new layer to the layer list.
  54. Launches property dialog if needed (raster, vector, etc.)
  55. :param ltype: layer type (raster, vector, raster_3d, ...)
  56. :param name: layer name
  57. :param checked: if True layer is checked
  58. :param opacity: layer opacity level
  59. :param cmd: command (given as a list)
  60. """
  61. raise NotImplementedError()
  62. def GetLayersByName(self, name):
  63. """Returns list of layers with a given name.
  64. :param name: fully qualified map name
  65. .. todo::
  66. if common usage is just to check the presence of layer,
  67. intoroduce a new method ContainsLayerByName(name)
  68. """
  69. raise NotImplementedError()
  70. def GetLayerByData(self, key, value):
  71. """Returns layer with specified.
  72. .. note::
  73. Returns only one layer. This might change.
  74. .. warning::
  75. Avoid using this method, it might be removed in the future.
  76. """
  77. raise NotImplementedError()
  78. class GrassInterface:
  79. """GrassInterface provides the functionality which should be available
  80. to every GUI component.
  81. .. note::
  82. The GrassInterface process is not finished.
  83. """
  84. def RunCmd(self, *args, **kwargs):
  85. """Executes a command."""
  86. raise NotImplementedError()
  87. def Help(self, entry):
  88. """Shows a manual page for a given entry."""
  89. raise NotImplementedError()
  90. def WriteLog(self, text, wrap=None, notification=Notification.HIGHLIGHT):
  91. """Writes log message."""
  92. raise NotImplementedError()
  93. def WriteCmdLog(self, text, pid=None, notification=Notification.MAKE_VISIBLE):
  94. """Writes message related to start or end of the command."""
  95. raise NotImplementedError()
  96. def WriteWarning(self, text):
  97. """Writes warning message for the user."""
  98. raise NotImplementedError()
  99. def WriteError(self, text):
  100. """Writes error message for the user."""
  101. raise NotImplementedError()
  102. def GetLayerTree(self):
  103. """Returns LayerManager's tree GUI object.
  104. .. note::
  105. Will be removed from the interface.
  106. """
  107. raise NotImplementedError()
  108. def GetLayerList(self):
  109. """Returns a layer management object."""
  110. raise NotImplementedError()
  111. def GetMapDisplay(self):
  112. """Returns current map display.
  113. .. note::
  114. For layer related tasks use GetLayerList().
  115. :return: MapFrame instance
  116. :return: None when no mapdisplay open
  117. """
  118. raise NotImplementedError()
  119. def GetAllMapDisplays(self):
  120. """Get list of all map displays.
  121. .. note::
  122. Might be removed from the interface.
  123. :return: list of MapFrame instances
  124. """
  125. raise NotImplementedError()
  126. def GetMapWindow(self):
  127. """Returns current map window.
  128. .. note::
  129. For layer related tasks use GetLayerList().
  130. """
  131. raise NotImplementedError()
  132. def GetProgress(self):
  133. """Returns object which shows the progress.
  134. .. note::
  135. Some implementations may not implement this method.
  136. """
  137. raise NotImplementedError()
  138. def UpdateCmdHistory(self, cmd):
  139. """Add the command to the current history list shown to the user
  140. .. note::
  141. Some implementations may not implement this method or do nothing.
  142. """
  143. raise NotImplementedError()
  144. class StandaloneGrassInterface(GrassInterface):
  145. """@implements GrassInterface"""
  146. def __init__(self):
  147. # Signal when some map is created or updated by a module.
  148. # Used for adding/refreshing displayed layers.
  149. # attributes: name: map name, ltype: map type,
  150. # add: if map should be added to layer tree (questionable attribute)
  151. self.mapCreated = Signal("StandaloneGrassInterface.mapCreated")
  152. # Signal for communicating current mapset has been switched
  153. self.currentMapsetChanged = Signal(
  154. "StandaloneGrassInterface.currentMapsetChanged"
  155. )
  156. # Signal for communicating something in current grassdb has changed.
  157. # Parameters:
  158. # action: required, is one of 'new', 'rename', 'delete'
  159. # element: required, can be one of 'grassdb', 'location', 'mapset', 'raster', 'vector' and 'raster_3d'
  160. # grassdb: path to grass db, required
  161. # location: location name, required
  162. # mapset: mapset name, required when element is 'mapset', 'raster', 'vector' or 'raster_3d'
  163. # map: map name, required when element is 'raster', 'vector' or 'raster_3d'
  164. # newname: new name (of mapset, map), required with action='rename'
  165. self.grassdbChanged = Signal("StandaloneGrassInterface.grassdbChanged")
  166. # Signal emitted to request updating of map
  167. self.updateMap = Signal("StandaloneGrassInterface.updateMap")
  168. # Signal emitted when workspace is changed
  169. self.workspaceChanged = Signal("StandaloneGrassInterface.workspaceChanged")
  170. # workaround, standalone grass interface should be moved to sep. file
  171. from core.gconsole import GConsole, EVT_CMD_OUTPUT, EVT_CMD_PROGRESS
  172. self._gconsole = GConsole()
  173. self._gconsole.Bind(EVT_CMD_PROGRESS, self._onCmdProgress)
  174. self._gconsole.Bind(EVT_CMD_OUTPUT, self._onCmdOutput)
  175. self._gconsole.writeLog.connect(self.WriteLog)
  176. self._gconsole.writeCmdLog.connect(self.WriteCmdLog)
  177. self._gconsole.writeWarning.connect(self.WriteWarning)
  178. self._gconsole.writeError.connect(self.WriteError)
  179. def _onCmdOutput(self, event):
  180. """Print command output"""
  181. message = event.text
  182. style = event.type
  183. if style == "warning":
  184. self.WriteWarning(message)
  185. elif style == "error":
  186. self.WriteError(message)
  187. else:
  188. self.WriteLog(message)
  189. event.Skip()
  190. def _onCmdProgress(self, event):
  191. """Update progress message info"""
  192. grass.percent(event.value, 100, 1)
  193. event.Skip()
  194. def RunCmd(
  195. self,
  196. command,
  197. compReg=True,
  198. env=None,
  199. skipInterface=False,
  200. onDone=None,
  201. onPrepare=None,
  202. userData=None,
  203. addLayer=None,
  204. notification=Notification.MAKE_VISIBLE,
  205. ):
  206. self._gconsole.RunCmd(
  207. command=command,
  208. compReg=compReg,
  209. env=env,
  210. skipInterface=skipInterface,
  211. onDone=onDone,
  212. onPrepare=onPrepare,
  213. userData=userData,
  214. addLayer=addLayer,
  215. notification=notification,
  216. )
  217. def Help(self, entry):
  218. self._gconsole.RunCmd(["g.manual", "entry=%s" % entry])
  219. def WriteLog(self, text, wrap=None, notification=Notification.HIGHLIGHT):
  220. self._write(grass.message, text)
  221. def WriteCmdLog(self, text, pid=None, notification=Notification.MAKE_VISIBLE):
  222. if pid:
  223. text = "(" + str(pid) + ") " + text
  224. self._write(grass.message, text)
  225. def WriteWarning(self, text):
  226. self._write(grass.warning, text)
  227. def WriteError(self, text):
  228. self._write(grass.error, text)
  229. def _write(self, function, text):
  230. orig = os.getenv("GRASS_MESSAGE_FORMAT")
  231. os.environ["GRASS_MESSAGE_FORMAT"] = "standard"
  232. function(text)
  233. os.environ["GRASS_MESSAGE_FORMAT"] = orig
  234. def GetLayerList(self):
  235. return []
  236. def GetLayerTree(self):
  237. return None
  238. def GetMapDisplay(self):
  239. """Get current map display."""
  240. return None
  241. def GetAllMapDisplays(self):
  242. """Get list of all map displays."""
  243. return []
  244. def GetMapWindow(self):
  245. raise NotImplementedError()
  246. def GetProgress(self):
  247. # TODO: implement some progress with same inface as gui one
  248. # (probably using g.message or similarly to Write... functions)
  249. raise NotImplementedError()
  250. def UpdateCmdHistory(self, cmd):
  251. """There is no history displayed to the user, doing nothing"""
  252. pass