1 ## This file is part of Crapvine.
3 ## Copyright (C) 2007 Andrew Sayman <lorien420@myrealbox.com>
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.
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/>.
20 from menu
import MenuLoader
, MenuModel
, MenuReference
23 def __init__(self
, xml
):
25 self
.menu_loader
= MenuLoader()
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
):
38 def __is_showing_menu(self
, menu_name
):
39 current_model
= self
.treeMenu
.get_model()
41 current_menu
= current_model
.menu
42 if current_menu
and current_menu
.name
== menu_name
:
45 def __is_showing_menu_or_submenu(self
, menu_name
):
46 current_model
= self
.treeMenu
.get_model()
48 current_menu
= current_model
.menu
49 if current_menu
and current_menu
.name
== menu_name
:
52 for menu
in self
.menu_path
:
53 if menu_name
== menu
.name
:
56 def show_menu(self
, trait_category
):
57 if self
.__is
_showing
_menu
_or
_submenu
(trait_category
):
59 self
.__change
_menu
_model
(trait_category
)
62 def __change_menu_model(self
, trait_category
):
63 menu
= self
.menu_loader
.get_expanded_menu(trait_category
)
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:
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:
84 (mainModel
, selIter
) = self
.treeMenu
.get_selection().get_selected()
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!')
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
):
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
()
111 self
.__add
_to
_menu
_path
(menu_item
.reference
)
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
):
122 menu_item - The MenuItem that was selected when the add button was
124 menu_path - A list of Menus that lead up to this item
131 Meant to be used to remove the currently selected trait in whatever
132 assigned this handler.
136 def add_custom(self
):
137 """Used to provide a custom dialog for new entries not included in the
143 """Used to handle adding a note to a currently selected entry.