[hamster-applet] still cleaning



commit dbd2fc3769932c58bb3fc0110981181e63796fcd
Author: Toms Bauģis <toms baugis gmail com>
Date:   Sat Dec 5 23:04:09 2009 +0000

    still cleaning

 data/stats_reports.ui    |   19 ++-
 hamster/stats_reports.py |  405 +++++++++++++++-------------------------------
 hamster/stats_stats.py   |   13 ++
 3 files changed, 160 insertions(+), 277 deletions(-)
---
diff --git a/data/stats_reports.ui b/data/stats_reports.ui
index 5dad34a..c70339f 100644
--- a/data/stats_reports.ui
+++ b/data/stats_reports.ui
@@ -12,11 +12,12 @@
             <property name="visible">True</property>
             <property name="events">GDK_POINTER_MOTION_MASK | GDK_POINTER_MOTION_HINT_MASK | GDK_BUTTON_PRESS_MASK | GDK_BUTTON_RELEASE_MASK</property>
             <child>
-              <object class="GtkToolButton" id="reports_prev">
+              <object class="GtkToolButton" id="prev">
                 <property name="visible">True</property>
                 <property name="events">GDK_POINTER_MOTION_MASK | GDK_POINTER_MOTION_HINT_MASK | GDK_BUTTON_PRESS_MASK | GDK_BUTTON_RELEASE_MASK</property>
                 <property name="label" translatable="yes">Previous</property>
                 <property name="stock_id">gtk-go-back</property>
+                <signal name="clicked" handler="on_prev_clicked"/>
               </object>
               <packing>
                 <property name="expand">False</property>
@@ -24,12 +25,13 @@
               </packing>
             </child>
             <child>
-              <object class="GtkToolButton" id="reports_home">
+              <object class="GtkToolButton" id="home">
                 <property name="visible">True</property>
                 <property name="events">GDK_POINTER_MOTION_MASK | GDK_POINTER_MOTION_HINT_MASK | GDK_BUTTON_PRESS_MASK | GDK_BUTTON_RELEASE_MASK</property>
                 <property name="label" translatable="yes">This Week</property>
                 <property name="use_underline">True</property>
                 <property name="stock_id">gtk-home</property>
+                <signal name="clicked" handler="on_home_clicked"/>
               </object>
               <packing>
                 <property name="expand">False</property>
@@ -37,11 +39,12 @@
               </packing>
             </child>
             <child>
-              <object class="GtkToolButton" id="reports_next">
+              <object class="GtkToolButton" id="next">
                 <property name="visible">True</property>
                 <property name="events">GDK_POINTER_MOTION_MASK | GDK_POINTER_MOTION_HINT_MASK | GDK_BUTTON_PRESS_MASK | GDK_BUTTON_RELEASE_MASK</property>
                 <property name="label" translatable="yes">Next</property>
                 <property name="stock_id">gtk-go-forward</property>
+                <signal name="clicked" handler="on_next_clicked"/>
               </object>
               <packing>
                 <property name="expand">False</property>
@@ -58,13 +61,14 @@
               </packing>
             </child>
             <child>
-              <object class="GtkRadioToolButton" id="reports_day_view">
+              <object class="GtkRadioToolButton" id="day">
                 <property name="visible">True</property>
                 <property name="is_important">True</property>
                 <property name="label" translatable="yes"> _Day</property>
                 <property name="use_underline">True</property>
                 <property name="icon_widget">day_icon1</property>
                 <property name="active">True</property>
+                <signal name="toggled" handler="on_day_toggled"/>
               </object>
               <packing>
                 <property name="expand">False</property>
@@ -72,7 +76,7 @@
               </packing>
             </child>
             <child>
-              <object class="GtkRadioToolButton" id="reports_week_view">
+              <object class="GtkRadioToolButton" id="week">
                 <property name="visible">True</property>
                 <property name="events">GDK_POINTER_MOTION_MASK | GDK_POINTER_MOTION_HINT_MASK | GDK_BUTTON_PRESS_MASK | GDK_BUTTON_RELEASE_MASK</property>
                 <property name="is_important">True</property>
