source: src/mlx/gui/prefs.py@ 131:822f47eec5a8

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

Added option to take the flare time from the simulator

File size: 8.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 advanced = self._buildAdvanced()
40 label = gtk.Label(xstr("prefs_tab_advanced"))
41 label.set_use_underline(True)
42 label.set_tooltip_text(xstr("prefs_tab_advanced_tooltip"))
43 notebook.append_page(advanced, label)
44
45 def run(self, config):
46 """Run the preferences dialog.
47
48 The dialog will be set up from data in the given configuration. If the
49 changes are accepted by the user, the configuration is updated and saved."""
50 self._fromConfig(config)
51
52 self.show_all()
53 response = super(Preferences, self).run()
54 self.hide()
55
56 if response==RESPONSETYPE_ACCEPT:
57 self._toConfig(config)
58 config.save()
59
60 def _fromConfig(self, config):
61 """Setup the dialog from the given configuration."""
62 self._setLanguage(config.language)
63 self._flareTimeFromFS.set_active(config.flareTimeFromFS)
64
65 self._togglingAutoUpdate = True
66 self._autoUpdate.set_active(config.autoUpdate)
67 self._togglingAutoUpdate = False
68 if not config.autoUpdate:
69 self._warnedAutoUpdate = True
70
71 self._updateURL.set_text(config.updateURL)
72
73 def _toConfig(self, config):
74 """Setup the given config from the settings in the dialog."""
75 config.language = self._getLanguage()
76 config.flareTimeFromFS = self._flareTimeFromFS.get_active()
77 config.autoUpdate = self._autoUpdate.get_active()
78 config.updateURL = self._updateURL.get_text()
79
80 def _buildGeneral(self):
81 """Build the page for the general settings."""
82 mainAlignment = gtk.Alignment(xalign = 0.0, yalign = 0.0,
83 xscale = 0.0, yscale = 0.0)
84 mainAlignment.set_padding(padding_top = 16, padding_bottom = 32,
85 padding_left = 4, padding_right = 48)
86 mainBox = gtk.VBox()
87 mainAlignment.add(mainBox)
88
89 languageBox = gtk.HBox()
90 mainBox.pack_start(languageBox, False, False, 4)
91
92 label = gtk.Label(xstr("prefs_language"))
93 label.set_use_underline(True)
94
95 languageBox.pack_start(label, False, False, 4)
96
97 self._languageList = gtk.ListStore(str, str)
98 for language in const.languages:
99 self._languageList.append([xstr("prefs_lang_" + language),
100 language])
101
102 self._languageComboBox = languageComboBox = \
103 gtk.ComboBox(model = self._languageList)
104 cell = gtk.CellRendererText()
105 languageComboBox.pack_start(cell, True)
106 languageComboBox.add_attribute(cell, 'text', 0)
107 languageComboBox.set_tooltip_text(xstr("prefs_language_tooltip"))
108 languageComboBox.connect("changed", self._languageChanged)
109 languageBox.pack_start(languageComboBox, False, False, 4)
110
111 label.set_mnemonic_widget(languageComboBox)
112
113 self._changingLanguage = False
114 self._warnedRestartNeeded = False
115
116 self._flareTimeFromFS = gtk.CheckButton(xstr("prefs_flaretimeFromFS"))
117 self._flareTimeFromFS.set_use_underline(True)
118 self._flareTimeFromFS.set_tooltip_text(xstr("prefs_flaretimeFromFS_tooltip"))
119
120 mainBox.pack_start(self._flareTimeFromFS, False, False, 4)
121
122 return mainAlignment
123
124 def _setLanguage(self, language):
125 """Set the language to the given one."""
126 iter = self._languageList.get_iter_first()
127 while iter is not None:
128 (lang,) = self._languageList.get(iter, 1)
129 if (not language and lang=="$system") or \
130 lang==language:
131 self._changingLanguage = True
132 self._languageComboBox.set_active_iter(iter)
133 self._changingLanguage = False
134 break
135 else:
136 iter = self._languageList.iter_next(iter)
137
138 def _getLanguage(self):
139 """Get the language selected by the user."""
140 iter = self._languageComboBox.get_active_iter()
141 (lang,) = self._languageList.get(iter, 1)
142 return "" if lang=="$system" else lang
143
144 def _languageChanged(self, comboBox):
145 """Called when the language has changed."""
146 if not self._changingLanguage and not self._warnedRestartNeeded:
147 dialog = gtk.MessageDialog(parent = self,
148 type = MESSAGETYPE_INFO,
149 message_format = xstr("prefs_restart"))
150 dialog.add_button(xstr("button_ok"), RESPONSETYPE_OK)
151 dialog.set_title(self.get_title())
152 dialog.format_secondary_markup(xstr("prefs_language_restart_sec"))
153 dialog.run()
154 dialog.hide()
155 self._warnedRestartNeeded = True
156
157 def _buildAdvanced(self):
158 """Build the page for the advanced settings."""
159
160 mainAlignment = gtk.Alignment(xalign = 0.0, yalign = 0.0,
161 xscale = 0.0, yscale = 0.0)
162 mainAlignment.set_padding(padding_top = 16, padding_bottom = 32,
163 padding_left = 4, padding_right = 48)
164 mainBox = gtk.VBox()
165 mainAlignment.add(mainBox)
166
167 self._autoUpdate = gtk.CheckButton(xstr("prefs_update_auto"))
168 mainBox.pack_start(self._autoUpdate, False, False, 4)
169
170 self._autoUpdate.set_use_underline(True)
171 self._autoUpdate.connect("toggled", self._autoUpdateToggled)
172 self._autoUpdate.set_tooltip_text(xstr("prefs_update_auto_tooltip"))
173 self._warnedAutoUpdate = False
174 self._togglingAutoUpdate = False
175
176 updateURLBox = gtk.HBox()
177 mainBox.pack_start(updateURLBox, False, False, 4)
178 label = gtk.Label(xstr("prefs_update_url"))
179 label.set_use_underline(True)
180 updateURLBox.pack_start(label, False, False, 4)
181
182 self._updateURL = gtk.Entry()
183 label.set_mnemonic_widget(self._updateURL)
184 self._updateURL.set_width_chars(40)
185 self._updateURL.set_tooltip_text(xstr("prefs_update_url_tooltip"))
186 self._updateURL.connect("changed", self._updateURLChanged)
187 updateURLBox.pack_start(self._updateURL, False, False, 4)
188
189 return mainAlignment
190
191 def _setOKButtonSensitivity(self):
192 """Set the sensitive state of the OK button."""
193 sensitive = False
194 try:
195 result = urlparse.urlparse(self._updateURL.get_text())
196 sensitive = result.scheme!="" and (result.netloc + result.path)!=""
197 except:
198 pass
199
200 okButton = self.get_widget_for_response(RESPONSETYPE_ACCEPT)
201 okButton.set_sensitive(sensitive)
202
203 def _autoUpdateToggled(self, button):
204 """Called when the auto update check button is toggled."""
205 if not self._togglingAutoUpdate and not self._warnedAutoUpdate and \
206 not self._autoUpdate.get_active():
207 dialog = gtk.MessageDialog(parent = self,
208 type = MESSAGETYPE_INFO,
209 message_format = xstr("prefs_update_auto_warning"))
210 dialog.add_button(xstr("button_ok"), RESPONSETYPE_OK)
211 dialog.set_title(self.get_title())
212 dialog.run()
213 dialog.hide()
214 self._warnedAutoUpdate = True
215
216 def _updateURLChanged(self, entry):
217 """Called when the update URL is changed."""
218 self._setOKButtonSensitivity()
219
220
Note: See TracBrowser for help on using the repository browser.