Fix ListView to make sense.
authorMatthijs Kooijman <matthijs@stdin.nl>
Tue, 3 Nov 2009 09:12:26 +0000 (10:12 +0100)
committerMatthijs Kooijman <matthijs@stdin.nl>
Tue, 3 Nov 2009 09:53:08 +0000 (10:53 +0100)
src/gui/gui.py

index f03880854f2310bdccda1216bc322c4a60e1277a..20f4d05a34ac3f8aa8294a3525f4e32700909aeb 100644 (file)
@@ -166,6 +166,21 @@ class ListView(View):
     def __init__(self):
         super(ListView, self).__init__()
         self.set_view(appuifw.Listbox(self.items(),self.entry_selected))
+        self.view.bind(EKeyUpArrow,lambda: self.arrow_key_pressed(-1))
+        self.view.bind(EKeyDownArrow,lambda: self.arrow_key_pressed(1))
+
+    def arrow_key_pressed(self, dir):
+        """
+        This function is called when an arrow key is pressed. Since we
+        don't get any "current list index has changed" events, we'll
+        have to create these ourselves this way.
+
+        Since the current index is only updated after the key event,
+        we'll have to adjust the index with the direction of the
+        keypress (-1 for up, +1 for down).
+        """
+        new_index = (self.selected_index() + dir) % len(self.items())
+        self.index_changed(new_index)
 
     def entry_selected(self):
         """
@@ -173,26 +188,26 @@ class ListView(View):
         navigates to it and push the ok button).
         """
         pass
-    
-    def index_changed(self,adjustment=None):
-        if adjustment:
-            index = self.selected_index() + adjustment
-        else:
-            index = self.selected_index()
-        if index < 0:
-            index = len(self.widgets) - 1
-        if index >= len(self.widgets):
-            index = 0
-        self.set_bindings_for_selection(index)
+
+    def index_changed(self,new_index):
+        """
+        This function is called when the index changes. The given index
+        is the new index (don't use self.selected_index() here, since it
+        won't be correct yet!).
+        """
+        pass
+
+    def items(self):
+        """ This function should return the list of items to display.
+        See appuifw.ListBox for valid elements for this list. """
+        return []
 
     def set_index(self,index):
-        if index > len(self.widgets):
-            index = len(self.widgets)
-        if index < 0:
-            index = 0
-        self.view.set_list(self.items(),index)
+        """ Changes the currently selected item to index. """
+        self.view.set_list(self.items(),index % len(self.items()))
 
     def selected_index(self):
+        """ Returns the currently selected index. """
         return self.view.current()
 
 class WidgetBasedListView(ListView):
@@ -205,6 +220,10 @@ class WidgetBasedListView(ListView):
         self.set_bindings_for_selection(0)
         ListView.run(self)
 
+    def index_changed(self,new_index):
+        self.set_bindings_for_selection(new_index)
+        super(WidgeteBasedListView, self).index_changed(new_index)
+
     def notify(self,object,attribute,new=None,old=None):
         self.refresh()
     def refresh(self):
@@ -262,8 +281,6 @@ class KeyBindingView(View):
         for key,key_name,description,function in self.key_and_menu_bindings(selected_index):
             if key:
                 self.view.bind(key,function)
-        self.view.bind(EKeyUpArrow,lambda: self.index_changed(-1))
-        self.view.bind(EKeyDownArrow,lambda: self.index_changed(1))
         
     def remove_all_key_bindings(self):
         for key in all_key_values():