From ecb0b7d6bf6f6b91cc12db45365452db81f0d51a Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Manuel=20Qui=C3=B1ones?= <manuq@laptop.org>
Date: Fri, 15 Feb 2013 10:18:09 -0300
Subject: [PATCH 7/8] Draw the simple background only when the widget resizes -
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>
The static elements of the simple clock can be drawn each time the
widget resizes, instead of for each tick of the clock. This are the
white circle background, the ticks for hour and minutes, and the
numbers. This patch adds a helper surface to do it, the same as the
nice clock is doing. The activity gets a performance improvement
doing this.
Signed-off-by: Manuel Quiñones <manuq@laptop.org>
---
clock.py | 49 ++++++++++++++++++++++++++++++-------------------
1 file changed, 30 insertions(+), 19 deletions(-)
diff --git a/clock.py b/clock.py
index 3394675..dad46ae 100755
a
|
b
|
class ClockFace(gtk.DrawingArea): |
545 | 545 | self._center_x = None |
546 | 546 | self._center_y = None |
547 | 547 | self._radius = -1 |
548 | | self._width = None |
549 | | self._height = None |
550 | 548 | self._line_width = 2 |
551 | 549 | self._hand_sizes = {} |
552 | 550 | |
… |
… |
class ClockFace(gtk.DrawingArea): |
614 | 612 | self._center_y = int(allocation.height / 2.0) |
615 | 613 | self._radius = max(min(int(allocation.width / 2.0), \ |
616 | 614 | int(allocation.height / 2.0)) - 20, 0) |
617 | | self._width = allocation.width |
618 | | self._height = allocation.height |
619 | 615 | self._line_width = int(self._radius / 150) |
620 | 616 | |
| 617 | cr = self.window.cairo_create() |
| 618 | |
| 619 | # Draw simple clock background |
| 620 | self._simple_background_cache = cr.get_target().create_similar( |
| 621 | cairo.CONTENT_COLOR_ALPHA, self._radius * 2, |
| 622 | self._radius * 2) |
| 623 | cache_ctx = cairo.Context(self._simple_background_cache) |
| 624 | self._draw_simple_background(cache_ctx) |
| 625 | self._draw_numbers(cache_ctx) |
| 626 | |
621 | 627 | # Reload the svg handle |
622 | 628 | self._svg_handle = rsvg.Handle(file="clock.svg") |
623 | | cr = self.window.cairo_create() |
| 629 | |
| 630 | # Draw nice clock background |
624 | 631 | self._nice_background_cache = cr.get_target().create_similar( |
625 | 632 | cairo.CONTENT_COLOR_ALPHA, self._radius * 2, |
626 | 633 | self._radius * 2) |
… |
… |
class ClockFace(gtk.DrawingArea): |
762 | 769 | def _draw_simple_clock(self): |
763 | 770 | """Draw the simple clock variants. |
764 | 771 | """ |
765 | | self._draw_simple_background() |
766 | | self._draw_numbers() |
| 772 | # Place the simple background |
| 773 | cr = self.window.cairo_create() |
| 774 | cr.translate(self._center_x - self._radius, |
| 775 | self._center_y - self._radius) |
| 776 | cr.set_source_surface(self._simple_background_cache) |
| 777 | cr.paint() |
| 778 | |
767 | 779 | self._draw_hands() |
768 | 780 | |
769 | | def _draw_simple_background(self): |
| 781 | def _draw_simple_background(self, cr): |
770 | 782 | """Draw the background of the simple clock. |
771 | 783 | The simple clock background is a white disk, with hours and minutes |
772 | 784 | ticks, and the hour numbers. |
773 | 785 | """ |
774 | | cr = self.window.cairo_create() |
775 | 786 | cr.set_line_width(4 * self._line_width) |
776 | 787 | cr.set_line_cap(cairo.LINE_CAP_ROUND) |
777 | 788 | |
778 | 789 | # Simple clock background |
779 | 790 | cr.set_source_rgba(*style.Color(self._COLOR_WHITE).get_rgba()) |
780 | | cr.arc(self._width / 2, self._height / 2, self._radius, 0, 2 * math.pi) |
| 791 | cr.arc(self._radius, self._radius, self._radius - self._line_width * 2, |
| 792 | 0, 2 * math.pi) |
781 | 793 | cr.fill_preserve() |
782 | 794 | cr.set_source_rgba(*style.Color(self._COLOR_BLACK).get_rgba()) |
783 | 795 | cr.stroke() |
… |
… |
class ClockFace(gtk.DrawingArea): |
796 | 808 | |
797 | 809 | cos = math.cos(i * math.pi / 30.0) |
798 | 810 | sin = math.sin(i * math.pi / 30.0) |
799 | | cr.move_to(int(self._center_x + (self._radius - inset) * cos), |
800 | | int(self._center_y + (self._radius - inset) * sin)) |
801 | | cr.line_to(int(self._center_x + (self._radius - 3) * cos), |
802 | | int(self._center_y + (self._radius - 3) * sin)) |
| 811 | cr.move_to(int(self._radius + (self._radius - inset) * cos), |
| 812 | int(self._radius + (self._radius - inset) * sin)) |
| 813 | cr.line_to(int(self._radius + (self._radius - 3) * cos), |
| 814 | int(self._radius + (self._radius - 3) * sin)) |
803 | 815 | cr.stroke() |
804 | 816 | |
805 | 817 | def _draw_nice_background(self): |
… |
… |
class ClockFace(gtk.DrawingArea): |
809 | 821 | rsvg handle, and we just transform this handle and render it |
810 | 822 | with cairo. |
811 | 823 | """ |
812 | | # We transform the background SVG |
| 824 | # Place the nice background |
813 | 825 | cr = self.window.cairo_create() |
814 | 826 | cr.translate(self._center_x - self._radius, |
815 | 827 | self._center_y - self._radius) |
… |
… |
class ClockFace(gtk.DrawingArea): |
871 | 883 | int(self._center_y - self._hand_sizes['seconds'] * cos)) |
872 | 884 | cr.stroke() |
873 | 885 | |
874 | | def _draw_numbers(self): |
| 886 | def _draw_numbers(self, cr): |
875 | 887 | """Draw the numbers of the hours. |
876 | 888 | """ |
877 | | cr = self.window.cairo_create() |
878 | 889 | cr = pangocairo.CairoContext(cr) |
879 | 890 | cr.set_source_rgba(*style.Color(self._COLOR_HOURS).get_rgba()) |
880 | 891 | pango_layout = cr.create_layout() |
… |
… |
font_desc="Sans Bold 40">%d</span></markup>') % (i + 1) |
887 | 898 | cr.save() |
888 | 899 | pango_layout.set_markup(hour_number) |
889 | 900 | dx, dy = pango_layout.get_pixel_size() |
890 | | cr.translate(- dx / 2.0 + self._center_x + 0.75 * |
| 901 | cr.translate(- dx / 2.0 + self._radius + 0.75 * |
891 | 902 | self._radius * math.cos((i - 2) * math.pi / 6.0), |
892 | | - dy / 2.0 + self._center_y + 0.75 * self._radius * |
| 903 | - dy / 2.0 + self._radius + 0.75 * self._radius * |
893 | 904 | math.sin((i - 2) * math.pi / 6.0)) |
894 | 905 | cr.update_layout(pango_layout) |
895 | 906 | cr.show_layout(pango_layout) |