source: src/mlx/gui/checklist.py@ 919:2ce8ca39525b

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

Ran 2to3

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 path = text2unicode(path)
207 numSelected += 1
208 if os.path.isfile(path): numFiles += 1
209
210 self._addButton.set_sensitive(numFiles>0 and numFiles==numSelected)
211
212 def _addButtonClicked(self, button):
213 """Called when the Add button is clicked."""
214 for path in self._fileChooser.get_filenames():
215 path = text2unicode(path)
216 self._fileListModel.append([os.path.basename(path),
217 path])
218 self._fileChooser.unselect_all()
219
220 def _removeButtonClicked(self, button):
221 """Called when the Remove button is clicked."""
222 self._removeSelected()
223
224 def _removeSelected(self):
225 """Remove the currently selected files."""
226 selection = self._fileList.get_selection()
227 (model, paths) = selection.get_selected_rows()
228
229 iters = [model.get_iter(path) for path in paths]
230
231 for i in iters:
232 if i is not None:
233 model.remove(i)
234
235 def _moveUpButtonClicked(self, button):
236 """Called when the move up button is clicked."""
237 self._moveSelected(True)
238
239 def _moveDownButtonClicked(self, button):
240 """Called when the move down button is clicked."""
241 self._moveSelected(False)
242
243 def _moveSelected(self, up):
244 """Move the selected files up or down."""
245 selection = self._fileList.get_selection()
246 (model, paths) = selection.get_selected_rows()
247 indexes = [(path.get_indices() if pygobject else path)[0]
248 for path in paths]
249 indexes.sort()
250 if not up:
251 indexes.reverse()
252
253 for index in indexes:
254 fromIter = model.iter_nth_child(None, index)
255 toIter = model.iter_nth_child(None, index-1 if up else index + 1)
256 if up:
257 model.move_before(fromIter, toIter)
258 else:
259 model.move_after(fromIter, toIter)
260
261 self._moveUpButton.set_sensitive(indexes[0]>1 if up else True)
262 numRows = model.iter_n_children(None)
263 self._moveDownButton.set_sensitive(True if up else
264 indexes[0]<(numRows-2))
265
266 def _fileListSelectionChanged(self, selection):
267 """Called when the selection in the file list changes."""
268 anySelected = selection.count_selected_rows()>0
269 self._removeButton.set_sensitive(anySelected)
270 self._popupRemoveItem.set_sensitive(anySelected)
271
272 if anySelected:
273 (model, paths) = selection.get_selected_rows()
274 minIndex = None
275 maxIndex = None
276 for path in paths:
277 [index] = path.get_indices() if pygobject else path
278 if minIndex is None or index<minIndex: minIndex = index
279 if maxIndex is None or index>maxIndex: maxIndex = index
280
281 self._moveUpButton.set_sensitive(minIndex>0)
282 self._popupMoveUpItem.set_sensitive(minIndex>0)
283
284 numRows = model.iter_n_children(None)
285 self._moveDownButton.set_sensitive(maxIndex<(numRows-1))
286 self._popupMoveDownItem.set_sensitive(maxIndex<(numRows-1))
287 else:
288 self._moveUpButton.set_sensitive(False)
289 self._popupMoveUpItem.set_sensitive(False)
290 self._moveDownButton.set_sensitive(False)
291 self._popupMoveDownItem.set_sensitive(False)
292
293 def _getAircraftType(self):
294 """Get the currently selected aircraft type."""
295 index = self._aircraftType.get_active()
296 return self._aircraftTypeModel[index][1]
297
298 def _saveChecklist(self):
299 """Save the currently displayed checklist for the previously displayed
300 aircraft type."""
301 fileList = []
302 model = self._fileListModel
303 iter = model.get_iter_first()
304 while iter is not None:
305 path = model.get(iter, 1)[0]
306 fileList.append(path)
307 iter = model.iter_next(iter)
308
309 self._checklists[self._currentAircraftType] = config.Checklist(fileList)
310
311 def _displayCurrentChecklist(self):
312 """Display the checklist for the currently selected aircraft type."""
313 aircraftType = self._getAircraftType()
314 self._currentAircraftType = aircraftType
315 if aircraftType not in self._checklists:
316 self._checklists[aircraftType] = \
317 self._gui.config.getChecklist(aircraftType).clone()
318 checklist = self._checklists[aircraftType]
319
320 self._fileListModel.clear()
321 for path in checklist:
322 self._fileListModel.append([os.path.basename(path), path])
323
324 def _fileListButtonPressed(self, widget, event):
325 """Called when a mouse button is pressed on the file list."""
326 if event.type!=EVENT_BUTTON_PRESS or event.button!=3:
327 return
328
329 menu = self._fileListPopupMenu
330 if pygobject:
331 menu.popup(None, None, None, None, event.button, event.time)
332 else:
333 menu.popup(None, None, None, event.button, event.time)
334
335 def _buildFileListPopupMenu(self):
336 """Build the file list popup menu."""
337 menu = gtk.Menu()
338
339 menuItem = gtk.MenuItem()
340 menuItem.set_label(xstr("chklst_remove"))
341 menuItem.set_use_underline(True)
342 menuItem.connect("activate", self._popupRemove)
343 menuItem.show()
344 self._popupRemoveItem = menuItem
345
346 menu.append(menuItem)
347
348 menuItem = gtk.MenuItem()
349 menuItem.set_label(xstr("chklst_moveUp"))
350 menuItem.set_use_underline(True)
351 menuItem.connect("activate", self._popupMoveUp)
352 menuItem.show()
353 self._popupMoveUpItem = menuItem
354
355 menu.append(menuItem)
356
357 menuItem = gtk.MenuItem()
358 menuItem.set_label(xstr("chklst_moveDown"))
359 menuItem.set_use_underline(True)
360 menuItem.connect("activate", self._popupMoveDown)
361 menuItem.show()
362 self._popupMoveDownItem = menuItem
363
364 menu.append(menuItem)
365
366 self._fileListPopupMenu = menu
367
368 def _popupRemove(self, menuItem):
369 """Remove the currently selected menu items."""
370 self._removeSelected()
371
372 def _popupMoveUp(self, menuItem):
373 """Move up the currently selected menu items."""
374 self._moveSelected(True)
375
376 def _popupMoveDown(self, menuItem):
377 """Move down the currently selected menu items."""
378 self._moveSelected(False)
379
380#------------------------------------------------------------------------------
Note: See TracBrowser for help on using the repository browser.