/[thuban]/branches/WIP-pyshapelib-bramz/Thuban/UI/menu.py
ViewVC logotype

Contents of /branches/WIP-pyshapelib-bramz/Thuban/UI/menu.py

Parent Directory Parent Directory | Revision Log Revision Log


Revision 236 - (show annotations)
Tue Jul 23 16:53:14 2002 UTC (22 years, 7 months ago) by bh
Original Path: trunk/thuban/Thuban/UI/menu.py
File MIME type: text/x-python
File size: 3947 byte(s)
(Menu.item_index): Also search for menus not
just commands.

1 # Copyright (C) 2001, 2002 by Intevation GmbH
2 # Author:
3 # Bernhard Herzog <[email protected]>
4 #
5 # This program is free software under the GPL (>=v2)
6 # Read the file COPYING coming with Thuban for details.
7
8 """Menu management"""
9
10 __version__ = "$Revision$"
11
12
13 class Menu:
14
15 """Represent a menu or submenu.
16
17 A menu has a name and a title. The name can be used to identify
18 menus internally while the title is intended for use in the GUI.
19
20 Menu items can be added with the Insert* methods.
21 """
22
23 def __init__(self, name, title, items = None):
24 """Initialize the menu.
25
26 Parameters:
27 name -- the name of the menu
28 title -- the (possibly localized) title of the menu
29 items -- (optional) a list of menu items.
30
31 The items list may contains strings with command names, None to
32 indicate separators or Menu instances for submenus.
33 """
34 self.name = name
35 self.title = title
36 if items is None:
37 self.items = []
38 else:
39 self.items = items
40
41 def item_index(self, item):
42 """Return the index of item in the menu.
43
44 The item parameter may be the name of a non-menu entry or the
45 name of a menu or a menu itself.
46
47 Return None it item is not found.
48 """
49 for i in range(len(self.items)):
50 temp = self.items[i]
51 if temp == item:
52 # this case takes care of item being the name of an
53 # entry or a menu.
54 return i
55 elif isinstance(temp, Menu) and temp.name == item:
56 # item is the name of a menu
57 return i
58 # Didn't find the item so return None
59 return None
60
61 def find_menu(self, name):
62 """Return the submenu named name or None if no such item exists"""
63 for item in self.items:
64 if isinstance(item, Menu) and item.name == name:
65 return item
66 return None
67
68 def InsertItem(self, item, menu = (), after = None):
69 """Insert a menu item.
70
71 Parameters:
72
73 item -- the menu item to insert must be either a string with
74 the command's name or a Menu instance.
75
76 menu -- (optional) the submenu to insert into. It should be a
77 sequence of menu names.
78
79 after -- (optional) insert the new item after this one. after
80 should be the name of a command.
81 """
82 # if menu is given, get the first submenu
83 if menu:
84 submenu_index = self.find_menu(menu[0])
85 if submenu_index is not None:
86 submenu_index.InsertItem(item, menu = menu[1:], after = after)
87 else:
88 # the submenu doesn't exist yet. Raise an error.
89 raise KeyError("Submenu %s doesn't exist" % menu[0])
90 else:
91 if after is not None:
92 idx = self.item_index(after)
93 else:
94 idx = None
95
96 if idx is not None:
97 self.items.insert(idx + 1, item)
98 else:
99 self.items.append(item)
100
101 def InsertSeparator(self):
102 """Insert a separator"""
103 self.InsertItem(None)
104
105 def InsertMenu(self, name, title, menu = (), after = None):
106 """Insert and return a new menu.
107
108 Parameters:
109
110 name -- the (internal) name of the menu
111
112 title -- the (possibly localized) title
113
114 menu -- (optional) the submenu to insert into. It should be a
115 sequence of menu names.
116
117 after -- (optional) insert the new item after this one. after
118 should be the name of a command.
119 """
120 newmenu = Menu(name, title)
121 self.InsertItem(newmenu, menu = menu, after = after)
122 return newmenu
123
124 def SetItems(self, items):
125 """Replace the contents of the menu by items."""
126 self.items = items

Properties

Name Value
svn:eol-style native
svn:keywords Author Date Id Revision

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26