demoMenu.py

demoMenu.py
1 '''
2  Demonstrates the menu manager API.
3 '''
4 import MaxPlus
5 
6 def outputMenuItem(item, recurse = True, indent = ''):
7  text = item.GetTitle()
8  print indent, text if text else "----"
9  if item.HasSubMenu and recurse:
10  outputMenu(item.SubMenu, recurse, indent + ' ')
11 
12 def outputMenu(menu, recurse = True, indent = ''):
13  for i in menu.Items:
14  outputMenuItem(i, recurse, indent)
15 
16 somethingHappened = False
17 def doSomething():
18  global somethingHappened
19  somethingHappened = True
20  print 'Something happened'
21 
22 action = MaxPlus.ActionFactory.Create('Do something', 'Python demos', doSomething)
23 
24 def createTestMenu(name):
25  if not MaxPlus.MenuManager.MenuExists(name):
26  mb = MaxPlus.MenuBuilder(name)
27  if action._IsValidWrapper():
28  print "Created action"
29  else:
30  print "Failed to create action"
31  mb.AddItem(action)
32  mb.AddSeparator()
33  menu = mb.Create(MaxPlus.MenuManager.GetMainMenu())
34  print 'menu created', menu.Title
35  else:
36  print 'The menu ', name, ' already exists'
37 
38 def getLastMenuItem(menu = MaxPlus.MenuManager.GetMainMenu()):
39  return list(menu.Items)[-1]
40 
41 def testLastItem(text):
42  assert(getLastMenuItem().Title == text)
43 
44 def main():
45  print "Removing any previously left 'menu items'"
47 
48  print "Creating a new menu"
49  testLastItem(u"&Help")
50  outputMenu(MaxPlus.MenuManager.GetMainMenu(), False)
51 
52  print "Creating a new menu"
53  createTestMenu(u"Test")
54  outputMenu(MaxPlus.MenuManager.GetMainMenu(), False)
55  testLastItem(u"Test")
56 
57  assert(not somethingHappened)
58  mi = getLastMenuItem()
59  mi = list(mi.SubMenu.Items)[0]
60  ai = mi.ActionItem
61  ai.Execute()
62  assert(somethingHappened)
63 
64  print "Unregistering the 'test' menu"
66  outputMenu(MaxPlus.MenuManager.GetMainMenu(), False)
67  testLastItem(u"&Help")
68 
69 if __name__ == '__main__':
70  main()