changeset 224:c8f85809607b

Implement a more generalized ListWidget
author Björn Ricks <bjoern.ricks@intevation.de>
date Fri, 05 Apr 2013 20:10:37 +0200
parents aa7b1efb7689
children f958ce12ba5a
files getan/view.py
diffstat 1 files changed, 56 insertions(+), 32 deletions(-) [+]
line wrap: on
line diff
--- a/getan/view.py	Fri Apr 05 20:09:26 2013 +0200
+++ b/getan/view.py	Fri Apr 05 20:10:37 2013 +0200
@@ -16,43 +16,65 @@
 
 from getan.nodes import ProjectNode, EntryNode
 from getan.resources import gettext as _
-from getan.states    import *
-from getan.utils     import short_time, format_datetime
+from getan.utils import human_time
+from getan.walker import ListWalker
 
 logger = logging.getLogger()
 
 class ListWidget(urwid.BoxWidget):
-    def _update_view(self):
-        logger.debug("ListWidget: update view now.")
-        if self.size and len(self.rows) > self.size[1]:
-            start_idx = self.top
-            end_idx   = start_idx + self.size[1] - 6
-        else:
-            start_idx = self.top
-            end_idx   = start_idx + len(self.rows)
-        listbox = urwid.LineBox(urwid.Padding(
-            urwid.ListBox(urwid.SimpleListWalker(self.rows[start_idx:
-                                                           end_idx])),
-            ('fixed left', 1),
-            ('fixed right', 1)))
 
-        self.body = urwid.AttrWrap(listbox, 'body')
-
-        self.frame.set_body(self.body)
-        self.frame.set_footer(self.footer)
-        self.frame.set_header(self.header)
+    node_class = None
 
-    def update_focus(self, focus, unfocus=-1):
-        if self.rows:
-            self.focused = focus
-            if focus >= 0 and focus <= len(self.rows)-1:
-                self.rows[focus].focus = True
-                self.rows[focus].update_w()
-            if unfocus >= 0:
-                self.rows[unfocus].focus = False
-                self.rows[unfocus].update_w()
-        if self.size:
-            self.validate_view()
+    def __init__(self, title, rows, header=None, footer=None):
+        self.title = title
+        self.rows = [self.node_class(x) for x in rows]
+        self.header = header
+        self.footer = footer or urwid.Text("")
+        self.selection = []
+        self.set_node_rows(self.rows)
+        self.set_focus(0)
+
+    def set_title(self, title):
+        self.title = title
+        self.body.set_title(self.title)
+
+    def set_header(self, header):
+        self.header = header
+        self.frame.set_header(header)
+
+    def set_footer(self, footer):
+        self.footer = footer
+        self.frame.set_footer(footer)
+
+    def set_body(self, body):
+        self.body = body
+        self.frame.set_body(body)
+
+    def set_focus(self, idx):
+        self.frame.set_focus("body")
+        self.listbox.set_focus(idx)
+        self._invalidate()
+
+    def keypress(self, size, key):
+        logger.info("Handling keypres for %r" % self)
+        return self.frame.keypress(size, key)
+
+    def set_rows(self, rows):
+        logger.info("ListView setting rows %s" % rows)
+        if rows:
+            self.rows = [self.node_class(x) for x in rows]
+        else:
+            self.rows = []
+        self.set_node_rows(self.rows)
+
+    def set_node_rows(self, rows):
+        """ Sets node_class rows in the walker """
+        self.walker = ListWalker(self.rows, self)
+        self.listbox = urwid.ListBox(self.walker)
+        self.body = urwid.LineBox(self.listbox, title=self.title)
+        self.frame = urwid.Frame(self.body, header=self.header,
+                                 footer=self.footer)
+        self._invalidate()
 
     def render(self, size, focus=False):
         return self.frame.render(size, focus)
@@ -79,7 +101,7 @@
 
     def node_in_focus(self):
         if self.rows:
-            return self.rows[self.focused]
+            return self.listbox.get_focus()[0]
         return None
 
     def select(self):
@@ -103,6 +125,8 @@
                 node.select()
         self.set_focus(0)
 
+    def content_focus_changed(self):
+        pass
 
 class ProjectNode(urwid.WidgetWrap):
     MODES = [
This site is hosted by Intevation GmbH (Datenschutzerklärung und Impressum | Privacy Policy and Imprint)