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): |
527 | 527 | # SVG Background handle |
528 | 528 | self._svg_handle = None |
529 | 529 | |
| 530 | # This are calculated on widget resize |
| 531 | self._center_x = None |
| 532 | self._center_y = None |
530 | 533 | self._radius = -1 |
| 534 | self._width = None |
| 535 | self._height = None |
531 | 536 | self._line_width = 2 |
| 537 | self._hand_sizes = {} |
| 538 | |
| 539 | self._hand_angles = {} |
532 | 540 | |
533 | 541 | # Color codes (approved colors for XO screen: |
534 | 542 | # http://wiki.laptop.org/go/XO_colors) |
… |
… |
class ClockFace(gtk.DrawingArea): |
605 | 613 | cache_ctx.transform(matrix) |
606 | 614 | self._svg_handle.render_cairo(cache_ctx) |
607 | 615 | |
| 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 | |
608 | 621 | self.initialized = True |
609 | 622 | |
610 | 623 | def _expose_cb(self, widget, event): |
… |
… |
class ClockFace(gtk.DrawingArea): |
794 | 807 | def _draw_hands(self): |
795 | 808 | """Draw the hands of the analog clocks. |
796 | 809 | """ |
797 | | hours = self._time.hour |
798 | | minutes = self._time.minute |
799 | | seconds = self._time.second |
800 | | |
801 | 810 | cr = self.window.cairo_create() |
802 | 811 | cr.set_line_cap(cairo.LINE_CAP_ROUND) |
803 | 812 | |
… |
… |
class ClockFace(gtk.DrawingArea): |
809 | 818 | cr.arc(self._center_x, self._center_y, 5 * self._line_width, 0, 2 * math.pi) |
810 | 819 | cr.fill_preserve() |
811 | 820 | 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)) |
816 | 826 | cr.stroke() |
817 | 827 | |
818 | 828 | # Minute hand: |
… |
… |
class ClockFace(gtk.DrawingArea): |
822 | 832 | cr.arc(self._center_x, self._center_y, 4 * self._line_width, 0, 2 * math.pi) |
823 | 833 | cr.fill_preserve() |
824 | 834 | 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)) |
829 | 840 | cr.stroke() |
830 | 841 | |
831 | 842 | # Seconds hand: |
… |
… |
class ClockFace(gtk.DrawingArea): |
835 | 846 | cr.arc(self._center_x, self._center_y, 3 * self._line_width, 0, 2 * math.pi) |
836 | 847 | cr.fill_preserve() |
837 | 848 | 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)) |
842 | 854 | cr.stroke() |
843 | 855 | |
844 | 856 | def _draw_numbers(self): |
… |
… |
font_desc="Sans Bold 40">%d</span></markup>') % (i + 1) |
879 | 891 | # update the time and force a redraw of the clock |
880 | 892 | self._time = datetime.now() |
881 | 893 | |
| 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 | |
882 | 900 | gobject.idle_add(self._redraw_canvas) |
883 | 901 | |
884 | 902 | # When the minutes change, we raise the 'time_minute' |