giface.py 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  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 generaly 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,
  53. opacity=1.0, cmd=None):
  54. """Adds a new layer to the layer list.
  55. Launches property dialog if needed (raster, vector, etc.)
  56. :param ltype: layer type (raster, vector, raster_3d, ...)
  57. :param name: layer name
  58. :param checked: if True layer is checked
  59. :param opacity: layer opacity level
  60. :param cmd: command (given as a list)
  61. """
  62. raise NotImplementedError()
  63. def GetLayersByName(self, name):
  64. """Returns list of layers with a given name.
  65. :param name: fully qualified map name
  66. .. todo::
  67. if common usage is just to check the presence of layer,
  68. intoroduce a new method ContainsLayerByName(name)
  69. """
  70. raise NotImplementedError()
  71. def GetLayerByData(self, key, value):
  72. """Returns layer with specified.
  73. .. note::
  74. Returns only one layer. This might change.
  75. .. warning::
  76. Avoid using this method, it might be removed in the future.
  77. """
  78. raise NotImplementedError()
  79. class GrassInterface:
  80. """GrassInterface provides the functionality which should be available
  81. to every GUI component.
  82. .. note::
  83. The GrassInterface process is not finished.
  84. """
  85. def RunCmd(self, *args, **kwargs):
  86. """Executes a command.
  87. """
  88. raise NotImplementedError()
  89. def Help(self, entry):
  90. """Shows a manual page for a given entry.
  91. """
  92. raise NotImplementedError()
  93. def WriteLog(self, text, wrap=None, notification=Notification.HIGHLIGHT):
  94. """Writes log message.
  95. """
  96. raise NotImplementedError()
  97. def WriteCmdLog(self, text, pid=None,
  98. notification=Notification.MAKE_VISIBLE):
  99. """Writes message related to start or end of the command.
  100. """
  101. raise NotImplementedError()
  102. def WriteWarning(self, text):
  103. """Writes warning message for the user.
  104. """
  105. raise NotImplementedError()
  106. def WriteError(self, text):
  107. """Writes error message for the user."""
  108. raise NotImplementedError()
  109. def GetLayerTree(self):
  110. """Returns LayerManager's tree GUI object.
  111. .. note::
  112. Will be removed from the interface.
  113. """
  114. raise NotImplementedError()
  115. def GetLayerList(self):
  116. """Returns a layer management object.
  117. """
  118. raise NotImplementedError()
  119. def GetMapDisplay(self):
  120. """Returns current map display.
  121. .. note::
  122. For layer related tasks use GetLayerList().
  123. :return: MapFrame instance
  124. :return: None when no mapdisplay open
  125. """
  126. raise NotImplementedError()
  127. def GetAllMapDisplays(self):
  128. """Get list of all map displays.
  129. .. note::
  130. Might be removed from the interface.
  131. :return: list of MapFrame instances
  132. """
  133. raise NotImplementedError()
  134. def GetMapWindow(self):
  135. """Returns current map window.
  136. .. note::
  137. For layer related tasks use GetLayerList().
  138. """
  139. raise NotImplementedError()
  140. def GetProgress(self):
  141. """Returns object which shows the progress.
  142. .. note::
  143. Some implementations may not implement this method.
  144. """
  145. raise NotImplementedError()
  146. class StandaloneGrassInterface():
  147. """@implements GrassInterface"""
  148. def __init__(self):
  149. # Signal when some map is created or updated by a module.
  150. # attributes: name: map name, ltype: map type,
  151. # add: if map should be added to layer tree (questionable attribute)
  152. self.mapCreated = Signal('StandaloneGrassInterface.mapCreated')
  153. # Signal emitted to request updating of map
  154. self.updateMap = Signal('StandaloneGrassInterface.updateMap')
  155. # workaround, standalone grass interface should be moved to sep. file
  156. from core.gconsole import GConsole, \
  157. EVT_CMD_OUTPUT, EVT_CMD_PROGRESS
  158. self._gconsole = GConsole()
  159. self._gconsole.Bind(EVT_CMD_PROGRESS, self._onCmdProgress)
  160. self._gconsole.Bind(EVT_CMD_OUTPUT, self._onCmdOutput)
  161. self._gconsole.writeLog.connect(self.WriteLog)
  162. self._gconsole.writeCmdLog.connect(self.WriteCmdLog)
  163. self._gconsole.writeWarning.connect(self.WriteWarning)
  164. self._gconsole.writeError.connect(self.WriteError)
  165. def _onCmdOutput(self, event):
  166. """Print command output"""
  167. message = event.text
  168. style = event.type
  169. if style == 'warning':
  170. self.WriteWarning(message)
  171. elif style == 'error':
  172. self.WriteError(message)
  173. else:
  174. self.WriteLog(message)
  175. event.Skip()
  176. def _onCmdProgress(self, event):
  177. """Update progress message info"""
  178. grass.percent(event.value, 100, 1)
  179. event.Skip()
  180. def RunCmd(self, command, compReg=True, env=None, skipInterface=False,
  181. onDone=None, onPrepare=None, userData=None, addLayer=None,
  182. notification=Notification.MAKE_VISIBLE):
  183. self._gconsole.RunCmd(
  184. command=command,
  185. compReg=compReg,
  186. env=env,
  187. skipInterface=skipInterface,
  188. onDone=onDone,
  189. onPrepare=onPrepare,
  190. userData=userData,
  191. addLayer=addLayer,
  192. notification=notification)
  193. def Help(self, entry):
  194. self._gconsole.RunCmd(['g.manual', 'entry=%s' % entry])
  195. def WriteLog(self, text, wrap=None,
  196. notification=Notification.HIGHLIGHT):
  197. self._write(grass.message, text)
  198. def WriteCmdLog(self, text, pid=None,
  199. notification=Notification.MAKE_VISIBLE):
  200. if pid:
  201. text = '(' + str(pid) + ') ' + text
  202. self._write(grass.message, text)
  203. def WriteWarning(self, text):
  204. self._write(grass.warning, text)
  205. def WriteError(self, text):
  206. self._write(grass.error, text)
  207. def _write(self, function, text):
  208. orig = os.getenv("GRASS_MESSAGE_FORMAT")
  209. os.environ["GRASS_MESSAGE_FORMAT"] = 'standard'
  210. function(text)
  211. os.environ["GRASS_MESSAGE_FORMAT"] = orig
  212. def GetLayerList(self):
  213. return []
  214. def GetLayerTree(self):
  215. return None
  216. def GetMapDisplay(self):
  217. """Get current map display.
  218. """
  219. return None
  220. def GetAllMapDisplays(self):
  221. """Get list of all map displays.
  222. """
  223. return []
  224. def GetMapWindow(self):
  225. raise NotImplementedError()
  226. def GetProgress(self):
  227. # TODO: implement some progress with same inface as gui one
  228. # (probably using g.message or similarly to Write... functions)
  229. raise NotImplementedError()
  230. def UpdateCmdHistory(self, cmd):
  231. raise NotImplementedError()