def draw_icon(self, cc, key, w, h, hover): cc.save() cc.save() cc.scale(w, h) roundedrec(cc, 0, 0, 1, 1, self.conf['roundness']) cc.save() cc.identity_matrix() self.idata[(key, 'cairo_path')] = cc.copy_path() cc.restore() if hover: cc.set_source_rgba(1, 1, 1, 0.3) else: if self.playing and key == 'play': cc.set_source_rgba(0, 0, 0, 1) else: cc.set_source_rgba(0, 0, 0, 0.3) cc.fill() cc.restore() x = max(0, (w-h)/2) y = max(0, (h-w)/2) cc.translate(x, y) d = min(h, w) cc.scale(d, d) self.idata[(key, 'draw')](cc, key) cc.restore()
def draw_svg_icon(self, cc, key): cc.push_group() cc.set_operator(cairo.OPERATOR_OVER) cc.scale(self.idata[(key, 'scale')], self.idata[(key, 'scale')]) self.idata[(key, 'image')].render_cairo(cc) cc.set_source(cc.pop_group()) roundedrec(cc, 0, 0, 1, 1, self.conf['roundness']) cc.fill()
def draw_svg(self, cc, size = None): cc.save() cc.scale(self.scale, self.scale) cc.push_group() cc.set_operator(cairo.OPERATOR_OVER) cc.set_source_rgba(self.conf['background_color_r'], self.conf['background_color_g'], self.conf['background_color_b'], self.conf['background_color_a']) cc.paint() cc.translate(self.x, self.y) self.image.render_cairo(cc) cc.set_source(cc.pop_group()) roundedrec(cc, self.x, self.y, self.w, self.h, self.conf['roundness']) cc.fill() cc.restore()
def draw(self, cc): cc.save() cc.set_operator(cairo.OPERATOR_OVER) cc.set_source_rgba(self.conf['background_color_r'], self.conf['background_color_g'], self.conf['background_color_b'], self.conf['background_color_a'] + 0.1) roundedrec(cc, 0, 0, 1, 1, self.conf['roundness']) cc.fill() y = self.conf['hover_size'] + 2 * self.conf['border'] h = 1 - y - self.conf['border'] n = len(self.icon_keys) w = (1 - (2 + n - 1) * self.conf['border']) / n cc.translate(self.conf['border'], y) for k in self.icon_keys: self.draw_icon(cc, k, w, h, self.idata[(k, 'hover')]) cc.fill() cc.translate(self.conf['border'] + w, 0) cc.restore()
def draw_pixbuf(self, cc, size = None): img_scale = size/self.dim scaled_image = self.image.scale_simple(int(self.w * img_scale + 1.5), int(self.h * img_scale + 1.5), gtk.gdk.INTERP_TILES) cc.save() cc.set_operator(cairo.OPERATOR_SOURCE) cc.scale(self.scale, self.scale) roundedrec(cc, self.x, self.y, self.w, self.h, self.conf['roundness']) cc.set_source_rgba(self.conf['background_color_r'], self.conf['background_color_g'], self.conf['background_color_b'], self.conf['background_color_a']) cc.fill_preserve() cc.set_operator(cairo.OPERATOR_OVER) cc.scale(1/img_scale, 1/img_scale) cc.set_source_pixbuf(scaled_image, img_scale * self.x, img_scale * self.y) cs = cc.get_source() try: ## 3 = cairo.EXTEND_PAD, but doesn't appear in pycairo before 1.6 cs.set_extend(3) except AttributeError : cs.set_extend(cairo.EXTEND_REFLECT) cc.fill() cc.restore()
def draw_pixbuf(self, cc, size = None): img_scale = size/self.dim scaled_image = self.image.scale_simple(int(self.w * img_scale + 1.5), int(self.h * img_scale + 1.5), GdkPixbuf.InterpType.TILES) cc.save() cc.set_operator(cairo.OPERATOR_SOURCE) cc.scale(self.scale, self.scale) roundedrec(cc, self.x, self.y, self.w, self.h, self.conf['roundness']) cc.set_source_rgba(self.conf['background_color_r'], self.conf['background_color_g'], self.conf['background_color_b'], self.conf['background_color_a']) cc.fill_preserve() cc.set_operator(cairo.OPERATOR_OVER) cc.scale(1/img_scale, 1/img_scale) #cc.set_source_pixbuf(scaled_image, img_scale * self.x, img_scale * self.y) Gdk.cairo_set_source_pixbuf(cc, scaled_image, img_scale * self.x, img_scale * self.y) cs = cc.get_source() try: ## 3 = cairo.EXTEND_PAD, but doesn't appear in pycairo before 1.6 cs.set_extend(3) except AttributeError : cs.set_extend(cairo.EXTEND_REFLECT) cc.fill() cc.restore()
def draw_pixbuf_icon(self, cc, key): cc.scale(self.idata[(key, 'scale')], self.idata[(key, 'scale')]) #cc.set_source_pixbuf(self.idata[(key, 'image')], 0, 0) Gdk.cairo_set_source_pixbuf(cc, self.idata[(key, 'image')], 0, 0) roundedrec(cc, 0, 0, self.idata[(key, 'w')], self.idata[(key, 'h')], self.conf['roundness']) cc.fill()
def draw_cb(self, widget, cc): print("do_draw_cb") # Clear cairo context cc.set_source_rgba(0, 0, 0, 0) cc.set_operator(cairo.OPERATOR_SOURCE) cc.paint() # Scale the context so that the cover image area is 1 x 1 rect = self.get_allocation() if self.conf['draw_reflection']: cover_area_size = min(rect.width - self.conf['blur']/2, (rect.height - self.conf['blur']/2) / (1 + self.conf['reflection_height'])) else: cover_area_size = min(rect.width - self.conf['blur']/2, (rect.height - self.conf['blur']/2)) if self.conf['text_position'] in [POSITION_SW, POSITION_NW]: x_trans = int(round(rect.width - cover_area_size - self.conf['blur']/2)) else: x_trans = int(round(self.conf['blur']/2)) cc.translate(x_trans, 0) cc.scale(cover_area_size, cover_area_size) cc.push_group() self.song_info.draw(cc) if self.mouse_over: self.desktop_buttons.draw(cc) cc.save() cc.translate((1 - self.conf['hover_size']) / 2, self.conf['border']) cc.scale(self.conf['hover_size'], self.conf['hover_size']) if (self.conf['text_position'] in [POSITION_NW, POSITION_NE]) and (not self.mouse_over): if(self.cover_image.w > self.cover_image.h): cc.save() cc.translate(0, (self.cover_image.h - self.cover_image.w) / self.cover_image.w) self.cover_image.draw(cc, cover_area_size) if (self.conf['text_position'] in [POSITION_NW, POSITION_NE]) and (not self.mouse_over): if(self.cover_image.w > self.cover_image.h): cc.restore() if self.mouse_over: cc.restore() graphics = cc.pop_group() # Draw main graphics cc.set_source(graphics) cc.set_operator(cairo.OPERATOR_OVER) cc.paint() # Draw reflections if self.conf['draw_reflection']: cc.save() cc.set_operator(cairo.OPERATOR_ADD) cc.translate(0, 2.02) if (self.conf['text_position'] in [POSITION_NW, POSITION_NE]) and (not self.mouse_over): if(self.cover_image.w > self.cover_image.h): cc.save() cc.translate(0, 2 * (self.cover_image.h - self.cover_image.w) / self.cover_image.w) cc.scale(1, -1) cc.push_group() x_scale = cc.get_matrix()[0] r1 = int(self.conf['blur'] / 2 + 1.5) r0 = r1 - self.conf['blur'] - 1 bn = (self.conf['blur'] + 1)**2 for dx in range(r0, r1): for dy in range(r0, r1): cc.save() cc.translate(dx/x_scale, dy/x_scale) cc.set_source(graphics) cc.paint_with_alpha(1/bn) cc.restore() graphics = cc.pop_group() cc.set_source(graphics) shadow_mask = cairo.LinearGradient(0, 1 - self.conf['reflection_height'], 0, 1) shadow_mask.add_color_stop_rgba(0, 0, 0, 0, 0) shadow_mask.add_color_stop_rgba(1, 0, 0, 0, self.conf['reflection_intensity']) cc.mask(shadow_mask) if (self.conf['text_position'] in [POSITION_NW, POSITION_NE]) and (not self.mouse_over): if(self.cover_image.w > self.cover_image.h): cc.restore() cc.restore() # Input mask, only the cover image is clickable # Will, (and should) only work if parent is Gtk.Window surface = cairo.ImageSurface(cairo.FORMAT_ARGB32, int(rect.width), int(rect.height)) ccmask = cairo.Context(surface) roundedrec(ccmask, int(x_trans), 0, cover_area_size, cover_area_size, self.conf['roundness']) ccmask.fill() # bug in python-cairo, upstream is patched, but only for python3-cairo # https://bugs.launchpad.net/ubuntu/+source/pygobject/+bug/1028115 # https://bugzilla.gnome.org/show_bug.cgi?id=667959 # https://bugs.freedesktop.org/show_bug.cgi?id=44336 # for the moment wrap this into a try except so that it fails gracefully # need to test this on a distro patched and only running python3-cairo #21/04/14 - crash observed on 14.04 - currently presume this is a bug so #lets work around this by a test with the pygobject-version 3.12 try: print (desktop_rb3compat.pygobject_version()) if desktop_rb3compat.pygobject_version() < 3.12: region = Gdk.cairo_region_create_from_surface(surface) self.get_parent().input_shape_combine_region(region) except: pass # Draw border if self.draw_border: print("drawborder") cc.identity_matrix() cc.rectangle(0, 0, rect.width, rect.height) cc.set_line_width(2) cc.set_source_rgba(1, 1, 1, 0.35) cc.set_dash([10,10], 0) cc.stroke_preserve() cc.set_source_rgba(0, 0, 0, 0.35) cc.set_dash([10,10], 10) cc.stroke()
def draw_background(self, cc, size = None): cc.save() cc.set_source_rgba(self.conf['background_color_r'], self.conf['background_color_g'], self.conf['background_color_b'], self.conf['background_color_a']) roundedrec(cc, 0, 0, 1, 1, self.conf['roundness']) cc.fill() cc.restore()
def draw(self, cc): # Clear cairo context cc.set_source_rgba(0, 0, 0, 0) cc.set_operator(cairo.OPERATOR_SOURCE) cc.paint() # Scale the context so that the cover image area is 1 x 1 rect = self.get_allocation() if self.conf['draw_reflection']: cover_area_size = min(rect.width - self.conf['blur']/2, (rect.height - self.conf['blur']/2) / (1 + self.conf['reflection_height'])) else: cover_area_size = min(rect.width - self.conf['blur']/2, (rect.height - self.conf['blur']/2)) if self.conf['text_position'] in [POSITION_SW, POSITION_NW]: x_trans = int(round(rect.width - cover_area_size - self.conf['blur']/2)) else: x_trans = int(round(self.conf['blur']/2)) cc.translate(x_trans, 0) cc.scale(cover_area_size, cover_area_size) cc.push_group() self.song_info.draw(cc) if self.mouse_over: self.desktop_buttons.draw(cc) cc.save() cc.translate((1 - self.conf['hover_size']) / 2, self.conf['border']) cc.scale(self.conf['hover_size'], self.conf['hover_size']) if (self.conf['text_position'] in [POSITION_NW, POSITION_NE]) and (not self.mouse_over): if(self.cover_image.w > self.cover_image.h): cc.save() cc.translate(0, (self.cover_image.h - self.cover_image.w) / self.cover_image.w) self.cover_image.draw(cc, cover_area_size) if (self.conf['text_position'] in [POSITION_NW, POSITION_NE]) and (not self.mouse_over): if(self.cover_image.w > self.cover_image.h): cc.restore() if self.mouse_over: cc.restore() graphics = cc.pop_group() # Draw main graphics cc.set_source(graphics) cc.set_operator(cairo.OPERATOR_OVER) cc.paint() # Draw reflections if self.conf['draw_reflection']: cc.save() cc.set_operator(cairo.OPERATOR_ADD) cc.translate(0, 2.02) if (self.conf['text_position'] in [POSITION_NW, POSITION_NE]) and (not self.mouse_over): if(self.cover_image.w > self.cover_image.h): cc.save() cc.translate(0, 2 * (self.cover_image.h - self.cover_image.w) / self.cover_image.w) cc.scale(1, -1) cc.push_group() x_scale = cc.get_matrix()[0] r1 = int(self.conf['blur'] / 2 + 1.5) r0 = r1 - self.conf['blur'] - 1 bn = (self.conf['blur'] + 1)**2 for dx in xrange(r0, r1): for dy in xrange(r0, r1): cc.save() cc.translate(dx/x_scale, dy/x_scale) cc.set_source(graphics) cc.paint_with_alpha(1/bn) cc.restore() graphics = cc.pop_group() cc.set_source(graphics) shadow_mask = cairo.LinearGradient(0, 1 - self.conf['reflection_height'], 0, 1) shadow_mask.add_color_stop_rgba(0, 0, 0, 0, 0) shadow_mask.add_color_stop_rgba(1, 0, 0, 0, self.conf['reflection_intensity']) cc.mask(shadow_mask) if (self.conf['text_position'] in [POSITION_NW, POSITION_NE]) and (not self.mouse_over): if(self.cover_image.w > self.cover_image.h): cc.restore() cc.restore() # Input mask, only the cover image is clickable # Will, (and should) only work if parent is gtk.Window pixmask = gtk.gdk.Pixmap(None, int(cover_area_size), int(cover_area_size), 1) ccmask = pixmask.cairo_create() roundedrec(ccmask, 0, 0, cover_area_size, cover_area_size, self.conf['roundness']) ccmask.fill() self.get_parent().input_shape_combine_mask(pixmask, int(x_trans), 0) # Draw border if self.draw_border: cc.identity_matrix() cc.rectangle(0, 0, rect.width, rect.height) cc.set_line_width(2) cc.set_source_rgba(1, 1, 1, 0.35) cc.set_dash([10,10], 0) cc.stroke_preserve() cc.set_source_rgba(0, 0, 0, 0.35) cc.set_dash([10,10], 10) cc.stroke()