ocarina/trunk/src/list.py

167 lines
3.7 KiB
Python

import gtk
import re
class List(gtk.ScrolledWindow):
def __init__(self,data,text):
gtk.ScrolledWindow.__init__(self)
self.set_policy(gtk.POLICY_AUTOMATIC,gtk.POLICY_AUTOMATIC)
self.text = text
self.count = 0
self.seconds = 0
self.list = gtk.ListStore(int,str,str,str,str,int)
self.tree = gtk.TreeView(self.list)
self.data = data
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,"cliked",list)
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)
self.show()
self.resizeCols()
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 selectSong(self):
print "song selected"
def resizeCols(self):
print "resize cols"
def insert(self,file):
self.list.insert(self.count,[file.id,file.title,file.length,file.artist,file.album,file.count])
self.makeLabel()
self.timeText()
def makeLabel(self):
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.makeLabel()
self.timeText()
def hideRows(self,list,iter,string):
file = self.data.library.files[list[iter][0]]
if self.string == "":
self.visibleSong(file)
return True
elif len(self.string) == 1:
try:
file.single[self.string]
self.visibleSong(file)
return True
except:
return False
elif len(self.string) == 2:
try:
file.double[self.string]
self.visibleSong(file)
return True
except:
return False
if re.search(self.string,file.titlel):
self.visibleSong(file)
return True
elif re.search(self.string,file.artistl):
self.visibleSong(file)
return True
elif re.search(self.string,file.albuml):
self.visibleSong(file)
return True
return False
def visibleSong(self,file):
self.seconds += file.duration
self.count+=1