ocarina/src/extra/oGtk/list.py

145 lines
3.6 KiB
Python

#! /usr/bin/python
# To change this template, choose Tools | Templates
# and open the template in the editor.
__author__="bjschuma"
__date__ ="$Mar 21, 2010 1:06:27 PM$"
import gtk
import guibuilder
import db
import ocarina
from et import needle
import re
import index
from ct import times
from ct.call import *
class SongList(gtk.TreeView):
def __init__(self):
gtk.TreeView.__init__(self)
self.countvar = None
self.timevar = None
ocarina.events.invite(ocarina.events.FILTER_START,self.filter)
self.list = gtk.ListStore(int,str,str,str,str,int)
self.set_reorderable(True)
cell = gtk.CellRendererText()
cols = ["Id","Title","Length","Artist","Album","Play Count"]
for i in range(len(cols)):
col = gtk.TreeViewColumn(cols[i],cell)
col.add_attribute(cell,'text',i)
col.set_resizable(True)
col.set_sort_column_id(i)
col.set_sizing(gtk.TREE_VIEW_COLUMN_FIXED)
col.set_min_width(2)
col.set_max_width(700)
col.set_fixed_width(200)
col.set_sort_indicator(True)
if cols[i] != "Id":
self.append_column(col)
self.set_rules_hint(True)
self.connect("row-activated",self.doubleClick)
#self.tree.connect("button_release_event",self.clicked)
self.sel = self.get_selection()
self.sel.set_mode(gtk.SELECTION_MULTIPLE)
#self.list.set_sort_column_id(self.data.sortedCol,gtk.SORT_ASCENDING)
self.cols = self.get_columns()
#self.loadCols()
#self.tree.show()
#self.add(self.tree)
self.filterModel = self.list.filter_new()
self.filterModel.set_visible_func(self.setvisible)
self.sort = gtk.TreeModelSort(self.filterModel)
self.set_model(self.sort)
#self.set_model(self.list)
#self.show()
self.show_all()
# Add a row to the list
def insert(self,trackid):
file = list(db.gettrack(trackid))
ocarina.vars[self.timevar] += file[2]
file[2] = times.ftime(file[2])
self.list.append(file)
if self.countvar != None:
ocarina.vars[self.countvar] += 1
def filter(self):
text = ocarina.vars["$filterText"]
ocarina.vars[self.timevar] = 0
self.results = index.search(ocarina.vars["$filterText"])
if self.countvar != None:
ocarina.vars[self.countvar] = len(self.results)
self.filterModel.refilter()
for row in self.sort:
ocarina.vars[self.timevar] += times.hms2sec(row[2])
def setvisible(self,songlist,iter):
if ocarina.vars["$filterText"] == "":
return True
if (songlist[iter][0] in self.results) == True:
return True
return False
def doubleClick(self,tree,index,col):
trid = self.sort[index][0]
path = db.getpath(trid)
load(path)
play()
class LibraryList(SongList):
def __init__(self):
SongList.__init__(self)
self.countvar = "$libcount"
self.timevar = "$LibraryLength"
ocarina.vars.LIBCOUNT = 0
ocarina.vars.LIBRARYLENGTH = 0
ocarina.events.invite(ocarina.events.GUI_DONE,self.populate)
def populate(self):
ocarina.events.start("ocarina-filter-start")
thread = needle.Needle(self.insertLibrary)
thread.start()
#self.insertLibrary()
def insertLibrary(self):
# Freeze the model before inserting a lot of rows
# this speeds up performance
self.hide()
self.freeze_child_notify()
self.set_model(None)
try:
libid = db.libid("Music")
for track in db.getsongs(libid):
self.insert(track[1])
except Exception,e:
print e
ocarina.events.start("ocarina-filter-start")
self.set_model(self.sort)
# Unfreeze the model
self.thaw_child_notify()
self.show()
def make_songlist(attrs=None):return SongList()
def make_librarylist(attrs=None):return LibraryList()
guibuilder.parts["songlist"] = make_songlist
guibuilder.parts["librarylist"] = make_librarylist