frame.py 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. """
  2. @package datacatalog::frame
  3. @brief Data catalog frame class
  4. Classes:
  5. - datacatalog::DataCatalogFrame
  6. (C) 2014-2018 by Tereza Fiedlerova, and the GRASS Development Team
  7. This program is free software under the GNU General Public
  8. License (>=v2). Read the file COPYING that comes with GRASS
  9. for details.
  10. @author Tereza Fiedlerova (original author)
  11. @author Martin Landa <landa.martin gmail.com> (various improvements)
  12. """
  13. import os
  14. import sys
  15. import wx
  16. from core.globalvar import ICONDIR
  17. from core.gcmd import RunCommand, GMessage
  18. from datacatalog.tree import DataCatalogTree
  19. from datacatalog.toolbars import DataCatalogToolbar
  20. from gui_core.wrap import Button
  21. class DataCatalogFrame(wx.Frame):
  22. """Frame for testing purposes only."""
  23. def __init__(self, parent, giface=None):
  24. wx.Frame.__init__(self, parent=parent,
  25. title=_('GRASS GIS Data Catalog'))
  26. self.SetName("DataCatalog")
  27. self.SetIcon(
  28. wx.Icon(
  29. os.path.join(
  30. ICONDIR,
  31. 'grass.ico'),
  32. wx.BITMAP_TYPE_ICO))
  33. self._giface = giface
  34. self.panel = wx.Panel(self)
  35. self.toolbar = DataCatalogToolbar(parent=self)
  36. # workaround for http://trac.wxwidgets.org/ticket/13888
  37. if sys.platform != 'darwin':
  38. self.SetToolBar(self.toolbar)
  39. # tree
  40. self.tree = DataCatalogTree(parent=self.panel, giface=self._giface)
  41. self.tree.InitTreeItems()
  42. self.tree.ExpandCurrentMapset()
  43. self.tree.changeMapset.connect(lambda mapset:
  44. self.ChangeLocationMapset(location=None,
  45. mapset=mapset))
  46. self.tree.changeLocation.connect(lambda mapset, location:
  47. self.ChangeLocationMapset(location=location,
  48. mapset=mapset))
  49. # buttons
  50. self.btnClose = Button(parent=self.panel, id=wx.ID_CLOSE)
  51. self.btnClose.SetToolTip(_("Close GRASS GIS Data Catalog"))
  52. self.btnClose.SetDefault()
  53. # events
  54. self.btnClose.Bind(wx.EVT_BUTTON, self.OnCloseWindow)
  55. self.Bind(wx.EVT_CLOSE, self.OnCloseWindow)
  56. self._layout()
  57. def _layout(self):
  58. sizer = wx.BoxSizer(wx.VERTICAL)
  59. sizer.Add(self.tree, proportion=1, flag=wx.EXPAND)
  60. btnSizer = wx.BoxSizer(wx.HORIZONTAL)
  61. btnSizer.AddStretchSpacer()
  62. btnSizer.Add(self.btnClose)
  63. sizer.Add(btnSizer, proportion=0,
  64. flag=wx.ALL | wx.EXPAND,
  65. border=5)
  66. self.panel.SetSizer(sizer)
  67. sizer.Fit(self.panel)
  68. self.SetMinSize((400, 500))
  69. def OnCloseWindow(self, event):
  70. """Cancel button pressed"""
  71. if not isinstance(event, wx.CloseEvent):
  72. self.Destroy()
  73. event.Skip()
  74. def OnReloadTree(self, event):
  75. """Reload whole tree"""
  76. self.tree.ReloadTreeItems()
  77. self.tree.ExpandCurrentMapset()
  78. def OnReloadCurrentMapset(self, event):
  79. """Reload current mapset tree only"""
  80. self.tree.ReloadCurrentMapset()
  81. def SetRestriction(self, restrict):
  82. """Allow editing other mapsets or restrict editing to current mapset"""
  83. self.tree.SetRestriction(restrict)
  84. def ChangeLocationMapset(self, mapset, location=None):
  85. """Change mapset or location"""
  86. if location:
  87. if RunCommand('g.mapset', parent=self,
  88. location=location,
  89. mapset=mapset) == 0:
  90. GMessage(parent=self,
  91. message=_("Current location is <%(loc)s>.\n"
  92. "Current mapset is <%(mapset)s>.") %
  93. {'loc': location, 'mapset': mapset})
  94. else:
  95. if RunCommand('g.mapset',
  96. parent=self,
  97. mapset=mapset) == 0:
  98. GMessage(parent=self,
  99. message=_("Current mapset is <%s>.") % mapset)
  100. def Filter(self, text):
  101. self.tree.Filter(text=text)