summaryrefslogtreecommitdiff
path: root/nephilim/plugins/Playlist.py
blob: b85a75931ac48c4088e4c323a8e8707c09ec14ee (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
#
#    Copyright (C) 2009 Anton Khirnov <wyskas@gmail.com>
#
#    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 <http://www.gnu.org/licenses/>.
#

from PyQt4 import QtGui, QtCore
from PyQt4.QtCore import QVariant

from ..plugin import Plugin

class Playlist(Plugin):
    # public, const
    info = 'Shows the playlist.'

    # public, read-only
    o        = None

    # private
    DEFAULTS  = {'columns': QtCore.QStringList(['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)
            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 fill_playlist(self):
        self.playlist.fill()