source: src/mlx/gui/prefs.py@ 141:3172532920ad

Last change on this file since 141:3172532920ad was 139:839016dcd0d1, checked in by István Váradi <ivaradi@…>, 13 years ago

Implemented ACARS sending

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