ocarina/ocarina/source.py
Bryan Schumaker d380d0ec45 Use built-in selected_foreach
I also made a wrapper so I can do things with each selected ID.
2010-12-17 08:18:33 -05:00

178 lines
4.8 KiB
Python

# Bryan Schumaker (8/16/2010)
import ocarina
import menu
from components import image
libsaria = ocarina.libsaria
from libsaria import sources
library = sources.library
playlist = sources.playlist
get_attrs = library.get_attrs
event = ocarina.libsaria.event
gtk = ocarina.gtk
cols = ["Id", "Title", "Length", "Artist", "Album", "Year"]
colw = [ 2, 300, 60, 125, 125, 50]
cell = gtk.CellRendererText()
cell.set_fixed_height_from_font(1)
class Column(gtk.TreeViewColumn):
def __init__(self, index, label):
gtk.TreeViewColumn.__init__(self, label, cell)
self.add_attribute(cell, 'text', index)
self.set_resizable(True)
self.set_sizing(gtk.TREE_VIEW_COLUMN_FIXED)
self.set_min_width(2)
self.set_max_width(700)
self.set_fixed_width(colw[index])
class List(gtk.ListStore):
def __init__(self):
gtk.ListStore.__init__(self, int, str, str, str, str, int)
class ListView(gtk.TreeView):
def __init__(self, is_visible, right_click, play_id):
gtk.TreeView.__init__(self)
self.list = List()
event.invite("POSTLOAD", self.goto)
for index, label in enumerate(cols):
col = Column(index, label)
if index != 0 and index != 6:
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(is_visible)
self.sel = self.get_selection()
self.sel.set_mode(gtk.SELECTION_MULTIPLE)
self.set_model(self.filter_model)
self.set_fixed_height_mode(True)
self.connect("row-activated", self.double_click)
self.connect("button-release-event", self.button_click)
self.connect("query-tooltip", self.query_tooltip)
self.insert = self.list.insert
self.refilter = self.filter_model.refilter
self.clear = self.list.clear
self.right_click = right_click
self.play_id = play_id
self.for_each_selected = self.sel.selected_foreach
self.show_all()
def button_click(self, widget, event):
if event.button == 3:
self.right_click(event.button, event.time)
def double_click(self, widget, path, column):
model = self.get_model()
iter = model.get_iter(path)
row = model[iter]
self.play_id(row[0])
def path_to_id(self, tree, path, iter, func):
func(tree[iter][0])
def for_each_selected_id(self, func):
self.for_each_selected(self.path_to_id, func)
def freeze(self, lock=False):
if lock == True:
gtk.gdk.threads_enter()
self.freeze_child_notify()
self.set_model(None)
def thaw(self, lock=False):
self.set_model(self.filter_model)
self.thaw_child_notify()
if lock == True:
gtk.gdk.threads_leave()
def goto(self, *args):
id = sources.get_attrs("id")
if id == -1: # Bad id
return
vis = self.get_visible_rect()
if vis[3] == 0: # The widget hasn't been realized yet
return
for index, row in enumerate(self.filter_model):
if row[0] == id:
col = self.get_column(0)
row_vis = self.get_cell_area(index, col)
if row_vis[3] > 0:
n = vis[3] / row_vis[3]
path = index - (n / 2)
if path < 0:
path = 0
self.scroll_to_cell(path, None, True, 0, 0)
self.set_cursor_on_cell(index, None, None, False)
return
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])
id = self.filter_model[iter][0]
#attrs = get_attrs(id, "art", "title", "artist", "album", "year", "lenstr", "count")
attrs = get_attrs(id, "title", "artist", "album", "year", "lenstr", "count")
#art = image.Image()
#art.set_from_file(attrs[0])
#art.set_height(52)
#tip.set_icon(art.get_pixbuf())
tip.set_markup("<b>%s</b>\nby %s\nfrom %s" %
(attrs[0].replace("&", "&amp;"),
attrs[1].replace("&", "&amp;"),
attrs[2].replace("&", "&amp;"))
)
extra = gtk.Label()
extra.set_markup(" Year: %s\n Length: %s\n Play count: %s" %
(attrs[3], attrs[4], attrs[5]) )
tip.set_custom(extra)
return True
class Source(gtk.ScrolledWindow):
def __init__(self):
gtk.ScrolledWindow.__init__(self)
self.set_policy(gtk.POLICY_AUTOMATIC, gtk.POLICY_AUTOMATIC)
self.list = None
self.filter = None
self.show()
def init(self, filter, is_visible, right_click, play_id, reset):
self.list = ListView(is_visible, right_click, play_id)
self.add(self.list)
self.refilter = self.list.refilter
self.filter = filter
self.reset = reset
self.clear = self.list.clear
self.goto = self.list.goto
self.for_each_selected_id = self.list.for_each_selected_id
def fill(self, walk_func, lock = False):
self.list.freeze(lock)
insert = self.list.insert
get_attrs = library.get_attrs
ins_next = 0
for id in walk_func():
attrs = get_attrs(id, "id", "title", "lenstr", "artist", "album", "year")
insert(ins_next, attrs)
ins_next += 1
self.list.thaw(lock)