[GtkUI] Refactor out duplicate code in piecesbar draw pieces

This commit is contained in:
Giorgos Retsinas 2016-10-22 02:26:35 +03:00 committed by Calum Lind
parent 642913b0f8
commit c4282f29ab
1 changed files with 27 additions and 64 deletions

View File

@ -12,22 +12,17 @@ from __future__ import division
import logging import logging
from math import pi from math import pi
import cairo
import gtk import gtk
import pango import pango
import pangocairo import pangocairo
from cairo import FORMAT_ARGB32, Context, ImageSurface
from deluge.configmanager import ConfigManager from deluge.configmanager import ConfigManager
log = logging.getLogger(__name__) log = logging.getLogger(__name__)
COLOR_STATES = { COLOR_STATES = ["missing", "waiting", "downloading", "completed"]
0: "missing",
1: "waiting",
2: "downloading",
3: "completed"
}
class PiecesBar(gtk.DrawingArea): class PiecesBar(gtk.DrawingArea):
@ -75,13 +70,7 @@ class PiecesBar(gtk.DrawingArea):
# Restrict Cairo to the exposed area; avoid extra work # Restrict Cairo to the exposed area; avoid extra work
self.__roundcorners_clipping() self.__roundcorners_clipping()
if self.__pieces: self.__draw_pieces()
self.__draw_pieces()
elif self.__num_pieces:
# Special case. Completed torrents do not send any pieces in their
# status.
self.__draw_pieces_completed()
self.__draw_progress_overlay() self.__draw_progress_overlay()
self.__write_text() self.__write_text()
self.__roundcorners_border() self.__roundcorners_border()
@ -104,7 +93,8 @@ class PiecesBar(gtk.DrawingArea):
self.__cr.set_source_rgba(0.0, 0.0, 0.0, 0.9) self.__cr.set_source_rgba(0.0, 0.0, 0.0, 0.9)
self.__cr.stroke() self.__cr.stroke()
def __create_roundcorners_subpath(self, ctx, x, y, width, height): @staticmethod
def __create_roundcorners_subpath(ctx, x, y, width, height):
aspect = 1.0 aspect = 1.0
corner_radius = height / 10 corner_radius = height / 10
radius = corner_radius / aspect radius = corner_radius / aspect
@ -118,24 +108,26 @@ class PiecesBar(gtk.DrawingArea):
return ctx return ctx
def __draw_pieces(self): def __draw_pieces(self):
if (self.__resized() or self.__pieces != self.__old_pieces or if not self.__pieces and not self.__num_pieces:
self.__pieces_overlay is None): # Nothing to draw.
# Need to recreate the cache drawing return
self.__pieces_overlay = cairo.ImageSurface(
cairo.FORMAT_ARGB32, self.__width, self.__height
)
ctx = cairo.Context(self.__pieces_overlay)
start_pos = 0
num_pieces = self.__num_pieces and self.__num_pieces or len(self.__pieces)
piece_width = self.__width * 1 / num_pieces
for state in self.__pieces: if self.__resized() or self.__pieces != self.__old_pieces or self.__pieces_overlay is None:
# Need to recreate the cache drawing
self.__pieces_overlay = ImageSurface(FORMAT_ARGB32, self.__width, self.__height)
ctx = Context(self.__pieces_overlay)
start_pos = 0
if self.__pieces:
pieces = self.__pieces
elif self.__num_pieces:
# Completed torrents do not send any pieces so create list using 'completed' state.
pieces = [COLOR_STATES.index("completed")] * self.__num_pieces
piece_width = self.__width / len(pieces)
for state in pieces:
color = self.gtkui_config["pieces_color_%s" % COLOR_STATES[state]] color = self.gtkui_config["pieces_color_%s" % COLOR_STATES[state]]
ctx.set_source_rgb( ctx.set_source_rgb(color[0] / 65535, color[1] / 65535, color[2] / 65535)
color[0] / 65535,
color[1] / 65535,
color[2] / 65535,
)
ctx.rectangle(start_pos, 0, piece_width, self.__height) ctx.rectangle(start_pos, 0, piece_width, self.__height)
ctx.fill() ctx.fill()
start_pos += piece_width start_pos += piece_width
@ -143,41 +135,14 @@ class PiecesBar(gtk.DrawingArea):
self.__cr.set_source_surface(self.__pieces_overlay) self.__cr.set_source_surface(self.__pieces_overlay)
self.__cr.paint() self.__cr.paint()
def __draw_pieces_completed(self):
if (self.__resized() or self.__pieces != self.__old_pieces or
self.__pieces_overlay is None):
# Need to recreate the cache drawing
self.__pieces_overlay = cairo.ImageSurface(
cairo.FORMAT_ARGB32, self.__width, self.__height
)
ctx = cairo.Context(self.__pieces_overlay)
piece_width = self.__width * 1 / self.__num_pieces
start = 0
for _ in range(self.__num_pieces):
# Like this to keep same aspect ratio
color = self.gtkui_config["pieces_color_%s" % COLOR_STATES[3]]
ctx.set_source_rgb(
color[0] / 65535,
color[1] / 65535,
color[2] / 65535,
)
ctx.rectangle(start, 0, piece_width, self.__height)
ctx.fill()
start += piece_width
self.__cr.set_source_surface(self.__pieces_overlay)
self.__cr.paint()
def __draw_progress_overlay(self): def __draw_progress_overlay(self):
if not self.__state: if not self.__state:
# Nothing useful to draw, return now! # Nothing useful to draw, return now!
return return
if (self.__resized() or self.__fraction != self.__old_fraction) or self.__progress_overlay is None: if (self.__resized() or self.__fraction != self.__old_fraction) or self.__progress_overlay is None:
# Need to recreate the cache drawing # Need to recreate the cache drawing
self.__progress_overlay = cairo.ImageSurface( self.__progress_overlay = ImageSurface(FORMAT_ARGB32, self.__width, self.__height)
cairo.FORMAT_ARGB32, self.__width, self.__height ctx = Context(self.__progress_overlay)
)
ctx = cairo.Context(self.__progress_overlay)
ctx.set_source_rgba(0.1, 0.1, 0.1, 0.3) # Transparent ctx.set_source_rgba(0.1, 0.1, 0.1, 0.3) # Transparent
ctx.rectangle(0.0, 0.0, self.__width * self.__fraction, self.__height) ctx.rectangle(0.0, 0.0, self.__width * self.__fraction, self.__height)
ctx.fill() ctx.fill()
@ -193,10 +158,8 @@ class PiecesBar(gtk.DrawingArea):
self.__state != self.__old_state or self.__state != self.__old_state or
self.__text_overlay is None): self.__text_overlay is None):
# Need to recreate the cache drawing # Need to recreate the cache drawing
self.__text_overlay = cairo.ImageSurface( self.__text_overlay = ImageSurface(FORMAT_ARGB32, self.__width, self.__height)
cairo.FORMAT_ARGB32, self.__width, self.__height ctx = Context(self.__text_overlay)
)
ctx = cairo.Context(self.__text_overlay)
pg = pangocairo.CairoContext(ctx) pg = pangocairo.CairoContext(ctx)
pl = pg.create_layout() pl = pg.create_layout()
pl.set_font_description(self.__text_font) pl.set_font_description(self.__text_font)