From 8594c3b33045a98f72d53f19bba193027e479648 Mon Sep 17 00:00:00 2001 From: Bryan Schumaker Date: Fri, 20 Aug 2010 19:37:55 -0400 Subject: [PATCH] Collection get_attr() and list hover popup Collections now have a get_attr function. This function takes a song id and a string with the requested attribute. The value of the attribute is returned. This is used for generating a hover popup with the playcount. --- libsaria/collection/__init__.py | 5 +++++ libsaria/collection/collection.py | 8 +++++++- libsaria/collection/tree.py | 1 - ocarina/collection.py | 27 ++++++++++++++++++++++----- ocarina/list.py | 2 +- 5 files changed, 35 insertions(+), 8 deletions(-) diff --git a/libsaria/collection/__init__.py b/libsaria/collection/__init__.py index 7e0f25be..d83774b4 100644 --- a/libsaria/collection/__init__.py +++ b/libsaria/collection/__init__.py @@ -29,3 +29,8 @@ def walk_library(): for track in library: yield track + +def lib_get_attr(id, attr): + global library + return library.get_attr(id, attr) + diff --git a/libsaria/collection/collection.py b/libsaria/collection/collection.py index c04b41d7..b46866ae 100644 --- a/libsaria/collection/collection.py +++ b/libsaria/collection/collection.py @@ -18,6 +18,13 @@ class Collection: for track in self.tree.walk(): yield track + def get_attr(self, id, attr): + node = self.tree + tags = self.table[id] + for tag in tags: + node = node[tag] + return node[attr] + def scan(self, path): print "Scanning path:", path global ins_table @@ -51,7 +58,6 @@ class Collection: return (self.table, self.index, self.tree) = objects - def insert(self, file, filepath): global tag global audio diff --git a/libsaria/collection/tree.py b/libsaria/collection/tree.py index c38df1db..5d5f10c4 100644 --- a/libsaria/collection/tree.py +++ b/libsaria/collection/tree.py @@ -28,7 +28,6 @@ class Tree(dict): def walk(self): keys = self.keys() keys.sort() - keys.reverse() get_item = self.__getitem__ for key in keys: for track in get_item(key).walk(): diff --git a/ocarina/collection.py b/ocarina/collection.py index 2634fcbe..dfa4b09f 100644 --- a/ocarina/collection.py +++ b/ocarina/collection.py @@ -12,32 +12,46 @@ gtk = ocarina.gtk class Collection(gtk.ScrolledWindow): - def __init__(self): + def __init__(self, mouse_motion): gtk.ScrolledWindow.__init__(self) self.set_policy(gtk.POLICY_AUTOMATIC, gtk.POLICY_AUTOMATIC) self.list = list.List() + self.list.connect("motion-notify-event", self.on_mouse_motion) + self.mouse_motion = mouse_motion self.add(self.list) self.show() def populate(self, func): self.list.freeze() insert = self.list.list.insert + ins_next = 0 for track in func(): get = track.__getitem__ - insert(0, [get("id"), + insert(ins_next, [get("id"), get("title"), get("length"), get("artist"), get("album"), - get("year"), - get("playcount")]) + get("year"), ""]) + ins_next += 1 self.list.thaw() + def on_mouse_motion(self, widget, pos): + list = self.list + x,y = list.convert_bin_window_to_widget_coords(int(pos.x), int(pos.y)) + row = list.get_dest_row_at_pos(x,y) + list = list.list + if row is not None: + row = row[0] + list_iter = list.get_iter(row) + list_row = list[list_iter] + string = self.mouse_motion(list_row) + list.set_value(list_iter, len(list_row)-1, "Played: %s"%string) class Library(Collection): def __init__(self): - Collection.__init__(self) + Collection.__init__(self, self.mouse_motion) libsaria.event.invite("POSTSTART", self.populate, bg=True) def populate(self): @@ -46,3 +60,6 @@ class Library(Collection): Collection.populate(self, collection.walk_library) after = datetime.datetime.now() print "Populating took: %s" % (after-before) + + def mouse_motion(self, row): + return collection.lib_get_attr(row[0], "playcount") diff --git a/ocarina/list.py b/ocarina/list.py index f9082794..f0f13ac9 100644 --- a/ocarina/list.py +++ b/ocarina/list.py @@ -9,7 +9,7 @@ gobject = ocarina.gobject class List(gtk.TreeView): def __init__(self): gtk.TreeView.__init__(self) - self.list = gtk.ListStore(int, str, str, str, str, int, int) + self.list = gtk.ListStore(int, str, str, str, str, int, str) self.append = self.list.append cell = gtk.CellRendererText()