import random import gobject import pygtk pygtk.require('2.0') import gtk class PlistView(gtk.ScrolledWindow): def __init__(self,data): gtk.ScrolledWindow.__init__(self) self.set_policy(gtk.POLICY_AUTOMATIC,gtk.POLICY_AUTOMATIC) self.data = data self.data.song.next = self.next self.controls = None self.tree = None self.label = gtk.Label("") self.makeList() #self.next(None,None) self.loadSong(True) gobject.timeout_add(1000,self.checkUpdate) self.show() # Check if the playlist has been updated def checkUpdate(self): if self.data.updateList == True: self.data.updateList = False self.makeList() return True # Make the playlist and show it def makeList(self): self.trackList= gtk.ListStore(int,str,str,str,str,int) if self.tree: self.remove(self.tree) time = 0 for index in self.data.curList: track = self.data.library.files[index] self.trackList.append([track.id,track.title,track.length,track.artist,track.album,track.count]) time+=track.duration self.tree = gtk.TreeView(self.trackList) cell = gtk.CellRendererText() cols = ["Id","Title","Length","Artist","Album","#"] self.trackList.set_sort_column_id(self.data.sortedCol,gtk.SORT_ASCENDING) lenSaved = len(self.data.colSizes) 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) if cols[i] != "Id": self.tree.append_column(col) self.tree.set_rules_hint(True) self.tree.set_enable_search(True) self.tree.connect("row-activated",self.selectSong,"clicked",self.trackList) self.treesel = self.tree.get_selection() self.tree.show() self.resizeCols() self.add(self.tree) self.makeTimeLabel(time) # Use to save column widths def saveCols(self): cols = self.tree.get_columns() self.data.colSizes = [] for col in cols: self.data.colSizes +=[col.get_width()] # Uses saved column settings def resizeCols(self): cols = self.tree.get_columns() for i in range(len(self.data.colSizes)): cols[i].set_fixed_width(self.data.colSizes[i]) # Shows total running time of the playlist def makeTimeLabel(self,sec): day = 0 hour = 0 min = 0 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.label.set_text(string) self.label.show() # Make a string for the amount of time def toStr(self,time,label): if time > 0: string=str(time) + " "+label if time > 1: string+="s" string += " " return string return "" # User selected a song with mouse click def selectSong(self,widgit,iter,path,data,list): self.data.curSong = self.data.curList.index(list[iter][0]) self.loadSong(False) self.controls.plause(None,None) # Go to the next song in the list def next(self,widgit,data): if self.data.random == True: self.data.curSong = random.randint(0,len(self.data.curList)) else: self.data.curSong+=1 if self.data.curSong >= len(self.data.curList): self.data.curSong = 0 self.loadSong(True) self.controls.plause(None,None) # Load a song and begin playback def loadSong(self,scroll): if len(self.data.curList) == 0: return if self.data.song: self.data.song.close() self.data.song.passInfo(self.data.library.files[self.data.curList[self.data.curSong]]) if scroll == True: self.gotoCurSong() # Scroll window and select row with current song def gotoCurSong(self): if len(self.data.curList) == 0: return for i in range(len(self.data.curList)): if self.trackList[i][0] == self.data.song.info.id: if i > 10: self.tree.scroll_to_cell(i-10,None,True,0,0) self.treesel.select_path(i)