window: connect action handlers using a function decorator

A bit esoteric, but better than maintaining the list of action names.
This commit is contained in:
René Stadler 2011-11-16 20:23:31 +01:00 committed by Stefan Sauer
parent 421b437167
commit 50dd570f3a

View file

@ -43,6 +43,31 @@ from GstDebugViewer.GUI.models import (FilteredLogModel,
LogModelBase,
RangeFilteredLogModel)
def action (func):
func.is_action_handler = True
return func
def iter_actions (manager):
cls = type (manager)
it = cls.__dict__.iteritems ()
for name, member in it:
try:
member.is_action_handler
except AttributeError:
continue
bound_method = getattr (manager, name)
assert name.startswith ("handle_")
assert name.endswith ("_action_activate")
action_name = name[len ("handle_"):-len ("_action_activate")]
action_name = action_name.replace ("_", "-")
yield (action_name, bound_method,)
class LineView (object):
def __init__ (self):
@ -51,9 +76,11 @@ class LineView (object):
def attach (self, window):
for action_name, handler in iter_actions (self):
action = getattr (window.actions, action_name)
action.connect ("activate", handler)
self.clear_action = window.actions.clear_line_view
handler = self.handle_clear_line_view_action_activate
self.clear_action.connect ("activate", handler)
self.line_view = window.widgets.line_view
self.line_view.connect ("row-activated", self.handle_line_view_row_activated)
@ -144,6 +171,7 @@ class LineView (object):
else:
line_model.replace_line (0, line_index)
@action
def handle_clear_line_view_action_activate (self, action):
self.clear ()
@ -300,16 +328,8 @@ class Window (object):
self.clipboard = gtk.Clipboard (self.gtk_window.get_display (),
gtk.gdk.SELECTION_CLIPBOARD)
for action_name in ("new-window", "open-file", "reload-file",
"close-window", "cancel-load",
"hide-before-line", "hide-after-line", "show-hidden-lines",
"edit-copy-line", "edit-copy-message", "set-base-time",
"hide-log-level", "hide-log-category", "hide-log-object",
"hide-filename", "show-about", "enlarge-text", "shrink-text",
"reset-text"):
name = action_name.replace ("-", "_")
action = getattr (self.actions, name)
handler = getattr (self, "handle_%s_action_activate" % (name,))
for action_name, handler in iter_actions (self):
action = getattr (self.actions, action_name)
action.connect ("activate", handler)
self.gtk_window.connect ("delete-event", self.handle_window_delete_event)
@ -479,10 +499,12 @@ class Window (object):
self.actions.close_window.activate ()
@action
def handle_new_window_action_activate (self, action):
self.app.open_window ()
@action
def handle_open_file_action_activate (self, action):
dialog = gtk.FileChooserDialog (None, self.gtk_window,
@ -495,6 +517,7 @@ class Window (object):
self.set_log_file (dialog.get_filename ())
dialog.destroy ()
@action
def handle_reload_file_action_activate (self, action):
if self.log_file is None:
@ -502,6 +525,7 @@ class Window (object):
self.set_log_file (self.log_file.path)
@action
def handle_cancel_load_action_activate (self, action):
self.logger.debug ("cancelling data load")
@ -515,14 +539,17 @@ class Window (object):
gobject.source_remove (self.update_progress_id)
self.update_progress_id = None
@action
def handle_close_window_action_activate (self, action):
self.close ()
@action
def handle_hide_after_line_action_activate (self, action):
self.hide_range (after = True)
@action
def handle_hide_before_line_action_activate (self, action):
self.hide_range (after = False)
@ -562,6 +589,7 @@ class Window (object):
self.pop_view_state ()
self.actions.show_hidden_lines.props.sensitive = True
@action
def handle_show_hidden_lines_action_activate (self, action):
self.logger.info ("restoring model filter to show all lines")
@ -572,6 +600,7 @@ class Window (object):
self.pop_view_state (scroll_to_selection = True)
self.actions.show_hidden_lines.props.sensitive = False
@action
def handle_edit_copy_line_action_activate (self, action):
# TODO: Should probably copy the _exact_ line as taken from the file.
@ -580,19 +609,23 @@ class Window (object):
log_line = Data.LogLine (line)
self.clipboard.set_text (log_line.line_string ())
@action
def handle_edit_copy_message_action_activate (self, action):
col_id = LogModelBase.COL_MESSAGE
self.clipboard.set_text (self.get_active_line ()[col_id])
@action
def handle_enlarge_text_action_activate (self, action):
self.update_zoom_level (1)
@action
def handle_shrink_text_action_activate (self, action):
self.update_zoom_level (-1)
@action
def handle_reset_text_action_activate (self, action):
self.update_zoom_level (-self.zoom_level)
@ -671,35 +704,41 @@ class Window (object):
self.actions.show_hidden_lines.props.sensitive = True
@action
def handle_set_base_time_action_activate (self, action):
row = self.get_active_line ()
self.column_manager.set_base_time (row[LogModelBase.COL_TIME])
@action
def handle_hide_log_level_action_activate (self, action):
row = self.get_active_line ()
debug_level = row[LogModelBase.COL_LEVEL]
self.add_model_filter (DebugLevelFilter (debug_level))
@action
def handle_hide_log_category_action_activate (self, action):
row = self.get_active_line ()
category = row[LogModelBase.COL_CATEGORY]
self.add_model_filter (CategoryFilter (category))
@action
def handle_hide_log_object_action_activate (self, action):
row = self.get_active_line ()
object_ = row[LogModelBase.COL_OBJECT]
self.add_model_filter (ObjectFilter (object_))
@action
def handle_hide_filename_action_activate (self, action):
row = self.get_active_line ()
filename = row[LogModelBase.COL_FILENAME]
self.add_model_filter (FilenameFilter (filename))
@action
def handle_show_about_action_activate (self, action):
from GstDebugViewer import version