Пример #1
0
 def export_png(self, filename, size=None):
     """
     Saves the contents of the widget to png file. The size of the image
     will be the size of the widget.
     
     @type filename: string
     @param filename: The path to the file where you want the chart to be saved.
     @type size: tuple
     @param size: Optional parameter to give the desired height and width of the image.
     """
     if size is None:
         rect = self.get_allocation()
         width = rect.width
         height = rect.height
     else:
         width, height = size
         old_alloc = self.get_allocation
         self.get_allocation = lambda: Gdk.Rectangle(0, 0, width, height)
     surface = cairo.ImageSurface(cairo.FORMAT_ARGB32, width, height)
     ctx = cairo.Context(surface)
     context = PangoCairo.CairoContext(ctx)
     self.set_size_request(width, height)
     self.draw(context)
     surface.write_to_png(filename)
     if size is not None:
         self.get_allocation = old_alloc
Пример #2
0
 def draw_label(self, cr):
     ''' Draw the label based on its attributes '''
     # Create a PangoCairo context
     cr = PangoCairo.CairoContext(cr)
     my_width = self.rect.width - self._margins[0] - self._margins[2]
     if my_width < 0:
         my_width = 0
     my_height = self.rect.height - self._margins[1] - self._margins[3]
     for i in range(len(self.labels)):
         pl = cr.create_layout()
         pl.set_text(str(self.labels[i]))
         self._fd.set_size(int(self._scale[i] * Pango.SCALE))
         pl.set_font_description(self._fd)
         w = pl.get_size()[0] / Pango.SCALE
         if w > my_width:
             if self._rescale[i]:
                 self._fd.set_size(
                     int(self._scale[i] * Pango.SCALE * my_width / w))
                 pl.set_font_description(self._fd)
                 w = pl.get_size()[0] / Pango.SCALE
             else:
                 j = len(self.labels[i]) - 1
                 while (w > my_width and j > 0):
                     pl.set_text("…" +
                                 self.labels[i][len(self.labels[i]) - j:])
                     self._fd.set_size(int(self._scale[i] * Pango.SCALE))
                     pl.set_font_description(self._fd)
                     w = pl.get_size()[0] / Pango.SCALE
                     j -= 1
         if self._horiz_align[i] == "center":
             x = int(self.rect.x + self._margins[0] + (my_width - w) / 2)
         elif self._horiz_align[i] == 'left':
             x = int(self.rect.x + self._margins[0])
         else:  # right
             x = int(self.rect.x + self.rect.width - w - self._margins[2])
         h = pl.get_size()[1] / Pango.SCALE
         if self._vert_align[i] == "middle":
             y = int(self.rect.y + self._margins[1] + (my_height - h) / 2)
         elif self._vert_align[i] == "top":
             y = int(self.rect.y + self._margins[1])
         else:  # bottom
             y = int(self.rect.y + self.rect.height - h - self._margins[3])
         cr.save()
         cr.translate(x, y)
         cr.set_source_rgb(self._color[0], self._color[1], self._color[2])
         cr.update_layout(pl)
         cr.show_layout(pl)
         cr.restore()
Пример #3
0
 def label_width(self):
     ''' Calculate the width of a label '''
     cr = PangoCairo.CairoContext(self._sprites.cr)
     if cr is not None:
         max = 0
         for i in range(len(self.labels)):
             pl = cr.create_layout()
             pl.set_text(self.labels[i])
             self._fd.set_size(int(self._scale[i] * Pango.SCALE))
             pl.set_font_description(self._fd)
             w = pl.get_size()[0] / Pango.SCALE
             if w > max:
                 max = w
         return max
     else:
         return self.rect.width
