source: src/mlx/gui/prefs.py@ 148:453ebaea9090

Last change on this file since 148:453ebaea9090 was 148:453ebaea9090, checked in by István Váradi <ivaradi@…>, 13 years ago

Implemented FS time synchronization

File size: 14.5 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._hideMinimizedWindow.set_active(config.hideMinimizedWindow)
70 self._onlineGateSystem.set_active(config.onlineGateSystem)
71 self._onlineACARS.set_active(config.onlineACARS)
72 self._flareTimeFromFS.set_active(config.flareTimeFromFS)
73 self._syncFSTime.set_active(config.syncFSTime)
74
75 for messageType in const.messageTypes:
76 level = config.getMessageTypeLevel(messageType)
77 button = self._msgFSCheckButtons[messageType]
78 button.set_active(level == const.MESSAGELEVEL_FS or
79 level == const.MESSAGELEVEL_BOTH)
80 button = self._msgSoundCheckButtons[messageType]
81 button.set_active(level == const.MESSAGELEVEL_SOUND or
82 level == const.MESSAGELEVEL_BOTH)
83
84 self._togglingAutoUpdate = True
85 self._autoUpdate.set_active(config.autoUpdate)
86 self._togglingAutoUpdate = False
87 if not config.autoUpdate:
88 self._warnedAutoUpdate = True
89
90 self._updateURL.set_text(config.updateURL)
91
92 def _toConfig(self, config):
93 """Setup the given config from the settings in the dialog."""
94 config.language = self._getLanguage()
95 config.hideMinimizedWindow = self._hideMinimizedWindow.get_active()
96 config.onlineGateSystem = self._onlineGateSystem.get_active()
97 config.onlineACARS = self._onlineACARS.get_active()
98 config.flareTimeFromFS = self._flareTimeFromFS.get_active()
99 config.syncFSTime = self._syncFSTime.get_active()
100
101 for messageType in const.messageTypes:
102 fsButtonActive = self._msgFSCheckButtons[messageType].get_active()
103 soundButtonActive = self._msgSoundCheckButtons[messageType].get_active()
104 if fsButtonActive:
105 level = const.MESSAGELEVEL_BOTH if soundButtonActive \
106 else const.MESSAGELEVEL_FS
107 elif soundButtonActive:
108 level = const.MESSAGELEVEL_SOUND
109 else:
110 level = const.MESSAGELEVEL_NONE
111 config.setMessageTypeLevel(messageType, level)
112
113 config.autoUpdate = self._autoUpdate.get_active()
114 config.updateURL = self._updateURL.get_text()
115
116 def _buildGeneral(self):
117 """Build the page for the general settings."""
118 mainAlignment = gtk.Alignment(xalign = 0.0, yalign = 0.0,
119 xscale = 0.0, yscale = 0.0)
120 mainAlignment.set_padding(padding_top = 16, padding_bottom = 32,
121 padding_left = 4, padding_right = 48)
122 mainBox = gtk.VBox()
123 mainAlignment.add(mainBox)
124
125 languageBox = gtk.HBox()
126 mainBox.pack_start(languageBox, False, False, 4)
127
128 label = gtk.Label(xstr("prefs_language"))
129 label.set_use_underline(True)
130
131 languageBox.pack_start(label, False, False, 4)
132
133 self._languageList = gtk.ListStore(str, str)
134 for language in const.languages:
135 self._languageList.append([xstr("prefs_lang_" + language),
136 language])
137
138 self._languageComboBox = languageComboBox = \
139 gtk.ComboBox(model = self._languageList)
140 cell = gtk.CellRendererText()
141 languageComboBox.pack_start(cell, True)
142 languageComboBox.add_attribute(cell, 'text', 0)
143 languageComboBox.set_tooltip_text(xstr("prefs_language_tooltip"))
144 languageComboBox.connect("changed", self._languageChanged)
145 languageBox.pack_start(languageComboBox, False, False, 4)
146
147 label.set_mnemonic_widget(languageComboBox)
148
149 self._changingLanguage = False
150 self._warnedRestartNeeded = False
151
152 self._hideMinimizedWindow = gtk.CheckButton(xstr("prefs_hideMinimizedWindow"))
153 self._hideMinimizedWindow.set_use_underline(True)
154 self._hideMinimizedWindow.set_tooltip_text(xstr("prefs_hideMinimizedWindow_tooltip"))
155 mainBox.pack_start(self._hideMinimizedWindow, False, False, 4)
156
157 self._onlineGateSystem = gtk.CheckButton(xstr("prefs_onlineGateSystem"))
158 self._onlineGateSystem.set_use_underline(True)
159 self._onlineGateSystem.set_tooltip_text(xstr("prefs_onlineGateSystem_tooltip"))
160 mainBox.pack_start(self._onlineGateSystem, False, False, 4)
161
162 self._onlineACARS = gtk.CheckButton(xstr("prefs_onlineACARS"))
163 self._onlineACARS.set_use_underline(True)
164 self._onlineACARS.set_tooltip_text(xstr("prefs_onlineACARS_tooltip"))
165 mainBox.pack_start(self._onlineACARS, False, False, 4)
166
167 self._flareTimeFromFS = gtk.CheckButton(xstr("prefs_flaretimeFromFS"))
168 self._flareTimeFromFS.set_use_underline(True)
169 self._flareTimeFromFS.set_tooltip_text(xstr("prefs_flaretimeFromFS_tooltip"))
170 mainBox.pack_start(self._flareTimeFromFS, False, False, 4)
171
172 self._syncFSTime = gtk.CheckButton(xstr("prefs_syncFSTime"))
173 self._syncFSTime.set_use_underline(True)
174 self._syncFSTime.set_tooltip_text(xstr("prefs_syncFSTime_tooltip"))
175 mainBox.pack_start(self._syncFSTime, False, False, 4)
176
177 return mainAlignment
178
179 def _setLanguage(self, language):
180 """Set the language to the given one."""
181 iter = self._languageList.get_iter_first()
182 while iter is not None:
183 (lang,) = self._languageList.get(iter, 1)
184 if (not language and lang=="$system") or \
185 lang==language:
186 self._changingLanguage = True
187 self._languageComboBox.set_active_iter(iter)
188 self._changingLanguage = False
189 break
190 else:
191 iter = self._languageList.iter_next(iter)
192
193 def _getLanguage(self):
194 """Get the language selected by the user."""
195 iter = self._languageComboBox.get_active_iter()
196 (lang,) = self._languageList.get(iter, 1)
197 return "" if lang=="$system" else lang
198
199 def _languageChanged(self, comboBox):
200 """Called when the language has changed."""
201 if not self._changingLanguage and not self._warnedRestartNeeded:
202 dialog = gtk.MessageDialog(parent = self,
203 type = MESSAGETYPE_INFO,
204 message_format = xstr("prefs_restart"))
205 dialog.add_button(xstr("button_ok"), RESPONSETYPE_OK)
206 dialog.set_title(self.get_title())
207 dialog.format_secondary_markup(xstr("prefs_language_restart_sec"))
208 dialog.run()
209 dialog.hide()
210 self._warnedRestartNeeded = True
211
212 def _buildMessages(self):
213 """Build the page for the message settings."""
214
215 mainAlignment = gtk.Alignment(xalign = 0.0, yalign = 0.0,
216 xscale = 0.0, yscale = 0.0)
217 mainAlignment.set_padding(padding_top = 16, padding_bottom = 8,
218 padding_left = 4, padding_right = 4)
219 mainBox = gtk.VBox()
220 mainAlignment.add(mainBox)
221
222 table = gtk.Table(len(const.messageTypes) + 1, 3)
223 table.set_row_spacings(8)
224 table.set_col_spacings(32)
225 table.set_homogeneous(False)
226 mainBox.pack_start(table, False, False, 4)
227
228 label = gtk.Label(xstr("prefs_msgs_fs"))
229 label.set_justify(JUSTIFY_CENTER)
230 label.set_alignment(0.5, 1.0)
231 table.attach(label, 1, 2, 0, 1)
232
233 label = gtk.Label(xstr("prefs_msgs_sound"))
234 label.set_justify(JUSTIFY_CENTER)
235 label.set_alignment(0.5, 1.0)
236 table.attach(label, 2, 3, 0, 1)
237
238 self._msgFSCheckButtons = {}
239 self._msgSoundCheckButtons = {}
240 row = 1
241 for messageType in const.messageTypes:
242 messageTypeStr = const.messageType2string(messageType)
243 label = gtk.Label(xstr("prefs_msgs_type_" + messageTypeStr))
244 label.set_justify(JUSTIFY_CENTER)
245 label.set_use_underline(True)
246 label.set_alignment(0.5, 0.5)
247 table.attach(label, 0, 1, row, row+1)
248
249 fsCheckButton = gtk.CheckButton()
250 alignment = gtk.Alignment(xscale = 0.0, yscale = 0.0,
251 xalign = 0.5, yalign = 0.5)
252 alignment.add(fsCheckButton)
253 table.attach(alignment, 1, 2, row, row+1)
254 self._msgFSCheckButtons[messageType] = fsCheckButton
255
256 soundCheckButton = gtk.CheckButton()
257 alignment = gtk.Alignment(xscale = 0.0, yscale = 0.0,
258 xalign = 0.5, yalign = 0.5)
259 alignment.add(soundCheckButton)
260 table.attach(alignment, 2, 3, row, row+1)
261 self._msgSoundCheckButtons[messageType] = soundCheckButton
262
263 mnemonicWidget = gtk.Label("")
264 table.attach(mnemonicWidget, 3, 4, row, row+1)
265 label.set_mnemonic_widget(mnemonicWidget)
266 mnemonicWidget.connect("mnemonic-activate",
267 self._msgLabelActivated,
268 messageType)
269
270 row += 1
271
272 return mainAlignment
273
274 def _msgLabelActivated(self, button, cycle_group, messageType):
275 """Called when the mnemonic of a label is activated.
276
277 It cycles the corresponding options."""
278 fsCheckButton = self._msgFSCheckButtons[messageType]
279 soundCheckButton = self._msgSoundCheckButtons[messageType]
280
281 num = 1 if fsCheckButton.get_active() else 0
282 num += 2 if soundCheckButton.get_active() else 0
283 num += 1
284
285 fsCheckButton.set_active((num&0x01)==0x01)
286 soundCheckButton.set_active((num&0x02)==0x02)
287
288 return True
289
290 def _buildAdvanced(self):
291 """Build the page for the advanced settings."""
292
293 mainAlignment = gtk.Alignment(xalign = 0.0, yalign = 0.0,
294 xscale = 0.0, yscale = 0.0)
295 mainAlignment.set_padding(padding_top = 16, padding_bottom = 32,
296 padding_left = 4, padding_right = 48)
297 mainBox = gtk.VBox()
298 mainAlignment.add(mainBox)
299
300 self._autoUpdate = gtk.CheckButton(xstr("prefs_update_auto"))
301 mainBox.pack_start(self._autoUpdate, False, False, 4)
302
303 self._autoUpdate.set_use_underline(True)
304 self._autoUpdate.connect("toggled", self._autoUpdateToggled)
305 self._autoUpdate.set_tooltip_text(xstr("prefs_update_auto_tooltip"))
306 self._warnedAutoUpdate = False
307 self._togglingAutoUpdate = False
308
309 updateURLBox = gtk.HBox()
310 mainBox.pack_start(updateURLBox, False, False, 4)
311 label = gtk.Label(xstr("prefs_update_url"))
312 label.set_use_underline(True)
313 updateURLBox.pack_start(label, False, False, 4)
314
315 self._updateURL = gtk.Entry()
316 label.set_mnemonic_widget(self._updateURL)
317 self._updateURL.set_width_chars(40)
318 self._updateURL.set_tooltip_text(xstr("prefs_update_url_tooltip"))
319 self._updateURL.connect("changed", self._updateURLChanged)
320 updateURLBox.pack_start(self._updateURL, False, False, 4)
321
322 return mainAlignment
323
324 def _setOKButtonSensitivity(self):
325 """Set the sensitive state of the OK button."""
326 sensitive = False
327 try:
328 result = urlparse.urlparse(self._updateURL.get_text())
329 sensitive = result.scheme!="" and (result.netloc + result.path)!=""
330 except:
331 pass
332
333 okButton = self.get_widget_for_response(RESPONSETYPE_ACCEPT)
334 okButton.set_sensitive(sensitive)
335
336 def _autoUpdateToggled(self, button):
337 """Called when the auto update check button is toggled."""
338 if not self._togglingAutoUpdate and not self._warnedAutoUpdate and \
339 not self._autoUpdate.get_active():
340 dialog = gtk.MessageDialog(parent = self,
341 type = MESSAGETYPE_INFO,
342 message_format = xstr("prefs_update_auto_warning"))
343 dialog.add_button(xstr("button_ok"), RESPONSETYPE_OK)
344 dialog.set_title(self.get_title())
345 dialog.run()
346 dialog.hide()
347 self._warnedAutoUpdate = True
348
349 def _updateURLChanged(self, entry):
350 """Called when the update URL is changed."""
351 self._setOKButtonSensitivity()
352
353
Note: See TracBrowser for help on using the repository browser.