Ticket #1959: 0007-Draw-the-simple-background-only-when-the-widget-resi.patch

File 0007-Draw-the-simple-background-only-when-the-widget-resi.patch, 5.9 KB (added by manuq, 11 years ago)
  • clock.py

    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): 
    545545        self._center_x = None
    546546        self._center_y = None
    547547        self._radius = -1
    548         self._width = None
    549         self._height = None
    550548        self._line_width = 2
    551549        self._hand_sizes = {}
    552550
    class ClockFace(gtk.DrawingArea): 
    614612        self._center_y = int(allocation.height / 2.0)
    615613        self._radius = max(min(int(allocation.width / 2.0), \
    616614                int(allocation.height / 2.0)) - 20, 0)
    617         self._width = allocation.width
    618         self._height = allocation.height
    619615        self._line_width = int(self._radius / 150)
    620616
     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
    621627        # Reload the svg handle
    622628        self._svg_handle = rsvg.Handle(file="clock.svg")
    623         cr = self.window.cairo_create()
     629
     630        # Draw nice clock background
    624631        self._nice_background_cache = cr.get_target().create_similar(
    625632                cairo.CONTENT_COLOR_ALPHA, self._radius * 2,
    626633                self._radius * 2)
    class ClockFace(gtk.DrawingArea): 
    762769    def _draw_simple_clock(self):
    763770        """Draw the simple clock variants.
    764771        """
    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
    767779        self._draw_hands()
    768780
    769     def _draw_simple_background(self):
     781    def _draw_simple_background(self, cr):
    770782        """Draw the background of the simple clock.
    771783        The simple clock background is a white disk, with hours and minutes
    772784        ticks, and the hour numbers.
    773785        """
    774         cr = self.window.cairo_create()
    775786        cr.set_line_width(4 * self._line_width)
    776787        cr.set_line_cap(cairo.LINE_CAP_ROUND)
    777788
    778789        # Simple clock background
    779790        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)
    781793        cr.fill_preserve()
    782794        cr.set_source_rgba(*style.Color(self._COLOR_BLACK).get_rgba())
    783795        cr.stroke()
    class ClockFace(gtk.DrawingArea): 
    796808
    797809            cos = math.cos(i * math.pi / 30.0)
    798810            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))
    803815            cr.stroke()
    804816
    805817    def _draw_nice_background(self):
    class ClockFace(gtk.DrawingArea): 
    809821        rsvg handle, and we just transform this handle and render it
    810822        with cairo.
    811823        """
    812         # We transform the background SVG
     824        # Place the nice background
    813825        cr = self.window.cairo_create()
    814826        cr.translate(self._center_x - self._radius,
    815827                self._center_y - self._radius)
    class ClockFace(gtk.DrawingArea): 
    871883            int(self._center_y - self._hand_sizes['seconds'] * cos))
    872884        cr.stroke()
    873885
    874     def _draw_numbers(self):
     886    def _draw_numbers(self, cr):
    875887        """Draw the numbers of the hours.
    876888        """
    877         cr = self.window.cairo_create()
    878889        cr = pangocairo.CairoContext(cr)
    879890        cr.set_source_rgba(*style.Color(self._COLOR_HOURS).get_rgba())
    880891        pango_layout = cr.create_layout()
    font_desc="Sans Bold 40">%d</span></markup>') % (i + 1) 
    887898            cr.save()
    888899            pango_layout.set_markup(hour_number)
    889900            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 *
    891902                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 *
    893904                math.sin((i - 2) * math.pi / 6.0))
    894905            cr.update_layout(pango_layout)
    895906            cr.show_layout(pango_layout)