Skip to content

Instantly share code, notes, and snippets.

@fossfreedom
Last active December 24, 2015 18:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fossfreedom/6846458 to your computer and use it in GitHub Desktop.
Save fossfreedom/6846458 to your computer and use it in GitHub Desktop.
python reworking of xnoise iconview renderer - intended for rhythmbox coverart-browser #220.
from gi.repository import Gtk, GObject, Gdk, Pango, PangoCairo, GdkPixbuf
from gi.repository.GdkPixbuf import Pixbuf
ICON_LARGE_PIXELSIZE=180
class CellRendererThumb(Gtk.CellRendererPixbuf):
markup=GObject.property(type=str, default="")
extra_info=GObject.property(type=str, default="")
def __init__(self, font_description):
super(CellRendererThumb, self).__init__()
self.font_description = font_description
self.set_fixed_size(ICON_LARGE_PIXELSIZE, ICON_LARGE_PIXELSIZE)
ypad = 0
print "thumb"
def do_render(self, cr, widget,
background_area,
cell_area,
flags):
x_offset = cell_area.x + 1
y_offset = cell_area.y + 1
wi = 0
he = 0
#IMAGE
Gdk.cairo_set_source_pixbuf(cr, self.props.pixbuf, x_offset, y_offset)
cr.paint()
#PANGO LAYOUT
layout_width = cell_area.width - 2
pango_layout = PangoCairo.create_layout(cr)
pango_layout.set_markup(self.markup , -1)
pango_layout.set_alignment(Pango.Alignment.CENTER)
pango_layout.set_font_description(self.font_description)
pango_layout.set_width( int(layout_width * Pango.SCALE))
pango_layout.set_wrap(Pango.WrapMode.WORD_CHAR)
wi,he = pango_layout.get_pixel_size()
rect_offset = y_offset + (int((2.0 * ICON_LARGE_PIXELSIZE) / 3.0))
rect_height = int(ICON_LARGE_PIXELSIZE / 3.0)
was_to_large = False;
if(he > rect_height):
was_to_large = True
pango_layout.set_ellipsize(Pango.EllipsizeMode.END)
pango_layout.set_height( int((ICON_LARGE_PIXELSIZE / 3.0) * Pango.SCALE))
wi, he = pango_layout.get_pixel_size()
#RECTANGLE
alpha = 0.65
if((flags & Gtk.CellRendererState.PRELIT) == Gtk.CellRendererState.PRELIT):
alpha -= 0.15
if((flags & Gtk.CellRendererState.SELECTED) == Gtk.CellRendererState.SELECTED or \
(flags & Gtk.CellRendererState.FOCUSED) == Gtk.CellRendererState.FOCUSED):
alpha -= 0.15
cr.set_source_rgba(0.0, 0.0, 0.0, alpha)
cr.set_line_width(0)
cr.rectangle(x_offset,
rect_offset,
cell_area.width - 1,
rect_height - 1)
cr.fill()
#DRAW FONT
cr.set_source_rgba(1.0, 1.0, 1.0, 1.0)
cr.move_to(x_offset,
y_offset
+ 2.0 * ICON_LARGE_PIXELSIZE / 3.0
+ (((ICON_LARGE_PIXELSIZE/3.0) - he) / 2.0)
)
PangoCairo.show_layout(cr, pango_layout)
if(self.extra_info != ""):
#PANGO LAYOUT
info_width = int(cell_area.width * 2.0 / 3.0)
info_layout = PangoCairo.create_layout(cr)
info_layout.set_text(self.extra_info, -1)
info_layout.set_alignment(Pango.Alignment.LEFT)
info_layout.set_font_description(self.font_description)
info_layout.set_width( int(info_width * Pango.SCALE))
info_layout.set_ellipsize(Pango.EllipsizeMode.END)
wi, he = info_layout.get_pixel_size()
info_layout.set_height(-1) #one line
#RECTANGLE
cr.set_source_rgba(0.0, 0.0, 0.0, alpha)
cr.set_line_width(0)
cr.rectangle(cell_area.x + 1,
cell_area.y + 1,
wi + 2,
he + 2)
cr.fill()
#DRAW FONT
cr.set_source_rgba(1.0, 1.0, 1.0, 1.0)
cr.move_to(cell_area.x + 2, cell_area.y + 2)
PangoCairo.show_layout(cr, info_layout)
class AlbumArtCellArea(Gtk.CellAreaBox):
font_family = GObject.property(type=str, default="Sans")
font_size = GObject.property(type=int, default=10)
def __init__(self):
super(AlbumArtCellArea, self).__init__()
#cells = new List<CellRenderer>();
self.font_description = Pango.FontDescription.new()
self.font_description.set_family(self.font_family)
self.font_description.set_size(int(self.font_size * Pango.SCALE))
#Add own cellrenderer
renderer_thumb = CellRendererThumb(self.font_description)
self.pack_end(renderer_thumb, False, False, False) # originally just 2 params - need to understand the last two values
self.attribute_connect(renderer_thumb, "pixbuf", 0)
self.attribute_connect(renderer_thumb, "markup", 1)
self.attribute_connect(renderer_thumb, "extra-info", 2)
print "cellarea"
def get_preferred_width(self, context,
widget):
retval = ICON_LARGE_PIXELSIZE + 1
return retval, retval
icons = ["gtk-cut", "gtk-paste", "gtk-copy"]
class IconViewWindow(Gtk.Window):
def __init__(self):
Gtk.Window.__init__(self)
self.set_default_size(200, 200)
liststore = Gtk.ListStore(Pixbuf, str, str)
iconview = Gtk.IconView.new_with_area(AlbumArtCellArea()) # this is with the custom cellareabox
#iconview = Gtk.IconView.new() # this is the default
iconview.set_model(liststore)
#iconview.set_pixbuf_column(0)
#iconview.set_text_column(1)
#iconview.set_text_column(2)
for icon in icons:
pixbuf = Gtk.IconTheme.get_default().load_icon(icon, 64, 0)
pixbuf = pixbuf.scale_simple(ICON_LARGE_PIXELSIZE, ICON_LARGE_PIXELSIZE, GdkPixbuf.InterpType.BILINEAR)
liststore.append([pixbuf, '<i>Label</i>', 'Label2'])
self.add(iconview)
win = IconViewWindow()
win.connect("delete-event", Gtk.main_quit)
win.show_all()
Gtk.main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment