giface.py 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. """!
  2. @package lmgr.giface
  3. @brief Layer Manager GRASS interface
  4. Classes:
  5. - giface::LayerManagerGrassInterface
  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. from grass.pydispatch.signal import Signal
  13. class Layer(object):
  14. """!@implements core::giface::Layer
  15. @note Currently implemented without specifying the interface.
  16. It only provides all attributes of existing layer as used in lmgr.
  17. """
  18. def __init__(self, pydata):
  19. self._pydata = pydata
  20. def __getattr__(self, name):
  21. return self._pydata[0][name]
  22. class LayerList(object):
  23. """!@implements core.giface.Layer"""
  24. def __init__(self, tree):
  25. self._tree = tree
  26. # def __iter__(self):
  27. # """!Iterates over the contents of the list."""
  28. # for in :
  29. # yield
  30. def GetSelectedLayers(self, checkedOnly=True):
  31. items = self._tree.GetSelectedLayer(multi=True,
  32. checkedOnly=checkedOnly)
  33. layers = []
  34. for item in items:
  35. layer = Layer(self._tree.GetPyData(item))
  36. layers.append(layer)
  37. return layers
  38. def GetLayerInfo(self, layer):
  39. """!For compatibility only, will be removed."""
  40. return Layer(self._tree.GetPyData(layer))
  41. class LayerManagerGrassInterface(object):
  42. """!@implements GrassInterface"""
  43. def __init__(self, lmgr):
  44. """!Costructor is specific to the current implementation.
  45. Uses Layer Manager object including its private attributes.
  46. (It encapsulates existing Layer Manager so access to private members
  47. is intention.)
  48. """
  49. self.lmgr = lmgr
  50. # Signal when some map is created or updated by a module.
  51. # attributes: name: map name, ltype: map type,
  52. # add: if map should be added to layer tree (questionable attribute)
  53. self.mapCreated = Signal('LayerManagerGrassInterface.mapCreated')
  54. # Signal emitted to request updating of map
  55. self.updateMap = Signal('LayerManagerGrassInterface.updateMap')
  56. def RunCmd(self, *args, **kwargs):
  57. self.lmgr._gconsole.RunCmd(*args, **kwargs)
  58. def Help(self, entry):
  59. cmdlist = ['g.manual', 'entry=%s' % entry]
  60. self.RunCmd(cmdlist, compReg = False, switchPage = False)
  61. def WriteLog(self, text, wrap = None,
  62. switchPage = False, priority = 1):
  63. self.lmgr._gconsole.WriteLog(text = text, wrap = wrap, switchPage = switchPage,
  64. priority = priority)
  65. def WriteCmdLog(self, line, pid = None, switchPage = True):
  66. self.lmgr._gconsole.WriteCmdLog(line = line, pid = pid, switchPage = switchPage)
  67. def WriteWarning(self, line):
  68. self.lmgr._gconsole.WriteWarning(line = line)
  69. def WriteError(self, line):
  70. self.lmgr._gconsole.WriteError(line = line)
  71. def GetLayerTree(self):
  72. return self.lmgr.GetLayerTree()
  73. def GetLayerList(self):
  74. return LayerList(self.lmgr.GetLayerTree())
  75. def GetMapDisplay(self):
  76. return self.lmgr.GetMapDisplay(onlyCurrent=True)
  77. def GetAllMapDisplays(self):
  78. return self.lmgr.GetMapDisplay(onlyCurrent=False)
  79. def GetMapWindow(self):
  80. if self.lmgr.GetMapDisplay(onlyCurrent=True):
  81. return self.lmgr.GetMapDisplay(onlyCurrent=True).GetMapWindow()
  82. else:
  83. return None
  84. def GetProgress(self):
  85. return self.lmgr.goutput.GetProgressBar()
  86. class LayerManagerGrassInterfaceForMapDisplay(object):
  87. """!Provides reference only to the given layer list (according to tree),
  88. not to the current.
  89. """
  90. def __init__(self, giface, tree):
  91. """!
  92. @giface original grass interface
  93. @tree tree which will be used instead of the tree from giface
  94. """
  95. self._giface = giface
  96. self.tree = tree
  97. # Signal emitted to request updating of map
  98. self.updateMap = Signal('LayerManagerGrassInterfaceForMapDisplay.updateMap')
  99. def GetLayerTree(self):
  100. return self.tree
  101. def GetLayerList(self):
  102. return LayerList(self.tree)
  103. def GetMapWindow(self):
  104. return self.tree.GetMapDisplay()
  105. def __getattr__(self, name):
  106. return getattr(self._giface, name)