switch to PyQt4 API v2 for QStrings
[nephilim.git] / nephilim / plugins / Songinfo.py
blob0701ddbd672989569e6bb075f7c3c3aa3727e7b7
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 # private
31 DEFAULTS = {'tagtypes' : ['track', 'title', 'artist', 'album',
32 'albumartist', 'disc', 'genre', 'date', 'composer', 'performer', 'file']}
34 #### private ####
36 class SettingsWidgetSonginfo(Plugin.SettingsWidget):
37 # private
38 taglist = None
40 def __init__(self, plugin):
41 Plugin.SettingsWidget.__init__(self, plugin)
42 self.settings.beginGroup(self.plugin.name)
44 tags_enabled = self.settings.value('tagtypes').toStringList()
45 tags = self.plugin.mpclient.tagtypes()
46 self.taglist = QtGui.QListWidget(self)
47 self.taglist.setDragDropMode(QtGui.QAbstractItemView.InternalMove)
48 for tag in [tag for tag in tags_enabled if tag in tags]:
49 it = QtGui.QListWidgetItem(tag)
50 it.setCheckState(QtCore.Qt.Checked)
51 self.taglist.addItem(it)
52 for tag in [tag for tag in tags if tag not in tags_enabled]:
53 it = QtGui.QListWidgetItem(tag)
54 it.setCheckState(QtCore.Qt.Unchecked)
55 self.taglist.addItem(it)
57 self.setLayout(QtGui.QVBoxLayout())
58 self._add_widget(self.taglist, label = 'Tags', tooltip = 'A list of tags that should be displayed.\n'
59 'Use drag and drop to change their order')
61 self.settings.endGroup()
63 def save_settings(self):
64 self.settings.beginGroup(self.plugin.name)
66 tags = []
67 for i in range(self.taglist.count()):
68 it = self.taglist.item(i)
69 if it.checkState() == QtCore.Qt.Checked:
70 tags.append(it.text())
71 self.settings.setValue('tagtypes', QtCore.QVariant(tags))
73 self.settings.endGroup()
74 self.plugin.update_tagtypes()
75 self.plugin.refresh()
77 #### public ####
78 def __init__(self, parent, mpclient, name):
79 Plugin.__init__(self, parent, mpclient, name)
81 self.__tags = []
83 def _load(self):
84 self.o = SonginfoWidget(self)
85 self.mpclient.song_changed.connect(self.refresh)
86 self.mpclient.connect_changed.connect(self.update_tagtypes)
88 if self.mpclient.is_connected():
89 self.update_tagtypes()
90 self.refresh()
92 def _unload(self):
93 self.mpclient.song_changed.disconnect(self.refresh)
94 self.mpclient.connect_changed.disconnect(self.update_tagtypes)
95 self.o = None
97 def _get_dock_widget(self):
98 return self._create_dock(self.o)
100 def get_settings_widget(self):
101 return self.SettingsWidgetSonginfo(self)
103 def update_tagtypes(self):
104 self.__tags = [tag for tag in self.settings.value(self.name + '/tagtypes').toStringList() if tag in self.mpclient.tagtypes()]
105 self.o.set_tagtypes(self.__tags)
106 def refresh(self):
107 self.logger.info('Refreshing.')
108 metadata = {}
109 song = self.mpclient.current_song()
111 for tag in self.__tags:
112 metadata[tag] = song[str(tag)] if song else '' #XXX i don't like the explicit conversion to python string
113 self.o.set_metadata(metadata)
115 class SonginfoWidget(QtGui.QWidget):
116 plugin = None
117 __labels = None
119 def __init__(self, plugin):
120 QtGui.QWidget.__init__(self)
121 self.plugin = plugin
122 self.__labels = {}
123 self.setLayout(QtGui.QGridLayout())
124 self.layout().setColumnStretch(1, 1)
126 def set_tagtypes(self, tagtypes):
127 """Setup labels for each tagtype in the list."""
128 for i in range(self.layout().rowCount()):
129 it = self.layout().itemAtPosition(i, 0)
130 it1 = self.layout().itemAtPosition(i, 1)
131 if it:
132 self.layout().removeItem(it)
133 it.widget().setParent(None)
134 if it1:
135 self.layout().removeItem(it1)
136 it1.widget().setParent(None)
137 self.__labels = {}
139 for tag in tagtypes:
140 label = QtGui.QLabel('<b>%s</b>'%tag) #TODO sort known tags
141 label1 = QtGui.QLabel() # tag value will go here
142 label1.setTextInteractionFlags(QtCore.Qt.TextSelectableByMouse)
143 label.setWordWrap(True)
144 label1.setWordWrap(True)
145 self.layout().addWidget(label, len(self.__labels), 0, QtCore.Qt.AlignRight)
146 self.layout().addWidget(label1, len(self.__labels), 1)
147 self.__labels[tag] = label1
149 def set_metadata(self, metadata):
150 """Set displayed metadata, which is provided in a dict of { tag : value }."""
151 for tag in metadata:
152 self.__labels[tag].setText(unicode(metadata[tag]))