pyqtSignal.disconnect() throws TypeError if no slots are connected
[nephilim.git] / nephilim / settings_wg.py
blobf1c07d1c9a39f3d2ebc34f3fd5eb67e0ce47fa4d
2 # Copyright (C) 2008 jerous <jerous@gmail.com>
3 # Copyright (C) 2009 Anton Khirnov <wyskas@gmail.com>
5 # Nephilim is free software: you can redistribute it and/or modify
6 # it under the terms of the GNU General Public License as published by
7 # the Free Software Foundation, either version 3 of the License, or
8 # (at your option) any later version.
10 # Nephilim is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
15 # You should have received a copy of the GNU General Public License
16 # along with Nephilim. If not, see <http://www.gnu.org/licenses/>.
19 from PyQt4 import QtGui, QtCore
20 from PyQt4.QtCore import QVariant
21 import os
23 from common import Button
24 import plugin
27 class SettingsWidget(QtGui.QWidget):
28 save_btn = None
29 close_btn = None
30 pluginlist = None
31 settings = None
32 settings_wg = None
34 mpclient = None
35 plugins = None
37 class SettingsWidgetMPD(plugin.Plugin.SettingsWidget):
38 mpclient = None
39 host_txt = None
40 port_txt = None
41 pass_txt = None
42 lib_txt = None
43 update = None
44 xfade = None
46 def __init__(self, mpclient):
47 plugin.Plugin.SettingsWidget.__init__(self, None)
48 self.mpclient = mpclient
50 self.settings.beginGroup('MPD')
51 self.host_txt = QtGui.QLineEdit(self.settings.value('host', QVariant('localhost')).toString())
52 self.port_txt = QtGui.QLineEdit(self.settings.value('port', QVariant('6600')).toString())
53 self.pass_txt = QtGui.QLineEdit(self.settings.value('password').toString())
54 self.pass_txt.setEchoMode(QtGui.QLineEdit.Password)
55 self.lib_txt = QtGui.QLineEdit(self.settings.value('music_dir', QVariant(os.path.expanduser('~/music/'))).toString())
56 self.settings.endGroup()
58 self.update = QtGui.QPushButton('Update MPD database')
59 self.update.clicked.connect(self.update_db)
61 outputs = QtGui.QGroupBox('Audio outputs')
62 outputs.setLayout(QtGui.QVBoxLayout())
64 for output in self.mpclient.outputs:
65 box = QtGui.QCheckBox(output.name, self)
66 if output.state:
67 box.setChecked(True)
68 else:
69 box.setChecked(False)
70 box.clicked.connect(output.set_state)
71 output.state_changed.connect(box.setChecked)
72 outputs.layout().addWidget(box)
74 self.xfade = QtGui.QSpinBox()
75 self.xfade.setValue(int(self.mpclient.status()['xfade']))
76 self.xfade.valueChanged.connect(self.mpclient.crossfade)
78 self.setLayout(QtGui.QVBoxLayout())
79 self._add_widget(self.host_txt, 'Host', 'Host or socket to connect to')
80 self._add_widget(self.port_txt, 'Port', 'Port to use (empty when using sockets)')
81 self._add_widget(self.pass_txt, 'Password', 'Password')
82 self._add_widget(self.lib_txt, 'Music library', 'Path to music library')
83 self.layout().addWidget(self.update)
84 self.layout().addWidget(outputs)
85 self._add_widget(self.xfade, 'Crossfade', 'Set crossfade between songs (in seconds).')
87 def save_settings(self):
88 reconnect = False
90 self.settings.beginGroup('MPD')
91 if self.host_txt.text() != self.settings.value('host').toString():
92 self.settings.setValue('host', QVariant(self.host_txt.text()))
93 reconnect = True
94 if self.port_txt.text() != self.settings.value('port').toString():
95 self.settings.setValue('port', QVariant(self.port_txt.text()))
96 reconnect = True
97 if self.pass_txt.text() != self.settings.value('password').toString():
98 self.settings.setValue('password', QVariant(self.pass_txt.text()))
99 if self.pass_txt.text():
100 self.mpclient.password(self.pass_txt.text())
101 self.settings.setValue('music_dir', QVariant(self.lib_txt.text()))
102 self.settings.endGroup()
104 if reconnect:
105 self.mpclient.disconnect_mpd()
106 self.mpclient.connect_mpd(str(self.host_txt.text()), str(self.port_txt.text()), str(self.pass_txt.text()))
108 def update_db(self):
109 self.mpclient.update_db()
111 def __init__(self, mpclient, plugins):
112 QtGui.QWidget.__init__(self, None, QtCore.Qt.Window)
113 self.settings = QtCore.QSettings()
114 self.plugins = plugins
115 self.mpclient = mpclient
117 self.save_btn = Button('save all', self.save_clicked)
118 self.close_btn = Button('close', self.close_clicked)
120 tab_wg = QtGui.QTabWidget(self)
121 self.settings_wg = []
122 self.settings_wg.append(self.SettingsWidgetMPD(mpclient))
123 tab_wg.addTab(self.settings_wg[-1], 'MPD settings')
125 self.pluginlist = QtGui.QListWidget(self)
126 self.fill_pluginlist()
127 tab_wg.addTab(self.pluginlist, 'Plugins')
129 for plugin in self.plugins.loaded_plugins():
130 wg = plugin.get_settings_widget()
131 if wg:
132 self.settings_wg.append(wg)
133 tab_wg.addTab(self.settings_wg[-1], plugin.name)
135 self.setLayout(QtGui.QGridLayout())
136 self.layout().addWidget(tab_wg, 0, 0, 1, 2)
137 self.layout().addWidget(self.save_btn, 1, 0)
138 self.layout().addWidget(self.close_btn, 1, 1)
140 self.pluginlist.itemChanged.connect(self.plugin_checked)
142 self.setWindowTitle('Settings')
143 self.setAttribute(QtCore.Qt.WA_DeleteOnClose)
144 self.center()
145 self.resize(800,400)
147 def fill_pluginlist(self):
148 self.pluginlist.clear()
149 for plugin in self.plugins.plugins():
150 item = QtGui.QListWidgetItem("%s\t%s"%(plugin.name, plugin.info))
151 if plugin.loaded:
152 item.setCheckState(QtCore.Qt.Checked)
153 else:
154 item.setCheckState(QtCore.Qt.Unchecked)
156 self.pluginlist.addItem(item)
158 def center(self):
159 screen = QtGui.QDesktopWidget().screenGeometry()
160 size = self.geometry()
161 self.move((screen.width()-size.width())/2, (screen.height()-size.height())/2+100)
163 def save_clicked(self):
164 for wg in self.settings_wg:
165 wg.save_settings()
167 def close_clicked(self):
168 self.close()
170 def plugin_checked(self, item):
171 toload = (item.checkState() == QtCore.Qt.Checked)
172 name = str(item.text()[0:str(item.text()).find('\t')])
173 if toload:
174 # refresh the plugin file
175 self.plugins.load(name)
176 self.fill_pluginlist()
177 else:
178 self.plugins.unload(name)
179 self.settings.setValue(name + '/load', QtCore.QVariant(toload))
181 def closeEvent(self, event):
182 self.settings_wg = None