mpclient: change info from function to var.
[nephilim.git] / nephilim / plugins / Playlist.py
blob44d7a403f7470957b899a788851002181d6f0267
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 # public, const
25 info = 'Shows the playlist.'
27 # public, read-only
28 o = None
30 # private
31 DEFAULTS = {'columns': ['track', 'title', 'artist',
32 'date', 'album', 'length']}
34 def _load(self):
35 self.o = PlaylistWidget(self)
37 def _unload(self):
38 self.o = None
40 def _get_dock_widget(self):
41 return self._create_dock(self.o)
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)
59 class Playlist(QtGui.QTreeWidget):
60 song = None
61 plugin = None
63 def __init__(self, plugin):
64 QtGui.QTreeWidget.__init__(self)
65 self.plugin = plugin
67 self.setSelectionMode(QtGui.QTreeWidget.ExtendedSelection)
68 self.setAlternatingRowColors(True)
69 self.setRootIsDecorated(False)
70 columns = self.plugin.settings.value(self.plugin.name + '/columns').toStringList()
71 self.setColumnCount(len(columns))
72 self.setHeaderLabels(columns)
73 self.header().restoreState(self.plugin.settings.value(self.plugin.name + '/header_state').toByteArray())
74 self.itemActivated.connect(self._song_activated)
75 self.header().geometriesChanged.connect(self._save_state)
77 self.plugin.mpclient.playlist_changed.connect(self.fill)
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()