Ticket #1959: 0003-Improve-ClockFace-attributes-and-simplify-calculatio.patch

File 0003-Improve-ClockFace-attributes-and-simplify-calculatio.patch, 5.0 KB (added by manuq, 11 years ago)
  • clock.py

    From 87d0259eec3ab6f430533dd3764ba5b69c3f1ccd Mon Sep 17 00:00:00 2001
    From: =?UTF-8?q?Manuel=20Qui=C3=B1ones?= <manuq@laptop.org>
    Date: Thu, 14 Feb 2013 23:11:44 -0300
    Subject: [PATCH 3/6] Improve ClockFace attributes and simplify calculations -
     SL #1959
    MIME-Version: 1.0
    Content-Type: text/plain; charset=UTF-8
    Content-Transfer-Encoding: 8bit
    Mail-Followup-To: <sugar-devel@lists.sugarlabs.org>
    
    - Initialize all attributes in the __init__ method, for PEP8
      compliance.
    
    - For the grab hands feature, the sizes and angles of the hands need
      to be stored in attributes.  This also makes the formula to draw the
      hands more readable.
    
    Signed-off-by: Manuel Quiñones <manuq@laptop.org>
    ---
     clock.py | 50 ++++++++++++++++++++++++++++++++++----------------
     1 file changed, 34 insertions(+), 16 deletions(-)
    
    diff --git a/clock.py b/clock.py
    index db9de81..bcebf3e 100755
    a b class ClockFace(gtk.DrawingArea): 
    527527        # SVG Background handle
    528528        self._svg_handle = None
    529529
     530        # This are calculated on widget resize
     531        self._center_x = None
     532        self._center_y = None
    530533        self._radius = -1
     534        self._width = None
     535        self._height = None
    531536        self._line_width = 2
     537        self._hand_sizes = {}
     538
     539        self._hand_angles = {}
    532540
    533541        # Color codes (approved colors for XO screen:
    534542        # http://wiki.laptop.org/go/XO_colors)
    class ClockFace(gtk.DrawingArea): 
    605613        cache_ctx.transform(matrix)
    606614        self._svg_handle.render_cairo(cache_ctx)
    607615
     616        # The hands sizes are proportional to the radius
     617        self._hand_sizes['hour'] = self._radius * 0.5
     618        self._hand_sizes['minutes'] = self._radius * 0.8
     619        self._hand_sizes['seconds'] = self._radius * 0.7
     620
    608621        self.initialized = True
    609622
    610623    def _expose_cb(self, widget, event):
    class ClockFace(gtk.DrawingArea): 
    794807    def _draw_hands(self):
    795808        """Draw the hands of the analog clocks.
    796809        """
    797         hours = self._time.hour
    798         minutes = self._time.minute
    799         seconds = self._time.second
    800 
    801810        cr = self.window.cairo_create()
    802811        cr.set_line_cap(cairo.LINE_CAP_ROUND)
    803812
    class ClockFace(gtk.DrawingArea): 
    809818        cr.arc(self._center_x, self._center_y, 5 * self._line_width, 0, 2 * math.pi)
    810819        cr.fill_preserve()
    811820        cr.move_to(self._center_x, self._center_y)
    812         cr.line_to(int(self._center_x + self._radius * 0.5 *
    813             math.sin(math.pi / 6 * hours + math.pi / 360 * minutes)),
    814             int(self._center_y + self._radius * 0.5 *
    815             - math.cos(math.pi / 6 * hours + math.pi / 360 * minutes)))
     821        sin = math.sin(self._hand_angles['hour'])
     822        cos = math.cos(self._hand_angles['hour'])
     823        cr.line_to(
     824            int(self._center_x + self._hand_sizes['hour'] * sin),
     825            int(self._center_y - self._hand_sizes['hour'] * cos))
    816826        cr.stroke()
    817827
    818828        # Minute hand:
    class ClockFace(gtk.DrawingArea): 
    822832        cr.arc(self._center_x, self._center_y, 4 * self._line_width, 0, 2 * math.pi)
    823833        cr.fill_preserve()
    824834        cr.move_to(self._center_x, self._center_y)
    825         cr.line_to(int(self._center_x + self._radius * 0.7 *
    826                 math.sin(math.pi / 30 * minutes)),
    827                 int(self._center_y + self._radius * 0.7 *
    828                 - math.cos(math.pi / 30 * minutes)))
     835        sin = math.sin(self._hand_angles['minutes'])
     836        cos = math.cos(self._hand_angles['minutes'])
     837        cr.line_to(
     838            int(self._center_x + self._hand_sizes['minutes'] * sin),
     839            int(self._center_y - self._hand_sizes['minutes'] * cos))
    829840        cr.stroke()
    830841
    831842        # Seconds hand:
    class ClockFace(gtk.DrawingArea): 
    835846        cr.arc(self._center_x, self._center_y, 3 * self._line_width, 0, 2 * math.pi)
    836847        cr.fill_preserve()
    837848        cr.move_to(self._center_x, self._center_y)
    838         cr.line_to(int(self._center_x + self._radius * 0.8 *
    839                 math.sin(math.pi / 30 * seconds)),
    840                 int(self._center_y + self._radius * 0.8 *
    841                 - math.cos(math.pi / 30 * seconds)))
     849        sin = math.sin(self._hand_angles['seconds'])
     850        cos = math.cos(self._hand_angles['seconds'])
     851        cr.line_to(
     852            int(self._center_x + self._hand_sizes['seconds'] * sin),
     853            int(self._center_y - self._hand_sizes['seconds'] * cos))
    842854        cr.stroke()
    843855
    844856    def _draw_numbers(self):
    font_desc="Sans Bold 40">%d</span></markup>') % (i + 1) 
    879891        # update the time and force a redraw of the clock
    880892        self._time = datetime.now()
    881893
     894        self._hand_angles['hour'] = (math.pi / 6 * self._time.hour +
     895                                     math.pi / 360 * self._time.minute)
     896
     897        self._hand_angles['minutes'] = math.pi / 30 * self._time.minute
     898        self._hand_angles['seconds'] = math.pi / 30 * self._time.second
     899
    882900        gobject.idle_add(self._redraw_canvas)
    883901
    884902        # When the minutes change, we raise the 'time_minute'