Add a nice border to the pieces bar.

This commit is contained in:
Pedro Algarvio 2011-05-06 18:45:55 +01:00
commit 81637f4572

View file

@ -39,6 +39,7 @@ import cairo
import pango import pango
import pangocairo import pangocairo
import logging import logging
from math import pi
from deluge.configmanager import ConfigManager from deluge.configmanager import ConfigManager
log = logging.getLogger(__name__) log = logging.getLogger(__name__)
@ -91,6 +92,7 @@ class PiecesBar(gtk.DrawingArea):
def do_expose_event(self, event): def do_expose_event(self, event):
# Create cairo context # Create cairo context
self.__cr = self.window.cairo_create() self.__cr = self.window.cairo_create()
self.__cr.set_line_width(max(self.__cr.device_to_user_distance(0.5, 0.5)))
# Restrict Cairo to the exposed area; avoid extra work # Restrict Cairo to the exposed area; avoid extra work
self.__roundcorners_clipping() self.__roundcorners_clipping()
@ -104,6 +106,7 @@ class PiecesBar(gtk.DrawingArea):
self.__draw_progress_overlay() self.__draw_progress_overlay()
self.__write_text() self.__write_text()
self.__roundcorners_border()
# Drawn once, update width, eight # Drawn once, update width, eight
if self.__resized(): if self.__resized():
@ -111,22 +114,30 @@ class PiecesBar(gtk.DrawingArea):
self.__old_height = self.__height self.__old_height = self.__height
def __roundcorners_clipping(self): def __roundcorners_clipping(self):
from math import pi self.__create_roundcorners_subpath(
x = 0 self.__cr, 0, 0, self.__width, self.__height
y = 0 )
width = self.__width self.__cr.clip()
height = self.__height
def __roundcorners_border(self):
self.__create_roundcorners_subpath(
self.__cr, 0.5, 0.5, self.__width-1, self.__height-1
)
self.__cr.set_source_rgba(0.0, 0.0, 0.0, 0.9)
self.__cr.stroke()
def __create_roundcorners_subpath(self, ctx, x, y, width, height):
aspect = 1.0 aspect = 1.0
corner_radius = height/10.0 corner_radius = height/10.0
radius = corner_radius/aspect radius = corner_radius/aspect
degrees = pi/180.0 degrees = pi/180.0
self.__cr.new_sub_path() ctx.new_sub_path()
self.__cr.arc(x + width - radius, y + radius, radius, -90 * degrees, 0 * degrees) ctx.arc(x + width - radius, y + radius, radius, -90 * degrees, 0 * degrees)
self.__cr.arc(x + width - radius, y + height - radius, radius, 0 * degrees, 90 * degrees) ctx.arc(x + width - radius, y + height - radius, radius, 0 * degrees, 90 * degrees)
self.__cr.arc(x + radius, y + height - radius, radius, 90 * degrees, 180 * degrees) ctx.arc(x + radius, y + height - radius, radius, 90 * degrees, 180 * degrees)
self.__cr.arc(x + radius, y + radius, radius, 180 * degrees, 270 * degrees) ctx.arc(x + radius, y + radius, radius, 180 * degrees, 270 * degrees)
self.__cr.close_path() ctx.close_path()
self.__cr.clip() return ctx
def __draw_pieces(self): def __draw_pieces(self):
if (self.__resized() or self.__pieces != self.__old_pieces or if (self.__resized() or self.__pieces != self.__old_pieces or
@ -288,8 +299,11 @@ class PiecesBar(gtk.DrawingArea):
def clear(self): def clear(self):
self.__pieces = self.__old_pieces = () self.__pieces = self.__old_pieces = ()
self.__num_pieces = self.__old_num_pieces = None self.__num_pieces = self.__old_num_pieces = None
self.__text = self.__oldtext = "" self.__text = self.__old_text = ""
self.__fraction = self.__old_fraction = 0.0 self.__fraction = self.__old_fraction = 0.0
self.__state = self.__old_state = None
self.__progress_overlay = self.__text_overlay = self.__pieces_overlay = None
self.__cr = None
self.update() self.update()
def update(self): def update(self):