Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorManuel Quiñones <manuq@laptop.org>2013-02-15 02:11:44 (GMT)
committer Manuel Quiñones <manuq@laptop.org>2013-02-27 23:31:10 (GMT)
commitfffab04cf4c4fa8a1c12ec242c15ffd8f0747b56 (patch)
tree19e174575d017cc31db63bbf5b429a7978807991
parente048e1455825e66fe71a4c7028ba822dd7938d51 (diff)
Improve ClockFace attributes and simplify calculations - SL #1959
- 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>
-rwxr-xr-xclock.py50
1 files changed, 34 insertions, 16 deletions
diff --git a/clock.py b/clock.py
index db9de81..bcebf3e 100755
--- a/clock.py
+++ b/clock.py
@@ -527,8 +527,16 @@ class ClockFace(gtk.DrawingArea):
# SVG Background handle
self._svg_handle = None
+ # This are calculated on widget resize
+ self._center_x = None
+ self._center_y = None
self._radius = -1
+ self._width = None
+ self._height = None
self._line_width = 2
+ self._hand_sizes = {}
+
+ self._hand_angles = {}
# Color codes (approved colors for XO screen:
# http://wiki.laptop.org/go/XO_colors)
@@ -605,6 +613,11 @@ class ClockFace(gtk.DrawingArea):
cache_ctx.transform(matrix)
self._svg_handle.render_cairo(cache_ctx)
+ # The hands sizes are proportional to the radius
+ self._hand_sizes['hour'] = self._radius * 0.5
+ self._hand_sizes['minutes'] = self._radius * 0.8
+ self._hand_sizes['seconds'] = self._radius * 0.7
+
self.initialized = True
def _expose_cb(self, widget, event):
@@ -794,10 +807,6 @@ class ClockFace(gtk.DrawingArea):
def _draw_hands(self):
"""Draw the hands of the analog clocks.
"""
- hours = self._time.hour
- minutes = self._time.minute
- seconds = self._time.second
-
cr = self.window.cairo_create()
cr.set_line_cap(cairo.LINE_CAP_ROUND)
@@ -809,10 +818,11 @@ class ClockFace(gtk.DrawingArea):
cr.arc(self._center_x, self._center_y, 5 * self._line_width, 0, 2 * math.pi)
cr.fill_preserve()
cr.move_to(self._center_x, self._center_y)
- cr.line_to(int(self._center_x + self._radius * 0.5 *
- math.sin(math.pi / 6 * hours + math.pi / 360 * minutes)),
- int(self._center_y + self._radius * 0.5 *
- - math.cos(math.pi / 6 * hours + math.pi / 360 * minutes)))
+ sin = math.sin(self._hand_angles['hour'])
+ cos = math.cos(self._hand_angles['hour'])
+ cr.line_to(
+ int(self._center_x + self._hand_sizes['hour'] * sin),
+ int(self._center_y - self._hand_sizes['hour'] * cos))
cr.stroke()
# Minute hand:
@@ -822,10 +832,11 @@ class ClockFace(gtk.DrawingArea):
cr.arc(self._center_x, self._center_y, 4 * self._line_width, 0, 2 * math.pi)
cr.fill_preserve()
cr.move_to(self._center_x, self._center_y)
- cr.line_to(int(self._center_x + self._radius * 0.7 *
- math.sin(math.pi / 30 * minutes)),
- int(self._center_y + self._radius * 0.7 *
- - math.cos(math.pi / 30 * minutes)))
+ sin = math.sin(self._hand_angles['minutes'])
+ cos = math.cos(self._hand_angles['minutes'])
+ cr.line_to(
+ int(self._center_x + self._hand_sizes['minutes'] * sin),
+ int(self._center_y - self._hand_sizes['minutes'] * cos))
cr.stroke()
# Seconds hand:
@@ -835,10 +846,11 @@ class ClockFace(gtk.DrawingArea):
cr.arc(self._center_x, self._center_y, 3 * self._line_width, 0, 2 * math.pi)
cr.fill_preserve()
cr.move_to(self._center_x, self._center_y)
- cr.line_to(int(self._center_x + self._radius * 0.8 *
- math.sin(math.pi / 30 * seconds)),
- int(self._center_y + self._radius * 0.8 *
- - math.cos(math.pi / 30 * seconds)))
+ sin = math.sin(self._hand_angles['seconds'])
+ cos = math.cos(self._hand_angles['seconds'])
+ cr.line_to(
+ int(self._center_x + self._hand_sizes['seconds'] * sin),
+ int(self._center_y - self._hand_sizes['seconds'] * cos))
cr.stroke()
def _draw_numbers(self):
@@ -879,6 +891,12 @@ font_desc="Sans Bold 40">%d</span></markup>') % (i + 1)
# update the time and force a redraw of the clock
self._time = datetime.now()
+ self._hand_angles['hour'] = (math.pi / 6 * self._time.hour +
+ math.pi / 360 * self._time.minute)
+
+ self._hand_angles['minutes'] = math.pi / 30 * self._time.minute
+ self._hand_angles['seconds'] = math.pi / 30 * self._time.second
+
gobject.idle_add(self._redraw_canvas)
# When the minutes change, we raise the 'time_minute'