giface.py 3.8 KB

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