1 from config.config import *
2 from model.projects import Projects
6 from log.logging import logger
7 from e32 import Ao_lock, in_emulator
8 from key_codes import *
14 for k, v in cfg.items():
15 v = cfg.format_value(v)
16 if isinstance(v, int) or isinstance(v, long):
19 elif isinstance(v, list) or isinstance(v, tuple):
21 if not isinstance(item, unicode):
22 raise Exception("list can contain only unicode objects, "\
23 "object %r is not supported" % item)
26 elif isinstance(v, unicode):
29 raise Exception("%s has non-supported value" % k)
31 fields.append((unicode(k), tname, v))
34 form = appuifw.Form(fields=fields, flags=appuifw.FFormEditModeOnly | \
35 appuifw.FFormDoubleSpaced)
40 form.save_hook = save_hook
44 # return true if user saved, false otherwise
48 for label, tname, value in form:
50 value = (value[0], int(value[1]))
52 cfg[str(label)] = cfg.parse_value(value)
60 def applicable_functions(obj,allowed_function_names):
61 function_names = [function_name for function_name in dir(obj) if function_name in allowed_function_names]
62 return [eval('obj.%s'%function_name) for function_name in function_names]
64 def get_key(key_name):
68 key=eval('EKey%s'%key_name)
72 return filter(lambda entry:entry[0:4]=='EKey',dir(key_codes))
92 object.old_gui = appuifw.app.body
93 object.old_menu = appuifw.app.menu
94 object.old_exit_key_handler = appuifw.app.exit_key_handler
95 object.old_title=appuifw.app.title
97 def restore_gui(object):
98 appuifw.app.body = object.old_gui
99 appuifw.app.menu = object.old_menu
100 appuifw.app.exit_key_handler = object.old_exit_key_handler
101 appuifw.app.title = object.old_title
107 self.lock = Ao_lock()
108 self.exit_flag = False
109 super(View, self).__init__()
111 def set_title(self, title):
114 def set_view(self, view):
116 Sets the main view to be displayed (e.g., an appuifw.Listbox
122 self.adjustment = None
124 appuifw.app.screen=COMMON_CONFIG['screen'].encode('utf-8')
125 appuifw.app.title=self.title
126 appuifw.app.body=self.view
127 appuifw.app.exit_key_handler=self.exit
130 while not self.exit_flag:
138 self.exit_flag = True
143 Update the gui after a change in model or some user interaction.
144 Should be filled by subclasses.
146 appuifw.app.menu=self.get_menu_entries()
148 def get_menu_entries(self):
149 """ Returns a list of menu entries to display. Will be
150 automatically updated on each refresh.
152 Each menu entry is a tuple of a title for the entry and a
153 function to call when the entry is selected.
157 class ListView(View):
159 super(ListView, self).__init__()
160 self.set_view(appuifw.Listbox(self.items(),self.entry_selected))
162 def entry_selected(self):
164 This function is called when the user selects an an entry (e.g.,
165 navigates to it and push the ok button).
169 def update(self,subject=None):
170 #logger.log(u'Updated %s'%repr(self))
175 def index_changed(self,adjustment=None):
177 index = self.selected_index() + adjustment
179 index = self.selected_index()
181 index = len(self.widgets) - 1
182 if index >= len(self.widgets):
184 self.set_bindings_for_selection(index)
186 def set_index(self,index):
187 if index > len(self.widgets):
188 index = len(self.widgets)
191 self.view.set_list(self.items(),index)
193 def selected_index(self):
194 return self.view.current()
196 class WidgetBasedListView(ListView):
198 self.widgets = self.generate_widgets()
199 super(WidgetBasedListView,self).__init__()
203 self.set_bindings_for_selection(0)
206 def notify(self,object,attribute,new=None,old=None):
209 self.widgets = self.generate_widgets()
210 self.redisplay_widgets()
211 super(WidgetBasedListView,self).refresh()
212 def redisplay_widgets(self):
213 self.set_index(self.selected_index())
215 return self.all_widget_texts()
216 def all_widget_texts(self):
217 return [entry.list_repr() for entry in self.widgets]
221 def current_widget(self):
222 return self.widgets[self.selected_index()]
225 class KeyBindingView(View):
228 self.binding_map = {}
229 super(KeyBindingView,self).__init__()
231 def set_keybindings(self, binding_map):
233 Set a new map of key bindings. This map maps method names to a
234 tuple of keyname and description.
236 The method name refers to a method on the selected item, or the
239 Example: { 'search_item' : ('0', 'Search item') }
242 self.binding_map = binding_map
244 def get_menu_entries(self):
246 for key,key_name,description,function in self.key_and_menu_bindings(self.selected_index()):
247 if description != '':
249 if key_name == 'Backspace': key_name='C'
250 description='[%s] '%key_name +description
252 description=' '+description
253 menu_entries.append((description,function))
254 menu_entries.append((u'Exit', self.exit))
255 return menu_entries + super(KeyBindingView, self).get_menu_entries()
257 def set_bindings_for_selection(self,selected_index):
258 self.remove_all_key_bindings()
260 for key,key_name,description,function in self.key_and_menu_bindings(selected_index):
262 self.view.bind(key,function)
263 self.view.bind(EKeyUpArrow,lambda: self.index_changed(-1))
264 self.view.bind(EKeyDownArrow,lambda: self.index_changed(1))
266 def remove_all_key_bindings(self):
267 for key in all_key_values():
268 self.view.bind(key,no_action)
270 class SearchableListView(WidgetBasedListView):
272 self.current_entry_filter_index = -1
273 self.entry_filters = []
274 self.filtered_list = lambda:[]
276 super(SearchableListView,self).__init__()
278 def set_filters(self, entry_filters):
280 Set the filters that could be applied to this list. Each filter
281 can be applied in turn by calling switch_entry_filter (for
282 example from a key binding).
284 The entry_filters argument should be a list of filters. The
285 active filter is stored into self.filtered_list and should be
286 processed by generate_widgets in the subclass.
288 self.current_entry_filter_index = 0
289 self.entry_filters = entry_filters
290 self.filtered_list = self.entry_filters[0]
292 def search_item(self):
293 selected_item = appuifw.selection_list(self.all_widget_texts(),search_field=1)
294 if selected_item == None or selected_item == -1:
295 selected_item = self.selected_index()
296 self.view.set_list(self.items(),selected_item)
297 self.set_bindings_for_selection(selected_item)
299 def switch_entry_filter(self):
300 self.current_entry_filter_index += 1
301 self.filtered_list = self.entry_filters[self.current_entry_filter_index % len(self.entry_filters)]
305 class EditableListView(SearchableListView,KeyBindingView):
307 super(EditableListView, self).__init__()
309 def key_and_menu_bindings(self,selected_index):
310 key_and_menu_bindings=[]
311 for function in applicable_functions(self.widgets[selected_index],self.binding_map)+\
312 applicable_functions(self,self.binding_map):
313 execute_and_update_function = self.execute_and_update(function)
314 (key,description) = self.binding_map[function.__name__]
315 key_and_menu_bindings.append((get_key(key),key,description,execute_and_update_function))
316 return key_and_menu_bindings
318 def entry_selected(self):
319 self.current_widget().change()
321 def execute_and_update(self,function):
322 return lambda: (function(),self.refresh(),self.index_changed())
324 def notify(self,item,attribute,new=None,old=None):
327 #class DisplayableFunction:
328 # def __init__(self,display_name,function):
329 # self.display_name = display_name
330 # self.function = function
331 # def list_repr(self):
332 # return self.display_name
337 __all__= ('EditableListView','show_config')