1 from PyQt4
import QtGui
, QtCore
2 from thread
import start_new_thread
4 from wgSongList
import SongList
7 from clSettings
import settings
10 class Playlist(QtGui
.QWidget
):
11 """The Playlist widget is a list optimized for displaying a playlist, with filtering."""
12 name
=None # name of this playlist
13 txtFilter
=None # filter input
15 cmbMode
=None # what mode? library, playlist?
18 winMain
=None # the main window
22 def __init__(self
, parent
, wMain
, headers
, name
, onDoubleClick
, onKeyPress
, modes
):
23 QtGui
.QWidget
.__init
__(self
, parent
)
28 self
.onKeyPress
=onKeyPress
30 self
.lstSongs
=SongList(parent
, self
.name
, headers
, onDoubleClick
)
32 self
.txtFilter
=QtGui
.QLineEdit()
33 self
.btnClearFilter
=Button("Clear", self
.clearTxtFilter
)
35 self
.cmbMode
=QtGui
.QComboBox()
38 frame
=QtGui
.QVBoxLayout()
39 filter=QtGui
.QHBoxLayout()
40 filter.addWidget(self
.txtFilter
)
41 filter.addWidget(self
.btnClearFilter
)
42 frame
.addWidget(self
.cmbMode
)
43 frame
.addLayout(filter);
44 frame
.addWidget(self
.lstSongs
)
47 self
.connect(self
.txtFilter
, QtCore
.SIGNAL('textChanged(const QString&)'), self
.txtFilterChange
)
48 self
.connect(self
.txtFilter
, QtCore
.SIGNAL('editingFinished()'), self
.txtFilterQuit
)
49 self
.connect(self
.cmbMode
, QtCore
.SIGNAL('currentIndexChanged(const QString&)'), self
.onModeChangeChange
)
51 def clearTxtFilter(self
, event
):
52 self
.txtFilter
.setText("")
55 def timerEvent(self
, event
):
56 if self
.txtFilter
.text()!=self
._prevTxtFilter
:
57 # only filter after a little while
58 self
.killTimer(self
._timerID
)
61 def keyPressEvent(self
, event
):
62 self
.onKeyPress(event
)
63 def txtFilterChange(self
):
64 if self
._timerID
==None:
65 # only start filtering after a little while
66 self
._timerID
=self
.startTimer(500)
67 def txtFilterQuit(self
):
69 self
.killTimer(self
._timerID
)
71 def updateSongs(self
, songs
):
72 """Update the list of songs."""
73 start_new_thread(self
._update
, (songs
,))
74 def _update(self
, songs
):
75 self
.lstSongs
.updateSongs(songs
)
76 if len(self
.txtFilter
.text()):
79 def setMode(self
, mode
, levels
=''):
80 """Set the mode of showing the list."""
82 self
.cmbMode
.setCurrentIndex(0)
84 self
.cmbMode
.setCurrentIndex(self
.cmbMode
.findText(levels
))
85 def selectedSongs(self
):
86 """Get the selected songs."""
87 return self
.lstSongs
.selectedSongs()
88 def selectedIds(self
):
89 """Get the selected songIDs."""
90 return map(lambda song
: song
._data
['id'], self
.lstSongs
.selectedSongs())
91 def getSelItemID(self
):
92 """Get the id of the first selected song."""
94 return self
.lstSongs
.selectedSongs()[0].getID()
99 """Filter the songs according to the inputbox."""
100 self
.lstSongs
.killFilters()
101 start_new_thread(self
.lstSongs
.filter, (str(self
.txtFilter
.text()).strip().lower(), ))
103 def colorID(self
, id, clr
):
104 """Color song with ID $id with color $clr."""
105 self
.lstSongs
.colorID(id, clr
)
107 def onModeChangeChange(self
, value
):
108 if value
=='playlist':
109 self
.lstSongs
.setMode('playlist')
111 self
.lstSongs
.setMode('library', self
.modes
[self
.cmbMode
.currentIndex()])
112 settings
.set("l%s.mode"%(self
.name
), self
.cmbMode
.currentIndex())
115 def showColumn(self
, column
, show
=True):
116 """Show or hide column $column."""
117 self
.lstSongs
.showColumn(column
, show
)
118 def selectRow(self
, row
):
119 """Select row $row."""
120 self
.lstSongs
.selectRow(row
)
121 def ensureVisible(self
, id):
122 """Make sure song with ID $id is visible."""
123 self
.lstSongs
.ensureVisible(id)
125 def setModes(self
, modes
):
126 i
=int(settings
.get("l%s.mode"%(self
.name
), 0))
127 self
.modes
=['playlist']
128 self
.modes
.extend(modes
)
130 self
.cmbMode
.addItem('playlist')
133 self
.cmbMode
.addItem(mode
.replace('$', ''))
134 self
.cmbMode
.setCurrentIndex(i
)
135 self
.onModeChangeChange(str(self
.cmbMode
.itemText(i
)))