Notify: simplify code.
[nephilim.git] / wgPlaylist.py
blob2d4c25c7c65b3a6ca606170632a9761a85c34cb9
1 from PyQt4 import QtGui, QtCore
2 from thread import start_new_thread
4 from wgSongList import SongList
6 from misc import *
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
14 btnClearFilter=None
15 cmbMode=None # what mode? library, playlist?
16 modes=[]
17 lstSongs=None # list
18 winMain=None # the main window
19 onKeyPress=None
20 _timerID=None
21 frameLayout = None
22 filterLayout = None
24 def __init__(self, parent, wMain, headers, name, onDoubleClick, onKeyPress, modes):
25 QtGui.QWidget.__init__(self, parent)
27 self.name=name
28 self.winMain=wMain
30 self.onKeyPress=onKeyPress
32 self.lstSongs=SongList(parent, self.name, headers, onDoubleClick)
34 self.txtFilter=QtGui.QLineEdit()
36 self.btnClearFilter = QtGui.QPushButton(QtGui.QIcon('gfx/clear_left.png'), "")
37 self.btnClearFilter.palette().setColor(QtGui.QPalette.Button, self.palette().color(QtGui.QPalette.Window))
38 self.btnClearFilter.connect(self.btnClearFilter, QtCore.SIGNAL('clicked(bool)'), self.clearTxtFilter)
40 self.cmbMode=QtGui.QComboBox()
41 self.setModes(modes)
43 self.frameLayout = QtGui.QVBoxLayout()
44 self.frameLayout.setSpacing(1)
45 self.frameLayout.setMargin(0)
46 self.filterLayout = QtGui.QHBoxLayout()
47 self.filterLayout.setSpacing(0)
48 self.filterLayout.addWidget(self.txtFilter)
49 self.filterLayout.addWidget(self.btnClearFilter)
50 self.frameLayout.addWidget(self.cmbMode)
51 self.frameLayout.addLayout(self.filterLayout);
52 self.frameLayout.addWidget(self.lstSongs)
53 self.setLayout(self.frameLayout)
55 self.connect(self.txtFilter, QtCore.SIGNAL('textChanged(const QString&)'), self.txtFilterChange)
56 self.connect(self.txtFilter, QtCore.SIGNAL('editingFinished()'), self.txtFilterQuit)
57 self.connect(self.cmbMode, QtCore.SIGNAL('currentIndexChanged(const QString&)'), self.onModeChangeChange)
59 def clearTxtFilter(self, event):
60 self.txtFilter.setText("")
62 _prevTxtFilter=None
63 def timerEvent(self, event):
64 if self.txtFilter.text()!=self._prevTxtFilter:
65 # only filter after a little while
66 self.killTimer(self._timerID)
67 self._timerID=None
68 self.filter()
69 def keyPressEvent(self, event):
70 self.onKeyPress(event)
71 def txtFilterChange(self):
72 if self._timerID==None:
73 # only start filtering after a little while
74 self._timerID=self.startTimer(500)
75 def txtFilterQuit(self):
76 if self._timerID:
77 self.killTimer(self._timerID)
78 self._timerID=None
79 def updateSongs(self, songs):
80 """Update the list of songs."""
81 start_new_thread(self._update, (songs,))
82 def _update(self, songs):
83 self.lstSongs.updateSongs(songs)
84 if len(self.txtFilter.text()):
85 self.filter()
87 def setMode(self, mode, levels=''):
88 """Set the mode of showing the list."""
89 if mode=='playlist':
90 self.cmbMode.setCurrentIndex(0)
91 else:
92 self.cmbMode.setCurrentIndex(self.cmbMode.findText(levels))
93 def selectedSongs(self):
94 """Get the selected songs."""
95 return self.lstSongs.selectedSongs()
96 def selectedIds(self):
97 """Get the selected songIDs."""
98 return map(lambda song: song._data['id'], self.lstSongs.selectedSongs())
99 def getSelItemID(self):
100 """Get the id of the first selected song."""
101 try:
102 return self.lstSongs.selectedSongs()[0].getID()
103 except:
104 return -1
106 def filter(self):
107 """Filter the songs according to the inputbox."""
108 self.lstSongs.killFilters()
109 start_new_thread(self.lstSongs.filter, (str(self.txtFilter.text()).strip().lower(), ))
111 def colorID(self, id, clr):
112 """Color song with ID $id with color $clr."""
113 self.lstSongs.colorID(id, clr)
115 def onModeChangeChange(self, value):
116 if value=='playlist':
117 self.lstSongs.setMode('playlist')
118 else:
119 self.lstSongs.setMode('library', self.modes[self.cmbMode.currentIndex()])
120 settings.set("l%s.mode"%(self.name), self.cmbMode.currentIndex())
121 self.filter()
123 def showColumn(self, column, show=True):
124 """Show or hide column $column."""
125 self.lstSongs.showColumn(column, show)
126 def selectRow(self, row):
127 """Select row $row."""
128 self.lstSongs.selectRow(row)
129 def ensureVisible(self, id):
130 """Make sure song with ID $id is visible."""
131 self.lstSongs.ensureVisible(id)
133 def setModes(self, modes):
134 i=int(settings.get("l%s.mode"%(self.name), 0))
135 self.modes=['playlist']
136 self.modes.extend(modes)
137 self.cmbMode.clear()
138 self.cmbMode.addItem('playlist')
139 for mode in modes:
140 if mode:
141 self.cmbMode.addItem(mode.replace('$', ''))
142 self.cmbMode.setCurrentIndex(i)
143 self.onModeChangeChange(str(self.cmbMode.itemText(i)))