Index filtering
I have implemented simple filtering over the library. This will return a set of visible songs based on the search pattern.
This commit is contained in:
parent
168025fb60
commit
6c6477b677
|
@ -56,4 +56,8 @@ def lib_filter(text):
|
||||||
global library
|
global library
|
||||||
library.filter(text)
|
library.filter(text)
|
||||||
|
|
||||||
|
def lib_is_visible(text):
|
||||||
|
global library
|
||||||
|
return library.is_visible(text)
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -1,5 +1,7 @@
|
||||||
# Bryan Schumaker (8/10/2010)
|
# Bryan Schumaker (8/10/2010)
|
||||||
|
|
||||||
|
import re
|
||||||
|
|
||||||
translate = unicode.translate
|
translate = unicode.translate
|
||||||
split = unicode.split
|
split = unicode.split
|
||||||
space_ord = ord(" ")
|
space_ord = ord(" ")
|
||||||
|
@ -33,17 +35,37 @@ class Index(dict):
|
||||||
|
|
||||||
def insert(self, tags, id):
|
def insert(self, tags, id):
|
||||||
get = self.get
|
get = self.get
|
||||||
idset = set([id])
|
|
||||||
for tag in tags:
|
for tag in tags:
|
||||||
for word in format(tag):
|
for word in format(tag):
|
||||||
ids = get(word, None)
|
ids = get(word, None)
|
||||||
if ids == None:
|
if ids == None:
|
||||||
self[word] = idset
|
self[word] = set([id])
|
||||||
else:
|
else:
|
||||||
ids.update(idset)
|
ids.add(id)
|
||||||
|
|
||||||
def filter(self, text):
|
def filter(self, text):
|
||||||
text = unicode(text)
|
text = unicode(text)
|
||||||
search = format(text)
|
terms = format(text)
|
||||||
print search
|
results = dict()
|
||||||
|
#results = set()
|
||||||
|
|
||||||
|
search = re.search
|
||||||
|
get = self.get
|
||||||
|
|
||||||
|
for t in terms:
|
||||||
|
results[t] = set()
|
||||||
|
|
||||||
|
for key in self.keys():
|
||||||
|
for term in terms:
|
||||||
|
if search(term, key):
|
||||||
|
results[term].update(get(key))
|
||||||
|
|
||||||
|
visible = set()
|
||||||
|
for i,t in enumerate(terms):
|
||||||
|
if i == 0:
|
||||||
|
visible.update(results[t])
|
||||||
|
else:
|
||||||
|
visible.intersection_update(results[t])
|
||||||
|
return visible
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -24,6 +24,8 @@ def set_badfiles():
|
||||||
class Library(collection.Collection):
|
class Library(collection.Collection):
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
collection.Collection.__init__(self, "DLTree_test")
|
collection.Collection.__init__(self, "DLTree_test")
|
||||||
|
self.filtered = False
|
||||||
|
self.visible = None
|
||||||
|
|
||||||
def scan(self, path):
|
def scan(self, path):
|
||||||
print "Library scanning %s" % path
|
print "Library scanning %s" % path
|
||||||
|
@ -35,7 +37,17 @@ class Library(collection.Collection):
|
||||||
self.disp()
|
self.disp()
|
||||||
|
|
||||||
def filter(self, text):
|
def filter(self, text):
|
||||||
self.index.filter(text)
|
if len(text) > 0:
|
||||||
|
self.visible = self.index.filter(text)
|
||||||
|
self.filtered = True
|
||||||
|
else:
|
||||||
|
self.visible = None
|
||||||
|
self.filtered = False
|
||||||
|
|
||||||
|
def is_visible(self, id):
|
||||||
|
if self.filtered == False:
|
||||||
|
return True
|
||||||
|
return id in self.visible
|
||||||
|
|
||||||
def update(self, path):
|
def update(self, path):
|
||||||
global badfiles
|
global badfiles
|
||||||
|
|
|
@ -12,10 +12,10 @@ gtk = ocarina.gtk
|
||||||
|
|
||||||
|
|
||||||
class Collection(gtk.ScrolledWindow):
|
class Collection(gtk.ScrolledWindow):
|
||||||
def __init__(self, mouse_motion, select):
|
def __init__(self, mouse_motion, select, refilter):
|
||||||
gtk.ScrolledWindow.__init__(self)
|
gtk.ScrolledWindow.__init__(self)
|
||||||
self.set_policy(gtk.POLICY_AUTOMATIC, gtk.POLICY_AUTOMATIC)
|
self.set_policy(gtk.POLICY_AUTOMATIC, gtk.POLICY_AUTOMATIC)
|
||||||
self.list = list.List()
|
self.list = list.List(refilter)
|
||||||
|
|
||||||
self.list.connect("motion-notify-event", self.on_mouse_motion)
|
self.list.connect("motion-notify-event", self.on_mouse_motion)
|
||||||
self.list.connect("row-activated", self.row_activated)
|
self.list.connect("row-activated", self.row_activated)
|
||||||
|
@ -59,7 +59,8 @@ class Collection(gtk.ScrolledWindow):
|
||||||
|
|
||||||
class Library(Collection):
|
class Library(Collection):
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
Collection.__init__(self, self.mouse_motion, self.select_row)
|
Collection.__init__(self, self.mouse_motion, self.select_row,
|
||||||
|
self.refilter)
|
||||||
libsaria.event.invite("POSTSTART", self.populate, bg=True)
|
libsaria.event.invite("POSTSTART", self.populate, bg=True)
|
||||||
libsaria.event.invite("POSTNEWSOURCE", self.refresh, bg=True)
|
libsaria.event.invite("POSTNEWSOURCE", self.refresh, bg=True)
|
||||||
|
|
||||||
|
@ -70,9 +71,6 @@ class Library(Collection):
|
||||||
after = datetime.datetime.now()
|
after = datetime.datetime.now()
|
||||||
print "Populating took: %s" % (after-before)
|
print "Populating took: %s" % (after-before)
|
||||||
|
|
||||||
def filter(self, text):
|
|
||||||
collection.lib_filter(text)
|
|
||||||
|
|
||||||
def refresh(self, arg):
|
def refresh(self, arg):
|
||||||
self.clear()
|
self.clear()
|
||||||
self.populate()
|
self.populate()
|
||||||
|
@ -82,3 +80,10 @@ class Library(Collection):
|
||||||
|
|
||||||
def select_row(self, row):
|
def select_row(self, row):
|
||||||
collection.lib_play_id(row[0])
|
collection.lib_play_id(row[0])
|
||||||
|
|
||||||
|
def filter(self, text):
|
||||||
|
collection.lib_filter(text)
|
||||||
|
self.list.refilter()
|
||||||
|
|
||||||
|
def refilter(self, list, iter):
|
||||||
|
return collection.lib_is_visible(list[iter][0])
|
||||||
|
|
|
@ -1,6 +1,7 @@
|
||||||
# Bryan Schumaker (10/4/2010)
|
# Bryan Schumaker (10/4/2010)
|
||||||
|
|
||||||
import ocarina
|
import ocarina
|
||||||
|
timeout = ocarina.gobject.timeout_add
|
||||||
gtk = ocarina.gtk
|
gtk = ocarina.gtk
|
||||||
|
|
||||||
tabs = ocarina.tabs
|
tabs = ocarina.tabs
|
||||||
|
@ -10,6 +11,13 @@ class FilterEntry(gtk.Entry):
|
||||||
gtk.Entry.__init__(self)
|
gtk.Entry.__init__(self)
|
||||||
self.connect("changed", self.changed)
|
self.connect("changed", self.changed)
|
||||||
self.show()
|
self.show()
|
||||||
|
self.count = 0
|
||||||
|
|
||||||
def changed(self, entry):
|
def changed(self, entry):
|
||||||
tabs.filter(entry.get_text())
|
self.count += 1
|
||||||
|
timeout(200, self.filter, entry.get_text())
|
||||||
|
|
||||||
|
def filter(self, text):
|
||||||
|
self.count -= 1
|
||||||
|
if self.count == 0:
|
||||||
|
tabs.filter(text)
|
||||||
|
|
|
@ -7,10 +7,12 @@ gobject = ocarina.gobject
|
||||||
#UNI = gobject.TYPE_UNICHAR
|
#UNI = gobject.TYPE_UNICHAR
|
||||||
|
|
||||||
class List(gtk.TreeView):
|
class List(gtk.TreeView):
|
||||||
def __init__(self):
|
def __init__(self, refilter):
|
||||||
gtk.TreeView.__init__(self)
|
gtk.TreeView.__init__(self)
|
||||||
self.list = gtk.ListStore(int, str, str, str, str, int, str)
|
self.list = gtk.ListStore(int, str, str, str, str, int, str)
|
||||||
|
|
||||||
self.append = self.list.append
|
self.append = self.list.append
|
||||||
|
self.set_visible = refilter
|
||||||
|
|
||||||
cell = gtk.CellRendererText()
|
cell = gtk.CellRendererText()
|
||||||
cell.set_fixed_height_from_font(1)
|
cell.set_fixed_height_from_font(1)
|
||||||
|
@ -30,12 +32,15 @@ class List(gtk.TreeView):
|
||||||
|
|
||||||
self.set_rules_hint(True)
|
self.set_rules_hint(True)
|
||||||
self.set_tooltip_column(len(cols)-1)
|
self.set_tooltip_column(len(cols)-1)
|
||||||
|
|
||||||
self.filter_model = self.list.filter_new()
|
self.filter_model = self.list.filter_new()
|
||||||
self.filter_model.set_visible_func(self.set_visible)
|
self.filter_model.set_visible_func(self.set_visible)
|
||||||
|
self.refilter = self.filter_model.refilter
|
||||||
|
|
||||||
self.set_model(self.filter_model)
|
self.set_model(self.filter_model)
|
||||||
self.show_all()
|
self.show_all()
|
||||||
|
|
||||||
|
|
||||||
def freeze(self):
|
def freeze(self):
|
||||||
self.set_model(None)
|
self.set_model(None)
|
||||||
self.freeze_child_notify()
|
self.freeze_child_notify()
|
||||||
|
@ -44,9 +49,6 @@ class List(gtk.TreeView):
|
||||||
self.set_model(self.filter_model)
|
self.set_model(self.filter_model)
|
||||||
self.thaw_child_notify()
|
self.thaw_child_notify()
|
||||||
|
|
||||||
def set_visible(self, a, b):
|
|
||||||
return True
|
|
||||||
|
|
||||||
def clear(self):
|
def clear(self):
|
||||||
self.list.clear()
|
self.list.clear()
|
||||||
|
|
||||||
|
|
|
@ -4,6 +4,7 @@
|
||||||
from libsaria import collection
|
from libsaria import collection
|
||||||
|
|
||||||
|
|
||||||
collection.new_source("~/Music/")
|
collection.new_source("~/Desktop")
|
||||||
|
#collection.new_source("~/Music/")
|
||||||
#collection.new_source("/media/Music")
|
#collection.new_source("/media/Music")
|
||||||
print collection.library.size
|
print collection.library.size
|
||||||
|
|
Loading…
Reference in New Issue