menu.py 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. """!
  2. @package gui_core.menu
  3. @brief Menu classes for wxGUI
  4. Classes:
  5. - menu::Menu
  6. - menu::SearchModuleWindow
  7. (C) 2010-2012 by the GRASS Development Team
  8. This program is free software under the GNU General Public License
  9. (>=v2). Read the file COPYING that comes with GRASS for details.
  10. @author Martin Landa <landa.martin gmail.com>
  11. @author Pawel Netzel (menu customization)
  12. @author Milena Nowotarska (menu customization)
  13. @author Robert Szczepanek (menu customization)
  14. @author Vaclav Petras <wenzeslaus gmail.com> (menu customization)
  15. """
  16. import wx
  17. from core import globalvar
  18. from core import utils
  19. from core.gcmd import EncodeString
  20. from gui_core.widgets import SearchModuleWidget
  21. from gui_core.treeview import CTreeView
  22. from grass.pydispatch.signal import Signal
  23. class Menu(wx.MenuBar):
  24. def __init__(self, parent, model):
  25. """!Creates menubar"""
  26. wx.MenuBar.__init__(self)
  27. self.parent = parent
  28. self.model = model
  29. self.menucmd = dict()
  30. for child in self.model.root.children:
  31. self.Append(self._createMenu(child), child.label)
  32. def _createMenu(self, node):
  33. """!Creates menu"""
  34. menu = wx.Menu()
  35. for child in node.children:
  36. if child.children:
  37. label = child.label
  38. subMenu = self._createMenu(child)
  39. menu.AppendMenu(wx.ID_ANY, label, subMenu)
  40. else:
  41. self._createMenuItem(menu, **child.data)
  42. self.parent.Bind(wx.EVT_MENU_HIGHLIGHT_ALL, self.OnMenuHighlight)
  43. return menu
  44. def _createMenuItem(self, menu, label, description, handler, command, keywords,
  45. shortcut = '', wxId = wx.ID_ANY, kind = wx.ITEM_NORMAL):
  46. """!Creates menu items
  47. There are three menu styles (menu item text styles).
  48. 1 -- label only, 2 -- label and cmd name, 3 -- cmd name only
  49. """
  50. if not label:
  51. menu.AppendSeparator()
  52. return
  53. if command:
  54. helpString = command + ' -- ' + description
  55. else:
  56. helpString = description
  57. if shortcut:
  58. label += '\t' + shortcut
  59. menuItem = menu.Append(wxId, label, helpString, kind)
  60. self.menucmd[menuItem.GetId()] = command
  61. if command:
  62. try:
  63. cmd = utils.split(str(command))
  64. except UnicodeError:
  65. cmd = utils.split(EncodeString((command)))
  66. if cmd and cmd[0] not in globalvar.grassCmd:
  67. menuItem.Enable(False)
  68. rhandler = eval('self.parent.' + handler)
  69. self.parent.Bind(wx.EVT_MENU, rhandler, menuItem)
  70. def GetData(self):
  71. """!Get menu data"""
  72. return self.model
  73. def GetCmd(self):
  74. """!Get dictionary of commands (key is id)
  75. @return dictionary of commands
  76. """
  77. return self.menucmd
  78. def OnMenuHighlight(self, event):
  79. """
  80. Default menu help handler
  81. """
  82. # Show how to get menu item info from this event handler
  83. id = event.GetMenuId()
  84. item = self.FindItemById(id)
  85. if item:
  86. text = item.GetText()
  87. help = item.GetHelp()
  88. # but in this case just call Skip so the default is done
  89. event.Skip()
  90. class SearchModuleWindow(wx.Panel):
  91. """!Menu tree and search widget for searching modules.
  92. Signal:
  93. showNotification - attribute 'message'
  94. """
  95. def __init__(self, parent, model, id = wx.ID_ANY, **kwargs):
  96. self.parent = parent # LayerManager
  97. self.showNotification = Signal('SearchModuleWindow.showNotification')
  98. wx.Panel.__init__(self, parent = parent, id = id, **kwargs)
  99. # tree
  100. self._tree = CTreeView(model=model, parent=self)
  101. self._dataBox = wx.StaticBox(parent = self, id = wx.ID_ANY,
  102. label = " %s " % _("Menu tree (double-click or Ctrl-Enter to run command)"))
  103. # search widget
  104. self._search = SearchModuleWidget(parent=self,
  105. model=model,
  106. showChoice=False)
  107. self._search.showSearchResult.connect(lambda result: self._tree.Select(result))
  108. self._search.showNotification.connect(self.showNotification)
  109. # buttons
  110. self._btnRun = wx.Button(self, id=wx.ID_OK, label=_("&Run"))
  111. self._btnRun.SetToolTipString(_("Run selected command from the menu tree"))
  112. # bindings
  113. self._btnRun.Bind(wx.EVT_BUTTON, lambda evt: self.Run())
  114. self.Bind(wx.EVT_KEY_UP, self.OnKeyUp)
  115. self._tree.selectionChanged.connect(self.OnItemSelected)
  116. self._tree.itemActivated.connect(lambda node: self.Run(node))
  117. self._layout()
  118. self._search.SetFocus()
  119. def _layout(self):
  120. """!Do dialog layout"""
  121. sizer = wx.BoxSizer(wx.VERTICAL)
  122. # body
  123. dataSizer = wx.StaticBoxSizer(self._dataBox, wx.HORIZONTAL)
  124. dataSizer.Add(item = self._tree, proportion =1,
  125. flag = wx.EXPAND)
  126. # buttons
  127. btnSizer = wx.BoxSizer(wx.HORIZONTAL)
  128. btnSizer.Add(item = self._btnRun, proportion = 0)
  129. sizer.Add(item = dataSizer, proportion = 1,
  130. flag = wx.EXPAND | wx.ALL, border = 5)
  131. sizer.Add(item = self._search, proportion = 0,
  132. flag = wx.EXPAND | wx.LEFT | wx.RIGHT | wx.BOTTOM, border = 5)
  133. sizer.Add(item = btnSizer, proportion = 0,
  134. flag = wx.ALIGN_RIGHT | wx.BOTTOM | wx.RIGHT, border = 5)
  135. sizer.Fit(self)
  136. sizer.SetSizeHints(self)
  137. self.SetSizer(sizer)
  138. self.Fit()
  139. self.SetAutoLayout(True)
  140. self.Layout()
  141. def Run(self, module=None):
  142. """!Run selected command.
  143. @param module module (represented by tree node)
  144. """
  145. if module is None:
  146. if not self._tree.GetSelected():
  147. return
  148. module = self._tree.GetSelected()[0]
  149. data = module.data
  150. if not data:
  151. return
  152. handler = 'self.parent.' + data['handler'].lstrip('self.')
  153. if data['command']:
  154. eval(handler)(event=None, cmd=data['command'].split())
  155. else:
  156. eval(handler)(event=None)
  157. def OnKeyUp(self, event):
  158. """!Key or key combination pressed"""
  159. if event.ControlDown() and event.GetKeyCode() == wx.WXK_RETURN:
  160. self.Run()
  161. def OnItemSelected(self, node):
  162. """!Item selected"""
  163. data = node.data
  164. if not data or 'command' not in data:
  165. return
  166. if data['command']:
  167. label = data['command'] + ' -- ' + data['description']
  168. else:
  169. label = data['description']
  170. self.showNotification.emit(message=label)