Commit fc9d242f authored by Philipp Hörist's avatar Philipp Hörist

Refactor ManageSoundsWindow

- Move code into ui file
- Use FileChooserButton instead of FileChooserDialog
- Make methods private
- Simplify code
parent ad4c1347
......@@ -2939,141 +2939,104 @@ class ManagePEPServicesWindow:
window.set_title(title)
window.show_all()
class ManageSoundsWindow:
def __init__(self):
self.xml = gtkgui_helpers.get_gtk_builder('manage_sounds_window.ui')
self.window = self.xml.get_object('manage_sounds_window')
self._builder = gtkgui_helpers.get_gtk_builder(
'manage_sounds_window.ui')
self.window = self._builder.get_object('manage_sounds_window')
self.window.set_transient_for(
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
model = Gtk.ListStore(bool, str, str, str)
self.sound_tree.set_model(model)
col = Gtk.TreeViewColumn(_('Active'))
self.sound_tree.append_column(col)
renderer = Gtk.CellRendererToggle()
renderer.set_property('activatable', True)
renderer.connect('toggled', self.sound_toggled_cb)
col.pack_start(renderer, True)
col.add_attribute(renderer, 'active', 0)
col = Gtk.TreeViewColumn(_('Event'))
self.sound_tree.append_column(col)
renderer = Gtk.CellRendererText()
col.pack_start(renderer, True)
col.add_attribute(renderer, 'text', 1)
self.sound_button = self._builder.get_object('filechooser')
self.fill_sound_treeview()
filter_ = Gtk.FileFilter()
filter_.set_name(_('All files'))
filter_.add_pattern('*')
self.sound_button.add_filter(filter_)
self.xml.connect_signals(self)
filter_ = Gtk.FileFilter()
filter_.set_name(_('Wav Sounds'))
filter_.add_pattern('*.wav')
self.sound_button.add_filter(filter_)
self.sound_button.set_filter(filter_)
self.sound_tree = self._builder.get_object('sounds_treeview')
self.sound_tree.get_model().connect('row-changed',
self.on_sounds_treemodel_row_changed)
self._fill_sound_treeview()
self._builder.connect_signals(self)
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]
app.config.set_per('soundevents', sound_event, 'enabled',
bool(model[path][0]))
app.config.set_per('soundevents', sound_event, 'path',
model[iter_][2])
app.config.set_per('soundevents', sound_event,
'enabled', bool(model[path][0]))
app.config.set_per('soundevents', sound_event,
'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[path][0] = not model[path][0]
def fill_sound_treeview(self):
def _fill_sound_treeview(self):
model = self.sound_tree.get_model()
model.clear()
model.set_sort_column_id(1, Gtk.SortType.ASCENDING)
# NOTE: sounds_ui_names MUST have all items of
# sounds = app.config.get_per('soundevents') as keys
sounds_dict = {
'attention_received': _('Attention Message Received'),
'first_message_received': _('First Message Received'),
'next_message_received_focused': _('Next Message Received Focused'),
'next_message_received_unfocused':
_('Next Message Received Unfocused'),
'contact_connected': _('Contact Connected'),
'contact_disconnected': _('Contact Disconnected'),
'message_sent': _('Message Sent'),
'muc_message_highlight': _('Group Chat Message Highlight'),
'muc_message_received': _('Group Chat Message Received'),
'attention_received': _('Attention Message Received'),
'first_message_received': _('First Message Received'),
'next_message_received_focused': _('Next Message Received Focused'),
'next_message_received_unfocused': _('Next Message Received Unfocused'),
'contact_connected': _('Contact Connected'),
'contact_disconnected': _('Contact Disconnected'),
'message_sent': _('Message Sent'),
'muc_message_highlight': _('Group Chat Message Highlight'),
'muc_message_received': _('Group Chat Message Received'),
}
for sound_event_config_name, sound_ui_name in sounds_dict.items():
enabled = app.config.get_per('soundevents',
sound_event_config_name, 'enabled')
path = app.config.get_per('soundevents',
sound_event_config_name, 'path')
model.append((enabled, sound_ui_name, path, sound_event_config_name))
for config_name, sound_name in sounds_dict.items():
enabled = app.config.get_per('soundevents', config_name, 'enabled')
path = app.config.get_per('soundevents', config_name, 'path')
model.append((enabled, sound_name, path, config_name))
def on_treeview_sounds_cursor_changed(self, widget, data = None):
sounds_entry = self.xml.get_object('sounds_entry')
sel = self.sound_tree.get_selection()
if not sel:
sounds_entry.set_text('')
return
(model, iter_) = sel.get_selected()
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):
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()
if not path_to_snd_file:
model[iter_][2] = ''
self.xml.get_object('sounds_entry').set_text('')
model[iter_][0] = False
return
directory = os.path.dirname(path_to_snd_file)
app.config.set('last_sounds_dir', directory)
path_to_snd_file = helpers.strip_soundfile_path(path_to_snd_file)
self.xml.get_object('sounds_entry').set_text(path_to_snd_file)
def _on_cursor_changed(self, treeview):
model, iter_ = treeview.get_selection().get_selected()
path_to_snd_file = helpers.check_soundfile_path(model[iter_][2])
if path_to_snd_file is None:
self.sound_button.unselect_all()
else:
self.sound_button.set_filename(path_to_snd_file)
model[iter_][2] = path_to_snd_file # set new path to sounds_model
model[iter_][0] = True # set the sound to enabled
def _on_file_set(self, button):
model, iter_ = self.sound_tree.get_selection().get_selected()
def on_cancel(widget):
self.dialog.destroy()
filename = button.get_filename()
directory = os.path.dirname(filename)
app.config.set('last_sounds_dir', directory)
path_to_snd_file = helpers.strip_soundfile_path(filename)
path_to_snd_file = model[iter_][2]
self.dialog = dialogs.SoundChooserDialog(path_to_snd_file, on_ok,
on_cancel, transient_for=self.window)
# set new path to sounds_model
model[iter_][2] = path_to_snd_file
# set the sound to enabled
model[iter_][0] = True
def on_sounds_entry_changed(self, widget):
path_to_snd_file = widget.get_text()
def _on_clear(self, *args):
self.sound_button.unselect_all()
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):
sel = self.sound_tree.get_selection()
if not sel:
return
model, iter_ = sel.get_selected()
if not iter_:
return
def _on_play(self, *args):
model, iter_ = self.sound_tree.get_selection().get_selected()
snd_event_config_name = model[iter_][3]
helpers.play_sound(snd_event_config_name)
def on_close_button_clicked(self, widget):
self.window.hide()
def on_manage_sounds_window_delete_event(self, widget, event):
self.window.hide()
return True # do NOT destroy the window
def _on_destroy(self, *args):
self.window.destroy()
app.interface.instances['preferences'].sounds_preferences = None
<?xml version="1.0" encoding="UTF-8"?>
<!-- Generated with glade 3.18.3 -->
<!-- Generated with glade 3.22.1 -->
<interface>
<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">
<property name="width_request">400</property>
<property name="height_request">370</property>
......@@ -10,7 +23,10 @@
<property name="title" translatable="yes">Manage sounds</property>
<property name="window_position">center-on-parent</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>
<object class="GtkBox" id="vbox1">
<property name="visible">True</property>
......@@ -26,10 +42,36 @@
<object class="GtkTreeView" id="sounds_treeview">
<property name="visible">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">
<object class="GtkTreeSelection" id="treeview-selection1"/>
</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>
</child>
</object>
......@@ -45,29 +87,38 @@
<property name="can_focus">False</property>
<property name="spacing">6</property>
<child>
<object class="GtkEntry" id="sounds_entry">
<object class="GtkFileChooserButton" id="filechooser">
<property name="visible">True</property>
<property name="can_focus">True</property>
<signal name="changed" handler="on_sounds_entry_changed" swapped="no"/>
<property name="can_focus">False</property>
<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>
<packing>
<property name="expand">False</property>
<property name="expand">True</property>
<property name="fill">True</property>
<property name="position">0</property>
</packing>
</child>
<child>
<object class="GtkButton" id="browse_for_sounds_button">
<property name="label">...</property>
<object class="GtkButton">
<property name="visible">True</property>
<property name="can_focus">True</property>
<property name="receives_default">False</property>
<property name="use_underline">True</property>
<signal name="clicked" handler="on_browse_for_sounds_button_clicked" swapped="no"/>
<property name="receives_default">True</property>
<property name="tooltip_text" translatable="yes">Clear File</property>
<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>
<packing>
<property name="expand">False</property>
<property name="fill">False</property>
<property name="fill">True</property>
<property name="position">1</property>
</packing>
</child>
......@@ -76,12 +127,13 @@
<property name="visible">True</property>
<property name="can_focus">True</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>
<object class="GtkImage" id="image1370">
<property name="visible">True</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>
</child>
</object>
......@@ -111,7 +163,7 @@
<property name="can_focus">True</property>
<property name="receives_default">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>
<packing>
<property name="expand">False</property>
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment