source: src/mlx/gui/checklist.py@ 996:8035d80d5feb

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

Using 'Gtk' instead of 'gtk' (re #347)

File size: 14.8 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()[0] for path in paths]
246 indexes.sort()
247 if not up:
248 indexes.reverse()
249
250 for index in indexes:
251 fromIter = model.iter_nth_child(None, index)
252 toIter = model.iter_nth_child(None, index-1 if up else index + 1)
253 if up:
254 model.move_before(fromIter, toIter)
255 else:
256 model.move_after(fromIter, toIter)
257
258 self._moveUpButton.set_sensitive(indexes[0]>1 if up else True)
259 numRows = model.iter_n_children(None)
260 self._moveDownButton.set_sensitive(True if up else
261 indexes[0]<(numRows-2))
262
263 def _fileListSelectionChanged(self, selection):
264 """Called when the selection in the file list changes."""
265 anySelected = selection.count_selected_rows()>0
266 self._removeButton.set_sensitive(anySelected)
267 self._popupRemoveItem.set_sensitive(anySelected)
268
269 if anySelected:
270 (model, paths) = selection.get_selected_rows()
271 minIndex = None
272 maxIndex = None
273 for path in paths:
274 [index] = path.get_indices()
275 if minIndex is None or index<minIndex: minIndex = index
276 if maxIndex is None or index>maxIndex: maxIndex = index
277
278 self._moveUpButton.set_sensitive(minIndex>0)
279 self._popupMoveUpItem.set_sensitive(minIndex>0)
280
281 numRows = model.iter_n_children(None)
282 self._moveDownButton.set_sensitive(maxIndex<(numRows-1))
283 self._popupMoveDownItem.set_sensitive(maxIndex<(numRows-1))
284 else:
285 self._moveUpButton.set_sensitive(False)
286 self._popupMoveUpItem.set_sensitive(False)
287 self._moveDownButton.set_sensitive(False)
288 self._popupMoveDownItem.set_sensitive(False)
289
290 def _getAircraftType(self):
291 """Get the currently selected aircraft type."""
292 index = self._aircraftType.get_active()
293 return self._aircraftTypeModel[index][1]
294
295 def _saveChecklist(self):
296 """Save the currently displayed checklist for the previously displayed
297 aircraft type."""
298 fileList = []
299 model = self._fileListModel
300 iter = model.get_iter_first()
301 while iter is not None:
302 path = model.get(iter, 1)[0]
303 fileList.append(path)
304 iter = model.iter_next(iter)
305
306 self._checklists[self._currentAircraftType] = config.Checklist(fileList)
307
308 def _displayCurrentChecklist(self):
309 """Display the checklist for the currently selected aircraft type."""
310 aircraftType = self._getAircraftType()
311 self._currentAircraftType = aircraftType
312 if aircraftType not in self._checklists:
313 self._checklists[aircraftType] = \
314 self._gui.config.getChecklist(aircraftType).clone()
315 checklist = self._checklists[aircraftType]
316
317 self._fileListModel.clear()
318 for path in checklist:
319 self._fileListModel.append([os.path.basename(path), path])
320
321 def _fileListButtonPressed(self, widget, event):
322 """Called when a mouse button is pressed on the file list."""
323 if event.type!=EVENT_BUTTON_PRESS or event.button!=3:
324 return
325
326 menu = self._fileListPopupMenu
327 menu.popup(None, None, None, None, event.button, event.time)
328
329 def _buildFileListPopupMenu(self):
330 """Build the file list popup menu."""
331 menu = Gtk.Menu()
332
333 menuItem = Gtk.MenuItem()
334 menuItem.set_label(xstr("chklst_remove"))
335 menuItem.set_use_underline(True)
336 menuItem.connect("activate", self._popupRemove)
337 menuItem.show()
338 self._popupRemoveItem = menuItem
339
340 menu.append(menuItem)
341
342 menuItem = Gtk.MenuItem()
343 menuItem.set_label(xstr("chklst_moveUp"))
344 menuItem.set_use_underline(True)
345 menuItem.connect("activate", self._popupMoveUp)
346 menuItem.show()
347 self._popupMoveUpItem = menuItem
348
349 menu.append(menuItem)
350
351 menuItem = Gtk.MenuItem()
352 menuItem.set_label(xstr("chklst_moveDown"))
353 menuItem.set_use_underline(True)
354 menuItem.connect("activate", self._popupMoveDown)
355 menuItem.show()
356 self._popupMoveDownItem = menuItem
357
358 menu.append(menuItem)
359
360 self._fileListPopupMenu = menu
361
362 def _popupRemove(self, menuItem):
363 """Remove the currently selected menu items."""
364 self._removeSelected()
365
366 def _popupMoveUp(self, menuItem):
367 """Move up the currently selected menu items."""
368 self._moveSelected(True)
369
370 def _popupMoveDown(self, menuItem):
371 """Move down the currently selected menu items."""
372 self._moveSelected(False)
373
374#------------------------------------------------------------------------------
Note: See TracBrowser for help on using the repository browser.