Playlist: add empty header_state to default settings
[nephilim.git] / nephilim / plugins / Playlist.py
blobdc26f0c1c6f87a8683a44f9ed586cc75f078f9cf
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
48 def __init__(self, plugin):
49 QtGui.QWidget.__init__(self)
50 self.plugin = plugin
52 self.playlist = PlaylistTree(self.plugin)
54 self.setLayout(QtGui.QVBoxLayout())
55 self.layout().setSpacing(0)
56 self.layout().setMargin(0)
57 self.layout().addWidget(self.playlist)
59 self.plugin.mpclient.playlist(self.playlist.fill)
61 class PlaylistTree(QtGui.QTreeWidget):
62 plugin = None
64 ### PRIVATE ###
65 # popup menu
66 _menu = None
67 # add same... menu
68 _same_menu = None
70 def __init__(self, plugin):
71 QtGui.QTreeWidget.__init__(self)
72 self.plugin = plugin
74 self.setSelectionMode(QtGui.QTreeWidget.ExtendedSelection)
75 self.setAlternatingRowColors(True)
76 self.setRootIsDecorated(False)
78 # drag&drop
79 self.viewport().setAcceptDrops(True)
80 self.setDropIndicatorShown(True)
81 self.setDragDropMode(QtGui.QAbstractItemView.DragDrop)
83 columns = self.plugin.settings.value(self.plugin.name + '/columns')
84 self.setColumnCount(len(columns))
85 self.setHeaderLabels(columns)
86 self.header().restoreState(self.plugin.settings.value(self.plugin.name + '/header_state'))
88 # menu
89 self._menu = QtGui.QMenu()
90 self._same_menu = self._menu.addMenu('Add same...')
91 self.setContextMenuPolicy(QtCore.Qt.CustomContextMenu)
92 self.customContextMenuRequested.connect(self._show_context_menu)
94 self.itemActivated.connect(self._song_activated)
95 self.header().geometriesChanged.connect(self._save_state)
96 self.plugin.mpclient.playlist_changed.connect(lambda :self.plugin.mpclient.playlist(self.fill))
97 self.plugin.mpclient.connect_changed.connect(self._update_menu)
99 def _save_state(self):
100 self.plugin.settings.setValue(self.plugin.name + '/header_state', self.header().saveState())
102 def _song_activated(self, item):
103 self.plugin.mpclient.play(item.song['id'])
105 def fill(self, songs):
106 columns = self.plugin.settings.value(self.plugin.name + '/columns')
107 self.clear()
108 for song in songs:
109 item = PlaylistSongItem(PlaylistEntryRef(self.plugin.mpclient, song['id']))
110 for i in range(len(columns)):
111 item.setText(i, song['?' + columns[i]])
112 self.addTopLevelItem(item)
114 def keyPressEvent(self, event):
115 if event.matches(QtGui.QKeySequence.Delete):
116 ids = []
117 for item in self.selectedItems():
118 ids.append(item.song['id'])
120 self.plugin.mpclient.delete(ids)
121 else:
122 QtGui.QTreeWidget.keyPressEvent(self, event)
124 def mimeData(self, items):
125 data = SongsMimeData()
126 data.set_plistsongs([items[0].song['id']])
127 return data
129 def dropMimeData(self, parent, index, data, action):
130 if data.hasFormat(MIMETYPES['plistsongs']):
131 if parent:
132 index = self.indexOfTopLevelItem(parent)
133 elif index >= self.topLevelItemCount():
134 index = self.topLevelItemCount() - 1
135 self.plugin.mpclient.move(data.plistsongs()[0], index)
136 return True
137 elif data.hasFormat(MIMETYPES['songs']):
138 if parent:
139 index = self.indexOfTopLevelItem(parent)
140 self.plugin.mpclient.add(data.songs(), index)
141 return True
142 return False
144 def supportedDropActions(self):
145 return QtCore.Qt.CopyAction | QtCore.Qt.MoveAction
147 def mimeTypes(self):
148 return [MIMETYPES['songs'], MIMETYPES['plistsongs']]
150 def _update_menu(self):
151 """Update popup menu. Invoked on (dis)connect."""
152 self._same_menu.clear()
153 for tag in self.plugin.mpclient.tagtypes:
154 self._same_menu.addAction(tag, lambda tag = tag: self._add_selected_same(tag))
156 def _add_selected_same(self, tag):
157 """Adds all tracks in DB with tag 'tag' same as selected tracks."""
158 for it in self.selectedItems():
159 self.plugin.mpclient.findadd(tag, it.song['?' + tag])
161 def _show_context_menu(self, pos):
162 if not self.indexAt(pos).isValid():
163 return
164 self._menu.popup(self.mapToGlobal(pos))
166 class PlaylistSongItem(QtGui.QTreeWidgetItem):
167 ### PUBLIC ###
168 song = None
170 def __init__(self, song):
171 QtGui.QTreeWidgetItem.__init__(self)
172 self.song = song