Refactor ManageSoundsWindow

- Move code into ui file
- Use FileChooserButton instead of FileChooserDialog
- Make methods private
- Simplify code
This commit is contained in:
Philipp Hörist 2018-05-02 21:24:10 +02:00
parent ad4c1347ff
commit fc9d242f65
2 changed files with 136 additions and 121 deletions

View File

@ -2939,57 +2939,52 @@ class ManagePEPServicesWindow:
window.set_title(title) window.set_title(title)
window.show_all() window.show_all()
class ManageSoundsWindow: class ManageSoundsWindow:
def __init__(self): def __init__(self):
self.xml = gtkgui_helpers.get_gtk_builder('manage_sounds_window.ui') self._builder = gtkgui_helpers.get_gtk_builder(
self.window = self.xml.get_object('manage_sounds_window') 'manage_sounds_window.ui')
self.window = self._builder.get_object('manage_sounds_window')
self.window.set_transient_for( self.window.set_transient_for(
app.interface.instances['preferences'].window) app.interface.instances['preferences'].window)
# sounds treeview
self.sound_tree = self.xml.get_object('sounds_treeview')
# active, event ui name, path to sound file, event_config_name self.sound_button = self._builder.get_object('filechooser')
model = Gtk.ListStore(bool, str, str, str)
self.sound_tree.set_model(model)
col = Gtk.TreeViewColumn(_('Active')) filter_ = Gtk.FileFilter()
self.sound_tree.append_column(col) filter_.set_name(_('All files'))
renderer = Gtk.CellRendererToggle() filter_.add_pattern('*')
renderer.set_property('activatable', True) self.sound_button.add_filter(filter_)
renderer.connect('toggled', self.sound_toggled_cb)
col.pack_start(renderer, True)
col.add_attribute(renderer, 'active', 0)
col = Gtk.TreeViewColumn(_('Event')) filter_ = Gtk.FileFilter()
self.sound_tree.append_column(col) filter_.set_name(_('Wav Sounds'))
renderer = Gtk.CellRendererText() filter_.add_pattern('*.wav')
col.pack_start(renderer, True) self.sound_button.add_filter(filter_)
col.add_attribute(renderer, 'text', 1) self.sound_button.set_filter(filter_)
self.fill_sound_treeview() self.sound_tree = self._builder.get_object('sounds_treeview')
self.xml.connect_signals(self) self._fill_sound_treeview()
self.sound_tree.get_model().connect('row-changed', self._builder.connect_signals(self)
self.on_sounds_treemodel_row_changed)
self.window.show_all() self.window.show_all()
def on_sounds_treemodel_row_changed(self, model, path, iter_): def _on_row_changed(self, model, path, iter_):
sound_event = model[iter_][3] sound_event = model[iter_][3]
app.config.set_per('soundevents', sound_event, 'enabled', app.config.set_per('soundevents', sound_event,
bool(model[path][0])) 'enabled', bool(model[path][0]))
app.config.set_per('soundevents', sound_event, 'path', app.config.set_per('soundevents', sound_event,
model[iter_][2]) 'path', model[iter_][2])
def sound_toggled_cb(self, cell, path): def _on_toggle(self, cell, path):
if self.sound_button.get_filename() is None:
return
model = self.sound_tree.get_model() model = self.sound_tree.get_model()
model[path][0] = not model[path][0] model[path][0] = not model[path][0]
def fill_sound_treeview(self): def _fill_sound_treeview(self):
model = self.sound_tree.get_model() model = self.sound_tree.get_model()
model.clear() model.clear()
model.set_sort_column_id(1, Gtk.SortType.ASCENDING)
# NOTE: sounds_ui_names MUST have all items of # NOTE: sounds_ui_names MUST have all items of
# sounds = app.config.get_per('soundevents') as keys # sounds = app.config.get_per('soundevents') as keys
@ -2997,8 +2992,7 @@ class ManageSoundsWindow:
'attention_received': _('Attention Message Received'), 'attention_received': _('Attention Message Received'),
'first_message_received': _('First Message Received'), 'first_message_received': _('First Message Received'),
'next_message_received_focused': _('Next Message Received Focused'), 'next_message_received_focused': _('Next Message Received Focused'),
'next_message_received_unfocused': 'next_message_received_unfocused': _('Next Message Received Unfocused'),
_('Next Message Received Unfocused'),
'contact_connected': _('Contact Connected'), 'contact_connected': _('Contact Connected'),
'contact_disconnected': _('Contact Disconnected'), 'contact_disconnected': _('Contact Disconnected'),
'message_sent': _('Message Sent'), 'message_sent': _('Message Sent'),
@ -3006,74 +3000,43 @@ class ManageSoundsWindow:
'muc_message_received': _('Group Chat Message Received'), 'muc_message_received': _('Group Chat Message Received'),
} }
for sound_event_config_name, sound_ui_name in sounds_dict.items(): for config_name, sound_name in sounds_dict.items():
enabled = app.config.get_per('soundevents', enabled = app.config.get_per('soundevents', config_name, 'enabled')
sound_event_config_name, 'enabled') path = app.config.get_per('soundevents', config_name, 'path')
path = app.config.get_per('soundevents', model.append((enabled, sound_name, path, config_name))
sound_event_config_name, 'path')
model.append((enabled, sound_ui_name, path, sound_event_config_name))
def on_treeview_sounds_cursor_changed(self, widget, data = None): def _on_cursor_changed(self, treeview):
sounds_entry = self.xml.get_object('sounds_entry') model, iter_ = treeview.get_selection().get_selected()
sel = self.sound_tree.get_selection() path_to_snd_file = helpers.check_soundfile_path(model[iter_][2])
if not sel: if path_to_snd_file is None:
sounds_entry.set_text('') self.sound_button.unselect_all()
return else:
(model, iter_) = sel.get_selected() self.sound_button.set_filename(path_to_snd_file)
if not iter_:
sounds_entry.set_text('')
return
path_to_snd_file = model[iter_][2]
sounds_entry.set_text(path_to_snd_file)
def on_browse_for_sounds_button_clicked(self, widget, data = None): def _on_file_set(self, button):
sel = self.sound_tree.get_selection()
if not sel:
return
(model, iter_) = sel.get_selected()
if not iter_:
return
def on_ok(widget, path_to_snd_file):
self.dialog.destroy()
model, iter_ = self.sound_tree.get_selection().get_selected() model, iter_ = self.sound_tree.get_selection().get_selected()
if not path_to_snd_file:
model[iter_][2] = '' filename = button.get_filename()
self.xml.get_object('sounds_entry').set_text('') directory = os.path.dirname(filename)
model[iter_][0] = False
return
directory = os.path.dirname(path_to_snd_file)
app.config.set('last_sounds_dir', directory) app.config.set('last_sounds_dir', directory)
path_to_snd_file = helpers.strip_soundfile_path(path_to_snd_file) path_to_snd_file = helpers.strip_soundfile_path(filename)
self.xml.get_object('sounds_entry').set_text(path_to_snd_file)
model[iter_][2] = path_to_snd_file # set new path to sounds_model # set new path to sounds_model
model[iter_][0] = True # set the sound to enabled model[iter_][2] = path_to_snd_file
# set the sound to enabled
model[iter_][0] = True
def on_cancel(widget): def _on_clear(self, *args):
self.dialog.destroy() self.sound_button.unselect_all()
path_to_snd_file = model[iter_][2]
self.dialog = dialogs.SoundChooserDialog(path_to_snd_file, on_ok,
on_cancel, transient_for=self.window)
def on_sounds_entry_changed(self, widget):
path_to_snd_file = widget.get_text()
model, iter_ = self.sound_tree.get_selection().get_selected() model, iter_ = self.sound_tree.get_selection().get_selected()
model[iter_][2] = path_to_snd_file # set new path to sounds_model model[iter_][2] = ''
model[iter_][0] = False
def on_play_button_clicked(self, widget): def _on_play(self, *args):
sel = self.sound_tree.get_selection() model, iter_ = self.sound_tree.get_selection().get_selected()
if not sel:
return
model, iter_ = sel.get_selected()
if not iter_:
return
snd_event_config_name = model[iter_][3] snd_event_config_name = model[iter_][3]
helpers.play_sound(snd_event_config_name) helpers.play_sound(snd_event_config_name)
def on_close_button_clicked(self, widget): def _on_destroy(self, *args):
self.window.hide() self.window.destroy()
app.interface.instances['preferences'].sounds_preferences = None
def on_manage_sounds_window_delete_event(self, widget, event):
self.window.hide()
return True # do NOT destroy the window

