menu.py 7.5 KB

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