manager.py 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. """
  2. @package dbmgr.manager
  3. @brief GRASS Attribute Table Manager
  4. This program is based on FileHunter, published in 'The wxPython Linux
  5. Tutorial' on wxPython WIKI pages.
  6. It also uses some functions at
  7. http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/426407
  8. List of classes:
  9. - manager::AttributeManager
  10. (C) 2007-2014 by the GRASS Development Team
  11. This program is free software under the GNU General Public License
  12. (>=v2). Read the file COPYING that comes with GRASS for details.
  13. @author Jachym Cepicky <jachym.cepicky gmail.com>
  14. @author Martin Landa <landa.martin gmail.com>
  15. @author Refactoring by Stepan Turek <stepan.turek seznam.cz> (GSoC 2012, mentor: Martin Landa)
  16. """
  17. import sys
  18. import os
  19. import wx
  20. from core import globalvar
  21. if globalvar.wxPythonPhoenix:
  22. try:
  23. import agw.flatnotebook as FN
  24. except ImportError: # if it's not there locally, try the wxPython lib.
  25. import wx.lib.agw.flatnotebook as FN
  26. else:
  27. import wx.lib.flatnotebook as FN
  28. import grass.script as grass
  29. from core.gcmd import GMessage
  30. from core.debug import Debug
  31. from dbmgr.base import DbMgrBase
  32. from gui_core.widgets import GNotebook
  33. from gui_core.wrap import Button, ClearButton, CloseButton
  34. class AttributeManager(wx.Frame, DbMgrBase):
  35. def __init__(self, parent, id=wx.ID_ANY,
  36. title=None, vectorName=None, item=None, log=None,
  37. selection=None, **kwargs):
  38. """GRASS Attribute Table Manager window
  39. :param parent: parent window
  40. :param id: window id
  41. :param title: window title or None for default title
  42. :param vectorName: name of vector map
  43. :param item: item from Layer Tree
  44. :param log: log window
  45. :param selection: name of page to be selected
  46. :param kwagrs: other wx.Frame's arguments
  47. """
  48. self.parent = parent
  49. try:
  50. mapdisplay = self.parent.GetMapDisplay()
  51. except:
  52. mapdisplay = None
  53. DbMgrBase.__init__(self, id=id, mapdisplay=mapdisplay,
  54. vectorName=vectorName, item=item,
  55. log=log, statusbar=self,
  56. **kwargs)
  57. wx.Frame.__init__(self, parent, id, *kwargs)
  58. # title
  59. if not title:
  60. title = "%s" % _("GRASS GIS Attribute Table Manager - ")
  61. if not self.dbMgrData['editable']:
  62. title += _("READONLY - ")
  63. title += "<%s>" % (self.dbMgrData['vectName'])
  64. self.SetTitle(title)
  65. # icon
  66. self.SetIcon(
  67. wx.Icon(
  68. os.path.join(
  69. globalvar.ICONDIR,
  70. 'grass_sql.ico'),
  71. wx.BITMAP_TYPE_ICO))
  72. self.panel = wx.Panel(parent=self, id=wx.ID_ANY)
  73. if len(self.dbMgrData['mapDBInfo'].layers.keys()) == 0:
  74. GMessage(
  75. parent=self.parent, message=_(
  76. "Database connection for vector map <%s> "
  77. "is not defined in DB file. "
  78. "You can define new connection in "
  79. "'Manage layers' tab.") %
  80. self.dbMgrData['vectName'])
  81. busy = wx.BusyInfo(_("Please wait, loading attribute data..."),
  82. parent=self.parent)
  83. wx.SafeYield()
  84. self.CreateStatusBar(number=1)
  85. self.notebook = GNotebook(self.panel, style=globalvar.FNPageDStyle)
  86. self.CreateDbMgrPage(parent=self, pageName='browse')
  87. self.notebook.AddPage(page=self.pages['browse'], text=_("Browse data"),
  88. name='browse')
  89. self.pages['browse'].SetTabAreaColour(globalvar.FNPageColor)
  90. self.CreateDbMgrPage(parent=self, pageName='manageTable')
  91. self.notebook.AddPage(
  92. page=self.pages['manageTable'],
  93. text=_("Manage tables"),
  94. name='table')
  95. self.pages['manageTable'].SetTabAreaColour(globalvar.FNPageColor)
  96. self.CreateDbMgrPage(parent=self, pageName='manageLayer')
  97. self.notebook.AddPage(
  98. page=self.pages['manageLayer'],
  99. text=_("Manage layers"),
  100. name='layers')
  101. del busy
  102. if selection:
  103. wx.CallAfter(self.notebook.SetSelectionByName, selection)
  104. else:
  105. wx.CallAfter(self.notebook.SetSelection, 0) # select browse tab
  106. # buttons
  107. self.btnClose = CloseButton(parent=self.panel)
  108. self.btnClose.SetToolTip(_("Close Attribute Table Manager"))
  109. self.btnReload = Button(parent=self.panel, id=wx.ID_REFRESH)
  110. self.btnReload.SetToolTip(
  111. _("Reload currently selected attribute data"))
  112. self.btnReset = ClearButton(parent=self.panel)
  113. self.btnReset.SetToolTip(
  114. _("Reload all attribute data (drop current selection)"))
  115. # bind closing to ESC
  116. self.Bind(wx.EVT_MENU, self.OnCloseWindow, id=wx.ID_CANCEL)
  117. accelTableList = [(wx.ACCEL_NORMAL, wx.WXK_ESCAPE, wx.ID_CANCEL)]
  118. accelTable = wx.AcceleratorTable(accelTableList)
  119. self.SetAcceleratorTable(accelTable)
  120. # events
  121. self.btnClose.Bind(wx.EVT_BUTTON, self.OnCloseWindow)
  122. self.btnReload.Bind(wx.EVT_BUTTON, self.OnReloadData)
  123. self.btnReset.Bind(wx.EVT_BUTTON, self.OnReloadDataAll)
  124. self.notebook.Bind(
  125. FN.EVT_FLATNOTEBOOK_PAGE_CHANGED,
  126. self.OnPageChanged)
  127. self.Bind(wx.EVT_CLOSE, self.OnCloseWindow)
  128. # do layout
  129. self._layout()
  130. # self.SetMinSize(self.GetBestSize())
  131. self.SetSize((700, 550)) # FIXME hard-coded size
  132. self.SetMinSize(self.GetSize())
  133. def _layout(self):
  134. """Do layout"""
  135. # frame body
  136. mainSizer = wx.BoxSizer(wx.VERTICAL)
  137. # buttons
  138. btnSizer = wx.BoxSizer(wx.HORIZONTAL)
  139. btnSizer.Add(self.btnReset, proportion=1,
  140. flag=wx.ALL, border=5)
  141. btnSizer.Add(self.btnReload, proportion=1,
  142. flag=wx.ALL, border=5)
  143. btnSizer.Add(self.btnClose, proportion=1,
  144. flag=wx.ALL, border=5)
  145. mainSizer.Add(self.notebook, proportion=1, flag=wx.EXPAND)
  146. mainSizer.Add(btnSizer, flag=wx.ALIGN_RIGHT | wx.ALL, border=5)
  147. self.panel.SetAutoLayout(True)
  148. self.panel.SetSizer(mainSizer)
  149. mainSizer.Fit(self.panel)
  150. self.Layout()
  151. def OnCloseWindow(self, event):
  152. """Cancel button pressed"""
  153. if self.parent and self.parent.GetName() == 'LayerManager':
  154. # deregister ATM
  155. self.parent.dialogs['atm'].remove(self)
  156. if not isinstance(event, wx.CloseEvent):
  157. self.Destroy()
  158. event.Skip()
  159. def OnReloadData(self, event):
  160. """Reload data"""
  161. if self.pages['browse']:
  162. self.pages['browse'].OnDataReload(event) # TODO replace by signal
  163. def OnReloadDataAll(self, event):
  164. """Reload all data"""
  165. if self.pages['browse']:
  166. self.pages['browse'].ResetPage()
  167. def OnPageChanged(self, event):
  168. """On page in ATM is changed"""
  169. try:
  170. if self.pages["browse"]:
  171. selPage = self.pages["browse"].selLayer
  172. id = self.pages["browse"].layerPage[selPage]['data']
  173. else:
  174. id = None
  175. except KeyError:
  176. id = None
  177. if event.GetSelection() == self.notebook.GetPageIndexByName('browse') and id:
  178. win = self.FindWindowById(id)
  179. if win:
  180. self.log.write(
  181. _("Number of loaded records: %d") %
  182. win.GetItemCount())
  183. else:
  184. self.log.write("")
  185. self.btnReload.Enable()
  186. self.btnReset.Enable()
  187. else:
  188. self.log.write("")
  189. self.btnReload.Enable(False)
  190. self.btnReset.Enable(False)
  191. event.Skip()
  192. def OnTextEnter(self, event):
  193. pass
  194. def UpdateDialog(self, layer):
  195. """Updates dialog layout for given layer"""
  196. DbMgrBase.UpdateDialog(self, layer=layer)
  197. # set current page selection
  198. self.notebook.SetSelectionByName('layers')