View File

@ -1,7 +1,20 @@
<?xml version="1.0" encoding="UTF-8"?> <?xml version="1.0" encoding="UTF-8"?>
<!-- Generated with glade 3.18.3 --> <!-- Generated with glade 3.22.1 -->
<interface> <interface>
<requires lib="gtk+" version="3.12"/> <requires lib="gtk+" version="3.12"/>
<object class="GtkListStore" id="liststore1">
<columns>
<!-- column-name enabled -->
<column type="gboolean"/>
<!-- column-name name -->
<column type="gchararray"/>
<!-- column-name path -->
<column type="gchararray"/>
<!-- column-name config -->
<column type="gchararray"/>
</columns>
<signal name="row-changed" handler="_on_row_changed" swapped="no"/>
</object>
<object class="GtkWindow" id="manage_sounds_window"> <object class="GtkWindow" id="manage_sounds_window">
<property name="width_request">400</property> <property name="width_request">400</property>
<property name="height_request">370</property> <property name="height_request">370</property>
@ -10,7 +23,10 @@
<property name="title" translatable="yes">Manage sounds</property> <property name="title" translatable="yes">Manage sounds</property>
<property name="window_position">center-on-parent</property> <property name="window_position">center-on-parent</property>
<property name="type_hint">dialog</property> <property name="type_hint">dialog</property>
<signal name="delete-event" handler="on_manage_sounds_window_delete_event" swapped="no"/> <signal name="delete-event" handler="_on_destroy" swapped="no"/>
<child>
<placeholder/>
</child>
<child> <child>
<object class="GtkBox" id="vbox1"> <object class="GtkBox" id="vbox1">
<property name="visible">True</property> <property name="visible">True</property>
@ -26,10 +42,36 @@
<object class="GtkTreeView" id="sounds_treeview"> <object class="GtkTreeView" id="sounds_treeview">
<property name="visible">True</property> <property name="visible">True</property>
<property name="can_focus">True</property> <property name="can_focus">True</property>
<signal name="cursor-changed" handler="on_treeview_sounds_cursor_changed" swapped="no"/> <property name="model">liststore1</property>
<signal name="cursor-changed" handler="_on_cursor_changed" swapped="no"/>
<child internal-child="selection"> <child internal-child="selection">
<object class="GtkTreeSelection" id="treeview-selection1"/> <object class="GtkTreeSelection" id="treeview-selection1"/>
</child> </child>
<child>
<object class="GtkTreeViewColumn">
<property name="title" translatable="yes">Active</property>
<property name="clickable">True</property>
<child>
<object class="GtkCellRendererToggle">
<signal name="toggled" handler="_on_toggle" swapped="no"/>
</object>
<attributes>
<attribute name="active">0</attribute>
</attributes>
</child>
</object>
</child>
<child>
<object class="GtkTreeViewColumn">
<property name="title" translatable="yes">Event</property>
<child>
<object class="GtkCellRendererText"/>
<attributes>
<attribute name="text">1</attribute>
</attributes>
</child>
</object>
</child>
</object> </object>
</child> </child>
</object> </object>
@ -45,29 +87,38 @@
<property name="can_focus">False</property> <property name="can_focus">False</property>
<property name="spacing">6</property> <property name="spacing">6</property>
<child> <child>
<object class="GtkEntry" id="sounds_entry"> <object class="GtkFileChooserButton" id="filechooser">
<property name="visible">True</property> <property name="visible">True</property>
<property name="can_focus">True</property> <property name="can_focus">False</property>
<signal name="changed" handler="on_sounds_entry_changed" swapped="no"/> <property name="preview_widget_active">False</property>
<property name="use_preview_label">False</property>
<property name="title" translatable="yes">Choose Sound</property>
<signal name="file-set" handler="_on_file_set" swapped="no"/>
</object> </object>
<packing> <packing>
<property name="expand">False</property> <property name="expand">True</property>
<property name="fill">True</property> <property name="fill">True</property>
<property name="position">0</property> <property name="position">0</property>
</packing> </packing>
</child> </child>
<child> <child>
<object class="GtkButton" id="browse_for_sounds_button"> <object class="GtkButton">
<property name="label">...</property>
<property name="visible">True</property> <property name="visible">True</property>
<property name="can_focus">True</property> <property name="can_focus">True</property>
<property name="receives_default">False</property> <property name="receives_default">True</property>
<property name="use_underline">True</property> <property name="tooltip_text" translatable="yes">Clear File</property>
<signal name="clicked" handler="on_browse_for_sounds_button_clicked" swapped="no"/> <signal name="clicked" handler="_on_clear" swapped="no"/>
<child>
<object class="GtkImage">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="icon_name">edit-clear-all-symbolic</property>
</object>
</child>
</object> </object>
<packing> <packing>
<property name="expand">False</property> <property name="expand">False</property>
<property name="fill">False</property> <property name="fill">True</property>
<property name="position">1</property> <property name="position">1</property>
</packing> </packing>
</child> </child>
@ -76,12 +127,13 @@
<property name="visible">True</property> <property name="visible">True</property>
<property name="can_focus">True</property> <property name="can_focus">True</property>
<property name="receives_default">False</property> <property name="receives_default">False</property>
<signal name="clicked" handler="on_play_button_clicked" swapped="no"/> <signal name="clicked" handler="_on_play" swapped="no"/>
<child> <child>
<object class="GtkImage" id="image1370"> <object class="GtkImage" id="image1370">
<property name="visible">True</property> <property name="visible">True</property>
<property name="can_focus">False</property> <property name="can_focus">False</property>
<property name="stock">gtk-media-play</property> <property name="icon_name">media-playback-start-symbolic</property>
<property name="use_fallback">True</property>
</object> </object>
</child> </child>
</object> </object>
@ -111,7 +163,7 @@
<property name="can_focus">True</property> <property name="can_focus">True</property>
<property name="receives_default">True</property> <property name="receives_default">True</property>
<property name="use_stock">True</property> <property name="use_stock">True</property>
<signal name="clicked" handler="on_close_button_clicked" swapped="no"/> <signal name="clicked" handler="_on_destroy" swapped="no"/>
</object> </object>
<packing> <packing>
<property name="expand">False</property> <property name="expand">False</property>