Small program to test initial bin reading work
[crapvine.git] / menu_navigator.py
blob05410766974d89587e4f1310135be111370db0dd
1 ## This file is part of Crapvine.
2 ##
3 ## Copyright (C) 2007 Andrew Sayman <lorien420@myrealbox.com>
4 ##
5 ## Crapvine is free software; you can redistribute it and/or modify
6 ## it under the terms of the GNU General Public License as published by
7 ## the Free Software Foundation; either version 3 of the License, or
8 ## (at your option) any later version.
9 ##
10 ## Crapvine is distributed in the hope that it will be useful,
11 ## but WITHOUT ANY WARRANTY; without even the implied warranty of
12 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 ## GNU General Public License for more details.
15 ## You should have received a copy of the GNU General Public License
16 ## along with this program. If not, see <http://www.gnu.org/licenses/>.
18 import gtk
19 import copy
20 from menu import MenuLoader, MenuModel, MenuReference
22 class MenuNavigator:
23 def __init__(self, xml):
24 self.target = None
25 self.menu_loader = MenuLoader()
26 self.menu_path = []
27 self.xml = xml
28 self.treeMenu = self.xml.get_widget('treeMenu')
29 renderer = gtk.CellRendererText()
30 renderer.set_data("column", 0)
32 column = gtk.TreeViewColumn("Name", renderer, text=0)
33 self.treeMenu.append_column(column)
35 def __create_menu_model(self):
36 pass
38 def __is_showing_menu(self, menu_name):
39 current_model = self.treeMenu.get_model()
40 if current_model:
41 current_menu = current_model.menu
42 if current_menu and current_menu.name == menu_name:
43 return True
44 return False
45 def __is_showing_menu_or_submenu(self, menu_name):
46 current_model = self.treeMenu.get_model()
47 if current_model:
48 current_menu = current_model.menu
49 if current_menu and current_menu.name == menu_name:
50 return True
51 else:
52 for menu in self.menu_path:
53 if menu_name == menu.name:
54 return True
55 return False
56 def show_menu(self, trait_category):
57 if self.__is_showing_menu_or_submenu(trait_category):
58 return
59 self.__change_menu_model(trait_category)
60 self.menu_path = []
62 def __change_menu_model(self, trait_category):
63 menu = self.menu_loader.get_expanded_menu(trait_category)
64 if not menu:
65 raise ValueError('Selected invalid menu %s' % (trait_category))
66 model = MenuModel(menu)
67 self.treeMenu.set_model(model)
68 self.xml.get_widget('lblMenuTitle').set_label(trait_category)
70 def __back_up_menu_path(self):
71 if len(self.menu_path) == 0:
72 return
73 self.__change_menu_model(self.menu_path[-1].name)
74 del self.menu_path[-1]
76 def __add_to_menu_path(self, trait_category):
77 old_menu = self.treeMenu.get_model().menu
78 self.__change_menu_model(trait_category)
79 self.menu_path.append(old_menu)
81 def __add_menu_item_to_target(self):
82 if self.target is None:
83 return
84 (mainModel, selIter) = self.treeMenu.get_selection().get_selected()
85 if selIter == None:
86 raise ValueError('No menu item selected')
87 path = mainModel.get_path(selIter)
88 event_menu_item = copy.copy(mainModel.get_item(path[0]))
89 if isinstance(event_menu_item, MenuReference):
90 raise ValueError('Cannot add an entire submenu at once!')
91 event_menu_path = []
92 event_menu_path.extend(self.menu_path)
93 event_menu_path.append(self.treeMenu.get_model().menu)
94 self.target.add(event_menu_item, event_menu_path)
96 def on_btnAddTrait_clicked(self, widget):
97 self.__add_menu_item_to_target()
98 def on_btnRemoveTrait_clicked(self, widget):
99 self.target.remove()
100 def add_custom(self, widget=None):
101 self.target.add_custom()
102 def add_note(self, widget=None):
103 self.target.add_note()
105 def on_treeMenu_row_activated(self, treeview, path, view_column):
106 menu_item = treeview.get_model().get_item_from_path(path)
107 if isinstance(menu_item, MenuReference) and menu_item.tagname == 'submenu':
108 if menu_item.reference == '(back)':
109 self.__back_up_menu_path()
110 else:
111 self.__add_to_menu_path(menu_item.reference)
112 else:
113 self.__add_menu_item_to_target()
116 class MenuTarget(object):
117 """A handler for events from the menu navigation pane"""
119 def add(self, menu_item, menu_path):
120 """Add a trait
122 menu_item - The MenuItem that was selected when the add button was
123 pressed
124 menu_path - A list of Menus that lead up to this item
126 pass
128 def remove(self):
129 """Remove a trait
131 Meant to be used to remove the currently selected trait in whatever
132 assigned this handler.
134 pass
136 def add_custom(self):
137 """Used to provide a custom dialog for new entries not included in the
138 menu.
140 pass
142 def add_note(self):
143 """Used to handle adding a note to a currently selected entry.
145 pass