source: src/mlx/gui/callouts.py@ 265:441bc4537a91

Last change on this file since 265:441bc4537a91 was 264:6fbadb22b2c3, checked in by István Váradi <ivaradi@…>, 12 years ago

Started implementing the approach callout editor

File size: 14.1 KB
Line 
1# Module for editing checklists
2
3#------------------------------------------------------------------------------
4
5from common import *
6
7from mlx.i18n import xstr
8import mlx.const as const
9import mlx.config as config
10
11import os
12
13#------------------------------------------------------------------------------
14
15class ApproachCalloutsEditor(gtk.Dialog):
16 """The dialog to edit the approach callouts."""
17 def __init__(self, gui):
18 super(ApproachCalloutsEditor, self).__init__(WINDOW_TITLE_BASE + " - " +
19 xstr("callouts_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 self._approachCallouts = {}
28 self._currentAircraftType = const.aircraftTypes[0]
29
30 contentArea = self.get_content_area()
31
32 # FIXME: common code with the checklist editor
33 typeBox = gtk.HBox()
34
35 label = gtk.Label(xstr("callouts_aircraftType"))
36 label.set_use_underline(True)
37
38 typeBox.pack_start(label, False, False, 4)
39
40 self._aircraftTypeModel = gtk.ListStore(str, int)
41 for type in const.aircraftTypes:
42 name = aircraftNames[type] if type in aircraftNames \
43 else "Aircraft type #%d" % (type,)
44 self._aircraftTypeModel.append([name, type])
45 self._aircraftType = gtk.ComboBox(model = self._aircraftTypeModel)
46 renderer = gtk.CellRendererText()
47 self._aircraftType.pack_start(renderer, True)
48 self._aircraftType.add_attribute(renderer, "text", 0)
49 self._aircraftType.set_tooltip_text(xstr("callouts_aircraftType_tooltip"))
50 self._aircraftType.set_active(0)
51 self._aircraftType.connect("changed", self._aircraftTypeChanged)
52 label.set_mnemonic_widget(self._aircraftType)
53
54 typeBox.pack_start(self._aircraftType, True, True, 4)
55
56 typeBoxAlignment = gtk.Alignment(xalign = 0.5, yalign = 0.5,
57 xscale = 0.0, yscale = 0.0)
58 typeBoxAlignment.set_size_request(400, -1)
59 typeBoxAlignment.add(typeBox)
60
61 contentArea.pack_start(typeBoxAlignment, False, False, 12)
62 # Until here, but not that some texts are different
63
64 fileBox = gtk.HBox()
65
66 # self._fileChooser = gtk.FileChooserWidget()
67 # self._fileChooser.set_select_multiple(True)
68
69 # filter = gtk.FileFilter()
70 # filter.set_name(xstr("chklst_filter_audio"))
71 # filter.add_pattern("*.wav")
72 # filter.add_pattern("*.mp3")
73 # self._fileChooser.add_filter(filter)
74
75 # filter = gtk.FileFilter()
76 # filter.set_name(xstr("file_filter_all"))
77 # filter.add_pattern("*.*")
78 # self._fileChooser.add_filter(filter)
79
80 # self._fileChooser.connect("selection-changed",
81 # self._fileChooserSelectionChanged)
82
83 # fileBox.pack_start(self._fileChooser, True, True, 4)
84
85 controlBox = gtk.VBox()
86 controlAlignment = gtk.Alignment(xalign = 0.0, yalign = 0.0,
87 xscale = 0.0, yscale = 0.0)
88 controlAlignment.set_padding(padding_top = 0, padding_bottom = 0,
89 padding_left = 32, padding_right = 32)
90 controlAlignment.add(controlBox)
91 fileBox.pack_start(controlAlignment, False, False, 0)
92
93 self._addButton = gtk.Button(xstr("callouts_add"))
94 self._addButton.set_use_underline(True)
95 self._addButton.set_tooltip_text(xstr("callouts_add_tooltip"))
96 self._addButton.connect("clicked", self._addButtonClicked)
97 addAlignment = gtk.Alignment(xalign = 0.5, yalign = 0.0,
98 xscale = 0.0, yscale = 0.0)
99 addAlignment.set_padding(padding_top = 64, padding_bottom = 0,
100 padding_left = 0, padding_right = 0)
101 addAlignment.add(self._addButton)
102 controlBox.pack_start(addAlignment, False, False, 0)
103
104 self._removeButton = gtk.Button(xstr("callouts_remove"))
105 self._removeButton.set_use_underline(True)
106 self._removeButton.set_tooltip_text(xstr("callouts_remove_tooltip"))
107 self._removeButton.set_sensitive(False)
108 self._removeButton.connect("clicked", self._removeButtonClicked)
109
110 removeAlignment = gtk.Alignment(xalign = 0.5, yalign = 0.0,
111 xscale = 0.0, yscale = 0.0)
112 removeAlignment.set_padding(padding_top = 64, padding_bottom = 0,
113 padding_left = 0, padding_right = 0)
114 removeAlignment.add(self._removeButton)
115 controlBox.pack_start(removeAlignment, False, False, 0)
116
117 # self._moveUpButton = gtk.Button(xstr("chklst_moveUp"))
118 # self._moveUpButton.set_use_underline(True)
119 # self._moveUpButton.set_tooltip_text(xstr("chklst_moveUp_tooltip"))
120 # self._moveUpButton.set_sensitive(False)
121 # self._moveUpButton.connect("clicked", self._moveUpButtonClicked)
122
123 # moveUpAlignment = gtk.Alignment(xalign = 0.5, yalign = 0.0,
124 # xscale = 0.0, yscale = 0.0)
125 # moveUpAlignment.set_padding(padding_top = 16, padding_bottom = 0,
126 # padding_left = 0, padding_right = 0)
127 # moveUpAlignment.add(self._moveUpButton)
128 # controlBox.pack_start(moveUpAlignment, False, False, 0)
129
130 # self._moveDownButton = gtk.Button(xstr("chklst_moveDown"))
131 # self._moveDownButton.set_use_underline(True)
132 # self._moveDownButton.set_tooltip_text(xstr("chklst_moveDown_tooltip"))
133 # self._moveDownButton.set_sensitive(False)
134 # self._moveDownButton.connect("clicked", self._moveDownButtonClicked)
135
136 # moveDownAlignment = gtk.Alignment(xalign = 0.5, yalign = 0.0,
137 # xscale = 0.0, yscale = 0.0)
138 # moveDownAlignment.set_padding(padding_top = 4, padding_bottom = 0,
139 # padding_left = 0, padding_right = 0)
140 # moveDownAlignment.add(self._moveDownButton)
141 # controlBox.pack_start(moveDownAlignment, False, False, 0)
142
143 self._fileListModel = gtk.ListStore(int, str, str)
144 self._fileListModel.set_sort_column_id(0, SORT_DESCENDING)
145 self._fileList = gtk.TreeView(model = self._fileListModel)
146
147 renderer = gtk.CellRendererSpin()
148 renderer.set_property("editable", True)
149
150 adjustment = gtk.Adjustment(0, 0, 5000, 10, 100)
151 renderer.set_property("adjustment", adjustment);
152 renderer.connect("edited", self._altitudeEdited)
153
154 column = gtk.TreeViewColumn(xstr("callouts_header_altitude"),
155 renderer, text = 0)
156 self._fileList.append_column(column)
157 column.set_expand(True)
158 column.set_clickable(True)
159 column.set_reorderable(False)
160 column.set_sort_indicator(True)
161 column.set_sort_column_id(0)
162 column.set_sort_order(SORT_DESCENDING)
163 column.set_expand(False)
164
165 column = gtk.TreeViewColumn(xstr("callouts_header_path"),
166 gtk.CellRendererText(), text = 1)
167 self._fileList.append_column(column)
168 column.set_expand(True)
169 column.set_clickable(False)
170 column.set_reorderable(False)
171 column.set_expand(True)
172
173 self._fileList.set_tooltip_column(2)
174 self._fileList.set_reorderable(False)
175 self._fileList.set_size_request(400, -1)
176 selection = self._fileList.get_selection()
177 selection.set_mode(SELECTION_MULTIPLE)
178 selection.connect("changed", self._fileListSelectionChanged)
179
180 fileBox.pack_start(self._fileList, False, False, 4)
181
182 contentArea.pack_start(fileBox, True, True, 4)
183
184 self.set_size_request(500, 500)
185
186 def run(self):
187 """Run the approach callouts editor dialog."""
188 self._approachCallouts = {}
189 self._displayCurrentApproachCallouts()
190 self.show_all()
191 response = super(ApproachCalloutsEditor, self).run()
192 self.hide()
193
194 if response==RESPONSETYPE_ACCEPT:
195 self._saveApproachCallouts()
196 config = self._gui.config
197 for (aircraftType, approachCallouts) in \
198 self._approachCallouts.iteritems():
199 config.setApproachCallouts(aircraftType, approachCallouts)
200 config.save()
201
202 def _aircraftTypeChanged(self, comboBox):
203 """Called when the aircraft's type has changed."""
204 self._saveApproachCallouts()
205 self._displayCurrentApproachCallouts()
206
207 # def _fileChooserSelectionChanged(self, fileChooser):
208 # """Called when the selection of the given file chooser is changed."""
209 # numFiles = 0
210 # numSelected = 0
211 # for path in fileChooser.get_filenames():
212 # path = text2unicode(path)
213 # numSelected += 1
214 # if os.path.isfile(path): numFiles += 1
215
216 # self._addButton.set_sensitive(numFiles>0 and numFiles==numSelected)
217
218 def _addButtonClicked(self, button):
219 """Called when the Add button is clicked."""
220 selection = self._fileList.get_selection()
221 (model, paths) = selection.get_selected_rows()
222 iters = [model.get_iter(path) for path in paths]
223 print paths, iters
224 self._fileListModel.append([1300, "1300.mp3", "1300.mp3"])
225
226 def _removeButtonClicked(self, button):
227 """Called when the Remove button is clicked."""
228 selection = self._fileList.get_selection()
229 (model, paths) = selection.get_selected_rows()
230
231 iters = [model.get_iter(path) for path in paths]
232
233 for i in iters:
234 if i is not None:
235 model.remove(i)
236
237 # def _moveUpButtonClicked(self, button):
238 # """Called when the move up button is clicked."""
239 # self._moveSelected(True)
240
241 # def _moveDownButtonClicked(self, button):
242 # """Called when the move down button is clicked."""
243 # self._moveSelected(False)
244
245 # def _moveSelected(self, up):
246 # """Move the selected files up or down."""
247 # selection = self._fileList.get_selection()
248 # (model, paths) = selection.get_selected_rows()
249 # indexes = [(path.get_indices() if pygobject else path)[0]
250 # for path in paths]
251 # indexes.sort()
252 # if not up:
253 # indexes.reverse()
254
255 # for index in indexes:
256 # fromIter = model.iter_nth_child(None, index)
257 # toIter = model.iter_nth_child(None, index-1 if up else index + 1)
258 # if up:
259 # model.move_before(fromIter, toIter)
260 # else:
261 # model.move_after(fromIter, toIter)
262
263 # self._moveUpButton.set_sensitive(indexes[0]>1 if up else True)
264 # numRows = model.iter_n_children(None)
265 # self._moveDownButton.set_sensitive(True if up else
266 # indexes[0]<(numRows-2))
267
268 def _fileListSelectionChanged(self, selection):
269 """Called when the selection in the file list changes."""
270 anySelected = selection.count_selected_rows()>0
271 self._removeButton.set_sensitive(anySelected)
272
273 def _getAircraftType(self):
274 """Get the currently selected aircraft type."""
275 # FIXME: the same code as in the checklist editor
276 index = self._aircraftType.get_active()
277 return self._aircraftTypeModel[index][1]
278
279 def _altitudeEdited(self, widget, path, value):
280 """Called when an altitude is edited"""
281 newAltitude = int(value)
282
283 model = self._fileListModel
284 editedIter = model.get_iter_from_string(path)
285 editedPath = model.get_path(editedIter)
286 iter = model.get_iter_first()
287 while iter is not None and newAltitude is not None:
288 path = model.get_path(iter)
289 if editedPath!=path and newAltitude==model[path][0]:
290 dialog = gtk.MessageDialog(parent = self,
291 type = MESSAGETYPE_QUESTION,
292 message_format =
293 xstr("callouts_altitude_clash"))
294 dialog.format_secondary_markup(xstr("callouts_altitude_clash_sec"))
295 dialog.add_button(xstr("button_no"), RESPONSETYPE_NO)
296 dialog.add_button(xstr("button_yes"), RESPONSETYPE_YES)
297 dialog.set_title(WINDOW_TITLE_BASE)
298
299 result = dialog.run()
300 dialog.hide()
301
302 if result==RESPONSETYPE_NO:
303 newAltitude = None
304 iter = model.iter_next(iter)
305
306 if newAltitude is not None:
307 self._fileListModel[editedPath][0] = newAltitude
308
309 def _saveApproachCallouts(self):
310 """Save the currently displayed list of approach callouts for the
311 previously displayed aircraft type."""
312 mapping = {}
313 model = self._fileListModel
314 iter = model.get_iter_first()
315 while iter is not None:
316 altitude = int(model.get(iter, 0)[0])
317 path = model.get(iter, 2)[0]
318 mapping[altitude] = path
319 iter = model.iter_next(iter)
320
321 self._approachCallouts[self._currentAircraftType] = \
322 config.ApproachCallouts(mapping)
323
324 def _displayCurrentApproachCallouts(self):
325 """Display the approach callouts for the currently selected aircraft
326 type."""
327 aircraftType = self._getAircraftType()
328 self._currentAircraftType = aircraftType
329 if aircraftType not in self._approachCallouts:
330 self._approachCallouts[aircraftType] = \
331 self._gui.config.getApproachCallouts(aircraftType).clone()
332 approachCallouts = self._approachCallouts[aircraftType]
333
334 self._fileListModel.clear()
335 for (altitude, path) in approachCallouts:
336 self._fileListModel.append([altitude, os.path.basename(path), path])
337
338#------------------------------------------------------------------------------
Note: See TracBrowser for help on using the repository browser.