Baby Language Lab Scripts
A collection of data processing tools.
 All Classes Namespaces Files Functions Variables Pages
handler_manager.py
Go to the documentation of this file.
1 ## @package utils.handler_manager
2 
3 ## This class is a data structure that stores information about handler methods for UI widgets. Handler methods are called
4 # when a UI widget generates an event, such as a button click. The main benefit of using this class is that it
5 # provides a lookup mechansim.
6 # You can lookup handlers for a particular widget based on a specified type of signal (event). Plain PyGTK does
7 # not provide a straightforward way to do this.
8 # Another benefit is that if we have a centralized data structure for all handlers in a form, we don't have to
9 # pass around lots of handler ids or function pointers when we want to do things like temporarily block a handler for a particular widget.
10 # Instead, you can just call the block_handler() / unblock_handler() methods for a particular widget and signal.
11 class HandlerManager(object):
12  ## Constructor
13  # @param self
14  def __init__(self):
15  self.conn_dict = {}
16 
17  ## Internal method to check if a particular handler (for a particular widget) is already stored in this instance.
18  # @param self
19  # @param obj (UI object) a UI widget object
20  # @param sig (string) a GTK+ event string (like 'clicked' or 'destroy' - see PyGTK docs for a list for your desired widget)
21  # @returns (boolean) True if the obj has a handler for sig that is stored in this instance, False otherwise
22  def _have_handler(self, obj, sig):
23  return obj in self.conn_dict and sig in self.conn_dict[obj]
24 
25  ## Adds a handler (for a particular widget) to this instance.
26  # @param self
27  # @param obj (UI object) the UI widget object for which we are adding a handler
28  # @param sig (string) a GTK+ event string (like 'clicked' or 'destroy' - see PyGTK docs for a list for your desired widget)
29  # @param fcn (function pointer) the function that the signal will invoke
30  # @param data (object=None) optional extra data to pass to the function when it is invoked (see pyGTK signal tutorials to learn more about this)
31  def add_handler(self, obj, sig, fcn, data=None):
32  handler_id = obj.connect(sig, fcn, data) if data else obj.connect(sig, fcn)
33 
34  if not obj in self.conn_dict:
35  self.conn_dict[obj] = {}
36 
37  if not sig in self.conn_dict[obj]:
38  self.conn_dict[obj][sig] = handler_id
39 
40  ## Retrieves a handler id (number used by GTK+ to identify a handler) for a given UI object and signal.
41  # @param self
42  # @param obj (UI object) the UI widget object to lookup
43  # @param sig (string) a GTK+ event string (eg. 'clicked') indicating the signal to look for handlers for
44  # @returns (int) a GTK+ handler id (see GTK+ docs for more info), or None if the (obj, sig) is not stored in this instance
45  def get_handler_id(self, obj, sig):
46  handler_id = None
47  if self._have_handler(obj, sig):
48  handler_id = self.conn_dict[obj][sig]
49 
50  return handler_id
51 
52  ## Temporarily prevents a specified signal from invoking handlers, for a given UI widget.
53  # This does not remove the handler from this instance, nor does it disconnect the handler from the widget object.
54  # The signal will be blocked for this widget until unblock_handler() is called (see below).
55  # @param self
56  # @param obj (UI object) the UI widget on which to block a handler
57  # @param sig (string) a GTK+ event string (eg. 'clicked') indicating the signal to block
58  def block_handler(self, obj, sig):
59  if self._have_handler(obj, sig):
60  obj.handler_block(self.conn_dict[obj][sig])
61 
62  ## Unblocks a signal that has been blocked with block_handler().
63  # @param self
64  # @param obj (UI object) the UI widget on which to unblock signal handlers.
65  # @param sig (string) a GTK+ event string (eg. 'clicked') indicating the signal to unblock.
66  def unblock_handler(self, obj, sig):
67  if self._have_handler(obj, sig):
68  obj.handler_unblock(self.conn_dict[obj][sig])
69 
70  ## Disconnects handler methods from a widget, and removes all record of the handler from this instance.
71  # @param self
72  # @param obj (UI object) the UI object whose handler we are disconnecting
73  # @param sigs (list=None) list of GTK+ event strings (eg. ['clicked']) indicating the signals to remove handlers for. Pass None to remove all handlers for all signals for the widget.
74  def remove_handlers(self, obj, sigs=None):
75  if sigs:
76  for cur_sig in sigs:
77  if self._have_handler(obj, cur_sig):
78  obj.handler_disconnect(self.conn_dict[obj][cur_sig])
79  self.conn_dict[obj].pop(cur_sig)
80  if not len(self.conn_dict[obj]):
81  self.conn_dict.pop(obj)
82 
83  else:
84  if obj in self.conn_dict:
85  for sig in self.conn_dict[obj]:
86  obj.handler_disconnect(self.conn_dict[obj][sig])
87  self.conn_dict.pop(obj)