[hamster-applet] removed the obsolete functions



commit 54b7313e47b61819cbb0d9f929a7b8a475050314
Author: Toms Bauģis <toms baugis gmail com>
Date:   Fri Nov 27 19:35:13 2009 +0000

    removed the obsolete functions

 hamster/widgets/dayline.py  |  138 ++++++++----------------------------------
 hamster/widgets/timeline.py |  131 ++++++-----------------------------------
 2 files changed, 45 insertions(+), 224 deletions(-)
---
diff --git a/hamster/widgets/dayline.py b/hamster/widgets/dayline.py
index 310594f..3054f1a 100644
--- a/hamster/widgets/dayline.py
+++ b/hamster/widgets/dayline.py
@@ -29,92 +29,9 @@ import colorsys
 
 
 class DayLine(graphics.Area):
-    #TODO remove these obsolete functions with in-house transformations
-    def set_value_range(self, x_min = None, x_max = None, y_min = None, y_max = None):
-        """sets up our internal conversion matrix, because cairo one will
-        scale also fonts and we need something in between!"""
-        
-        #store given params, we might redo the math later
-        if not self.value_boundaries:
-            self.value_boundaries = [x_min, x_max, y_min, y_max]
-        else:
-            if x_min != None:
-                self.value_boundaries[0] = x_min
-            if x_max != None:
-                self.value_boundaries[1] = x_max
-            if y_min != None:
-                self.value_boundaries[2] = y_min
-            if y_max != None:
-                self.value_boundaries[3] = y_max 
-        self.x_factor, self.y_factor = None, None
-        self._get_factors()
-
-
-    def move_to(self, x, y):
-        """our copy of moveto that takes into account our transformations"""
-        self.context.move_to(*self.get_pixel(x, y))
-
-    def line_to(self, x, y):
-        self.context.line_to(*self.get_pixel(x, y))
-
-    def _get_factors(self):
-        if not self.x_factor:
-            self.x_factor = 1
-            if self.value_boundaries and self.value_boundaries[0] != None and self.value_boundaries[1] != None:
-                self.x_factor = float(self.width) / abs(self.value_boundaries[1] - self.value_boundaries[0])
-                
-        if not self.y_factor:            
-            self.y_factor = 1
-            if self.value_boundaries and self.value_boundaries[2] != None and self.value_boundaries[3] != None:
-                self.y_factor = float(self.height) / abs(self.value_boundaries[3] - self.value_boundaries[2])
-
-        return self.x_factor, self.y_factor        
-
-
-    def get_pixel(self, x_value = None, y_value = None):
-        """returns screen pixel position for value x and y. Useful to
-        get and then pad something
-
-        x = min1 + (max1 - min1) * (x / abs(max2-min2))  
-            => min1 + const1 * x / const2
-            => const3 = const1 / const2
-            => min + x * const3
-        """
-        x_factor, y_factor = self._get_factors()
-
-        if x_value != None:
-            if self.value_boundaries and self.value_boundaries[0] != None:
-                if self.value_boundaries[1] > self.value_boundaries[0]:
-                    x_value = self.value_boundaries[0] + x_value * x_factor
-                else: #case when min is larger than max (flipped)
-                    x_value = self.value_boundaries[1] - x_value * x_factor
-            if y_value is None:
-                return x_value + self.graph_x
-
-        if y_value != None:
-            if self.value_boundaries and self.value_boundaries[2] != None:
-                if self.value_boundaries[3] > self.value_boundaries[2]:
-                    y_value = self.value_boundaries[2] + y_value * y_factor
-                else: #case when min is larger than max (flipped)
-                    y_value = self.value_boundaries[2] - y_value * y_factor
-            if x_value is None:
-                return y_value + self.graph_y
-            
-        return x_value + self.graph_x, y_value + self.graph_y
-
-    def get_value_at_pos(self, x = None, y = None):
+    def get_value_at_pos(self, x):
         """returns mapped value at the coordinates x,y"""
-        x_factor, y_factor = self._get_factors()
-        
-        if x != None:
-            x = (x - self.graph_x)  / x_factor
-            if y is None:
-                return x
-        if y != None:
-            y = (y - self.graph_x) / y_factor
-            if x is None:
-                return y
-        return x, y            
+        return x / float(self.width / self.view_minutes)
     
     
     #normal stuff
@@ -140,9 +57,9 @@ class DayLine(graphics.Area):
         self.in_motion = False
         self.days = []
 
+        self.view_minutes = float(12 * 60) #how many minutes are we going to show
+
         # TODO - get rid of these
-        self.value_boundaries = None #x_min, x_max, y_min, y_max
-        self.x_factor, self.y_factor = None, None        
         # use these to mark area where the "real" drawing is going on
         self.graph_x, self.graph_y = 0, 0
 
