add installer script
[nephilim.git] / nephilim / plugins / Songinfo.py
blob144033bf4f4648c2e87995b2d247a6dcfe9f6d51
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' : QtCore.QStringList(['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 = QtCore.QStringList()
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 def _unload(self):
89 self.mpclient.song_changed.disconnect(self.refresh)
90 self.mpclient.connect_changed.disconnect(self.update_tagtypes)
91 self.o = None
93 def _get_dock_widget(self):
94 return self._create_dock(self.o)
96 def get_settings_widget(self):
97 return self.SettingsWidgetSonginfo(self)
99 def update_tagtypes(self):
100 self.__tags = [tag for tag in self.settings.value(self.name + '/tagtypes').toStringList() if tag in self.mpclient.tagtypes()]
101 self.o.set_tagtypes(self.__tags)
102 def refresh(self):
103 self.logger.info('Refreshing.')
104 metadata = {}
105 song = self.mpclient.current_song()
107 for tag in self.__tags:
108 metadata[tag] = song[str(tag)] if song else '' #XXX i don't like the explicit conversion to python string
109 self.o.set_metadata(metadata)
111 class SonginfoWidget(QtGui.QWidget):
112 plugin = None
113 __labels = None
115 def __init__(self, plugin):
116 QtGui.QWidget.__init__(self)
117 self.plugin = plugin
118 self.__labels = {}
119 self.setLayout(QtGui.QGridLayout())
120 self.layout().setColumnStretch(1, 1)
122 def set_tagtypes(self, tagtypes):
123 """Setup labels for each tagtype in the list."""
124 for i in range(self.layout().rowCount()):
125 it = self.layout().itemAtPosition(i, 0)
126 it1 = self.layout().itemAtPosition(i, 1)
127 if it:
128 self.layout().removeItem(it)
129 it.widget().setParent(None)
130 if it1:
131 self.layout().removeItem(it1)
132 it1.widget().setParent(None)
133 self.__labels = {}
135 for tag in tagtypes:
136 label = QtGui.QLabel('<b>%s</b>'%tag) #TODO sort known tags
137 label1 = QtGui.QLabel() # tag value will go here
138 label1.setTextInteractionFlags(QtCore.Qt.TextSelectableByMouse)
139 label.setWordWrap(True)
140 label1.setWordWrap(True)
141 self.layout().addWidget(label, len(self.__labels), 0, QtCore.Qt.AlignRight)
142 self.layout().addWidget(label1, len(self.__labels), 1)
143 self.__labels[tag] = label1
145 def set_metadata(self, metadata):
146 """Set displayed metadata, which is provided in a dict of { tag : value }."""
147 for tag in metadata:
148 self.__labels[tag].setText(unicode(metadata[tag]))