winMain: remove useless variable
[nephilim.git] / nephilim / settings_wg.py
blob85a046ef801a5fb7de6a86ab7c5e63b7581df2a4
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 import os
22 from common import Button
23 import plugin
26 class SettingsWidget(QtGui.QWidget):
27 save_btn = None
28 close_btn = None
29 pluginlist = None
30 settings = None
31 settings_wg = None
33 mpclient = None
34 plugins = None
36 class SettingsWidgetMPD(plugin.Plugin.SettingsWidget):
37 mpclient = None
38 host_txt = None
39 port_txt = None
40 pass_txt = None
41 lib_txt = None
42 update = None
43 xfade = None
45 def __init__(self, mpclient):
46 plugin.Plugin.SettingsWidget.__init__(self, None)
47 self.mpclient = mpclient
49 self.settings.beginGroup('MPD')
50 self.host_txt = QtGui.QLineEdit(self.settings.value('host', 'localhost'))
51 self.port_txt = QtGui.QLineEdit(self.settings.value('port', '6600'))
52 self.port_txt.setValidator(QtGui.QIntValidator(0, 65535, self))
53 self.pass_txt = QtGui.QLineEdit(self.settings.value('password'))
54 self.pass_txt.setEchoMode(QtGui.QLineEdit.Password)
55 self.lib_txt = QtGui.QLineEdit(self.settings.value('music_dir', os.path.expanduser('~/music/')))
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'):
92 self.settings.setValue('host', self.host_txt.text())
93 reconnect = True
94 if self.port_txt.text() != self.settings.value('port'):
95 self.settings.setValue('port', self.port_txt.text())
96 reconnect = True
97 if self.pass_txt.text() != self.settings.value('password'):
98 self.settings.setValue('password', self.pass_txt.text())
99 if self.pass_txt.text():
100 self.mpclient.password(self.pass_txt.text())
101 self.settings.setValue('music_dir', self.lib_txt.text())
102 self.settings.endGroup()
104 if reconnect:
105 self.mpclient.disconnect_mpd()
106 self.mpclient.connect_mpd(self.host_txt.text(), int(self.port_txt.text()), 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 = int(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', toload)
181 def closeEvent(self, event):
182 self.hide()
183 event.ignore()