changeset 226:f8571308abef

Move ProjectNode and EntryNode to its own module
author Björn Ricks <bjoern.ricks@intevation.de>
date Fri, 05 Apr 2013 20:16:06 +0200
parents f958ce12ba5a
children 1b29cc99f457
files getan/nodes.py getan/view.py
diffstat 2 files changed, 152 insertions(+), 130 deletions(-) [+]
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/getan/nodes.py	Fri Apr 05 20:16:06 2013 +0200
@@ -0,0 +1,147 @@
+# -*- coding: utf-8 -*-
+#
+# (c) 2013 by Björn Ricks <bjoern.ricks@intevation.de>
+#
+# This is Free Software licensed under the terms of GPLv3 or later.
+# For details see LICENSE coming with the source of 'getan'.
+#
+
+import logging
+
+import urwid
+
+from getan.utils import short_time, format_datetime, human_time
+from getan.resources import gettext as _
+
+logger = logging.getLogger(__name__)
+
+class Node(urwid.WidgetWrap):
+
+    def __init__(self, item):
+        self.selected = False
+        self.focus = False
+        self.item = item
+        w = urwid.AttrMap(self.get_widget(), None)
+        self.__super.__init__(w)
+
+    def get_widget(self):
+        return urwid.Text(' %s ' % (self.get_item_text()), wrap='clip')
+
+    def get_item_text(self):
+        return str(self.item)
+
+    def update_w(self):
+        if self.focus:
+            if self.selected:
+                self._w.set_focus_map({None: 'selected focus entry'})
+                self._w.set_attr_map({None: 'selected focus entry'})
+            else:
+                self._w.set_focus_map({None: 'focus entry'})
+                self._w.set_attr_map({None: 'focus entry'})
+        else:
+            if self.selected:
+                self._w.set_focus_map({None: 'selected entry'})
+                self._w.set_attr_map({None: 'selected entry'})
+            else:
+                self._w.set_focus_map({None: 'entry body'})
+                self._w.set_attr_map({None: 'entry body'})
+
+    def select(self):
+        logger.debug("Node: update selection of item '%s'"
+                     % self.item)
+        self.selected = not self.selected
+        self._invalidate()
+
+    def get_item(self):
+        return self.item
+
+    def selectable(self):
+        return True
+
+    def keypress(self, size, key):
+        if "enter" in key:
+            self.select()
+            return None
+        return key
+
+    def render(self, size, focus=False):
+        self.focus = focus
+        self.update_w()
+        logger.info("Render Node %r focus %s" % (self, focus))
+        return self._w.render(size, focus)
+
+    def update(self):
+        self._w = self.get_widget()
+
+
+class ProjectNode(Node):
+
+    MODES = [
+        (0, _('Total')),
+        (1, _('Year')),
+        (2, _('Month')),
+        (3, _('Week')),
+        (4, _('Day'))
+    ]
+
+    def __init__(self, proj, mode=3, indent=0):
+        self.indent = indent
+        self.mode = self.MODES[mode]
+        super(ProjectNode, self).__init__(proj)
+
+    def get_widget(self):
+        time_str = self._get_formatted_time()
+        proj_desc = self.item.desc
+        if proj_desc is None:
+            proj_desc = ""
+
+        description = urwid.Text([' ' * self.indent,
+            ('project_key', self.item.key), (' '), (proj_desc)], wrap="clip")
+        if self._get_time():
+            time = urwid.Text('%s (%s)' % (self.mode[1], time_str))
+        else:
+            time = urwid.Text('')
+        return urwid.AttrMap(urwid.Columns([description, time]), None)
+
+    def _get_formatted_time(self):
+        return human_time(self._get_time())
+
+    def _get_time(self):
+        if self.mode == self.MODES[0]:
+            return self.item.total
+        if self.mode == self.MODES[1]:
+            return self.item.year()
+        if self.mode == self.MODES[2]:
+            return self.item.month()
+        if self.mode == self.MODES[3]:
+            return self.item.week()
+        if self.mode == self.MODES[4]:
+            return self.item.day()
+        return self.item.week()
+
+    def switch_time_mode(self):
+        tmp = self.mode[0] + 1
+        if tmp > 4:
+            self.mode = self.MODES[0]
+        else:
+            self.mode = self.MODES[tmp]
+        self._w = self.get_widget()
+
+
+class EntryNode(Node):
+
+    def __init__(self, entry):
+        super(EntryNode, self).__init__(entry)
+
+    def get_widget(self):
+        logger.debug("EntryNode: update entry '%s'." % self.item.desc)
+        row = urwid.Text(' %s [%s] %s' \
+                         % (format_datetime(self.item.start), 
+                            short_time(self.item.duration().seconds),
+                            self.item.desc), wrap='clip')
+        return urwid.AttrMap(row, None)
+
+    def render(self, size, focus=False):
+        logger.info("Render EntryNode %r focus %s" % (self, focus))
+        return super(EntryNode, self).render(size, focus)
+
--- a/getan/view.py	Fri Apr 05 20:14:20 2013 +0200
+++ b/getan/view.py	Fri Apr 05 20:16:06 2013 +0200
@@ -128,90 +128,6 @@
     def content_focus_changed(self):
         pass
 