@@ -80,6 +84,7 @@
                 <property name="use_underline">True</property>
                 <property name="icon_widget">week_icon1</property>
                 <property name="active">True</property>
+                <signal name="toggled" handler="on_week_toggled"/>
               </object>
               <packing>
                 <property name="expand">False</property>
@@ -87,7 +92,7 @@
               </packing>
             </child>
             <child>
-              <object class="GtkRadioToolButton" id="reports_month_view">
+              <object class="GtkRadioToolButton" id="month">
                 <property name="visible">True</property>
                 <property name="events">GDK_POINTER_MOTION_MASK | GDK_POINTER_MOTION_HINT_MASK | GDK_BUTTON_PRESS_MASK | GDK_BUTTON_RELEASE_MASK</property>
                 <property name="is_important">True</property>
@@ -95,6 +100,7 @@
                 <property name="use_underline">True</property>
                 <property name="icon_widget">month_icon1</property>
                 <property name="active">True</property>
+                <signal name="toggled" handler="on_month_toggled"/>
               </object>
               <packing>
                 <property name="expand">False</property>
@@ -117,6 +123,7 @@
                 <property name="label" translatable="yes">Save Report</property>
                 <property name="use_underline">True</property>
                 <property name="stock_id">gtk-save</property>
+                <signal name="clicked" handler="on_report_button_clicked"/>
               </object>
               <packing>
                 <property name="expand">False</property>
diff --git a/hamster/stats_reports.py b/hamster/stats_reports.py
index 7b9f039..442157e 100644
--- a/hamster/stats_reports.py
+++ b/hamster/stats_reports.py
@@ -45,103 +45,6 @@ import time
 from hamster.i18n import C_
 
 
-def parent_painter(column, cell, model, iter):
-    cell_text = "%s (&#215;%s)" % (stuff.escape_pango(model.get_value(iter, 0)), model.get_value(iter, 3))
-    
-    if model.iter_parent(iter) is None:
-        if model.get_path(iter) == (0,):
-            text = '<span weight="heavy">%s</span>' % cell_text
-        else:
-            text = '<span weight="heavy" rise="-20000">%s</span>' % cell_text
-            
-        cell.set_property('markup', text)
-
-    else:
-        cell.set_property('markup', cell_text)
-
-def duration_painter(column, cell, model, iter):
-    cell.set_property('xalign', 1)
-
-
-    text = model.get_value(iter, 2)
-    if model.iter_parent(iter) is None:
-        if model.get_path(iter) == (0,):
-            text = '<span weight="heavy">%s</span>' % text
-        else:
-            text = '<span weight="heavy" rise="-20000">%s</span>' % text
-    cell.set_property('markup', text)
-
-class TotalsTree(gtk.TreeView):
-    def __init__(self):
-        gtk.TreeView.__init__(self)
-        
-        self.set_headers_visible(False)
-        self.set_show_expanders(True)
-
-        # group name / activity name, tags, duration, occurences
-        self.set_model(gtk.TreeStore(str, gobject.TYPE_PYOBJECT, str, str))
-
-        # name
-        nameColumn = gtk.TreeViewColumn()
-        nameColumn.set_expand(True)
-        nameCell = gtk.CellRendererText()
-        #nameCell.set_property("ellipsize", pango.ELLIPSIZE_END)
-        nameColumn.pack_start(nameCell, True)
-        nameColumn.set_cell_data_func(nameCell, parent_painter)
-        self.append_column(nameColumn)
-
-        tag_cell = widgets.TagCellRenderer()
-        tag_cell.font_size = 8;
-        tagColumn = gtk.TreeViewColumn("", tag_cell, data=1)
-        tagColumn.set_expand(True)
-        self.append_column(tagColumn)
-
-        # duration
-        timeColumn = gtk.TreeViewColumn()
-        timeCell = gtk.CellRendererText()
-        timeColumn.pack_end(timeCell, True)
-        timeColumn.set_cell_data_func(timeCell, duration_painter)
-        self.append_column(timeColumn)
-
-
-        self.show()
-    
-    def clear(self):
-        self.model.clear()
-        
-    @property
-    def model(self):
-        return self.get_model()
-        
-    def add_total(self, total, parent = None):
-        duration = 24 * 60 * total[3].days + total[3].seconds / 60
-
-
-        self.model.append(parent, [total[1],
-                                   total[2],
-                                   stuff.format_duration(duration),
-                                   str(total[4])])
-
-    def add_group(self, group_label, totals):
-        total_duration = sum([total[3].seconds for total in totals]) / 60.0
-        total_occurences = sum([total[4] for total in totals])
-
-        
-        # adds group of facts with the given label
-        group_row = self.model.append(None,
-                                    [group_label,
-                                     None,
-                                     stuff.format_duration(total_duration),
-                                     str(total_occurences)])
-        
-        for total in totals:
-            self.add_total(total, group_row)
-
-        self.expand_all()
-
-
-        
-
 class ReportsBox(gtk.VBox):
     def __init__(self):
         gtk.VBox.__init__(self)
