source: src/mlx/gui/prefs.py@ 138:bc4d9d192a7b

Last change on this file since 138:bc4d9d192a7b was 136:6d206b573dee, checked in by István Váradi <ivaradi@…>, 13 years ago

Added option to enable/disable the online gate system

File size: 13.3 KB
Line 
1# Module for the preferences dialog
2
3#------------------------------------------------------------------------------
4
5from common import *
6
7from mlx.i18n import xstr
8import mlx.const as const
9
10import urlparse
11
12#------------------------------------------------------------------------------
13
14class Preferences(gtk.Dialog):
15 """The preferences dialog."""
16 def __init__(self, gui):
17 """Construct the dialog."""
18 super(Preferences, self).__init__(WINDOW_TITLE_BASE + " " +
19 xstr("prefs_title"),
20 gui.mainWindow,
21 DIALOG_MODAL)
22
23 self.add_button(xstr("button_cancel"), RESPONSETYPE_REJECT)
24 self.add_button(xstr("button_ok"), RESPONSETYPE_ACCEPT)
25
26 self._gui = gui
27
28 contentArea = self.get_content_area()
29
30 notebook = gtk.Notebook()
31 contentArea.pack_start(notebook, True, True, 4)
32
33 general = self._buildGeneral()
34 label = gtk.Label(xstr("prefs_tab_general"))
35 label.set_use_underline(True)
36 label.set_tooltip_text(xstr("prefs_tab_general_tooltip"))
37 notebook.append_page(general, label)
38
39 messages = self._buildMessages()
40 label = gtk.Label(xstr("prefs_tab_messages"))
41 label.set_use_underline(True)
42 label.set_tooltip_text(xstr("prefs_tab_message_tooltip"))
43 notebook.append_page(messages, label)
44
45 advanced = self._buildAdvanced()
46 label = gtk.Label(xstr("prefs_tab_advanced"))
47 label.set_use_underline(True)
48 label.set_tooltip_text(xstr("prefs_tab_advanced_tooltip"))
49 notebook.append_page(advanced, label)
50
51 def run(self, config):
52 """Run the preferences dialog.
53
54 The dialog will be set up from data in the given configuration. If the
55 changes are accepted by the user, the configuration is updated and saved."""
56 self._fromConfig(config)
57
58 self.show_all()
59 response = super(Preferences, self).run()
60 self.hide()
61
62 if response==RESPONSETYPE_ACCEPT:
63 self._toConfig(config)
64 config.save()
65
66 def _fromConfig(self, config):
67 """Setup the dialog from the given configuration."""
68 self._setLanguage(config.language)
69 self._onlineGateSystem.set_active(config.onlineGateSystem)
70 self._flareTimeFromFS.set_active(config.flareTimeFromFS)
71
72 for messageType in const.messageTypes:
73 level = config.getMessageTypeLevel(messageType)
74 button = self._msgFSCheckButtons[messageType]
75 button.set_active(level == const.MESSAGELEVEL_FS or
76 level == const.MESSAGELEVEL_BOTH)
77 button = self._msgSoundCheckButtons[messageType]
78 button.set_active(level == const.MESSAGELEVEL_SOUND or
79 level == const.MESSAGELEVEL_BOTH)
80
81 self._togglingAutoUpdate = True
82 self._autoUpdate.set_active(config.autoUpdate)
83 self._togglingAutoUpdate = False
84 if not config.autoUpdate:
85 self._warnedAutoUpdate = True
86
87 self._updateURL.set_text(config.updateURL)
88
89 def _toConfig(self, config):
90 """Setup the given config from the settings in the dialog."""
91 config.language = self._getLanguage()
92 config.onlineGateSystem = self._onlineGateSystem.get_active()
93 config.flareTimeFromFS = self._flareTimeFromFS.get_active()
94
95 for messageType in const.messageTypes:
96 fsButtonActive = self._msgFSCheckButtons[messageType].get_active()
97 soundButtonActive = self._msgSoundCheckButtons[messageType].get_active()
98 if fsButtonActive:
99 level = const.MESSAGELEVEL_BOTH if soundButtonActive \
100 else const.MESSAGELEVEL_FS
101 elif soundButtonActive:
102 level = const.MESSAGELEVEL_SOUND
103 else:
104 level = const.MESSAGELEVEL_NONE
105 config.setMessageTypeLevel(messageType, level)
106
107 config.autoUpdate = self._autoUpdate.get_active()
108 config.updateURL = self._updateURL.get_text()
109
110 def _buildGeneral(self):
111 """Build the page for the general settings."""
112 mainAlignment = gtk.Alignment(xalign = 0.0, yalign = 0.0,
113 xscale = 0.0, yscale = 0.0)
114 mainAlignment.set_padding(padding_top = 16, padding_bottom = 32,
115 padding_left = 4, padding_right = 48)
116 mainBox = gtk.VBox()
117 mainAlignment.add(mainBox)
118
119 languageBox = gtk.HBox()
120 mainBox.pack_start(languageBox, False, False, 4)
121
122 label = gtk.Label(xstr("prefs_language"))
123 label.set_use_underline(True)
124
125 languageBox.pack_start(label, False, False, 4)
126
127 self._languageList = gtk.ListStore(str, str)
128 for language in const.languages:
129 self._languageList.append([xstr("prefs_lang_" + language),
130 language])
131
132 self._languageComboBox = languageComboBox = \
133 gtk.ComboBox(model = self._languageList)
134 cell = gtk.CellRendererText()
135 languageComboBox.pack_start(cell, True)
136 languageComboBox.add_attribute(cell, 'text', 0)
137 languageComboBox.set_tooltip_text(xstr("prefs_language_tooltip"))
138 languageComboBox.connect("changed", self._languageChanged)
139 languageBox.pack_start(languageComboBox, False, False, 4)
140
141 label.set_mnemonic_widget(languageComboBox)
142
143 self._changingLanguage = False
144 self._warnedRestartNeeded = False
145
146 self._onlineGateSystem = gtk.CheckButton(xstr("prefs_onlineGateSystem"))
147 self._onlineGateSystem.set_use_underline(True)
148 self._onlineGateSystem.set_tooltip_text(xstr("prefs_onlineGateSystem_tooltip"))
149 mainBox.pack_start(self._onlineGateSystem, False, False, 4)
150
151 self._flareTimeFromFS = gtk.CheckButton(xstr("prefs_flaretimeFromFS"))
152 self._flareTimeFromFS.set_use_underline(True)
153 self._flareTimeFromFS.set_tooltip_text(xstr("prefs_flaretimeFromFS_tooltip"))
154 mainBox.pack_start(self._flareTimeFromFS, False, False, 4)
155
156 return mainAlignment
157
158 def _setLanguage(self, language):
159 """Set the language to the given one."""
160 iter = self._languageList.get_iter_first()
161 while iter is not None:
162 (lang,) = self._languageList.get(iter, 1)
163 if (not language and lang=="$system") or \
164 lang==language:
165 self._changingLanguage = True
166 self._languageComboBox.set_active_iter(iter)
167 self._changingLanguage = False
168 break
169 else:
170 iter = self._languageList.iter_next(iter)
171
172 def _getLanguage(self):
173 """Get the language selected by the user."""
174 iter = self._languageComboBox.get_active_iter()
175 (lang,) = self._languageList.get(iter, 1)
176 return "" if lang=="$system" else lang
177
178 def _languageChanged(self, comboBox):
179 """Called when the language has changed."""
180 if not self._changingLanguage and not self._warnedRestartNeeded:
181 dialog = gtk.MessageDialog(parent = self,
182 type = MESSAGETYPE_INFO,
183 message_format = xstr("prefs_restart"))
184 dialog.add_button(xstr("button_ok"), RESPONSETYPE_OK)
185 dialog.set_title(self.get_title())
186 dialog.format_secondary_markup(xstr("prefs_language_restart_sec"))
187 dialog.run()
188 dialog.hide()
189 self._warnedRestartNeeded = True
190
191 def _buildMessages(self):
192 """Build the page for the message settings."""
193
194 mainAlignment = gtk.Alignment(xalign = 0.0, yalign = 0.0,
195 xscale = 0.0, yscale = 0.0)
196 mainAlignment.set_padding(padding_top = 16, padding_bottom = 8,
197 padding_left = 4, padding_right = 4)
198 mainBox = gtk.VBox()
199 mainAlignment.add(mainBox)
200
201 table = gtk.Table(len(const.messageTypes) + 1, 3)
202 table.set_row_spacings(8)
203 table.set_col_spacings(32)
204 table.set_homogeneous(False)
205 mainBox.pack_start(table, False, False, 4)
206
207 label = gtk.Label(xstr("prefs_msgs_fs"))
208 label.set_justify(JUSTIFY_CENTER)
209 label.set_alignment(0.5, 1.0)
210 table.attach(label, 1, 2, 0, 1)
211
212 label = gtk.Label(xstr("prefs_msgs_sound"))
213 label.set_justify(JUSTIFY_CENTER)
214 label.set_alignment(0.5, 1.0)
215 table.attach(label, 2, 3, 0, 1)
216
217 self._msgFSCheckButtons = {}
218 self._msgSoundCheckButtons = {}
219 row = 1
220 for messageType in const.messageTypes:
221 messageTypeStr = const.messageType2string(messageType)
222 label = gtk.Label(xstr("prefs_msgs_type_" + messageTypeStr))
223 label.set_justify(JUSTIFY_CENTER)
224 label.set_use_underline(True)
225 label.set_alignment(0.5, 0.5)
226 table.attach(label, 0, 1, row, row+1)
227
228 fsCheckButton = gtk.CheckButton()
229 alignment = gtk.Alignment(xscale = 0.0, yscale = 0.0,
230 xalign = 0.5, yalign = 0.5)
231 alignment.add(fsCheckButton)
232 table.attach(alignment, 1, 2, row, row+1)
233 self._msgFSCheckButtons[messageType] = fsCheckButton
234
235 soundCheckButton = gtk.CheckButton()
236 alignment = gtk.Alignment(xscale = 0.0, yscale = 0.0,
237 xalign = 0.5, yalign = 0.5)
238 alignment.add(soundCheckButton)
239 table.attach(alignment, 2, 3, row, row+1)
240 self._msgSoundCheckButtons[messageType] = soundCheckButton
241
242 mnemonicWidget = gtk.Label("")
243 table.attach(mnemonicWidget, 3, 4, row, row+1)
244 label.set_mnemonic_widget(mnemonicWidget)
245 mnemonicWidget.connect("mnemonic-activate",
246 self._msgLabelActivated,
247 messageType)
248
249 row += 1
250
251 return mainAlignment
252
253 def _msgLabelActivated(self, button, cycle_group, messageType):
254 """Called when the mnemonic of a label is activated.
255
256 It cycles the corresponding options."""
257 fsCheckButton = self._msgFSCheckButtons[messageType]
258 soundCheckButton = self._msgSoundCheckButtons[messageType]
259
260 num = 1 if fsCheckButton.get_active() else 0
261 num += 2 if soundCheckButton.get_active() else 0
262 num += 1
263
264 fsCheckButton.set_active((num&0x01)==0x01)
265 soundCheckButton.set_active((num&0x02)==0x02)
266
267 return True
268
269 def _buildAdvanced(self):
270 """Build the page for the advanced settings."""
271
272 mainAlignment = gtk.Alignment(xalign = 0.0, yalign = 0.0,
273 xscale = 0.0, yscale = 0.0)
274 mainAlignment.set_padding(padding_top = 16, padding_bottom = 32,
275 padding_left = 4, padding_right = 48)
276 mainBox = gtk.VBox()
277 mainAlignment.add(mainBox)
278
279 self._autoUpdate = gtk.CheckButton(xstr("prefs_update_auto"))
280 mainBox.pack_start(self._autoUpdate, False, False, 4)
281
282 self._autoUpdate.set_use_underline(True)
283 self._autoUpdate.connect("toggled", self._autoUpdateToggled)
284 self._autoUpdate.set_tooltip_text(xstr("prefs_update_auto_tooltip"))
285 self._warnedAutoUpdate = False
286 self._togglingAutoUpdate = False
287
288 updateURLBox = gtk.HBox()
289 mainBox.pack_start(updateURLBox, False, False, 4)
290 label = gtk.Label(xstr("prefs_update_url"))
291 label.set_use_underline(True)
292 updateURLBox.pack_start(label, False, False, 4)
293
294 self._updateURL = gtk.Entry()
295 label.set_mnemonic_widget(self._updateURL)
296 self._updateURL.set_width_chars(40)
297 self._updateURL.set_tooltip_text(xstr("prefs_update_url_tooltip"))
298 self._updateURL.connect("changed", self._updateURLChanged)
299 updateURLBox.pack_start(self._updateURL, False, False, 4)
300
301 return mainAlignment
302
303 def _setOKButtonSensitivity(self):
304 """Set the sensitive state of the OK button."""
305 sensitive = False
306 try:
307 result = urlparse.urlparse(self._updateURL.get_text())
308 sensitive = result.scheme!="" and (result.netloc + result.path)!=""
309 except:
310 pass
311
312 okButton = self.get_widget_for_response(RESPONSETYPE_ACCEPT)
313 okButton.set_sensitive(sensitive)
314
315 def _autoUpdateToggled(self, button):
316 """Called when the auto update check button is toggled."""
317 if not self._togglingAutoUpdate and not self._warnedAutoUpdate and \
318 not self._autoUpdate.get_active():
319 dialog = gtk.MessageDialog(parent = self,
320 type = MESSAGETYPE_INFO,
321 message_format = xstr("prefs_update_auto_warning"))
322 dialog.add_button(xstr("button_ok"), RESPONSETYPE_OK)
323 dialog.set_title(self.get_title())
324 dialog.run()
325 dialog.hide()
326 self._warnedAutoUpdate = True
327
328 def _updateURLChanged(self, entry):
329 """Called when the update URL is changed."""
330 self._setOKButtonSensitivity()
331
332
Note: See TracBrowser for help on using the repository browser.