add licence headers to all files.
[nephilim.git] / nephilim / plugins / Playlist.py
blob3d2fe8195f3983321e2ddfd836e97a815f768d93
2 # Copyright (C) 2009 Anton Khirnov <wyskas@gmail.com>
4 # Nephilim is free software: you can redistribute it and/or modify
5 # it under the terms of the GNU General Public License as published by
6 # the Free Software Foundation, either version 3 of the License, or
7 # (at your option) any later version.
9 # Nephilim is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 # GNU General Public License for more details.
14 # You should have received a copy of the GNU General Public License
15 # along with Nephilim. If not, see <http://www.gnu.org/licenses/>.
18 from PyQt4 import QtGui, QtCore
19 from PyQt4.QtCore import QVariant
21 from ..plugin import Plugin
23 class Playlist(Plugin):
24 o = None
25 DEFAULTS = {'columns': ['track', 'title', 'artist',
26 'date', 'album', 'length']}
28 def _load(self):
29 self.o = PlaylistWidget(self)
31 def _unload(self):
32 self.o = None
33 def getInfo(self):
34 return "The playlist showing the songs that will be played."
36 def _get_dock_widget(self):
37 return self._create_dock(self.o)
39 def on_playlist_change(self, params = None):
40 self.o.fill_playlist()
43 class PlaylistWidget(QtGui.QWidget):
44 plugin = None
45 playlist = None
47 def __init__(self, plugin):
48 QtGui.QWidget.__init__(self)
49 self.plugin = plugin
51 self.playlist = self.Playlist(self.plugin)
53 self.setLayout(QtGui.QVBoxLayout())
54 self.layout().setSpacing(0)
55 self.layout().setMargin(0)
56 self.layout().addWidget(self.playlist)
58 self.connect(self.plugin.mpclient(), QtCore.SIGNAL('playlist_changed'), self.fill_playlist)
59 self.connect(self.plugin.mpclient(), QtCore.SIGNAL('disconnected'), self.fill_playlist)
61 class Playlist(QtGui.QTreeWidget):
62 song = None
63 plugin = None
65 def __init__(self, plugin):
66 QtGui.QTreeWidget.__init__(self)
67 self.plugin = plugin
69 self.setSelectionMode(QtGui.QTreeWidget.ExtendedSelection)
70 self.setAlternatingRowColors(True)
71 self.setRootIsDecorated(False)
72 columns = self.plugin.settings().value(self.plugin.name() + '/columns').toStringList()
73 self.setColumnCount(len(columns))
74 self.setHeaderLabels(columns)
75 self.header().restoreState(self.plugin.settings().value(self.plugin.name() + '/header_state').toByteArray())
76 self.connect(self, QtCore.SIGNAL('itemActivated(QTreeWidgetItem*, int)'), self._song_activated)
77 self.connect(self.header(), QtCore.SIGNAL('geometriesChanged()'), self._save_state)
79 def _save_state(self):
80 self.plugin.settings().setValue(self.plugin.name() + '/header_state', QVariant(self.header().saveState()))
82 def _song_activated(self, item):
83 self.plugin.mpclient().play(item.data(0, QtCore.Qt.UserRole).toPyObject().id())
85 def fill(self):
86 columns = self.plugin.settings().value(self.plugin.name() + '/columns').toStringList()
87 self.clear()
88 for song in self.plugin.mpclient().playlist():
89 item = QtGui.QTreeWidgetItem()
90 for i in range(len(columns)):
91 item.setText(i, unicode(song.tag(str(columns[i]))))
92 item.setData(0, QtCore.Qt.UserRole, QVariant(song))
93 self.addTopLevelItem(item)
95 def keyPressEvent(self, event):
96 if event.matches(QtGui.QKeySequence.Delete):
97 ids = []
98 for item in self.selectedItems():
99 ids.append(item.data(0, QtCore.Qt.UserRole).toPyObject().id())
101 self.plugin.mpclient().delete(ids)
102 else:
103 QtGui.QTreeWidget.keyPressEvent(self, event)
105 def fill_playlist(self):
106 self.playlist.fill()