aboutsummaryrefslogtreecommitdiff
path: root/voctogui/lib/toolbar/composition.py
blob: 3cf5797c413337062f36f19c72dd59a332583a81 (plain)
  1. import logging
  2. from gi.repository import Gtk
  3. import lib.connection as Connection
  4. class CompositionToolbarController(object):
  5. """Manages Accelerators and Clicks on the Composition Toolbar-Buttons"""
  6. def __init__(self, drawing_area, win, uibuilder):
  7. self.log = logging.getLogger('CompositionToolbarController')
  8. accelerators = Gtk.AccelGroup()
  9. win.add_accel_group(accelerators)
  10. composites = [
  11. 'fullscreen',
  12. 'picture_in_picture',
  13. 'side_by_side_equal',
  14. 'side_by_side_preview',
  15. 'matrix_two_by_two'
  16. ]
  17. self.composite_btns = {}
  18. self.current_composition = None
  19. for idx, name in enumerate(composites):
  20. key, mod = Gtk.accelerator_parse('F%u' % (idx + 1))
  21. btn = uibuilder.find_widget_recursive(
  22. drawing_area,
  23. 'composite-' + name.replace('_', '-')
  24. )
  25. btn.set_name(name)
  26. tooltip = Gtk.accelerator_get_label(key, mod)
  27. btn.set_tooltip_text(tooltip)
  28. # Thanks to http://stackoverflow.com/a/19739855/1659732
  29. btn.get_child().add_accelerator('clicked', accelerators,
  30. key, mod, Gtk.AccelFlags.VISIBLE)
  31. btn.connect('toggled', self.on_btn_toggled)
  32. self.composite_btns[name] = btn
  33. # connect event-handler and request initial state
  34. Connection.on('composite_mode', self.on_composite_mode)
  35. Connection.send('get_composite_mode')
  36. def on_btn_toggled(self, btn):
  37. if not btn.get_active():
  38. return
  39. btn_name = btn.get_name()
  40. if self.current_composition == btn_name:
  41. self.log.info('composition-mode already active: %s', btn_name)
  42. return
  43. self.log.info('composition-mode activated: %s', btn_name)
  44. Connection.send('set_composite_mode', btn_name)
  45. def on_composite_mode(self, mode):
  46. self.log.info('on_composite_mode callback w/ mode %s', mode)
  47. self.current_composition = mode
  48. self.composite_btns[mode].set_active(True)