From 405db9d29530a063475e183a0609d67fc59fe3ac Mon Sep 17 00:00:00 2001 From: Anton Khirnov Date: Sun, 23 Aug 2009 20:37:03 +0200 Subject: Songinfo: rewrite to use all metadata from MPD. --- nephilim/mpclient.py | 2 +- nephilim/plugins/Songinfo.py | 75 +++++++++++++++++++++++++++++++++----------- 2 files changed, 57 insertions(+), 20 deletions(-) diff --git a/nephilim/mpclient.py b/nephilim/mpclient.py index a1298b1..35be3f5 100644 --- a/nephilim/mpclient.py +++ b/nephilim/mpclient.py @@ -211,7 +211,7 @@ class MPClient(QtCore.QObject): if not self.__check_command_ok('tagtypes'): return [] - return self._retrieve(self._client.tagtypes) + return map(str.lower, self._retrieve(self._client.tagtypes) + ['file']) def commands(self): """List all currently available MPD commands.""" return self._commands diff --git a/nephilim/plugins/Songinfo.py b/nephilim/plugins/Songinfo.py index 960f179..44de8be 100644 --- a/nephilim/plugins/Songinfo.py +++ b/nephilim/plugins/Songinfo.py @@ -24,38 +24,75 @@ class Songinfo(Plugin): info = 'Displays song metadata provided by MPD.' # public, read-only - o = None + o = None + tags = None + + #### 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) def _unload(self): + self.mpclient.song_changed.disconnect(self.refresh) + self.mpclient.connect_changed.disconnect(self.__update_tagtypes) self.o = None + def __update_tagtypes(self): + self.__tags = self.mpclient.tagtypes() + self.o.set_tagtypes(self.__tags) + def _get_dock_widget(self): return self._create_dock(self.o) + def refresh(self): + self.logger.info('Refreshing.') + metadata = {} + song = self.mpclient.current_song() + + for tag in self.__tags: + metadata[tag] = song.tag(tag, '') if song else '' + self.o.set_metadata(metadata) + class SonginfoWidget(QtGui.QWidget): - plugin = None - labels = {} + plugin = None + __labels = None def __init__(self, plugin): QtGui.QWidget.__init__(self) - self.plugin = plugin - - self.setLayout(QtGui.QVBoxLayout()) + self.plugin = plugin + self.__labels = {} + self.setLayout(QtGui.QGridLayout()) - for item in 'track', 'title', 'album', 'disc', 'artist', 'composer', 'date', 'genre', 'file': - self.labels[item] = QtGui.QLabel('%s:'%item) - self.labels[item].setWordWrap(True) - self.layout().addWidget(self.labels[item]) + 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 = {} - self.plugin.mpclient.song_changed.connect(self.on_song_change) + for tag in tagtypes: + label = QtGui.QLabel('%s'%tag.title()) #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 on_song_change(self): - song = self.plugin.mpclient.current_song() - if not song: - for item in self.labels: - self.labels[item].setText('%s:'%item) - else: - for item in self.labels: - self.labels[item].setText('%s: %s'%(item, unicode(song.tag(item)))) + 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(unicode(metadata[tag])) -- cgit v1.2.3