giface.py 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338
  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. def UpdateCmdHistory(self, cmd):
  147. """Add the command to the current history list shown to the user
  148. .. note::
  149. Some implementations may not implement this method or do nothing.
  150. """
  151. raise NotImplementedError()
  152. class StandaloneGrassInterface(GrassInterface):
  153. """@implements GrassInterface"""
  154. def __init__(self):
  155. # Signal when some map is created or updated by a module.
  156. # Used for adding/refreshing displayed layers.
  157. # attributes: name: map name, ltype: map type,
  158. # add: if map should be added to layer tree (questionable attribute)
  159. self.mapCreated = Signal('StandaloneGrassInterface.mapCreated')
  160. # Signal for communicating current mapset has been switched
  161. self.currentMapsetChanged = Signal('StandaloneGrassInterface.currentMapsetChanged')
  162. # Signal for communicating something in current grassdb has changed.
  163. # Parameters:
  164. # action: required, is one of 'new', 'rename', 'delete'
  165. # element: required, can be one of 'grassdb', 'location', 'mapset', 'raster', 'vector' and 'raster_3d'
  166. # grassdb: path to grass db, required
  167. # location: location name, required
  168. # mapset: mapset name, required when element is 'mapset', 'raster', 'vector' or 'raster_3d'
  169. # map: map name, required when element is 'raster', 'vector' or 'raster_3d'
  170. # newname: new name (of mapset, map), required with action='rename'
  171. self.grassdbChanged = Signal('StandaloneGrassInterface.grassdbChanged')
  172. # Signal emitted to request updating of map
  173. self.updateMap = Signal('StandaloneGrassInterface.updateMap')
  174. # workaround, standalone grass interface should be moved to sep. file
  175. from core.gconsole import GConsole, \
  176. EVT_CMD_OUTPUT, EVT_CMD_PROGRESS
  177. self._gconsole = GConsole()
  178. self._gconsole.Bind(EVT_CMD_PROGRESS, self._onCmdProgress)
  179. self._gconsole.Bind(EVT_CMD_OUTPUT, self._onCmdOutput)
  180. self._gconsole.writeLog.connect(self.WriteLog)
  181. self._gconsole.writeCmdLog.connect(self.WriteCmdLog)
  182. self._gconsole.writeWarning.connect(self.WriteWarning)
  183. self._gconsole.writeError.connect(self.WriteError)
  184. def _onCmdOutput(self, event):
  185. """Print command output"""
  186. message = event.text
  187. style = event.type
  188. if style == 'warning':
  189. self.WriteWarning(message)
  190. elif style == 'error':
  191. self.WriteError(message)
  192. else:
  193. self.WriteLog(message)
  194. event.Skip()
  195. def _onCmdProgress(self, event):
  196. """Update progress message info"""
  197. grass.percent(event.value, 100, 1)
  198. event.Skip()
  199. def RunCmd(self, command, compReg=True, env=None, skipInterface=False,
  200. onDone=None, onPrepare=None, userData=None, addLayer=None,
  201. notification=Notification.MAKE_VISIBLE):
  202. self._gconsole.RunCmd(
  203. command=command,
  204. compReg=compReg,
  205. env=env,
  206. skipInterface=skipInterface,
  207. onDone=onDone,
  208. onPrepare=onPrepare,
  209. userData=userData,
  210. addLayer=addLayer,
  211. notification=notification)
  212. def Help(self, entry):
  213. self._gconsole.RunCmd(['g.manual', 'entry=%s' % entry])
  214. def WriteLog(self, text, wrap=None,
  215. notification=Notification.HIGHLIGHT):
  216. self._write(grass.message, text)
  217. def WriteCmdLog(self, text, pid=None,
  218. notification=Notification.MAKE_VISIBLE):
  219. if pid:
  220. text = '(' + str(pid) + ') ' + text
  221. self._write(grass.message, text)
  222. def WriteWarning(self, text):
  223. self._write(grass.warning, text)
  224. def WriteError(self, text):
  225. self._write(grass.error, text)
  226. def _write(self, function, text):
  227. orig = os.getenv("GRASS_MESSAGE_FORMAT")
  228. os.environ["GRASS_MESSAGE_FORMAT"] = 'standard'
  229. function(text)
  230. os.environ["GRASS_MESSAGE_FORMAT"] = orig
  231. def GetLayerList(self):
  232. return []
  233. def GetLayerTree(self):
  234. return None
  235. def GetMapDisplay(self):
  236. """Get current map display.
  237. """
  238. return None
  239. def GetAllMapDisplays(self):
  240. """Get list of all map displays.
  241. """
  242. return []
  243. def GetMapWindow(self):
  244. raise NotImplementedError()
  245. def GetProgress(self):
  246. # TODO: implement some progress with same inface as gui one
  247. # (probably using g.message or similarly to Write... functions)
  248. raise NotImplementedError()
  249. def UpdateCmdHistory(self, cmd):
  250. """There is no history displayed to the user, doing nothing"""
  251. pass