Пример #4
0
    def expose(self, widget, event):
        cr = widget.window.cairo_create()
        if not hasattr(cr, 'set_source_pixbuf'):
            self._draw_gi(cr)
            return
        # Draw splash
        cr.set_source_pixbuf(self._pixbuf, 0, 0)
        cr.paint()

        # Draw version
        cr.set_source_rgb(1, 1, 1)
        pcr = PangoCairo.CairoContext(cr)
        layout = pcr.create_layout()
        layout.set_alignment(Pango.Alignment.CENTER)
        layout.set_font_description(Pango.FontDescription("Sans 10"))
        layout.set_markup(self._get_label())
        pcr.update_layout(layout)
        w, h = layout.get_pixel_size()
        cr.move_to((WIDTH - w) / 1.05, (HEIGHT - h) / 1.05)
        pcr.show_layout(layout)
Пример #5
0
    def _draw_numbers(self, cr):
        """Draw the numbers of the hours.
        """
        cr = PangoCairo.CairoContext(cr)
        cr.set_source_rgba(*style.Color(self._COLOR_HOURS).get_rgba())
        pango_layout = cr.create_layout()

        for i in xrange(12):
            # TRANS: The format of the font used to print hour
            # numbers, from 1 to 12.
            hour_number = _('<markup><span lang="en" \
font_desc="Sans Bold 40">%d</span></markup>') % (i + 1)
            cr.save()
            pango_layout.set_markup(hour_number)
            dx, dy = pango_layout.get_pixel_size()
            cr.translate(- dx / 2.0 + self._radius + 0.75 *
                         self._radius * math.cos((i - 2) * math.pi / 6.0),
                         - dy / 2.0 + self._radius + 0.75 * self._radius *
                         math.sin((i - 2) * math.pi / 6.0))
            cr.update_layout(pango_layout)
            cr.show_layout(pango_layout)
            cr.restore()
Пример #6
0
 def save_surface(self, surface, width, height, native):
     cairo_context = cairo.Context(surface)
     context = PangoCairo.CairoContext(cairo_context)
     self.MainArea.export(context, width, height, native)
     surface.finish()
Пример #7
0
    def _draw_hands(self):
        """Draw the hands of the analog clocks.
        """
        cr = self.window.cairo_create()
        cr.set_line_cap(cairo.LINE_CAP_ROUND)

        # AM/PM indicator:
        pangocairo_context = PangoCairo.CairoContext(cr)
        pangocairo_context.set_source_rgba(
            *style.Color(self._COLOR_HOURS).get_rgba())
        pango_layout = pangocairo_context.create_layout()
        if self._am_pm == 'AM':
            am_pm = _('<markup><span lang="en" font_desc="Sans Bold 28">\
<span foreground="white" background="black"> AM </span><span \
foreground="lightgray"> PM </span></span></markup>')
        else:
            am_pm = _('<markup><span lang="en" font_desc="Sans Bold 28">\
<span foreground="lightgray"> AM </span><span foreground="white" \
background="black"> PM </span></span></markup>')
        pangocairo_context.save()
        pango_layout.set_markup(am_pm)
        self.am_pm_width, self.am_pm_height = pango_layout.get_pixel_size()
        pangocairo_context.translate(- self.am_pm_width / 2.0 + self._center_x,
                                     - self.am_pm_height / 2.0 +
                                     (self._radius / 3) + self._center_y)
        pangocairo_context.update_layout(pango_layout)
        pangocairo_context.show_layout(pango_layout)
        pangocairo_context.restore()

        # Hour hand:
        # The hour hand is rotated 30 degrees (pi/6 r) per hour +
        # 1/2 a degree (pi/360) per minute
        cr.set_source_rgba(*style.Color(self._COLOR_HOURS).get_rgba())
        cr.set_line_width(9 * self._line_width)
        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)
        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:
        # The minute hand is rotated 6 degrees (pi/30 r) per minute
        cr.set_source_rgba(*style.Color(self._COLOR_MINUTES).get_rgba())
        cr.set_line_width(6 * self._line_width)
        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)
        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:
        # Operates identically to the minute hand
        cr.set_source_rgba(*style.Color(self._COLOR_SECONDS).get_rgba())
        cr.set_line_width(2 * self._line_width)
        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)
        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()