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
):
26 info
= 'Shows the playlist.'
32 DEFAULTS
= {'columns': ['track', 'title', 'artist', 'date', 'album', 'length'], 'header_state' : QtCore
.QByteArray()}
35 self
.o
= PlaylistWidget(self
)
40 def _get_dock_widget(self
):
41 return self
._create
_dock
(self
.o
)
43 class PlaylistWidget(QtGui
.QWidget
):
48 def __init__(self
, plugin
):
49 QtGui
.QWidget
.__init
__(self
)
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
):
69 # currently playing song item (highlighted
72 def __init__(self
, plugin
):
73 QtGui
.QTreeWidget
.__init
__(self
)
76 self
.setSelectionMode(QtGui
.QTreeWidget
.ExtendedSelection
)
77 self
.setAlternatingRowColors(True)
78 self
.setRootIsDecorated(False)
81 self
.viewport().setAcceptDrops(True)
82 self
.setDropIndicatorShown(True)
83 self
.setDragDropMode(QtGui
.QAbstractItemView
.DragDrop
)
85 columns
= self
.plugin
.settings
.value(self
.plugin
.name
+ '/columns')
86 self
.setColumnCount(len(columns
))
87 self
.setHeaderLabels(columns
)
88 self
.header().restoreState(self
.plugin
.settings
.value(self
.plugin
.name
+ '/header_state'))
91 self
._menu
= QtGui
.QMenu()
92 self
._same
_menu
= self
._menu
.addMenu('Add same...')
93 self
.setContextMenuPolicy(QtCore
.Qt
.CustomContextMenu
)
94 self
.customContextMenuRequested
.connect(self
._show
_context
_menu
)
96 self
.itemActivated
.connect(self
._song
_activated
)
97 self
.header().geometriesChanged
.connect(self
._save
_state
)
98 self
.plugin
.mpclient
.playlist_changed
.connect(lambda :self
.plugin
.mpclient
.playlist(self
.fill
))
99 self
.plugin
.mpclient
.connect_changed
.connect(self
._update
_menu
)
100 self
.plugin
.mpclient
.song_changed
.connect(self
._update
_cur
_song
)
101 self
.plugin
.mpclient
.songpos_changed
.connect(self
._update
_cur
_song
)
103 def _save_state(self
):
104 self
.plugin
.settings
.setValue(self
.plugin
.name
+ '/header_state', self
.header().saveState())
106 def _song_activated(self
, item
):
107 self
.plugin
.mpclient
.play(item
.song
['id'])
109 def _update_cur_song(self
, song
):
111 self
._cur
_song
.set_current(False)
114 self
._cur
_song
= None
117 self
._cur
_song
= self
.topLevelItem(int(song
['pos']))
119 self
._cur
_song
.set_current(True)
121 def fill(self
, songs
):
122 columns
= self
.plugin
.settings
.value(self
.plugin
.name
+ '/columns')
123 self
._cur
_song
= None
126 item
= PlaylistSongItem(PlaylistEntryRef(self
.plugin
.mpclient
, song
['id']))
127 for i
in range(len(columns
)):
128 item
.setText(i
, song
['?' + columns
[i
]])
129 self
.addTopLevelItem(item
)
130 self
._update
_cur
_song
(self
.plugin
.mpclient
.cur_song
)
132 def keyPressEvent(self
, event
):
133 if event
.matches(QtGui
.QKeySequence
.Delete
):
135 for item
in self
.selectedItems():
136 ids
.append(item
.song
['id'])
138 self
.plugin
.mpclient
.delete(ids
)
140 QtGui
.QTreeWidget
.keyPressEvent(self
, event
)
142 def mimeData(self
, items
):
143 data
= SongsMimeData()
144 data
.set_plistsongs([items
[0].song
['id']])
147 def dropMimeData(self
, parent
, index
, data
, action
):
148 if data
.hasFormat(MIMETYPES
['plistsongs']):
150 index
= self
.indexOfTopLevelItem(parent
)
151 elif index
>= self
.topLevelItemCount():
152 index
= self
.topLevelItemCount() - 1
153 self
.plugin
.mpclient
.move(data
.plistsongs()[0], index
)
155 elif data
.hasFormat(MIMETYPES
['songs']):
157 index
= self
.indexOfTopLevelItem(parent
)
158 self
.plugin
.mpclient
.add(data
.songs(), index
)
162 def supportedDropActions(self
):
163 return QtCore
.Qt
.CopyAction | QtCore
.Qt
.MoveAction
166 return [MIMETYPES
['songs'], MIMETYPES
['plistsongs']]
168 def _update_menu(self
):
169 """Update popup menu. Invoked on (dis)connect."""
170 self
._same
_menu
.clear()
171 for tag
in self
.plugin
.mpclient
.tagtypes
:
172 self
._same
_menu
.addAction(tag
, lambda tag
= tag
: self
._add
_selected
_same
(tag
))
174 def _add_selected_same(self
, tag
):
175 """Adds all tracks in DB with tag 'tag' same as selected tracks."""
176 for it
in self
.selectedItems():
177 self
.plugin
.mpclient
.findadd(tag
, it
.song
['?' + tag
])
179 def _show_context_menu(self
, pos
):
180 if not self
.indexAt(pos
).isValid():
182 self
._menu
.popup(self
.mapToGlobal(pos
))
184 class PlaylistSongItem(QtGui
.QTreeWidgetItem
):
188 def __init__(self
, song
):
189 QtGui
.QTreeWidgetItem
.__init
__(self
)
192 def set_current(self
, val
):
198 for i
in xrange(self
.columnCount()):
199 self
.setFont(i
, font
)