Extract some common view stuff into a View class.
authorMatthijs Kooijman <matthijs@stdin.nl>
Mon, 17 Aug 2009 22:03:55 +0000 (00:03 +0200)
committerMatthijs Kooijman <matthijs@stdin.nl>
Mon, 17 Aug 2009 22:03:55 +0000 (00:03 +0200)
This class forms a superclass to ListView, and does all the
non-list-specific stuff. This also moves some code from classes below
ListView into View, since that code really shouldn't be where it was.

src/gui/gui.py

index e4b3db05d88086b6aea5f619f31c76aa2384ba34..a8f217164e1d3b399dc6e63a683be89e814d2e0e 100644 (file)
@@ -93,7 +93,6 @@ def save_gui(object):
     object.old_menu = appuifw.app.menu
     object.old_exit_key_handler = appuifw.app.exit_key_handler
     object.old_title=appuifw.app.title
-    object.lock = Ao_lock()
 
 def restore_gui(object):
     appuifw.app.body = object.old_gui
@@ -101,22 +100,28 @@ def restore_gui(object):
     appuifw.app.exit_key_handler = object.old_exit_key_handler
     appuifw.app.title = object.old_title
 
-
-class ListView(object):
+class View(object):
     def __init__(self):
-        self.view = appuifw.Listbox(self.items(),self.change_entry)
-        super(ListView, self).__init__()
+        self.title = None
+        self.view = None
+        self.lock = Ao_lock()
+        self.exit_flag = False
+        super(View, self).__init__()
 
     def set_title(self, title):
         self.title = title
-    
-    def change_entry(self):
-        pass
-    
+   
+    def set_view(self, view):
+        """
+        Sets the main view to be displayed (e.g., an appuifw.Listbox
+        instance).
+        """
+        self.view = view
+
     def run(self):
         self.adjustment = None
-        appuifw.app.screen=COMMON_CONFIG['screen'].encode('utf-8')
         save_gui(self)
+        appuifw.app.screen=COMMON_CONFIG['screen'].encode('utf-8')
         appuifw.app.title=self.title
         appuifw.app.body=self.view
         appuifw.app.exit_key_handler=self.exit
@@ -128,10 +133,27 @@ class ListView(object):
         except:
             pass
         restore_gui(self)
+
     def exit(self):
         self.exit_flag = True
         self.lock.signal()
+        super(View, self).exit()
+
+    def refresh(self):
+        """
+        Update the gui after a change in model or some user interaction.
+        Should be filled by subclasses.
+        """
+        super(View, self).refresh()
+
+class ListView(View):
+    def __init__(self):
+        super(ListView, self).__init__()
+        self.set_view(appuifw.Listbox(self.items(),self.change_entry))
 
+    def change_entry(self):
+        pass
+    
     def update(self,subject=None):
         #logger.log(u'Updated %s'%repr(self))
         if self.lock:
@@ -167,7 +189,6 @@ class WidgetBasedListView(ListView):
     def __init__(self):
         self.widgets = self.generate_widgets()
         super(WidgetBasedListView,self).__init__()
-        self.exit_flag = False
 
     def run(self):
         self.refresh()