[hamster-applet] Minor refactoring - cool kids seem to be overriding cairo expose events, so will we



commit dbeedb0dfd5dc7715eaf191700f375098ceb90b9
Author: Toms Bauģis <toms baugis gmail com>
Date:   Sat Apr 25 13:31:22 2009 +0100

    Minor refactoring - cool kids seem to be overriding cairo expose events, so will we
---
 hamster/charting.py      |   19 +++++++----------
 hamster/edit_activity.py |   11 +--------
 hamster/graphics.py      |   51 ++++++++++++++++++++++++++++++---------------
 3 files changed, 44 insertions(+), 37 deletions(-)

diff --git a/hamster/charting.py b/hamster/charting.py
index 98292a4..a5bed7a 100644
--- a/hamster/charting.py
+++ b/hamster/charting.py
@@ -149,7 +149,6 @@ class Chart(graphics.Area):
     """
     def __init__(self, **args):
         graphics.Area.__init__(self)
-        self.connect("expose_event", self._expose)
 
         self.max_bar_width     = args.get("max_bar_width", 0)
         self.legend_width      = args.get("legend_width", 0)
@@ -327,20 +326,13 @@ class Chart(graphics.Area):
         
         return self.moving #return if there is still work to do
 
-    def _expose(self, widget, event):
-        """expose is when drawing's going on, like on invalidate"""
+    def _render(self):
         # fill whole area 
         if self.background:
             self.context.rectangle(0, 0, self.width, self.height)
             self.context.set_source_rgb(*self.background)
             self.context.fill()
         
-        #forward to specific implementations
-        self._draw(self.context)
-        self.context.stroke()
-
-        return False
-
 
     def _update_targets(self):
         # calculates new factors and then updates existing set
@@ -368,7 +360,10 @@ class Chart(graphics.Area):
 
 
 class BarChart(Chart):
-    def _draw(self, context):
+    def _render(self):
+        context = self.context
+        Chart._render(self)
+        
         # determine graph dimensions
         if self.show_stack_labels:
             legend_width = self.legend_width or self.longest_label(self.keys)
@@ -565,7 +560,9 @@ class BarChart(Chart):
 
 
 class HorizontalBarChart(Chart):
-    def _draw(self, context):
+    def _render(self):
+        context = self.context
+        Chart._render(self)
         rowcount, keys = len(self.keys), self.keys
         
         # push graph to the right, so it doesn't overlap
diff --git a/hamster/edit_activity.py b/hamster/edit_activity.py
index 02c2cb1..76945f3 100644
--- a/hamster/edit_activity.py
+++ b/hamster/edit_activity.py
@@ -45,9 +45,6 @@ import cairo, pango
 class Dayline(graphics.Area):
     def __init__(self):
         graphics.Area.__init__(self)
-        self.context = None
-        self.layout = None
-        self.connect("expose_event", self._expose)
 
         self.set_events(gtk.gdk.EXPOSURE_MASK
                                  | gtk.gdk.LEAVE_NOTIFY_MASK
@@ -217,16 +214,12 @@ class Dayline(graphics.Area):
         self.redraw_canvas()
 
 
-    def _expose(self, widget, event):
-        """expose is when drawing's going on, like on _invalidate"""
-        self._draw(self.context)
-        return False
-    
     def _minutes_from_start(self, date):
             delta = (date - self.range_start.value)
             return delta.days * 24 * 60 + delta.seconds / 60
             
-    def _draw(self, context):
+    def _render(self):
+        context = self.context
         #TODO - use system colors and fonts
  
         context.set_line_width(1)
diff --git a/hamster/graphics.py b/hamster/graphics.py
index 461607a..08e79d7 100644
--- a/hamster/graphics.py
+++ b/hamster/graphics.py
@@ -5,11 +5,42 @@ import pango, cairo
 
 class Area(gtk.DrawingArea):
     """Abstraction on top of DrawingArea to work specifically with cairo"""
+    __gsignals__ = {
+        "expose-event": "override",
+        "configure_event": "override",
+    }
+
+    def do_configure_event ( self, event ):
+        (self.__width, self.__height) = self.window.get_size()
+        self.queue_draw()
+                    
+    def do_expose_event ( self, event ):
+        self.width, self.height = self.window.get_size()
+        self.context = self.window.cairo_create()
+
+
+        self.context.set_antialias(cairo.ANTIALIAS_NONE)
+        self.context.rectangle(event.area.x, event.area.y,
+                               event.area.width, event.area.height)
+        self.context.clip()
+
+        self.layout = self.context.create_layout()
+        default_font = pango.FontDescription(gtk.Style().font_desc.to_string())
+        default_font.set_size(8 * pango.SCALE)
+        self.layout.set_font_description(default_font)
+        
+
+        alloc = self.get_allocation()  #x, y, width, height
+        self.width, self.height = alloc.width, alloc.height
+        
+        self._render()
+
+
+
     def __init__(self):
         gtk.DrawingArea.__init__(self)
         self.context = None
         self.layout = None
-        self.connect("expose_event", self._on_expose)
         self.width = None
         self.height = None
         self.value_boundaries = None #x_min, x_max, y_min, y_max
@@ -26,22 +57,8 @@ class Area(gtk.DrawingArea):
             self.window.process_updates(True)
 
 
-    def _on_expose(self, widget, event):
-        """expose is when drawing's going on, like on _invalidate"""
-        self.context = widget.window.cairo_create()
-        self.context.set_antialias(cairo.ANTIALIAS_NONE)
-        self.context.rectangle(event.area.x, event.area.y,
-                               event.area.width, event.area.height)
-        self.context.clip()
-
-        self.layout = self.context.create_layout()
-        default_font = pango.FontDescription(gtk.Style().font_desc.to_string())
-        default_font.set_size(8 * pango.SCALE)
-        self.layout.set_font_description(default_font)
-        
-
-        alloc = self.get_allocation()  #x, y, width, height
-        self.width, self.height = alloc.width, alloc.height
+    def _render(self):
+        raise NotImplementedError
 
 
     def set_value_range(self, x_min = None, x_max = None, y_min = None, y_max = None):



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