from PyQt4 import QtGui, QtCore from wgSongList import SongList from misc import * from clSettings import settings class Playlist(QtGui.QWidget): """The Playlist widget is a list optimized for displaying a playlist, with filtering.""" name=None # name of this playlist txtFilter=None # filter input cmbMode=None # what mode? library, playlist? lstSongs=None # list winMain=None # the main window onKeyPress=None _timerID=None def __init__(self, parent, wMain, headers, name, onDoubleClick, onKeyPress): QtGui.QWidget.__init__(self, parent) self.name=name self.winMain=wMain self.onKeyPress=onKeyPress self.txtFilter=QtGui.QLineEdit() self.cmbMode=QtGui.QComboBox() self.cmbMode.addItem('playlist') self.cmbMode.addItem('artist') self.cmbMode.addItem('artist/album') self.cmbMode.addItem('artist/date/album') self.cmbMode.addItem('album') self.cmbMode.addItem('genre') self.cmbMode.addItem('genre/artist') self.cmbMode.addItem('genre/artist/album') self.lstSongs=SongList(parent, self.name, headers, onDoubleClick) i=int(settings.get("l%s.mode"%(self.name), 0)) self.cmbMode.setCurrentIndex(i) self.onModeChangeChange(str(self.cmbMode.itemText(i))) frame=QtGui.QVBoxLayout() frame.addWidget(self.cmbMode) frame.addWidget(self.txtFilter) frame.addWidget(self.lstSongs) self.setLayout(frame) self.connect(self.txtFilter, QtCore.SIGNAL('textChanged(const QString&)'), self.txtFilterChange) self.connect(self.txtFilter, QtCore.SIGNAL('editingFinished()'), self.txtFilterQuit) self.connect(self.cmbMode, QtCore.SIGNAL('currentIndexChanged(const QString&)'), self.onModeChangeChange) _prevTxtFilter=None def timerEvent(self, event): if self.txtFilter.text()!=self._prevTxtFilter: # only filter after a little while self.filter() def keyPressEvent(self, event): self.onKeyPress(event) def txtFilterChange(self): if self._timerID==None: # only start filtering after a little while self._timerID=self.startTimer(500) def txtFilterQuit(self): if self._timerID: self.killTimer(self._timerID) self._timerID=None def update(self, songs): """Update the list of songs.""" self.lstSongs.updateSongs(songs) if len(self.txtFilter.text()): self.filter() def setMode(self, mode, levels=''): """Set the mode of showing the list.""" if mode=='playlist': self.cmbMode.setCurrentIndex(0) else: self.cmbMode.setCurrentIndex(self.cmbMode.findText(levels)) def selectedSongs(self): """Get the selected songs.""" return self.lstSongs.selectedSongs() def selectedIds(self): """Get the selected songIDs.""" return map(lambda song: song._data['id'], self.lstSongs.selectedSongs()) def getSelItemID(self): """Get the id of the first selected song.""" try: return self.lstSongs.selectedSongs()[0].getID() except: return -1 def filter(self): """Filter the songs according to the inputbox.""" self.lstSongs.filter(str(self.txtFilter.text()).strip().lower()) def colorID(self, id, clr): """Color song with ID $id with color $clr.""" self.lstSongs.colorID(id, clr) def onModeChangeChange(self, value): if value=='playlist': self.lstSongs.setMode('playlist') else: self.lstSongs.setMode('library', str(value)) settings.set("l%s.mode"%(self.name), self.cmbMode.currentIndex()) self.filter() def showColumn(self, column, show=True): """Show or hide column $column.""" self.lstSongs.showColumn(column, show) def selectRow(self, row): """Select row $row.""" self.lstSongs.selectRow(row) def ensureVisible(self, id): """Make sure song with ID $id is visible.""" self.lstSongs.ensureVisible(id)