menu.py 8.0 KB

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