source: src/mlx/gui/prefs.py@ 147:7a297d30a0ce

Last change on this file since 147:7a297d30a0ce was 147:7a297d30a0ce, checked in by István Váradi <ivaradi@…>, 12 years ago

Added option to hide the window when minimized

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