-class ProjectNode(urwid.WidgetWrap):
-    MODES = [
-        (0, _('Total')),
-        (1, _('Year')),
-        (2, _('Month')),
-        (3, _('Week')),
-        (4, _('Day'))
-    ]
-
-    def __init__(self, proj, mode=3, indent=0):
-        self.selected   = False
-        self.focus      = False
-        self.indent     = indent
-        self.mode       = self.MODES[mode]
-        self.item       = proj
-        w               = self.update()
-        self.__super.__init__(w)
-        self.update_w()
-
-    def update(self):
-        logger.debug("Update ProjectNode '%s'" % self.item.key)
-        time_str    = self._get_formatted_time()
-        proj_desc = self.item.desc
-        if proj_desc is None:
-            proj_desc = ""
-
-        description = urwid.Text([' ' * self.indent,
-                                  ('project_key', self.item.key),
-                                  (' '),
-                                  (proj_desc)], wrap="clip")
-        if self._get_time():
-            time = urwid.Text('%s (%s)' % (self.mode[1], time_str))
-        else:
-            time = urwid.Text('')
-        self.widget = urwid.AttrWrap(urwid.Columns([description, time]),None)
-        self._w     = self.widget
-        return self._w
-
-    def _get_formatted_time(self):
-        return human_time(self._get_time())
-
-    def _get_time(self):
-        if self.mode == self.MODES[0]:
-            return self.item.total
-        if self.mode == self.MODES[1]:
-            return self.item.year()
-        if self.mode == self.MODES[2]:
-            return self.item.month()
-        if self.mode == self.MODES[3]:
-            return self.item.week()
-        if self.mode == self.MODES[4]:
-            return self.item.day()
-        return self.item.week()
-
-    def get_item(self):
-        return self.item
-
-    def switch_time_mode(self):
-        tmp = self.mode[0] + 1
-        if tmp > 4:
-            self.mode = self.MODES[0]
-        else:
-            self.mode = self.MODES[tmp]
-        self.update()
-
-    def update_w(self):
-        if self.focus:
-            if self.selected:
-                self._w.focus_attr = 'selected focus'
-                self._w.attr       = 'selected focus'
-            else:
-                self._w.focus_attr = 'focus'
-                self._w.attr       = 'focus'
-        else:
-            if self.selected:
-                self._w.focus_attr = 'selected'
-                self._w.attr       = 'selected'
-            else:
-                self._w.focus_attr = 'body'
-                self._w.attr       = 'body'
-
-    def select(self):
-        self.selected = not self.selected
-        self.update_w()
 
 
 class ProjectList(ListWidget):
@@ -322,52 +238,11 @@
         self.update_rows()
         self.set_focus(0)
 
-
-class EntryNode(urwid.WidgetWrap):
-
-    def __init__(self, entry):
-        self.selected = False
-        self.focus    = False
-        self.item     = entry
-        w             = self.update()
-        self.__super.__init__(w)
-        self.update_w()
-
-    def update(self):
-        logger.debug("EntryNode: update entry '%s'." % self.item.desc)
-        row = urwid.Text(' %s [%s] %s' \
-                         % (format_datetime(self.item.start), 
-                            short_time(self.item.duration().seconds),
-                            self.item.desc), wrap='clip')
-        self.widget = urwid.AttrWrap(row, None)
-        self._w     = self.widget
-        self.update_w()
-        return self._w
-
-    def update_w(self):
-        if self.focus:
-            if self.selected:
-                self._w.focus_attr = 'selected focus entry'
-                self._w.attr       = 'selected focus entry'
-            else:
-                self._w.focus_attr = 'focus entry'
-                self._w.attr       = 'focus entry'
-        else:
-            if self.selected:
-                self._w.focus_attr = 'selected entry'
-                self._w.attr       = 'selected entry'
-            else:
-                self._w.focus_attr = 'entry body'
-                self._w.attr       = 'entry body'
-
-    def select(self):
-        logger.debug("EntryNode: update selection of entry '%s'"
-                     % self.item.desc)
-        self.selected = not self.selected
-        self.update_w()
-
-    def get_item(self):
-        return self.item
+    def content_focus_changed(self):
+        item = self.item_in_focus()
+        logger.info("Conten in focus changed %s" % item)
+        if item:
+            self.controller.update_entries(item)
 
 
 class EntryList(ListWidget):
This site is hosted by Intevation GmbH (Datenschutzerklärung und Impressum | Privacy Policy and Imprint)