Playlist: add a toolbar
[nephilim.git] / nephilim / plugins / Playlist.py
blob94f7fc02b73731389e87b3ecd7d073d8defe7ed6
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
20 from ..plugin import Plugin
21 from ..common import MIMETYPES, SongsMimeData
22 from ..song import PlaylistEntryRef
24 class Playlist(Plugin):
25 # public, const
26 info = 'Shows the playlist.'
28 # public, read-only
29 o = None
31 # private
32 DEFAULTS = {'columns': ['track', 'title', 'artist', 'date', 'album', 'length'], 'header_state' : QtCore.QByteArray()}
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
46 toolbar = None
48 def __init__(self, plugin):
49 QtGui.QWidget.__init__(self)
50 self.plugin = plugin
52 self.playlist = PlaylistTree(self.plugin)
54 self.toolbar = QtGui.QToolBar(self.plugin.name + ' toolbar', self)
55 self.toolbar.addAction(QtGui.QIcon(':icons/delete.png'), 'Clear current playlist.', self.plugin.mpclient.clear)
56 add_url = PlaylistAddURL(self.plugin.mpclient, self)
57 add_url.setToolTip('Add an URL to current playlist.')
58 self.toolbar.addWidget(add_url)
60 self.setLayout(QtGui.QVBoxLayout())
61 self.layout().setSpacing(0)
62 self.layout().setMargin(0)
63 self.layout().addWidget(self.toolbar)
64 self.layout().addWidget(self.playlist)
66 self.plugin.mpclient.playlist(self.playlist.fill)
68 class PlaylistAddURL(QtGui.QLineEdit):
70 ### PRIVATE ####
71 _mpclient = None
73 def __init__(self, mpclient, parent = None):
74 QtGui.QLineEdit.__init__(self, parent)
75 self._mpclient = mpclient
76 self.returnPressed.connect(self._return_pressed)
78 def _return_pressed(self):
79 self._mpclient.add([self.text()])
80 self.clear()
82 class PlaylistTree(QtGui.QTreeWidget):
83 plugin = None
85 ### PRIVATE ###
86 # popup menu
87 _menu = None
88 # add same... menu
89 _same_menu = None
90 # currently playing song item (highlighted
91 _cur_song = None
93 def __init__(self, plugin):
94 QtGui.QTreeWidget.__init__(self)
95 self.plugin = plugin
97 self.setSelectionMode(QtGui.QTreeWidget.ExtendedSelection)
98 self.setAlternatingRowColors(True)
99 self.setRootIsDecorated(False)
101 # drag&drop
102 self.viewport().setAcceptDrops(True)
103 self.setDropIndicatorShown(True)
104 self.setDragDropMode(QtGui.QAbstractItemView.DragDrop)
106 columns = self.plugin.settings.value(self.plugin.name + '/columns')
107 self.setColumnCount(len(columns))
108 self.setHeaderLabels(columns)
109 self.header().restoreState(self.plugin.settings.value(self.plugin.name + '/header_state'))
111 # menu
112 self._menu = QtGui.QMenu()
113 self._same_menu = self._menu.addMenu('Add same...')
114 self.setContextMenuPolicy(QtCore.Qt.CustomContextMenu)
115 self.customContextMenuRequested.connect(self._show_context_menu)
117 self.itemActivated.connect(self._song_activated)
118 self.header().geometriesChanged.connect(self._save_state)
119 self.plugin.mpclient.playlist_changed.connect(lambda :self.plugin.mpclient.playlist(self.fill))
120 self.plugin.mpclient.connect_changed.connect(self._update_menu)
121 self.plugin.mpclient.song_changed.connect(self._update_cur_song)
122 self.plugin.mpclient.songpos_changed.connect(self._update_cur_song)
124 def _save_state(self):
125 self.plugin.settings.setValue(self.plugin.name + '/header_state', self.header().saveState())
127 def _song_activated(self, item):
128 self.plugin.mpclient.play(item.song['id'])
130 def _update_cur_song(self, song):
131 if self._cur_song:
132 self._cur_song.set_current(False)
134 if not song:
135 self._cur_song = None
136 return
138 self._cur_song = self.topLevelItem(int(song['pos']))
139 if self._cur_song:
140 self._cur_song.set_current(True)
142 def fill(self, songs):
143 columns = self.plugin.settings.value(self.plugin.name + '/columns')
144 self._cur_song = None
145 self.clear()
146 for song in songs:
147 item = PlaylistSongItem(PlaylistEntryRef(self.plugin.mpclient, song['id']))
148 for i in range(len(columns)):
149 item.setText(i, song['?' + columns[i]])
150 self.addTopLevelItem(item)
151 self._update_cur_song(self.plugin.mpclient.cur_song)
153 def keyPressEvent(self, event):
154 if event.matches(QtGui.QKeySequence.Delete):
155 ids = []
156 for item in self.selectedItems():
157 ids.append(item.song['id'])
159 self.plugin.mpclient.delete(ids)
160 else:
161 QtGui.QTreeWidget.keyPressEvent(self, event)
163 def mimeData(self, items):
164 data = SongsMimeData()
165 data.set_plistsongs([items[0].song['id']])
166 return data
168 def dropMimeData(self, parent, index, data, action):
169 if data.hasFormat(MIMETYPES['plistsongs']):
170 if parent:
171 index = self.indexOfTopLevelItem(parent)
172 elif index >= self.topLevelItemCount():
173 index = self.topLevelItemCount() - 1
174 self.plugin.mpclient.move(data.plistsongs()[0], index)
175 return True
176 elif data.hasFormat(MIMETYPES['songs']):
177 if parent:
178 index = self.indexOfTopLevelItem(parent)
179 self.plugin.mpclient.add(data.songs(), index)
180 return True
181 return False
183 def supportedDropActions(self):
184 return QtCore.Qt.CopyAction | QtCore.Qt.MoveAction
186 def mimeTypes(self):
187 return [MIMETYPES['songs'], MIMETYPES['plistsongs']]
189 def _update_menu(self):
190 """Update popup menu. Invoked on (dis)connect."""
191 self._same_menu.clear()
192 for tag in self.plugin.mpclient.tagtypes:
193 self._same_menu.addAction(tag, lambda tag = tag: self._add_selected_same(tag))
195 def _add_selected_same(self, tag):
196 """Adds all tracks in DB with tag 'tag' same as selected tracks."""
197 for it in self.selectedItems():
198 self.plugin.mpclient.findadd(tag, it.song['?' + tag])
200 def _show_context_menu(self, pos):
201 if not self.indexAt(pos).isValid():
202 return
203 self._menu.popup(self.mapToGlobal(pos))
205 class PlaylistSongItem(QtGui.QTreeWidgetItem):
206 ### PUBLIC ###
207 song = None
209 def __init__(self, song):
210 QtGui.QTreeWidgetItem.__init__(self)
211 self.song = song
213 def set_current(self, val):
214 font = QtGui.QFont()
215 if val:
216 font.setBold(True)
217 font.setItalic(True)
219 for i in xrange(self.columnCount()):
220 self.setFont(i, font)