ocarina/ocarina/list.py

107 lines
2.9 KiB
Python

# Bryan Schumaker (8/15/2010)
import ocarina
import image
libsaria = ocarina.libsaria
gtk = ocarina.gtk
gobject = ocarina.gobject
from libsaria.collection import library
get_attr = library.get_attr
#UNI = gobject.TYPE_UNICHAR
class List(gtk.TreeView):
def __init__(self, actions):
gtk.TreeView.__init__(self)
self.actions = actions
self.list = gtk.ListStore(int, str, str, str, str, int)
self.append = self.list.append
cell = gtk.CellRendererText()
cell.set_fixed_height_from_font(1)
cols = ["Id", "Title", "Length", "Artist", "Album", "Year", "Played"]
colw = [ 2, 300, 60, 125, 125, 50, 70]
for index, label in enumerate(cols):
col = gtk.TreeViewColumn(label, cell)
col.add_attribute(cell, 'text', index)
col.set_resizable(True)
col.set_sizing(gtk.TREE_VIEW_COLUMN_FIXED)
col.set_min_width(2)
col.set_max_width(700)
col.set_fixed_width(colw[index])
if label!="Id" and label!="Played":
self.append_column(col)
self.set_rules_hint(True)
self.set_has_tooltip(True)
self.filter_model = self.list.filter_new()
self.filter_model.set_visible_func(actions.refilter)
self.refilter = self.filter_model.refilter
self.sel = self.get_selection()
self.sel.set_mode(gtk.SELECTION_MULTIPLE)
self.connect("query-tooltip", self.query_tooltip)
self.connect("row-activated", self.row_activated)
self.connect("button-release-event", self.button_clicked)
self.set_model(self.filter_model)
self.show_all()
def button_clicked(self, widget, event):
if event.button == 3:
self.actions.show_rc_menu(event.button, event.time)
def row_activated(self, widget, path, column):
list = self.filter_model
iter = list.get_iter(path)
row = list[iter]
self.actions.selected_row(row)
def freeze(self):
self.set_model(None)
self.freeze_child_notify()
def thaw(self):
self.set_model(self.filter_model)
self.thaw_child_notify()
def clear(self):
self.list.clear()
def for_each_selected(self, func):
filter = self.filter_model
for iter in self.sel.get_selected_rows()[1]:
func(filter[iter][0])
def query_tooltip(self, widget, x, y, keyboard, tip):
row = self.get_dest_row_at_pos(x,y)
if row == None:
return False
iter = self.filter_model.get_iter(row[0])#self.list.get_iter(row[0])
id = self.filter_model[iter][0]
art = image.Image()
art.set_from_file(get_attr(id, "art"))
art.set_height(52)
tip.set_icon(art.get_pixbuf())
title = get_attr(id, "title").replace("&", "&")
artist = "by %s" % get_attr(id, "artist").replace("&", "&amp")
album = "from %s" % get_attr(id, "album").replace("&", "&")
tip.set_markup("<b>%s</b>\n%s\n%s" % (title, artist, album))
year = "Year: %s" % get_attr(id, "year")
length = "Length: %s" % get_attr(id, "lenstr")
count = "Play count: %s" % get_attr(id, "playcount")
extra = gtk.Label()
extra.set_markup(" %s\n %s\n %s" % (year, length, count))
tip.set_custom(extra)
return True