mouse_handlers.py 780 B

1234567891011121314151617181920212223242526272829
  1. from __future__ import unicode_literals
  2. from itertools import product
  3. from collections import defaultdict
  4. __all__ = (
  5. 'MouseHandlers',
  6. )
  7. class MouseHandlers(object):
  8. """
  9. Two dimentional raster of callbacks for mouse events.
  10. """
  11. def __init__(self):
  12. def dummy_callback(cli, mouse_event):
  13. """
  14. :param mouse_event: `MouseEvent` instance.
  15. """
  16. # Map (x,y) tuples to handlers.
  17. self.mouse_handlers = defaultdict(lambda: dummy_callback)
  18. def set_mouse_handler_for_range(self, x_min, x_max, y_min, y_max, handler=None):
  19. """
  20. Set mouse handler for a region.
  21. """
  22. for x, y in product(range(x_min, x_max), range(y_min, y_max)):
  23. self.mouse_handlers[x,y] = handler