source: src/mlx/gui/checklist.py@ 954:ad190b3a88c7

python3
Last change on this file since 954:ad190b3a88c7 was 954:ad190b3a88c7, checked in by István Váradi <ivaradi@…>, 5 years ago

Removed text2unicode (re #347).

File size: 15.0 KB
Line 
1
2from .common import *
3
4from mlx.i18n import xstr
5import mlx.const as const
6import mlx.config as config
7
8import os
9
10#------------------------------------------------------------------------------
11
12## @package mlx.gui.checklist
13#
14# The checklist editor dialog.
15#
16# This module implements the checklist editor dialog. A checklist is a list of
17# sound files that are played one-by-one when requested by the user. The top
18# part of the dialog contains an aircraft type selector. Below it, on the left
19# there is a file selector, and the list of the files in the checklist to the
20# right. There are some buttons in the middle to manipulate the checklist.
21
22#------------------------------------------------------------------------------
23
24class ChecklistEditor(gtk.Dialog):
25 """The dialog to edit the checklists."""
26 def __init__(self, gui):
27 super(ChecklistEditor, self).__init__(WINDOW_TITLE_BASE + " - " +
28 xstr("chklst_title"),
29 gui.mainWindow,
30 DIALOG_MODAL)
31
32 self.add_button(xstr("button_cancel"), RESPONSETYPE_REJECT)
33 self.add_button(xstr("button_ok"), RESPONSETYPE_ACCEPT)
34
35 self._gui = gui
36 self._checklists = {}
37 self._currentAircraftType = const.aircraftTypes[0]
38
39 contentArea = self.get_content_area()
40
41 typeBox = gtk.HBox()
42
43 label = gtk.Label(xstr("chklst_aircraftType"))
44 label.set_use_underline(True)
45
46 typeBox.pack_start(label, False, False, 4)
47
48 self._aircraftTypeModel = gtk.ListStore(str, int)
49 for type in const.aircraftTypes:
50 name = aircraftNames[type] if type in aircraftNames \
51 else "Aircraft type #%d" % (type,)
52 self._aircraftTypeModel.append([name, type])
53 self._aircraftType = gtk.ComboBox(model = self._aircraftTypeModel)
54 renderer = gtk.CellRendererText()
55 self._aircraftType.pack_start(renderer, True)
56 self._aircraftType.add_attribute(renderer, "text", 0)
57 self._aircraftType.set_tooltip_text(xstr("chklst_aircraftType_tooltip"))
58 self._aircraftType.set_active(0)
59 self._aircraftType.connect("changed", self._aircraftTypeChanged)
60 label.set_mnemonic_widget(self._aircraftType)
61
62 typeBox.pack_start(self._aircraftType, True, True, 4)
63
64 typeBoxAlignment = gtk.Alignment(xalign = 0.5, yalign = 0.5,
65 xscale = 0.0, yscale = 0.0)
66 typeBoxAlignment.set_size_request(400, -1)
67 typeBoxAlignment.add(typeBox)
68
69 contentArea.pack_start(typeBoxAlignment, False, False, 12)
70
71 fileBox = gtk.HBox()
72
73 self._fileChooser = gtk.FileChooserWidget()
74 self._fileChooser.set_select_multiple(True)
75
76 filter = gtk.FileFilter()
77 filter.set_name(xstr("file_filter_audio"))
78 filter.add_pattern("*.wav")
79 filter.add_pattern("*.mp3")
80 self._fileChooser.add_filter(filter)
81
82 filter = gtk.FileFilter()
83 filter.set_name(xstr("file_filter_all"))
84 filter.add_pattern("*.*")
85 self._fileChooser.add_filter(filter)
86
87 self._fileChooser.connect("selection-changed",
88 self._fileChooserSelectionChanged)
89
90 fileBox.pack_start(self._fileChooser, True, True, 4)
91
92 controlBox = gtk.VBox()
93 controlAlignment = gtk.Alignment(xalign = 0.0, yalign = 0.0,
94 xscale = 0.0, yscale = 0.0)
95 controlAlignment.set_padding(padding_top = 0, padding_bottom = 0,
96 padding_left = 32, padding_right = 32)
97 controlAlignment.add(controlBox)
98 fileBox.pack_start(controlAlignment, False, False, 0)
99
100 self._addButton = gtk.Button(xstr("chklst_add"))
101 self._addButton.set_use_underline(True)
102 self._addButton.set_tooltip_text(xstr("chklst_add_tooltip"))
103 self._addButton.connect("clicked", self._addButtonClicked)
104 addAlignment = gtk.Alignment(xalign = 0.5, yalign = 0.0,
105 xscale = 0.0, yscale = 0.0)
106 addAlignment.set_padding(padding_top = 64, padding_bottom = 0,
107 padding_left = 0, padding_right = 0)
108 addAlignment.add(self._addButton)
109 controlBox.pack_start(addAlignment, False, False, 0)
110
111 self._removeButton = gtk.Button(xstr("chklst_remove"))
112 self._removeButton.set_use_underline(True)
113 self._removeButton.set_tooltip_text(xstr("chklst_remove_tooltip"))
114 self._removeButton.set_sensitive(False)
115 self._removeButton.connect("clicked", self._removeButtonClicked)
116
117 removeAlignment = gtk.Alignment(xalign = 0.5, yalign = 0.0,
118 xscale = 0.0, yscale = 0.0)
119 removeAlignment.set_padding(padding_top = 64, padding_bottom = 0,
120 padding_left = 0, padding_right = 0)
121 removeAlignment.add(self._removeButton)
122 controlBox.pack_start(removeAlignment, False, False, 0)
123
124 self._moveUpButton = gtk.Button(xstr("chklst_moveUp"))
125 self._moveUpButton.set_use_underline(True)
126 self._moveUpButton.set_tooltip_text(xstr("chklst_moveUp_tooltip"))
127 self._moveUpButton.set_sensitive(False)
128 self._moveUpButton.connect("clicked", self._moveUpButtonClicked)
129
130 moveUpAlignment = gtk.Alignment(xalign = 0.5, yalign = 0.0,
131 xscale = 0.0, yscale = 0.0)
132 moveUpAlignment.set_padding(padding_top = 16, padding_bottom = 0,
133 padding_left = 0, padding_right = 0)
134 moveUpAlignment.add(self._moveUpButton)
135 controlBox.pack_start(moveUpAlignment, False, False, 0)
136
137 self._moveDownButton = gtk.Button(xstr("chklst_moveDown"))
138 self._moveDownButton.set_use_underline(True)
139 self._moveDownButton.set_tooltip_text(xstr("chklst_moveDown_tooltip"))
140 self._moveDownButton.set_sensitive(False)
141 self._moveDownButton.connect("clicked", self._moveDownButtonClicked)
142
143 moveDownAlignment = gtk.Alignment(xalign = 0.5, yalign = 0.0,
144 xscale = 0.0, yscale = 0.0)
145 moveDownAlignment.set_padding(padding_top = 4, padding_bottom = 0,
146 padding_left = 0, padding_right = 0)
147 moveDownAlignment.add(self._moveDownButton)
148 controlBox.pack_start(moveDownAlignment, False, False, 0)
149
150 self._fileListModel = gtk.ListStore(str, str)
151 self._fileList = gtk.TreeView(model = self._fileListModel)
152 self._fileList.connect("button-press-event",
153 self._fileListButtonPressed)
154 column = gtk.TreeViewColumn(xstr("chklst_header"),
155 gtk.CellRendererText(), text = 0)
156 column.set_expand(True)
157 column.set_clickable(False)
158 column.set_reorderable(False)
159 self._fileList.append_column(column)
160 self._fileList.set_tooltip_column(1)
161 self._fileList.set_reorderable(True)
162 self._fileListPopupMenu = None
163 selection = self._fileList.get_selection()
164 selection.set_mode(SELECTION_MULTIPLE)
165 selection.connect("changed", self._fileListSelectionChanged)
166
167 self._buildFileListPopupMenu()
168
169 scrolledWindow = gtk.ScrolledWindow()
170 scrolledWindow.add(self._fileList)
171 scrolledWindow.set_size_request(200, -1)
172 scrolledWindow.set_policy(POLICY_AUTOMATIC, POLICY_AUTOMATIC)
173 scrolledWindow.set_shadow_type(SHADOW_IN)
174
175 fileBox.pack_start(scrolledWindow, False, False, 4)
176
177 contentArea.pack_start(fileBox, True, True, 4)
178
179 self.set_size_request(900, 500)
180
181 def run(self):
182 """Run the checklist editor dialog."""
183 self._checklists = {}
184 self._displayCurrentChecklist()
185 self.show_all()
186 response = super(ChecklistEditor, self).run()
187 self.hide()
188
189 if response==RESPONSETYPE_ACCEPT:
190 self._saveChecklist()
191 config = self._gui.config
192 for (aircraftType, checklist) in self._checklists.items():
193 config.setChecklist(aircraftType, checklist)
194 config.save()
195
196 def _aircraftTypeChanged(self, comboBox):
197 """Called when the aircraft's type has changed."""
198 self._saveChecklist()
199 self._displayCurrentChecklist()
200
201 def _fileChooserSelectionChanged(self, fileChooser):
202 """Called when the selection of the given file chooser is changed."""
203 numFiles = 0
204 numSelected = 0
205 for path in fileChooser.get_filenames():
206 numSelected += 1
207 if os.path.isfile(path): numFiles += 1
208
209 self._addButton.set_sensitive(numFiles>0 and numFiles==numSelected)
210
211 def _addButtonClicked(self, button):
212 """Called when the Add button is clicked."""
213 for path in self._fileChooser.get_filenames():
214 self._fileListModel.append([os.path.basename(path),
215 path])
216 self._fileChooser.unselect_all()
217
218 def _removeButtonClicked(self, button):
219 """Called when the Remove button is clicked."""
220 self._removeSelected()
221
222 def _removeSelected(self):
223 """Remove the currently selected files."""
224 selection = self._fileList.get_selection()
225 (model, paths) = selection.get_selected_rows()
226
227 iters = [model.get_iter(path) for path in paths]
228
229 for i in iters:
230 if i is not None:
231 model.remove(i)
232
233 def _moveUpButtonClicked(self, button):
234 """Called when the move up button is clicked."""
235 self._moveSelected(True)
236
237 def _moveDownButtonClicked(self, button):
238 """Called when the move down button is clicked."""
239 self._moveSelected(False)
240
241 def _moveSelected(self, up):
242 """Move the selected files up or down."""
243 selection = self._fileList.get_selection()
244 (model, paths) = selection.get_selected_rows()
245 indexes = [(path.get_indices() if pygobject else path)[0]
246 for path in paths]
247 indexes.sort()
248 if not up:
249 indexes.reverse()
250
251 for index in indexes:
252 fromIter = model.iter_nth_child(None, index)
253 toIter = model.iter_nth_child(None, index-1 if up else index + 1)
254 if up:
255 model.move_before(fromIter, toIter)
256 else:
257 model.move_after(fromIter, toIter)
258
259 self._moveUpButton.set_sensitive(indexes[0]>1 if up else True)
260 numRows = model.iter_n_children(None)
261 self._moveDownButton.set_sensitive(True if up else
262 indexes[0]<(numRows-2))
263
264 def _fileListSelectionChanged(self, selection):
265 """Called when the selection in the file list changes."""
266 anySelected = selection.count_selected_rows()>0
267 self._removeButton.set_sensitive(anySelected)
268 self._popupRemoveItem.set_sensitive(anySelected)
269
270 if anySelected:
271 (model, paths) = selection.get_selected_rows()
272 minIndex = None
273 maxIndex = None
274 for path in paths:
275 [index] = path.get_indices() if pygobject else path
276 if minIndex is None or index<minIndex: minIndex = index
277 if maxIndex is None or index>maxIndex: maxIndex = index
278
279 self._moveUpButton.set_sensitive(minIndex>0)
280 self._popupMoveUpItem.set_sensitive(minIndex>0)
281
282 numRows = model.iter_n_children(None)
283 self._moveDownButton.set_sensitive(maxIndex<(numRows-1))
284 self._popupMoveDownItem.set_sensitive(maxIndex<(numRows-1))
285 else:
286 self._moveUpButton.set_sensitive(False)
287 self._popupMoveUpItem.set_sensitive(False)
288 self._moveDownButton.set_sensitive(False)
289 self._popupMoveDownItem.set_sensitive(False)
290
291 def _getAircraftType(self):
292 """Get the currently selected aircraft type."""
293 index = self._aircraftType.get_active()
294 return self._aircraftTypeModel[index][1]
295
296 def _saveChecklist(self):
297 """Save the currently displayed checklist for the previously displayed
298 aircraft type."""
299 fileList = []
300 model = self._fileListModel
301 iter = model.get_iter_first()
302 while iter is not None:
303 path = model.get(iter, 1)[0]
304 fileList.append(path)
305 iter = model.iter_next(iter)
306
307 self._checklists[self._currentAircraftType] = config.Checklist(fileList)
308
309 def _displayCurrentChecklist(self):
310 """Display the checklist for the currently selected aircraft type."""
311 aircraftType = self._getAircraftType()
312 self._currentAircraftType = aircraftType
313 if aircraftType not in self._checklists:
314 self._checklists[aircraftType] = \
315 self._gui.config.getChecklist(aircraftType).clone()
316 checklist = self._checklists[aircraftType]
317
318 self._fileListModel.clear()
319 for path in checklist:
320 self._fileListModel.append([os.path.basename(path), path])
321
322 def _fileListButtonPressed(self, widget, event):
323 """Called when a mouse button is pressed on the file list."""
324 if event.type!=EVENT_BUTTON_PRESS or event.button!=3:
325 return
326
327 menu = self._fileListPopupMenu
328 if pygobject:
329 menu.popup(None, None, None, None, event.button, event.time)
330 else:
331 menu.popup(None, None, None, event.button, event.time)
332
333 def _buildFileListPopupMenu(self):
334 """Build the file list popup menu."""
335 menu = gtk.Menu()
336
337 menuItem = gtk.MenuItem()
338 menuItem.set_label(xstr("chklst_remove"))
339 menuItem.set_use_underline(True)
340 menuItem.connect("activate", self._popupRemove)
341 menuItem.show()
342 self._popupRemoveItem = menuItem
343
344 menu.append(menuItem)
345
346 menuItem = gtk.MenuItem()
347 menuItem.set_label(xstr("chklst_moveUp"))
348 menuItem.set_use_underline(True)
349 menuItem.connect("activate", self._popupMoveUp)
350 menuItem.show()
351 self._popupMoveUpItem = menuItem
352
353 menu.append(menuItem)
354
355 menuItem = gtk.MenuItem()
356 menuItem.set_label(xstr("chklst_moveDown"))
357 menuItem.set_use_underline(True)
358 menuItem.connect("activate", self._popupMoveDown)
359 menuItem.show()
360 self._popupMoveDownItem = menuItem
361
362 menu.append(menuItem)
363
364 self._fileListPopupMenu = menu
365
366 def _popupRemove(self, menuItem):
367 """Remove the currently selected menu items."""
368 self._removeSelected()
369
370 def _popupMoveUp(self, menuItem):
371 """Move up the currently selected menu items."""
372 self._moveSelected(True)
373
374 def _popupMoveDown(self, menuItem):
375 """Move down the currently selected menu items."""
376 self._moveSelected(False)
377
378#------------------------------------------------------------------------------
Note: See TracBrowser for help on using the repository browser.