menu.py 7.8 KB

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