hamster-applet r832 - trunk/hamster



Author: tbaugis
Date: Wed Mar  4 22:45:28 2009
New Revision: 832
URL: http://svn.gnome.org/viewvc/hamster-applet?rev=832&view=rev

Log:
removed unnecessary class and introduced human-readable 
time duration format  (1h 30min)

Modified:
   trunk/hamster/applet.py
   trunk/hamster/stuff.py

Modified: trunk/hamster/applet.py
==============================================================================
--- trunk/hamster/applet.py	(original)
+++ trunk/hamster/applet.py	Wed Mar  4 22:45:28 2009
@@ -291,7 +291,10 @@
         self.treeview.append_column(nameColumn)
 
         
-        timeColumn = gtk.TreeViewColumn(_("Duration"), gtk.CellRendererText(), text=3)
+        duration_cell = gtk.CellRendererText()
+        duration_cell.set_property("xalign", 1)
+        timeColumn = gtk.TreeViewColumn(_("Duration"), duration_cell, text=3)
+        
         self.treeview.append_column(timeColumn)
 
         edit_cell = gtk.CellRendererPixbuf()
@@ -408,9 +411,9 @@
             delta = datetime.datetime.now() - self.last_activity['start_time']
             duration = delta.seconds /  60
             label = "%s %s" % (self.last_activity['name'],
-                                                stuff.format_duration(duration))
+                                                stuff.format_duration(duration, False))
             self.button.set_text(self.last_activity['name'],
-                                                stuff.format_duration(duration))
+                                                stuff.format_duration(duration, False))
             
             self.glade.get_widget('stop_tracking').set_sensitive(1);
         else:
@@ -443,28 +446,63 @@
         """sets up today's tree and fills it with records
            returns information about last activity"""
         today = datetime.date.today()
-        day = stuff.DayStore(today);
-        self.treeview.set_model(day.fact_store)
 
         self.last_activity = None
         last_activity = storage.get_last_activity()
         if last_activity and last_activity["start_time"].date() >= \
                                             today - datetime.timedelta(days=1):
             self.last_activity = last_activity
+
+
+        # ID, Time, Name, Duration, Date, Description, Category
+        fact_store = gtk.ListStore(int, str, str, str, str, str, str)
+        facts = storage.get_facts(today)
+        
+        totals = {}
+        
+        for fact in facts:
+            duration = None
+            
+            if fact["delta"]:
+                duration = 24 * fact["delta"].days + fact["delta"].seconds / 60
+            
+            fact_category = fact['category']
+            
+            if fact_category not in totals:
+                totals[fact_category] = 0
+
+            if duration:
+                totals[fact_category] += duration
+
+            current_duration = stuff.format_duration(duration)
+            
+            fact_store.append([fact['id'],
+                                    stuff.escape_pango(fact['name']), 
+                                    fact["start_time"].strftime("%H:%M"), 
+                                    "%s" % current_duration,
+                                    fact["start_time"].strftime("%H:%M"),
+                                    stuff.escape_pango(fact["description"]),
+                                    stuff.escape_pango(fact["category"])])
+
+
+
+        self.treeview.set_model(fact_store)
+
         
-        if len(day.facts) == 0:
+        if len(facts) == 0:
             self.glade.get_widget("todays_scroll").hide()
             self.glade.get_widget("fact_totals").set_text(_("No records today"))
         else:
             self.glade.get_widget("todays_scroll").show()
             
-            total_string = ""
-            for total in day.totals:
-                total_string += _("%(category)s: %(duration)s, ") % \
+            total_strings = []
+            for total in totals:
+                # listing of today's categories and time spent in them
+                total_strings.append(_("%(category)s: %(duration)s") % \
                         ({'category': total,
-                          'duration': stuff.format_duration(day.totals[total])})
+                          'duration': _("%.1fh") % (totals[total] / 60.0)}))
 
-            total_string = total_string.rstrip(", ") # trailing slash
+            total_string = ", ".join(total_strings)
             self.glade.get_widget("fact_totals").set_text(total_string)
    
 

Modified: trunk/hamster/stuff.py
==============================================================================
--- trunk/hamster/stuff.py	(original)
+++ trunk/hamster/stuff.py	Wed Mar  4 22:45:28 2009
@@ -97,21 +97,28 @@
         cell.set_property("ellipsize", pango.ELLIPSIZE_END)
         self.set_cell_data_func(cell, self.activity_painter)
 
-def format_duration(minutes):
-    if minutes == None:
-        return None
+def format_duration(minutes, human = True):
+    if not minutes:
+        return ""
     
     hours = minutes / 60
-    days = hours / 24
-    hours %= 24
     minutes = minutes % 60
     formatted_duration = ""
     
-    #TODO - convert to list comprehension or that other thing
-    if days > 0:
-        formatted_duration += "%d:" % days
-    formatted_duration += "%02d:%02d" % (hours, minutes)
-            
+    if human:
+        if minutes % 60 == 0:
+            # duration in round hours
+            formatted_duration += _("%dh") % (hours)
+        elif hours == 0:
+            # duration less than hour
+            formatted_duration += _("%dmin") % (minutes % 60.0)
+        else:
+            # x hours, y minutes
+            formatted_duration += _("%dh %dmin") % (hours, minutes % 60)
+    else:
+        formatted_duration += "%02d:%02d" % (hours, minutes)
+    
+    
     return formatted_duration
 
 def dateDict(date, prefix):
@@ -153,43 +160,3 @@
     text = text.replace("<", "&lt;")
     text = text.replace(">", "&gt;")
     return text
-
-
-class DayStore(object):
-    """A day view contains a treeview for facts of the day and another
-       one for totals. It creates those widgets on init, use
-       fill_view(store) to fill the tree and calculate totals """
-
-    def __init__(self, date = None):
-        date = date or dt.date.today()
-        
-        # ID, Time, Name, Duration, Date, Description, Category
-        self.fact_store = gtk.ListStore(int, str, str, str, str, str, str)
-        
-        self.facts = storage.get_facts(date)
-        
-        self.totals = {}
-        
-        for fact in self.facts:
-            duration = None
-            
-            if fact["delta"]:
-                duration = 24 * fact["delta"].days + fact["delta"].seconds / 60
-            
-            fact_category = fact['category']
-            
-            if fact_category not in self.totals:
-                self.totals[fact_category] = 0
-
-            if duration:
-                self.totals[fact_category] += duration
-
-            current_duration = format_duration(duration)
-
-            self.fact_store.append([fact['id'], escape_pango(fact['name']), 
-                                    fact["start_time"].strftime("%H:%M"), 
-                                    current_duration,
-                                    fact["start_time"].strftime("%Y%m%d"),
-                                    escape_pango(fact["description"]),
-                                    escape_pango(fact["category"])])
-



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]