@@ -164,36 +67,6 @@ class ReportsBox(gtk.VBox):
         self.get_widget("totals_tree_box").add(self.totals_tree)
         
         
-        
-        facts = runtime.storage.get_facts(self.start_date, self.end_date)
-
-
-        #first group by category, activity and tags
-        #sort before grouping
-        facts = sorted(facts, key = lambda fact:(fact["category"], fact["name"], fact["tags"]))
-
-        totals = []
-        for group, facts in groupby(facts, lambda fact:(fact["category"], fact["name"], fact["tags"])):
-            facts = list(facts)
-            total_duration = dt.timedelta()
-            for fact in facts:
-                total_duration += fact["delta"]
-            
-            group = list(group)
-            group.extend([total_duration, len(facts)])
-            totals.append(group)
-
-        # second iteration - group the interim result by category
-        for category, totals in groupby(totals, lambda total:total[0]):
-            self.totals_tree.add_group(category, list(totals))
-
-
-        return
-
-        self.fact_store = gtk.TreeStore(int, str, str, str, str, str, gobject.TYPE_PYOBJECT) 
-        self.setup_tree()
-        
-        
         #graphs
         self.background = (0.975, 0.975, 0.975)
         self.get_widget("graph_frame").modify_bg(gtk.STATE_NORMAL,
@@ -233,10 +106,10 @@ class ReportsBox(gtk.VBox):
 
         
         
-        self.week_view = self.get_widget("reports_week_view")
-        self.month_view = self.get_widget("reports_month_view")
+        self.week_view = self.get_widget("week")
+        self.month_view = self.get_widget("month")
         self.month_view.set_group(self.week_view)
-        self.day_view = self.get_widget("reports_day_view")
+        self.day_view = self.get_widget("day")
         self.day_view.set_group(self.week_view)
         
         #initiate the form in the week view
@@ -246,84 +119,47 @@ class ReportsBox(gtk.VBox):
         runtime.dispatcher.add_handler('activity_updated', self.after_activity_update)
         runtime.dispatcher.add_handler('day_updated', self.after_fact_update)
 
-        selection = self.fact_tree.get_selection()
-        selection.connect('changed', self.on_fact_selection_changed,
-                          self.fact_store)
+
         self.popular_categories = [cat[0] for cat in runtime.storage.get_popular_categories()]
 
         self._gui.connect_signals(self)
-        self.fact_tree.grab_focus()
+        self.totals_tree.grab_focus()
 
         
         self.config = GconfStore()
         runtime.dispatcher.add_handler('gconf_on_day_start_changed', self.on_day_start_changed)
 
         self.report_chooser = None
+        self.fill_totals_tree()
         self.do_graph()
 
-    def setup_tree(self):
-        def parent_painter(column, cell, model, iter):
-            cell_text = model.get_value(iter, 1)
-            if model.iter_parent(iter) is None:
-                if model.get_path(iter) == (0,):
-                    text = '<span weight="heavy">%s</span>' % cell_text
-                else:
-                    text = '<span weight="heavy" rise="-20000">%s</span>' % cell_text
-                    
-                cell.set_property('markup', text)
-    
-            else:
-                activity_name = stuff.escape_pango(cell_text)
-                description = stuff.escape_pango(model.get_value(iter, 4))
-                category = stuff.escape_pango(model.get_value(iter, 5))
-
-                markup = stuff.format_activity(activity_name,
-                                               category,
-                                               description,
-                                               pad_description = True)            
-                cell.set_property('markup', markup)
-
-        def duration_painter(column, cell, model, iter):
-            cell.set_property('xalign', 1)
-            cell.set_property('yalign', 0)
-    
 
-            text = model.get_value(iter, 2)
-            if model.iter_parent(iter) is None:
-                if model.get_path(iter) == (0,):
-                    text = '<span weight="heavy">%s</span>' % text
-                else:
-                    text = '<span weight="heavy" rise="-20000">%s</span>' % text
-            cell.set_property('markup', text)
-    
 
-        self.fact_tree = self.get_widget("facts")
-        self.fact_tree.set_headers_visible(False)
-        self.fact_tree.set_tooltip_column(1)
-        self.fact_tree.set_property("show-expanders", False)
+    def fill_totals_tree(self, facts = None):        
+        facts = facts or runtime.storage.get_facts(self.start_date, self.end_date)
 
-        # name
-        nameColumn = gtk.TreeViewColumn()
-        nameColumn.set_expand(True)
-        nameCell = gtk.CellRendererText()
-        nameCell.set_property("ellipsize", pango.ELLIPSIZE_END)
-        nameColumn.pack_start(nameCell, True)
-        nameColumn.set_cell_data_func(nameCell, parent_painter)
-        self.fact_tree.append_column(nameColumn)
 
-        # duration
-        timeColumn = gtk.TreeViewColumn()
-        timeCell = gtk.CellRendererText()
-        timeColumn.pack_end(timeCell, True)
-        timeColumn.set_cell_data_func(timeCell, duration_painter)
+        #first group by category, activity and tags
+        #sort before grouping
+        facts = sorted(facts, key = lambda fact:(fact["category"], fact["name"], fact["tags"]))
 
+        totals = []
+        for group, facts in groupby(facts, lambda fact:(fact["category"], fact["name"], fact["tags"])):
+            facts = list(facts)
+            total_duration = dt.timedelta()
+            for fact in facts:
+                total_duration += fact["delta"]
+            
+            group = list(group)
+            group.extend([total_duration, len(facts)])
+            totals.append(group)
 
+        self.totals_tree.clear()
+        # second iteration - group the interim result by category
+        for category, totals in groupby(totals, lambda total:total[0]):
+            self.totals_tree.add_group(category, list(totals))
 
 
-        self.fact_tree.append_column(timeColumn)
-        
-        self.fact_tree.set_model(self.fact_store)
-    
     def on_graph_frame_size_allocate(self, widget, new_size):
         w = min(new_size.width / 4, 200)
         
@@ -331,48 +167,7 @@ class ReportsBox(gtk.VBox):
         self.category_chart.legend_width = w
         self.get_widget("totals_by_category").set_size_request(w + 40, -1)
     
-    def fill_tree(self, facts):
-        day_dict = {}
-        for day, facts in groupby(facts, lambda fact: fact["date"]):
-            day_dict[day] = sorted(list(facts),
-                                   key=lambda fact: fact["start_time"])
-        
-        for i in range((self.end_date - self.start_date).days  + 1):
-            current_date = self.start_date + dt.timedelta(i)
-            
-            # Date format for the label in overview window fact listing
-            # Using python datetime formatting syntax. See:
-            # http://docs.python.org/library/time.html#time.strftime
-            fact_date = current_date.strftime(C_("overview list", "%A, %b %d"))
-            
-            day_total = dt.timedelta()
-            for fact in day_dict.get(current_date, []):
-                day_total += fact["delta"]
-
-            day_row = self.fact_store.append(None,
-                                             [-1,
-                                              fact_date,
-                                              stuff.format_duration(day_total),
-                                              current_date.strftime('%Y-%m-%d'),
-                                              "",
-                                              "",
-                                              None])
-
-            for fact in day_dict.get(current_date, []):
-                self.fact_store.append(day_row,
-                                       [fact["id"],
-                                        fact["start_time"].strftime('%H:%M') + " " +
-                                        fact["name"],
-                                        stuff.format_duration(fact["delta"]),
-                                        fact["start_time"].strftime('%Y-%m-%d'),
-                                        fact["description"],
-                                        fact["category"],
-                                        fact
-                                        ])
-
-        self.fact_tree.expand_all()
 
-        
     def do_charts(self, facts):
         all_categories = self.popular_categories
         
@@ -490,9 +285,8 @@ class ReportsBox(gtk.VBox):
 
 
         self.get_widget("report_button").set_sensitive(len(facts) > 0)
-        self.fact_store.clear()
-        
-        self.fill_tree(facts)
+
+        self.fill_totals_tree(facts)
 
         if not facts:
             self.get_widget("graphs").hide()
@@ -519,46 +313,9 @@ class ReportsBox(gtk.VBox):
         self.do_graph()
     
     def after_fact_update(self, event, date):
-        self.stat_facts = runtime.storage.get_facts(dt.date(1970, 1, 1), dt.date.today())
         self.popular_categories = [cat[0] for cat in runtime.storage.get_popular_categories()]
-        
-        if self.get_widget("pages").get_current_page() == 0:
-            self.do_graph()
-        else:
-            self.stats()
-        
-    def on_fact_selection_changed(self, selection, model):
-        """ enables and disables action buttons depending on selected item """
-        (model, iter) = selection.get_selected()
-
-        id = -1
-        if iter:
-            id = model[iter][0]
-
-        self.get_widget('remove').set_sensitive(id != -1)
-        self.get_widget('edit').set_sensitive(id != -1)
-
-        return True
-
-    def on_facts_row_activated(self, tree, path, column):
-        selection = tree.get_selection()
-        (model, iter) = selection.get_selected()
-        custom_fact = CustomFactController(self, None, model[iter][0])
-        custom_fact.show()
-        
-    def on_add_clicked(self, button):
-        selection = self.fact_tree.get_selection()
-        (model, iter) = selection.get_selected()
-
-        selected_date = self.view_date
-        if iter:
-            selected_date = model[iter][3].split("-")
-            selected_date = dt.date(int(selected_date[0]),
-                                    int(selected_date[1]),
-                                    int(selected_date[2]))
+        self.do_graph()
 
-        custom_fact = CustomFactController(self, selected_date)
-        custom_fact.show()
 
     def on_prev_clicked(self, button):
         if self.day_view.get_active():
@@ -737,6 +494,112 @@ class ReportsBox(gtk.VBox):
 
 
 
+def parent_painter(column, cell, model, iter):
+    count = int(model.get_value(iter, 3))
+    
+    if count > 1:
+        cell_text = "%s (&#215;%s)" % (stuff.escape_pango(model.get_value(iter, 0)), count)
+    else:
+        cell_text = "%s" % stuff.escape_pango(model.get_value(iter, 0))
+        
+    
+    if model.iter_parent(iter) is None:
+        if model.get_path(iter) == (0,):
+            text = '<span weight="heavy">%s</span>' % cell_text
+        else:
+            text = '<span weight="heavy" rise="-20000">%s</span>' % cell_text
+            
+        cell.set_property('markup', text)
+
+    else:
+        cell.set_property('markup', cell_text)
+
+def duration_painter(column, cell, model, iter):
+    cell.set_property('xalign', 1)
+
+
+    text = model.get_value(iter, 2)
+    if model.iter_parent(iter) is None:
+        if model.get_path(iter) == (0,):
+            text = '<span weight="heavy">%s</span>' % text
+        else:
+            text = '<span weight="heavy" rise="-20000">%s</span>' % text
+    cell.set_property('markup', text)
+
+class TotalsTree(gtk.TreeView):
+    def __init__(self):
+        gtk.TreeView.__init__(self)
+        
+        self.set_headers_visible(False)
+        self.set_show_expanders(True)
+
+        # group name / activity name, tags, duration, occurences
+        self.set_model(gtk.TreeStore(str, gobject.TYPE_PYOBJECT, str, str))
+        
+        # name
+        nameColumn = gtk.TreeViewColumn()
+        nameColumn.set_expand(True)
+        nameCell = gtk.CellRendererText()
+        #nameCell.set_property("ellipsize", pango.ELLIPSIZE_END)
+        nameColumn.pack_start(nameCell, True)
+        nameColumn.set_cell_data_func(nameCell, parent_painter)
+        self.append_column(nameColumn)
+
+        tag_cell = widgets.TagCellRenderer()
+        tag_cell.font_size = 8;
+        tagColumn = gtk.TreeViewColumn("", tag_cell, data=1)
+        tagColumn.set_expand(True)
+
+        self.append_column(tagColumn)
+
+        # duration
+        timeColumn = gtk.TreeViewColumn()
+        timeCell = gtk.CellRendererText()
+        timeColumn.pack_end(timeCell, True)
+        timeColumn.set_cell_data_func(timeCell, duration_painter)
+        self.append_column(timeColumn)
+
+
+        self.show()
+    
+    def clear(self):
+        self.model.clear()
+        
+    @property
+    def model(self):
+        return self.get_model()
+        
+    def add_total(self, total, parent = None):
+        duration = 24 * 60 * total[3].days + total[3].seconds / 60
+
+
+        self.model.append(parent, [total[1],
+                                   total[2],
+                                   stuff.format_duration(duration),
+                                   str(total[4])])
+
+    def add_group(self, group_label, totals):
+        total_duration = sum([total[3].seconds for total in totals]) / 60.0
+        total_occurences = sum([total[4] for total in totals])
+
+        
+        # adds group of facts with the given label
+        group_row = self.model.append(None,
+                                    [group_label,
+                                     None,
+                                     stuff.format_duration(total_duration),
+                                     str(total_occurences)])
+        
+        for total in totals:
+            self.add_total(total, group_row)
+
+        self.expand_all()
+
+
+        
+
+
+
 if __name__ == "__main__":
     gtk.window_set_default_icon_name("hamster-applet")    
     window = gtk.Window()
diff --git a/hamster/stats_stats.py b/hamster/stats_stats.py
index 56cd3a2..3653993 100644
--- a/hamster/stats_stats.py
+++ b/hamster/stats_stats.py
@@ -63,6 +63,8 @@ class StatsBox(gtk.VBox):
         self.get_widget("explore_everything").add(self.timeline)
         self.get_widget("explore_everything").show_all()
 
+        runtime.dispatcher.add_handler('activity_updated', self.after_activity_update)
+        runtime.dispatcher.add_handler('day_updated', self.after_fact_update)
 
         self.init_stats()
 
@@ -407,6 +409,17 @@ than 15 minutes you seem to be a busy bee." % ("<b>%d</b>" % short_percent))
         self.stats(button.year)
 
 
+    def after_fact_update(self, event, date):
+        self.stat_facts = runtime.storage.get_facts(dt.date(1970, 1, 1), dt.date.today())
+        self.popular_categories = [cat[0] for cat in runtime.storage.get_popular_categories()]
+        
+        if self.get_widget("pages").get_current_page() == 0:
+            self.do_graph()
+        else:
+            self.stats()
+        
+
+
     def get_widget(self, name):
         """ skip one variable (huh) """
         return self._gui.get_object(name)



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