Songinfo: rewrite to use all metadata from MPD.
[nephilim.git] / nephilim / plugins / Songinfo.py
blob44de8bede7f05dc576e8ada08737965d438d4012
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
22 class Songinfo(Plugin):
23 # public, const
24 info = 'Displays song metadata provided by MPD.'
26 # public, read-only
27 o = None
28 tags = None
30 #### public ####
31 def __init__(self, parent, mpclient, name):
32 Plugin.__init__(self, parent, mpclient, name)
34 self.__tags = []
36 def _load(self):
37 self.o = SonginfoWidget(self)
38 self.mpclient.song_changed.connect(self.refresh)
39 self.mpclient.connect_changed.connect(self.__update_tagtypes)
41 def _unload(self):
42 self.mpclient.song_changed.disconnect(self.refresh)
43 self.mpclient.connect_changed.disconnect(self.__update_tagtypes)
44 self.o = None
46 def __update_tagtypes(self):
47 self.__tags = self.mpclient.tagtypes()
48 self.o.set_tagtypes(self.__tags)
50 def _get_dock_widget(self):
51 return self._create_dock(self.o)
53 def refresh(self):
54 self.logger.info('Refreshing.')
55 metadata = {}
56 song = self.mpclient.current_song()
58 for tag in self.__tags:
59 metadata[tag] = song.tag(tag, '') if song else ''
60 self.o.set_metadata(metadata)
62 class SonginfoWidget(QtGui.QWidget):
63 plugin = None
64 __labels = None
66 def __init__(self, plugin):
67 QtGui.QWidget.__init__(self)
68 self.plugin = plugin
69 self.__labels = {}
70 self.setLayout(QtGui.QGridLayout())
72 def set_tagtypes(self, tagtypes):
73 """Setup labels for each tagtype in the list."""
74 for i in range(self.layout().rowCount()):
75 it = self.layout().itemAtPosition(i, 0)
76 it1 = self.layout().itemAtPosition(i, 1)
77 if it:
78 self.layout().removeItem(it)
79 it.widget().setParent(None)
80 if it1:
81 self.layout().removeItem(it1)
82 it1.widget().setParent(None)
83 self.__labels = {}
85 for tag in tagtypes:
86 label = QtGui.QLabel('<b>%s</b>'%tag.title()) #TODO sort known tags
87 label1 = QtGui.QLabel() # tag value will go here
88 label1.setTextInteractionFlags(QtCore.Qt.TextSelectableByMouse)
89 label.setWordWrap(True)
90 label1.setWordWrap(True)
91 self.layout().addWidget(label, len(self.__labels), 0, QtCore.Qt.AlignRight)
92 self.layout().addWidget(label1, len(self.__labels), 1)
93 self.__labels[tag] = label1
95 def set_metadata(self, metadata):
96 """Set displayed metadata, which is provided in a dict of { tag : value }."""
97 for tag in metadata:
98 self.__labels[tag].setText(unicode(metadata[tag]))