@@ -155,7 +72,7 @@ class DayLine(graphics.Area):
         start_time = highlight[0] - dt.timedelta(minutes = highlight[0].minute) - dt.timedelta(hours = 10)
         
         if self.range_start:
-            self.range_start.target(start_time)
+            self.range_start.value = start_time
             self.scroll_to_range_start()
         else:
             self.range_start = Integrator(start_time, damping = 0.35, attraction = 0.5)
@@ -235,7 +152,6 @@ class DayLine(graphics.Area):
 
         mouse_down = state & gtk.gdk.BUTTON1_MASK
             
-        #print x, self.highlight_start, self.highlight_end
         if self.highlight_start != None:
             start_drag = 10 > (self.highlight_start - x) > -1
 
@@ -293,7 +209,7 @@ class DayLine(graphics.Area):
                     self.__call_parent_time_changed()
                 else:
                     self.range_start.target(self.drag_start_time +
-                                            dt.timedelta(minutes = self.get_value_at_pos(x = self.drag_start) - self.get_value_at_pos(x = x)))
+                                            dt.timedelta(minutes = self.get_value_at_pos(self.drag_start) - self.get_value_at_pos(x)))
                     self.scroll_to_range_start()
 
 
@@ -321,12 +237,12 @@ class DayLine(graphics.Area):
         #we will buffer 4 hours to both sides so partial labels also appear
         range_end = self.range_start.value + dt.timedelta(hours = 12 + 2 * 4)        
         self.graph_x = -self.width / 3 #so x moves one third out of screen
-        self.set_value_range(x_min = 0, x_max = 12 * 60)
+        
+        pixels_in_minute = self.width / self.view_minutes
 
         minutes = self._minutes_from_start(range_end)
 
 
-
         graph_y = 4
         graph_height = self.height - 10
         graph_y2 = graph_y + graph_height
@@ -347,22 +263,22 @@ class DayLine(graphics.Area):
                 else:
                     end_minutes = start_minutes
             
-            if self.get_pixel(end_minutes) > 0 and \
-                self.get_pixel(start_minutes) < self.width:
+            if end_minutes * pixels_in_minute > 0 and \
+                start_minutes * pixels_in_minute < self.width:
                     context.set_source_rgba(0.86, 0.86, 0.86, 0.5)
 
