ocarina/trunk/src/list.py

227 lines
5.4 KiB
Python

import gtk
import re
from menuItem import MenuItem
from song import Song
class List(gtk.ScrolledWindow):
def __init__(self,data,text,nextFunc,labelFunc,plauseFunc):
gtk.ScrolledWindow.__init__(self)
self.show()
self.set_policy(gtk.POLICY_AUTOMATIC,gtk.POLICY_AUTOMATIC)
self.text = text
self.data = data
# Function pointers...whoo...
self.next = nextFunc
self.labels = labelFunc
self.plause = plauseFunc
self.count = 0
self.seconds = 0
self.makeList()
self.label = gtk.Label()
self.makeLabel()
self.align = gtk.Alignment(1,0,0,0)
self.timeLabel = gtk.Label("")
self.timeLabel.show()
self.align.add(self.timeLabel)
self.timeText()
self.string = ""
self.filter = self.list.filter_new()
self.filter.set_visible_func(self.hideRows,"")
self.sort = gtk.TreeModelSort(self.filter)
self.tree.set_model(self.sort)
#self.align.show()
def makeList(self):
self.list = gtk.ListStore(int,str,str,str,str,int)
self.tree = gtk.TreeView(self.list)
cell = gtk.CellRendererText()
cols = ["Id","Title","Length","Artist","Album","#"]
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_sort_indicator(False)
if cols[i] != "Id":
self.tree.append_column(col)
col.set_fixed_width(self.data.colSizes[i-1])
self.tree.set_rules_hint(True)
self.tree.connect("row-activated",self.selectSong,"clicked",list)
self.tree.connect("button_release_event",self.clicked)
self.sel = self.tree.get_selection()
self.sel.set_mode(gtk.SELECTION_MULTIPLE)
self.list.set_sort_column_id(self.data.sortedCol,gtk.SORT_ASCENDING)
#self.tree.show()
self.add(self.tree)
def selectSong(self,widgit,iter,path,data,list):
self.loadSong(self.data.library.files[self.filter[iter][0]])
self.plause(None,None)
self.data.scrobbler.nowPlaying(self.data.song.info)
def clicked(self,widgit,data):
if data.button == 3:
self.rcmenu.popup(None,None,None,data.button,data.time)
def insert(self,file):
self.list.insert(self.count,[file.id,file.title,file.length,file.artist,file.album,file.count])
self.count+=1
self.seconds+=file.duration
def makeLabel(self):
#print self.count, self.text
self.label.set_text(self.text+" ("+str(self.count)+")")
def timeText(self):
day = 0
hour = 0
min = 0
sec = self.seconds
day = sec/86500
sec = sec - (day*86500)
hour = sec/3600
sec = sec - (hour*3600)
min = sec/60
sec = sec - min*60
string = ""
string = self.toStr(day,"day")+self.toStr(hour,"hour")
string += self.toStr(min,"minute")+self.toStr(sec,"second")
self.timeLabel.set_text(string)
def toStr(self,time,label):
if time > 0:
string = str(time) + " " + label
if time > 1:
string+="s"
string+=" "
return string
return ""
def drop(self):
if len(self.list) == 0:
return
iter = self.list.get_iter(0)
while iter:
if self.remove(iter) == False:
self.makeLabel()
self.timeText()
return
def remove(self,iter):
self.count -= 1
self.seconds -= self.data.library.files[self.list[iter][0]].duration
return self.list.remove(iter)
def filterRows(self,string):
self.string = string
self.seconds = 0
self.count = 0
self.filter.refilter()
#for row in self.filter:
#file = self.data.library.files[row[0]]
# self.seconds+=self.data.library.files[row[0]].duration
# self.count+=1
self.count = len(self.filter)
for i in range(self.count):
self.seconds+=self.data.library.files[self.filter[i][0]].duration
self.makeLabel()
self.timeText()
def hideRows(self,list,iter,string):
file = self.data.library.files[list[iter][0]]
if self.string == "":
return True
#elif len(self.string) == 1:
#return (self.string in file.single)
#try:
# file.single[self.string]
# return True
#except:
# return False
#elif len(self.string) == 2:
# return (self.string in file.double)
#try:
# file.double[self.string]
# return True
#except:
# return False
if re.search(self.string,file.titlel):
return True
elif re.search(self.string,file.artistl):
return True
elif re.search(self.string,file.albuml):
return True
return False
def makeRCMenu(self):
self.rcmenu = gtk.Menu()
rm = MenuItem("Remove from "+self.text.lower(),self.rm,None,None,None)
self.rcmenu.append(rm)
def rm(self,widgit,func,data):
#print widgit,data,selection
print "remove"
def selection(self,func):
(model,pathlist) = self.sel.get_selected_rows()
for path in pathlist:
func(self.data.library.files[model[path[0]][0]])
self.sel.unselect_path(path)
def loadSong(self,file):
if self.data.song:
self.data.scrobbler.submit(self.data.song.timestamp)
self.data.song.close()
self.data.song = Song(file,self.next)
self.data.curSong = file.id
self.labels()
def gotoCurSong(self):
if len(self.filter) == 0:
return
for i in range(len(self.tree)):
if self.tree[i][0] == self.data.curSong:
print self.tree[i][0],self.data.curSong
if i > 10:
self.tree.scroll_to_cell(i-10,None,True,0,0)
else:
self.tree.scroll_to_cell(0,None,True,0,0)
self.tree.set_cursor_on_cell(i,None,None,False)
#return
def visible(self,func):
if func=="show":
self.align.show()
self.tree.show()
else:
self.align.hide()
self.tree.hide()