giface.py 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  1. """!
  2. @package core.giface
  3. @brief GRASS interface for standalone application (without layer manager)
  4. Classes:
  5. - giface::StandaloneGrassInterface
  6. (C) 2012 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. from core.utils import _
  14. import grass.script as grass
  15. from grass.pydispatch.signal import Signal
  16. # to disable Abstract class not referenced
  17. #pylint: disable=R0921
  18. class Notification:
  19. """!Enum class for notifications suggestions.
  20. Can be used for log messages, commands, warnings, errors.
  21. The value is the suggestion how user should be notified
  22. about the new message.
  23. """
  24. NO_NOTIFICATION = 0
  25. HIGHLIGHT = 1
  26. MAKE_VISIBLE = 2
  27. RAISE_WINDOW = 3
  28. class Layer(object):
  29. """!Layer is generaly usable layer object.
  30. @note Currently without specifying the interface.
  31. Current implementations only provides all attributes of existing layer
  32. 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 Usage of checked and selected is still subject to change.
  39. Checked layers should be showed. Selected are for analyses.
  40. However, this may be the same for some implementations
  41. (e.g. it d.mon has all layers checked and selected).
  42. """
  43. raise NotImplementedError()
  44. def GetSelectedLayer(self, checkedOnly=False):
  45. """!Returns selected layer or None when there is no selected layer.
  46. @note Parameter checkedOnly is here False by default. This might change
  47. if we find the right way of handling unchecked layers.
  48. """
  49. raise NotImplementedError()
  50. def AddLayer(self, ltype, name=None, checked=None,
  51. opacity=1.0, cmd=None):
  52. """!Adds a new layer to the layer list.
  53. Launches property dialog if needed (raster, vector, etc.)
  54. @param ltype layer type (raster, vector, 3d-raster, ...)
  55. @param name layer name
  56. @param checked if True layer is checked
  57. @param opacity layer opacity level
  58. @param cmd command (given as a list)
  59. """
  60. raise NotImplementedError()
  61. def GetLayersByName(self, name):
  62. """!Returns list of layers with a given name.
  63. @param name fully qualified map name
  64. @todo if common usage is just to check the presence of layer,
  65. intoroduce a new method ContainsLayerByName(name)
  66. """
  67. raise NotImplementedError()
  68. def GetLayerByData(self, key, value):
  69. """!Returns layer with specified.
  70. @note Returns only one layer. This might change.
  71. @warning Avoid using this method, it might be removed in the future.
  72. """
  73. raise NotImplementedError()
  74. class GrassInterface:
  75. """!GrassInterface provides the functionality which should be available
  76. to every GUI component.
  77. @note The GrassInterface process is not finished.
  78. """
  79. def RunCmd(self, *args, **kwargs):
  80. """!Executes a command.
  81. """
  82. raise NotImplementedError()
  83. def Help(self, entry):
  84. """Shows a manual page for a given entry.
  85. """
  86. raise NotImplementedError()
  87. def WriteLog(self, text, wrap=None, notification=Notification.HIGHLIGHT):
  88. """!Writes log message.
  89. """
  90. raise NotImplementedError()
  91. def WriteCmdLog(self, line, pid=None, notification=Notification.MAKE_VISIBLE):
  92. """!Writes message related to start or end of the command.
  93. """
  94. raise NotImplementedError()
  95. def WriteWarning(self, line):
  96. """!Writes warning message for the user.
  97. """
  98. raise NotImplementedError()
  99. def WriteError(self, line):
  100. """!Writes error message for the user."""
  101. raise NotImplementedError()
  102. def GetLayerTree(self):
  103. """!Returns LayerManager's tree GUI object.
  104. @note Will be removed from the interface.
  105. """
  106. raise NotImplementedError()
  107. def GetLayerList(self):
  108. """!Returns a layer management object.
  109. """
  110. raise NotImplementedError()
  111. def GetMapDisplay(self):
  112. """!Returns current map display.
  113. @note For layer related tasks use GetLayerList().
  114. @return MapFrame instance
  115. @return None when no mapdisplay open
  116. """
  117. raise NotImplementedError()
  118. def GetAllMapDisplays(self):
  119. """!Get list of all map displays.
  120. @note Might be removed from the interface.
  121. @return list of MapFrame instances
  122. """
  123. raise NotImplementedError()
  124. def GetMapWindow(self):
  125. """!Returns current map window.
  126. @note For layer related tasks use GetLayerList().
  127. """
  128. raise NotImplementedError()
  129. def GetProgress(self):
  130. """!Returns object which shows the progress.
  131. @note Some implementations may not implement this method.
  132. """
  133. raise NotImplementedError()
  134. class StandaloneGrassInterface():
  135. """!@implements GrassInterface"""
  136. def __init__(self):
  137. # Signal when some map is created or updated by a module.
  138. # attributes: name: map name, ltype: map type,
  139. # add: if map should be added to layer tree (questionable attribute)
  140. self.mapCreated = Signal('StandaloneGrassInterface.mapCreated')
  141. # Signal emitted to request updating of map
  142. self.updateMap = Signal('StandaloneGrassInterface.updateMap')
  143. from core.gconsole import GConsole, \
  144. EVT_CMD_OUTPUT, EVT_CMD_PROGRESS, \
  145. EVT_WRITE_LOG, EVT_WRITE_CMD_LOG, EVT_WRITE_WARNING, EVT_WRITE_ERROR
  146. self._gconsole = GConsole()
  147. self._gconsole.Bind(EVT_CMD_PROGRESS, self._onCmdProgress)
  148. self._gconsole.Bind(EVT_CMD_OUTPUT, self._onCmdOutput)
  149. self._gconsole.Bind(EVT_WRITE_LOG,
  150. lambda event: self.WriteLog(text=event.text))
  151. self._gconsole.Bind(EVT_WRITE_CMD_LOG,
  152. lambda event: self.WriteCmdLog(line=event.line))
  153. self._gconsole.Bind(EVT_WRITE_WARNING,
  154. lambda event: self.WriteWarning(line=event.line))
  155. self._gconsole.Bind(EVT_WRITE_ERROR,
  156. lambda event: self.WriteError(line=event.line))
  157. def _onCmdOutput(self, event):
  158. """!Print command output"""
  159. message = event.text
  160. style = event.type
  161. if style == 'warning':
  162. self.WriteWarning(message)
  163. elif style == 'error':
  164. self.WriteError(message)
  165. else:
  166. self.WriteLog(message)
  167. event.Skip()
  168. def _onCmdProgress(self, event):
  169. """!Update progress message info"""
  170. grass.percent(event.value, 100, 1)
  171. event.Skip()
  172. def RunCmd(self, command, compReg=True, skipInterface=False,
  173. onDone=None, onPrepare=None, userData=None, notification=Notification.MAKE_VISIBLE):
  174. self._gconsole.RunCmd(command=command, compReg=compReg,
  175. skipInterface=skipInterface, onDone=onDone,
  176. onPrepare=onPrepare, userData=userData, notification=notification)
  177. def Help(self, entry):
  178. self._gconsole.RunCmd(['g.manual', 'entry=%s' % entry])
  179. def WriteLog(self, text, wrap=None,
  180. notification=Notification.HIGHLIGHT):
  181. self._write(grass.message, text)
  182. def WriteCmdLog(self, line, pid=None, notification=Notification.MAKE_VISIBLE):
  183. if pid:
  184. line = '(' + str(pid) + ') ' + line
  185. self._write(grass.message, line)
  186. def WriteWarning(self, line):
  187. self._write(grass.warning, line)
  188. def WriteError(self, line):
  189. self._write(grass.error, line)
  190. def _write(self, function, text):
  191. orig = os.getenv("GRASS_MESSAGE_FORMAT")
  192. os.environ["GRASS_MESSAGE_FORMAT"] = 'standard'
  193. function(text)
  194. os.environ["GRASS_MESSAGE_FORMAT"] = orig
  195. def GetLayerList(self):
  196. raise NotImplementedError()
  197. def GetMapDisplay(self):
  198. """!Get current map display.
  199. """
  200. return None
  201. def GetAllMapDisplays(self):
  202. """!Get list of all map displays.
  203. """
  204. return []
  205. def GetMapWindow(self):
  206. raise NotImplementedError()
  207. def GetProgress(self):
  208. # TODO: implement some progress with same inface as gui one
  209. # (probably using g.message or similarly to Write... functions)
  210. raise NotImplementedError()