-                    context.rectangle(round(self.get_pixel(start_minutes)),
+                    context.rectangle(round(start_minutes * pixels_in_minute),
                                       graph_y,
-                                      round(self.get_pixel(end_minutes) - self.get_pixel(start_minutes)),
+                                      round(end_minutes * pixels_in_minute - start_minutes * pixels_in_minute),
                                       graph_height - 1)
                     context.fill()
                     context.stroke()
 
                     context.set_source_rgba(0.86, 0.86, 0.86, 1)
-                    self.move_to(start_minutes, graph_y)
-                    self.line_to(start_minutes, graph_y2)
-                    self.move_to(end_minutes, graph_y)
-                    self.line_to(end_minutes, graph_y2)
+                    self.context.move_to(start_minutes * pixels_in_minute, graph_y)
+                    self.context.line_to(start_minutes * pixels_in_minute, graph_y2)
+                    self.context.move_to(end_minutes * pixels_in_minute, graph_y)
+                    self.context.line_to(end_minutes * pixels_in_minute, graph_y2)
                     context.stroke()
 
         
@@ -375,13 +291,13 @@ class DayLine(graphics.Area):
             
             if label_time.minute == 0:
                 context.set_source_rgb(0.8, 0.8, 0.8)
-                self.move_to(i, graph_y2 - 15)
-                self.line_to(i, graph_y2)
+                self.context.move_to(i * pixels_in_minute, graph_y2 - 15)
+                self.context.line_to(i * pixels_in_minute, graph_y2)
                 context.stroke()
             elif label_time.minute % 15 == 0:
                 context.set_source_rgb(0.8, 0.8, 0.8)
-                self.move_to(i, graph_y2 - 5)
-                self.line_to(i, graph_y2)
+                self.context.move_to(i * pixels_in_minute, graph_y2 - 5)
+                self.context.line_to(i * pixels_in_minute, graph_y2)
                 context.stroke()
                 
                 
@@ -389,8 +305,8 @@ class DayLine(graphics.Area):
             if label_time.minute == 0 and label_time.hour % 2 == 0:
                 if label_time.hour == 0:
                     context.set_source_rgb(0.8, 0.8, 0.8)
-                    self.move_to(i, graph_y)
-                    self.line_to(i, graph_y2)
+                    self.context.move_to(i * pixels_in_minute, graph_y)
+                    self.context.line_to(i * pixels_in_minute, graph_y2)
                     label_minutes = label_time.strftime("%b %d")
                 else:
                     label_minutes = label_time.strftime("%H<small><sup>%M</sup></small>")
@@ -399,15 +315,15 @@ class DayLine(graphics.Area):
                 self.layout.set_markup(label_minutes)
                 label_w, label_h = self.layout.get_pixel_size()
                 
-                context.move_to(self.get_pixel(i) + 2, graph_y2 - label_h - 8)                
+                context.move_to(i * pixels_in_minute + 2, graph_y2 - label_h - 8)                
 
                 context.show_layout(self.layout)
         context.stroke()
         
         #highlight rectangle
         if self.highlight:
-            self.highlight_start = round(self.get_pixel(self._minutes_from_start(self.highlight[0])))
-            self.highlight_end = round(self.get_pixel(self._minutes_from_start(self.highlight[1])))
+            self.highlight_start = round(self._minutes_from_start(self.highlight[0]) * pixels_in_minute)
+            self.highlight_end = round(self._minutes_from_start(self.highlight[1]) * pixels_in_minute)
 
         #TODO - make a proper range check here
         if self.highlight_end > 0 and self.highlight_start < self.width:
@@ -434,9 +350,9 @@ class DayLine(graphics.Area):
         
         if self.move_type == "move" and (self.highlight_start == 0 or self.highlight_end == self.width):
             if self.highlight_start == 0:
-                self.range_start.target(self.range_start.value - dt.timedelta(minutes=30))
+                self.range_start.value = self.range_start.value - dt.timedelta(minutes=30)
             if self.highlight_end == self.width:
-                self.range_start.target(self.range_start.value + dt.timedelta(minutes=30))
+                self.range_start.value = self.range_start.value + dt.timedelta(minutes=30)
             self.scroll_to_range_start()
 
 
diff --git a/hamster/widgets/timeline.py b/hamster/widgets/timeline.py
index 637faee..caf7133 100644
--- a/hamster/widgets/timeline.py
+++ b/hamster/widgets/timeline.py
@@ -21,6 +21,8 @@ from hamster import graphics
 import datetime as dt
 
 class TimeLine(graphics.Area):
+    """this widget is kind of half finished"""
+    
     MODE_YEAR = 0
     MODE_MONTH = 1
     MODE_WEEK = 1
@@ -31,99 +33,7 @@ class TimeLine(graphics.Area):
         self.draw_mode = None
         self.max_hours = None
 
-        # TODO - get rid of these
-        self.value_boundaries = None #x_min, x_max, y_min, y_max
-        self.x_factor, self.y_factor = None, None        
-
         
-    #TODO remove these obsolete functions with in-house transformations
-    def set_value_range(self, x_min = None, x_max = None, y_min = None, y_max = None):
-        """sets up our internal conversion matrix, because cairo one will
-        scale also fonts and we need something in between!"""
-        
-        #store given params, we might redo the math later
-        if not self.value_boundaries:
-            self.value_boundaries = [x_min, x_max, y_min, y_max]
-        else:
-            if x_min != None:
-                self.value_boundaries[0] = x_min
-            if x_max != None:
-                self.value_boundaries[1] = x_max
-            if y_min != None:
-                self.value_boundaries[2] = y_min
-            if y_max != None:
-                self.value_boundaries[3] = y_max 
-        self.x_factor, self.y_factor = None, None
-        self._get_factors()
-
-
-    def move_to(self, x, y):
-        """our copy of moveto that takes into account our transformations"""
-        self.context.move_to(*self.get_pixel(x, y))
-
-    def line_to(self, x, y):
-        self.context.line_to(*self.get_pixel(x, y))
-
-    def _get_factors(self):
-        if not self.x_factor:
-            self.x_factor = 1
-            if self.value_boundaries and self.value_boundaries[0] != None and self.value_boundaries[1] != None:
-                self.x_factor = float(self.width) / abs(self.value_boundaries[1] - self.value_boundaries[0])
-                
-        if not self.y_factor:            
-            self.y_factor = 1
-            if self.value_boundaries and self.value_boundaries[2] != None and self.value_boundaries[3] != None:
-                self.y_factor = float(self.height) / abs(self.value_boundaries[3] - self.value_boundaries[2])
-
-        return self.x_factor, self.y_factor        
-
-
-    def get_pixel(self, x_value = None, y_value = None):
-        """returns screen pixel position for value x and y. Useful to
-        get and then pad something
-
-        x = min1 + (max1 - min1) * (x / abs(max2-min2))  
-            => min1 + const1 * x / const2
-            => const3 = const1 / const2
-            => min + x * const3
-        """
-        x_factor, y_factor = self._get_factors()
-
-        if x_value != None:
-            if self.value_boundaries and self.value_boundaries[0] != None:
-                if self.value_boundaries[1] > self.value_boundaries[0]:
-                    x_value = self.value_boundaries[0] + x_value * x_factor
-                else: #case when min is larger than max (flipped)
-                    x_value = self.value_boundaries[1] - x_value * x_factor
-            if y_value is None:
-                return x_value
-
-        if y_value != None:
-            if self.value_boundaries and self.value_boundaries[2] != None:
-                if self.value_boundaries[3] > self.value_boundaries[2]:
-                    y_value = self.value_boundaries[2] + y_value * y_factor
-                else: #case when min is larger than max (flipped)
-                    y_value = self.value_boundaries[2] - y_value * y_factor
-            if x_value is None:
-                return y_value + self.graph_y
-            
-        return x_value, y_value
-
-    def get_value_at_pos(self, x = None, y = None):
-        """returns mapped value at the coordinates x,y"""
-        x_factor, y_factor = self._get_factors()
-        
-        if x != None:
-            x = x  / x_factor
-            if y is None:
-                return x
-        if y != None:
-            y = y / y_factor
-            if x is None:
-                return y
-        return x, y            
-
-
     # Normal stuff    
     def draw(self, facts):
         import itertools
@@ -173,12 +83,15 @@ class TimeLine(graphics.Area):
         self.fill_area(0, 0, self.width, self.height, (0.975,0.975,0.975))
         self.set_color((100,100,100))
 
-        self.set_value_range(x_min = 1, x_max = (self.end_date - self.start_date).days)        
+        
+        days = (self.end_date - self.start_date).days
+        pixels_in_day = self.width / float(days)
+
         month_label_fits = True
         for month in range(1, 13):
             self.layout.set_text(calendar.month_abbr[month])
             label_w, label_h = self.layout.get_pixel_size()
-            if label_w * 2 > self.x_factor * 30:
+            if label_w * 2 > pixels_in_day * 30:
                 month_label_fits = False
                 break
         
@@ -193,23 +106,19 @@ class TimeLine(graphics.Area):
             self.context.set_line_width(1)
             for month in range(1, 13):
                 for day in range(1, calendar.monthrange(year, month)[1] + 1):
-                    ticker_pos = year_pos + ticker_date.timetuple().tm_yday
+                    ticker_pos = (year_pos + ticker_date.timetuple().tm_yday * pixels_in_day)
                     
-                    #if ticker_date.weekday() in [0, 6]:
-                    #    self.fill_area(ticker_pos * self.x_factor + 1, 20, self.x_factor, self.height - 20, (240, 240, 240))
-                    #    self.context.stroke()
                         
     
-                    if self.x_factor > 5:
-                        self.move_to(ticker_pos, self.height - 20)
-                        self.line_to(ticker_pos, self.height)
+                    if pixels_in_day > 5:
+                        self.context.move_to(ticker_pos, self.height - 20)
+                        self.context.line_to(ticker_pos, self.height)
                    
                         self.layout.set_text(ticker_date.strftime("%d"))
                         label_w, label_h = self.layout.get_pixel_size()
                         
-                        if label_w < self.x_factor / 1.2: #if label fits
-                            self.context.move_to(self.get_pixel(ticker_pos) + 2,
-                                                 self.height - 20)
+                        if label_w < pixels_in_day / 1.2: #if label fits
+                            self.context.move_to(ticker_pos + 2, self.height - 20)
                             self.context.show_layout(self.layout)
                     
                         self.context.stroke()
@@ -223,9 +132,9 @@ class TimeLine(graphics.Area):
                         total_length = total_length.seconds / 60 / 60.0 + total_length.days * 24
                         total_length = total_length / float(self.max_hours) * self.height - 16
 
-                        self.fill_area(round(ticker_pos * self.x_factor),
+                        self.fill_area(round(ticker_pos),
                                        round(self.height - total_length),
-                                       round(self.x_factor),
+                                       round(pixels_in_day),
                                        round(total_length),
                                        (190,190,190))
 
@@ -238,14 +147,10 @@ class TimeLine(graphics.Area):
                 
                 if month_label_fits:
                     #roll back a little
-                    month_pos = ticker_pos - calendar.monthrange(year, month)[1] + 1
+                    month_pos = ticker_pos - (calendar.monthrange(year, month)[1] + 1) * pixels_in_day
 
-                    self.move_to(month_pos, 0)
-                    #self.line_to(month_pos, 20)
-                    
+                    self.context.move_to(month_pos, 0)
                     self.layout.set_text(dt.date(year, month, 1).strftime("%b"))
-    
-                    self.move_to(month_pos, 0)
                     self.context.show_layout(self.layout)
 
 
@@ -255,7 +160,7 @@ class TimeLine(graphics.Area):
             self.layout.set_text("%d" % year)
             label_w, label_h = self.layout.get_pixel_size()
                         
-            self.move_to(year_pos + 2 / self.x_factor, month_label_fits * label_h * 1.2)
+            self.context.move_to(year_pos + 2, month_label_fits * label_h * 1.2)
     
             self.context.show_layout(self.layout)
             



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