SongInfo: fix saving settings
[nephilim.git] / nephilim / plugins / Songinfo.py
blobaee58da60b0462c7872df770e14b86dab6d79954
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 #### public ####
35 def __init__(self, parent, mpclient, name):
36 Plugin.__init__(self, parent, mpclient, name)
38 self.__tags = []
40 def _load(self):
41 self.o = SonginfoWidget(self)
42 self.mpclient.song_changed.connect(self.refresh)
43 self.mpclient.connect_changed.connect(self.update_tagtypes)
45 if self.mpclient.is_connected():
46 self.update_tagtypes()
47 self.refresh()
49 def _unload(self):
50 self.mpclient.song_changed.disconnect(self.refresh)
51 self.mpclient.connect_changed.disconnect(self.update_tagtypes)
52 self.o = None
54 def _get_dock_widget(self):
55 return self._create_dock(self.o)
57 def get_settings_widget(self):
58 return SettingsWidgetSonginfo(self)
60 def update_tagtypes(self):
61 self.__tags = [tag for tag in self.settings.value(self.name + '/tagtypes') if tag in self.mpclient.tagtypes]
62 self.o.set_tagtypes(self.__tags)
63 def refresh(self):
64 self.logger.info('Refreshing.')
65 metadata = {}
66 song = self.mpclient.cur_song
68 if not song:
69 return self.o.clear()
71 for tag in self.__tags:
72 metadata[tag] = song[tag] if tag in song else ''
73 self.o.set_metadata(metadata)
75 class SonginfoWidget(QtGui.QWidget):
76 plugin = None
77 __labels = None
79 def __init__(self, plugin):
80 QtGui.QWidget.__init__(self)
81 self.plugin = plugin
82 self.__labels = {}
83 self.setLayout(QtGui.QGridLayout())
84 self.layout().setColumnStretch(1, 1)
86 def set_tagtypes(self, tagtypes):
87 """Setup labels for each tagtype in the list."""
88 for i in range(self.layout().rowCount()):
89 it = self.layout().itemAtPosition(i, 0)
90 it1 = self.layout().itemAtPosition(i, 1)
91 if it:
92 self.layout().removeItem(it)
93 it.widget().setParent(None)
94 if it1:
95 self.layout().removeItem(it1)
96 it1.widget().setParent(None)
97 self.__labels = {}
99 for tag in tagtypes:
100 label = QtGui.QLabel('<b>%s</b>'%tag) #TODO sort known tags
101 label1 = QtGui.QLabel() # tag value will go here
102 label1.setTextInteractionFlags(QtCore.Qt.TextSelectableByMouse)
103 label.setWordWrap(True)
104 label1.setWordWrap(True)
105 self.layout().addWidget(label, len(self.__labels), 0, QtCore.Qt.AlignRight)
106 self.layout().addWidget(label1, len(self.__labels), 1)
107 self.__labels[tag] = label1
109 def set_metadata(self, metadata):
110 """Set displayed metadata, which is provided in a dict of { tag : value }."""
111 for tag in metadata:
112 self.__labels[tag].setText(metadata[tag])
114 def clear(self):
115 """ Clear displayed metadata. """
116 for label in self.__labels.values():
117 label.clear()
119 class SettingsWidgetSonginfo(Plugin.SettingsWidget):
120 # private
121 taglist = None
123 def __init__(self, plugin):
124 Plugin.SettingsWidget.__init__(self, plugin)
125 self.settings.beginGroup(self.plugin.name)
127 self.taglist = QtGui.QListWidget(self)
128 self.taglist.setDragDropMode(QtGui.QAbstractItemView.InternalMove)
129 self.__update_tags()
130 self.plugin.mpclient.connect_changed.connect(self.__update_tags)
132 self.setLayout(QtGui.QVBoxLayout())
133 self._add_widget(self.taglist, label = 'Tags', tooltip = 'A list of tags that should be displayed.\n'
134 'Use drag and drop to change their order')
137 def __update_tags(self):
138 self.taglist.setEnabled(self.plugin.mpclient.is_connected())
139 if not self.plugin.mpclient.is_connected():
140 return
142 self.taglist.clear()
143 tags_enabled = self.settings.value('tagtypes')
144 tags = self.plugin.mpclient.tagtypes
145 for tag in [tag for tag in tags_enabled if tag in tags]:
146 it = QtGui.QListWidgetItem(tag)
147 it.setCheckState(QtCore.Qt.Checked)
148 self.taglist.addItem(it)
149 for tag in [tag for tag in tags if tag not in tags_enabled]:
150 it = QtGui.QListWidgetItem(tag)
151 it.setCheckState(QtCore.Qt.Unchecked)
152 self.taglist.addItem(it)
154 def save_settings(self):
155 if not self.taglist.isEnabled():
156 return
158 tags = []
159 for i in range(self.taglist.count()):
160 it = self.taglist.item(i)
161 if it.checkState() == QtCore.Qt.Checked:
162 tags.append(it.text())
163 self.settings.setValue('tagtypes', tags)
165 self.plugin.update_tagtypes()
166 self.plugin.refresh()