switch to PyQt4 API v2 for QStrings
[nephilim.git] / nephilim / plugins / Playlist.py
blob1e1bedbfa8d1544fa56f6c17cf71274234ddc242
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 class PlaylistSongItem(QtGui.QTreeWidgetItem):
48 # public
49 id = -1
51 def __init__(self, id):
52 QtGui.QTreeWidgetItem.__init__(self)
53 self.id = id
55 def __init__(self, plugin):
56 QtGui.QWidget.__init__(self)
57 self.plugin = plugin
59 self.playlist = self.Playlist(self.plugin)
61 self.setLayout(QtGui.QVBoxLayout())
62 self.layout().setSpacing(0)
63 self.layout().setMargin(0)
64 self.layout().addWidget(self.playlist)
66 if self.plugin.mpclient.is_connected():
67 self.playlist.fill()
69 class Playlist(QtGui.QTreeWidget):
70 song = None
71 plugin = None
73 def __init__(self, plugin):
74 QtGui.QTreeWidget.__init__(self)
75 self.plugin = plugin
77 self.setSelectionMode(QtGui.QTreeWidget.ExtendedSelection)
78 self.setAlternatingRowColors(True)
79 self.setRootIsDecorated(False)
80 columns = self.plugin.settings.value(self.plugin.name + '/columns').toStringList()
81 self.setColumnCount(len(columns))
82 self.setHeaderLabels(columns)
83 self.header().restoreState(self.plugin.settings.value(self.plugin.name + '/header_state').toByteArray())
84 self.itemActivated.connect(self._song_activated)
85 self.header().geometriesChanged.connect(self._save_state)
87 self.plugin.mpclient.playlist_changed.connect(self.fill)
89 def _save_state(self):
90 self.plugin.settings.setValue(self.plugin.name + '/header_state', QVariant(self.header().saveState()))
92 def _song_activated(self, item):
93 self.plugin.mpclient.play(item.id)
95 def fill(self):
96 columns = self.plugin.settings.value(self.plugin.name + '/columns').toStringList()
97 self.clear()
98 for song in self.plugin.mpclient.playlistinfo():
99 item = PlaylistWidget.PlaylistSongItem(song['id'])
100 for i in range(len(columns)):
101 item.setText(i, unicode(song[str(columns[i])]))
102 self.addTopLevelItem(item)
104 def keyPressEvent(self, event):
105 if event.matches(QtGui.QKeySequence.Delete):
106 ids = []
107 for item in self.selectedItems():
108 ids.append(item.id)
110 self.plugin.mpclient.delete(ids)
111 else:
112 QtGui.QTreeWidget.keyPressEvent(self, event)
114 def fill_playlist(self):
115 self.playlist.fill()