summaryrefslogtreecommitdiff
path: root/nephilim/plugins/Songinfo.py
blob: 2d73575a7d15715aba3e5cc49e543f3b4c2ce9d4 (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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
#
#    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 ..plugin import Plugin

class Songinfo(Plugin):
    # public, const
    info = 'Displays song metadata provided by MPD.'

    # public, read-only
    o    = None
    tags = None

    # private
    DEFAULTS = {'tagtypes' : ['track', 'title', 'artist', 'album',
                             'albumartist', 'disc', 'genre', 'date', 'composer', 'performer', 'file']}

    #### public ####
    def __init__(self, parent, mpclient, name):
        Plugin.__init__(self, parent, mpclient, name)

        self.__tags = []

    def _load(self):
        self.o = SonginfoWidget(self)
        self.mpclient.song_changed.connect(self.refresh)
        self.mpclient.connect_changed.connect(self.update_tagtypes)

        if self.mpclient.is_connected():
            self.update_tagtypes()
            self.refresh()

    def _unload(self):
        self.mpclient.song_changed.disconnect(self.refresh)
        self.mpclient.connect_changed.disconnect(self.update_tagtypes)
        self.o = None

    def _get_dock_widget(self):
        return self._create_dock(self.o)

    def get_settings_widget(self):
        return SettingsWidgetSonginfo(self)

    def update_tagtypes(self):
        self.__tags = [tag for tag in self.settings.value(self.name + '/tagtypes') if tag in self.mpclient.tagtypes]
        self.o.set_tagtypes(self.__tags)
    def refresh(self):
        self.logger.info('Refreshing.')
        metadata = {}
        song = self.mpclient.cur_song

        if not song:
            return self.o.clear()

        for tag in self.__tags:
            metadata[tag] = song[tag] if tag in song else ''
        self.o.set_metadata(metadata)

class SonginfoWidget(QtGui.QWidget):
    plugin   = None
    __labels = None

    def __init__(self, plugin):
        QtGui.QWidget.__init__(self)
        self.plugin  = plugin
        self.__labels = {}
        self.setLayout(QtGui.QGridLayout())
        self.layout().setColumnStretch(1, 1)

    def set_tagtypes(self, tagtypes):
        """Setup labels for each tagtype in the list."""
        for i in range(self.layout().rowCount()):
            it  = self.layout().itemAtPosition(i, 0)
            it1 = self.layout().itemAtPosition(i, 1)
            if it:
                self.layout().removeItem(it)
                it.widget().setParent(None)
            if it1:
                self.layout().removeItem(it1)
                it1.widget().setParent(None)
        self.__labels = {}

        for tag in tagtypes:
            label  = QtGui.QLabel('<b>%s</b>'%tag)   #TODO sort known tags
            label1 = QtGui.QLabel()                          # tag value will go here
            label1.setTextInteractionFlags(QtCore.Qt.TextSelectableByMouse)
            label.setWordWrap(True)
            label1.setWordWrap(True)
            self.layout().addWidget(label, len(self.__labels), 0, QtCore.Qt.AlignRight)
            self.layout().addWidget(label1, len(self.__labels), 1)
            self.__labels[tag] = label1

    def set_metadata(self, metadata):
        """Set displayed metadata, which is provided in a dict of { tag : value }."""
        for tag in metadata:
            self.__labels[tag].setText(metadata[tag])

    def clear(self):
        """ Clear displayed metadata. """
        for label in self.__labels.values():
            label.clear()

class SettingsWidgetSonginfo(Plugin.SettingsWidget):
    # private
    taglist = None

    def __init__(self, plugin):
        Plugin.SettingsWidget.__init__(self, plugin)
        self.settings.beginGroup(self.plugin.name)

        self.taglist = QtGui.QListWidget(self)
        self.taglist.setDragDropMode(QtGui.QAbstractItemView.InternalMove)
        self.__update_tags()
        self.plugin.mpclient.connect_changed.connect(self.__update_tags)

        self.setLayout(QtGui.QVBoxLayout())
        self._add_widget(self.taglist, label = 'Tags', tooltip = 'A list of tags that should be displayed.\n'
                                  'Use drag and drop to change their order')


    def __update_tags(self):
        self.taglist.setEnabled(self.plugin.mpclient.is_connected())
        if not self.plugin.mpclient.is_connected():
            return

        self.taglist.clear()
        tags_enabled = self.settings.value('tagtypes')
        tags         = self.plugin.mpclient.tagtypes
        for tag in [tag for tag in tags_enabled if tag in tags]:
            it = QtGui.QListWidgetItem(tag)
            it.setCheckState(QtCore.Qt.Checked)
            self.taglist.addItem(it)
        for tag in [tag for tag in tags if tag not in tags_enabled]:
            it = QtGui.QListWidgetItem(tag)
            it.setCheckState(QtCore.Qt.Unchecked)
            self.taglist.addItem(it)

    def save_settings(self):
        if not self.taglist.isEnabled():
            return

        self.settings.beginGroup(self.plugin.name)

        tags = []
        for i in range(self.taglist.count()):
            it = self.taglist.item(i)
            if it.checkState() == QtCore.Qt.Checked:
                tags.append(it.text())
        self.settings.setValue('tagtypes', tags)

        self.plugin.update_tagtypes()
        self.plugin.refresh()