# # Copyright (C) 2009 Anton Khirnov # # Nephilim is free software: you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation, either version 3 of the License, or # (at your option) any later version. # # Nephilim is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with Nephilim. If not, see . # from PyQt4 import QtGui, QtCore from PyQt4.QtCore import QVariant from ..plugin import Plugin from ..common import MIMETYPES, SongsMimeData class Playlist(Plugin): # public, const info = 'Shows the playlist.' # public, read-only o = None # private DEFAULTS = {'columns': ['track', 'title', 'artist', 'date', 'album', 'length']} def _load(self): self.o = PlaylistWidget(self) def _unload(self): self.o = None def _get_dock_widget(self): return self._create_dock(self.o) class PlaylistWidget(QtGui.QWidget): plugin = None playlist = None class PlaylistSongItem(QtGui.QTreeWidgetItem): # public id = -1 def __init__(self, id): QtGui.QTreeWidgetItem.__init__(self) self.id = id def __init__(self, plugin): QtGui.QWidget.__init__(self) self.plugin = plugin self.playlist = self.Playlist(self.plugin) self.setLayout(QtGui.QVBoxLayout()) self.layout().setSpacing(0) self.layout().setMargin(0) self.layout().addWidget(self.playlist) if self.plugin.mpclient.is_connected(): self.playlist.fill() class Playlist(QtGui.QTreeWidget): song = None plugin = None def __init__(self, plugin): QtGui.QTreeWidget.__init__(self) self.plugin = plugin self.setSelectionMode(QtGui.QTreeWidget.ExtendedSelection) self.setAlternatingRowColors(True) self.setRootIsDecorated(False) # drag&drop self.viewport().setAcceptDrops(True) self.setDropIndicatorShown(True) self.setDragDropMode(QtGui.QAbstractItemView.DragDrop) columns = self.plugin.settings.value(self.plugin.name + '/columns').toStringList() self.setColumnCount(len(columns)) self.setHeaderLabels(columns) self.header().restoreState(self.plugin.settings.value(self.plugin.name + '/header_state').toByteArray()) self.itemActivated.connect(self._song_activated) self.header().geometriesChanged.connect(self._save_state) self.plugin.mpclient.playlist_changed.connect(self.fill) def _save_state(self): self.plugin.settings.setValue(self.plugin.name + '/header_state', QVariant(self.header().saveState())) def _song_activated(self, item): self.plugin.mpclient.play(item.id) def fill(self): columns = self.plugin.settings.value(self.plugin.name + '/columns').toStringList() self.clear() for song in self.plugin.mpclient.playlistinfo(): item = PlaylistWidget.PlaylistSongItem(song['id']) for i in range(len(columns)): item.setText(i, unicode(song[str(columns[i])])) self.addTopLevelItem(item) def keyPressEvent(self, event): if event.matches(QtGui.QKeySequence.Delete): ids = [] for item in self.selectedItems(): ids.append(item.id) self.plugin.mpclient.delete(ids) else: QtGui.QTreeWidget.keyPressEvent(self, event) def mimeData(self, items): data = SongsMimeData() data.set_plistsongs([items[0].id]) return data def dropMimeData(self, parent, index, data, action): if data.hasFormat(MIMETYPES['plistsongs']): if parent: index = self.indexOfTopLevelItem(parent) elif index >= self.topLevelItemCount(): index = self.topLevelItemCount() - 1 self.plugin.mpclient.move(data.plistsongs()[0], index) return True elif data.hasFormat(MIMETYPES['songs']): if parent: index = self.indexOfTopLevelItem(parent) self.plugin.mpclient.add(data.songs(), index) return True return False def supportedDropActions(self): return QtCore.Qt.CopyAction | QtCore.Qt.MoveAction def mimeTypes(self): return [MIMETYPES['songs'], MIMETYPES['plistsongs']] def fill_playlist(self): self.playlist.fill()