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',
33 'date', 'album', 'length']}
36 self
.o
= PlaylistWidget(self
)
41 def _get_dock_widget(self
):
42 return self
._create
_dock
(self
.o
)
44 class PlaylistWidget(QtGui
.QWidget
):
49 def __init__(self
, plugin
):
50 QtGui
.QWidget
.__init
__(self
)
53 self
.playlist
= PlaylistTree(self
.plugin
)
55 self
.setLayout(QtGui
.QVBoxLayout())
56 self
.layout().setSpacing(0)
57 self
.layout().setMargin(0)
58 self
.layout().addWidget(self
.playlist
)
60 self
.plugin
.mpclient
.playlist(self
.playlist
.fill
)
62 class PlaylistTree(QtGui
.QTreeWidget
):
71 def __init__(self
, plugin
):
72 QtGui
.QTreeWidget
.__init
__(self
)
75 self
.setSelectionMode(QtGui
.QTreeWidget
.ExtendedSelection
)
76 self
.setAlternatingRowColors(True)
77 self
.setRootIsDecorated(False)
80 self
.viewport().setAcceptDrops(True)
81 self
.setDropIndicatorShown(True)
82 self
.setDragDropMode(QtGui
.QAbstractItemView
.DragDrop
)
84 columns
= self
.plugin
.settings
.value(self
.plugin
.name
+ '/columns')
85 self
.setColumnCount(len(columns
))
86 self
.setHeaderLabels(columns
)
87 self
.header().restoreState(self
.plugin
.settings
.value(self
.plugin
.name
+ '/header_state'))
90 self
._menu
= QtGui
.QMenu()
91 self
._same
_menu
= self
._menu
.addMenu('Add same...')
92 self
.setContextMenuPolicy(QtCore
.Qt
.CustomContextMenu
)
93 self
.customContextMenuRequested
.connect(self
._show
_context
_menu
)
95 self
.itemActivated
.connect(self
._song
_activated
)
96 self
.header().geometriesChanged
.connect(self
._save
_state
)
97 self
.plugin
.mpclient
.playlist_changed
.connect(lambda :self
.plugin
.mpclient
.playlist(self
.fill
))
98 self
.plugin
.mpclient
.connect_changed
.connect(self
._update
_menu
)
100 def _save_state(self
):
101 self
.plugin
.settings
.setValue(self
.plugin
.name
+ '/header_state', self
.header().saveState())
103 def _song_activated(self
, item
):
104 self
.plugin
.mpclient
.play(item
.song
['id'])
106 def fill(self
, songs
):
107 columns
= self
.plugin
.settings
.value(self
.plugin
.name
+ '/columns')
110 item
= PlaylistSongItem(PlaylistEntryRef(self
.plugin
.mpclient
, song
['id']))
111 for i
in range(len(columns
)):
112 item
.setText(i
, song
['?' + columns
[i
]])
113 self
.addTopLevelItem(item
)
115 def keyPressEvent(self
, event
):
116 if event
.matches(QtGui
.QKeySequence
.Delete
):
118 for item
in self
.selectedItems():
119 ids
.append(item
.song
['id'])
121 self
.plugin
.mpclient
.delete(ids
)
123 QtGui
.QTreeWidget
.keyPressEvent(self
, event
)
125 def mimeData(self
, items
):
126 data
= SongsMimeData()
127 data
.set_plistsongs([items
[0].song
['id']])
130 def dropMimeData(self
, parent
, index
, data
, action
):
131 if data
.hasFormat(MIMETYPES
['plistsongs']):
133 index
= self
.indexOfTopLevelItem(parent
)
134 elif index
>= self
.topLevelItemCount():
135 index
= self
.topLevelItemCount() - 1
136 self
.plugin
.mpclient
.move(data
.plistsongs()[0], index
)
138 elif data
.hasFormat(MIMETYPES
['songs']):
140 index
= self
.indexOfTopLevelItem(parent
)
141 self
.plugin
.mpclient
.add(data
.songs(), index
)
145 def supportedDropActions(self
):
146 return QtCore
.Qt
.CopyAction | QtCore
.Qt
.MoveAction
149 return [MIMETYPES
['songs'], MIMETYPES
['plistsongs']]
151 def _update_menu(self
):
152 """Update popup menu. Invoked on (dis)connect."""
153 self
._same
_menu
.clear()
154 for tag
in self
.plugin
.mpclient
.tagtypes
:
155 self
._same
_menu
.addAction(tag
, lambda tag
= tag
: self
._add
_selected
_same
(tag
))
157 def _add_selected_same(self
, tag
):
158 """Adds all tracks in DB with tag 'tag' same as selected tracks."""
159 for it
in self
.selectedItems():
160 self
.plugin
.mpclient
.findadd(tag
, it
.song
['?' + tag
])
162 def _show_context_menu(self
, pos
):
163 if not self
.indexAt(pos
).isValid():
165 self
._menu
.popup(self
.mapToGlobal(pos
))
167 class PlaylistSongItem(QtGui
.QTreeWidgetItem
):
171 def __init__(self
, song
):
172 QtGui
.QTreeWidgetItem
.__init
__(self
)