hamster-applet r778 - trunk/hamster



Author: tbaugis
Date: Sat Feb 21 22:55:55 2009
New Revision: 778
URL: http://svn.gnome.org/viewvc/hamster-applet?rev=778&view=rev

Log:
cleanup

Modified:
   trunk/hamster/charting.py

Modified: trunk/hamster/charting.py
==============================================================================
--- trunk/hamster/charting.py	(original)
+++ trunk/hamster/charting.py	Sat Feb 21 22:55:55 2009
@@ -81,7 +81,7 @@
     
 class Integrator(object):
     """an iterator, inspired by "visualizing data" book to simplify animation"""
-    def __init__(self, start_value, precision = 0, frames = 50):
+    def __init__(self, start_value, frames = 50):
         """precision determines, until which decimal number we go"""
         self.value = start_value
         self.target_value = start_value
@@ -102,18 +102,14 @@
         moving = self.current_frame < self.frames
         if moving:
             self.current_frame +=1
-            self.value = self._smoothstep(self.current_frame / self.frames,
-                                          self.value, self.target_value)
-        return (round(self.value,4) - round(self.target_value,4) != 0) and moving
+            v = self.current_frame / self.frames
+            self.value = (self.target_value * v) + (self.value * (1-v))
+        return moving and (round(self.value, 4) - round(self.target_value, 4) != 0)
 
     def finish(self):
         self.current_frame = 0.0
         self.value = self.target_value
 
-    def _smoothstep(self, v, start, end):
-        smooth = 1 - (1 - v)
-        return (end * smooth) + (start * (1-smooth))
-        
 
 def size_list(set, target_set):
     """turns set lenghts into target set - trim it, stretches it, but
@@ -199,34 +195,6 @@
         self.current_max = None
         self.integrators = []
         self.moving = False
-        
-    def _expose(self, widget, event):
-        """expose is when drawing's going on, like on _invalidate"""
-        self.context = widget.window.cairo_create()
-        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[2], alloc[3]
-
-        # 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_preserve()
-            self.context.stroke()
-        
-        #forward to specific implementations
-        self._draw()
-
-        return False
 
 
     def plot(self, keys, data, stack_keys = None):
@@ -247,11 +215,11 @@
         else:
             self.current_max.target(self.max_value)
         
-        self._redo_factors()
+        self._update_targets()
         
         if self.animate:
             if not self.moving: #if we are moving, then there is a timeout somewhere already
-                gobject.timeout_add(self.animation_timeout, self._replot)
+                gobject.timeout_add(self.animation_timeout, self._interpolate)
         else:
             def finish_all(integrators):
                 for i in range(len(integrators)):
@@ -264,38 +232,9 @@
 
 
             self._invalidate()
-            
-
-        
-    def _smoothstep(self, v, start, end):
-        smooth = 1 - (1 - v) * (1 - v)
-        return (end * smooth) + (start * (1-smooth))
-        
-    def _redo_factors(self):
-        # calculates new factors and then updates existing set
-        max_value = float(self.max_value) or 1 # avoid division by zero
-        
-        self.integrators = size_list(self.integrators, self.data)
-
-        #need function to go recursive
-        def retarget(integrators, new_values):
-            for i in range(len(new_values)):
-                if type(new_values[i]) == list:
-                    integrators[i] = retarget(integrators[i], new_values[i])
-                else:
-                    if isinstance(integrators[i], Integrator) == False:
-                        integrators[i] = Integrator(0, 1) #8 numbers after comma :)
-
-                    integrators[i].target(new_values[i] / max_value)
-            
-            return integrators
-    
-        retarget(self.integrators, self.data)
-    
 
 
-
-    def _replot(self):
+    def _interpolate(self):
         """Internal function to do the math, going from previous set to the
            new one, and redraw graph"""
         #this can get called before expose    
@@ -310,7 +249,7 @@
         def update_all(integrators):
             still_moving = False
             for z in range(len(integrators)):
-                if type(integrators[z]) == list:
+                if isinstance(integrators[z], list):
                     still_moving = update_all(integrators[z]) or still_moving
                 else:
                     still_moving = integrators[z].update() or still_moving
@@ -322,14 +261,66 @@
         
         return self.moving #return if there is still work to do
 
+
     def _invalidate(self):
-        """Force redrawal of chart"""
+        """Force graph redraw"""
         if self.window:    #this can get called before expose    
             alloc = self.get_allocation()
             rect = gtk.gdk.Rectangle(alloc.x, alloc.y, alloc.width, alloc.height)
             self.window.invalidate_rect(rect, True)
             self.window.process_updates(True)
 
+
+    def _expose(self, widget, event):
+        """expose is when drawing's going on, like on _invalidate"""
+        self.context = widget.window.cairo_create()
+        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[2], alloc[3]
+
+        # 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_preserve()
+            self.context.stroke()
+        
+        #forward to specific implementations
+        self._draw()
+
+        return False
+
+
+    def _update_targets(self):
+        # calculates new factors and then updates existing set
+        max_value = float(self.max_value) or 1 # avoid division by zero
+        
+        self.integrators = size_list(self.integrators, self.data)
+
+        #need function to go recursive
+        def retarget(integrators, new_values):
+            for i in range(len(new_values)):
+                if type(new_values[i]) == list:
+                    integrators[i] = retarget(integrators[i], new_values[i])
+                else:
+                    if isinstance(integrators[i], Integrator) == False:
+                        integrators[i] = Integrator(0)
+
+                    integrators[i].target(new_values[i] / max_value)
+            
+            return integrators
+    
+        retarget(self.integrators, self.data)
+    
     def _fill_area(self, x, y, w, h, color):
         self.context.rectangle(x, y, w, h)
         self.context.set_source_rgb(*[c / 256.0 for c in color])
@@ -355,7 +346,6 @@
         else:
             self._fill_area(x, y, w, h, base_color)
 
-    
 
     def _longest_label(self, labels):
         max_extent = 0



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