demoMenu.py

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