frame.py 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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.utils import _
  17. from core.globalvar import ICONDIR
  18. from core.gcmd import RunCommand, GMessage
  19. from datacatalog.tree import DataCatalogTree
  20. from datacatalog.toolbars import DataCatalogToolbar
  21. from gui_core.wrap import Button
  22. class DataCatalogFrame(wx.Frame):
  23. """Frame for testing purposes only."""
  24. def __init__(self, parent, giface=None):
  25. wx.Frame.__init__(self, parent=parent,
  26. title=_('GRASS GIS Data Catalog'))
  27. self.SetName("DataCatalog")
  28. self.SetIcon(
  29. wx.Icon(
  30. os.path.join(
  31. ICONDIR,
  32. 'grass.ico'),
  33. wx.BITMAP_TYPE_ICO))
  34. self._giface = giface
  35. self.panel = wx.Panel(self)
  36. self.toolbar = DataCatalogToolbar(parent=self)
  37. # workaround for http://trac.wxwidgets.org/ticket/13888
  38. if sys.platform != 'darwin':
  39. self.SetToolBar(self.toolbar)
  40. # tree
  41. self.tree = DataCatalogTree(parent=self.panel, giface=self._giface)
  42. self.tree.InitTreeItems()
  43. self.tree.ExpandCurrentMapset()
  44. self.tree.changeMapset.connect(lambda mapset:
  45. self.ChangeLocationMapset(location=None,
  46. mapset=mapset))
  47. self.tree.changeLocation.connect(lambda mapset, location:
  48. self.ChangeLocationMapset(location=location,
  49. mapset=mapset))
  50. # buttons
  51. self.btnClose = Button(parent=self.panel, id=wx.ID_CLOSE)
  52. self.btnClose.SetToolTip(_("Close GRASS GIS Data Catalog"))
  53. self.btnClose.SetDefault()
  54. # events
  55. self.btnClose.Bind(wx.EVT_BUTTON, self.OnCloseWindow)
  56. self.Bind(wx.EVT_CLOSE, self.OnCloseWindow)
  57. self._layout()
  58. def _layout(self):
  59. sizer = wx.BoxSizer(wx.VERTICAL)
  60. sizer.Add(self.tree, proportion=1, flag=wx.EXPAND)
  61. btnSizer = wx.BoxSizer(wx.HORIZONTAL)
  62. btnSizer.AddStretchSpacer()
  63. btnSizer.Add(self.btnClose)
  64. sizer.Add(btnSizer, proportion=0,
  65. flag=wx.ALL | wx.ALIGN_RIGHT | wx.EXPAND,
  66. border=5)
  67. self.panel.SetSizer(sizer)
  68. sizer.Fit(self.panel)
  69. self.SetMinSize((400, 500))
  70. def OnCloseWindow(self, event):
  71. """Cancel button pressed"""
  72. if not isinstance(event, wx.CloseEvent):
  73. self.Destroy()
  74. event.Skip()
  75. def OnReloadTree(self, event):
  76. """Reload whole tree"""
  77. self.tree.ReloadTreeItems()
  78. self.tree.ExpandCurrentMapset()
  79. def OnReloadCurrentMapset(self, event):
  80. """Reload current mapset tree only"""
  81. self.tree.ReloadCurrentMapset()
  82. def SetRestriction(self, restrict):
  83. """Allow editing other mapsets or restrict editing to current mapset"""
  84. self.tree.SetRestriction(restrict)
  85. def ChangeLocationMapset(self, mapset, location=None):
  86. """Change mapset or location"""
  87. if location:
  88. if RunCommand('g.mapset', parent=self,
  89. location=location,
  90. mapset=mapset) == 0:
  91. GMessage(parent=self,
  92. message=_("Current location is <%(loc)s>.\n"
  93. "Current mapset is <%(mapset)s>.") %
  94. {'loc': location, 'mapset': mapset})
  95. else:
  96. if RunCommand('g.mapset',
  97. parent=self,
  98. mapset=mapset) == 0:
  99. GMessage(parent=self,
  100. message=_("Current mapset is <%s>.") % mapset)
  101. def Filter(self, text):
  102. self.tree.Filter(text=text)