summaryrefslogtreecommitdiff
path: root/voctogui/lib/uibuilder.py
blob: 8ce01ab0f0f79fb9cb22cc2c80eb7a1cce2f4233 (plain)
  1. #!/usr/bin/python3
  2. import gi, logging
  3. from gi.repository import Gtk, Gst
  4. class UiBuilder(object):
  5. def __init__(self, uifile):
  6. if not self.log:
  7. self.log = logging.getLogger('UiBuilder')
  8. self.uifile = uifile
  9. self.builder = Gtk.Builder()
  10. self.builder.add_from_file(self.uifile)
  11. def find_widget_recursive(self, widget, name):
  12. widget = self._find_widget_recursive(widget, name)
  13. if not widget:
  14. self.log.error('could find required widget "%s" by name (not ID!) inside the parent %s', name, str(widget))
  15. raise Exception('Widget not found in parent')
  16. return widget
  17. def _find_widget_recursive(self, widget, name):
  18. if widget.get_name() == name:
  19. return widget
  20. if hasattr(widget, 'get_children'):
  21. for child in widget.get_children():
  22. widget = self._find_widget_recursive(child, name)
  23. if widget:
  24. return widget
  25. return None
  26. def get_check_widget(self, widget_id, clone=False):
  27. if clone:
  28. builder = Gtk.Builder()
  29. builder.add_from_file(self.uifile)
  30. else:
  31. builder = self.builder
  32. self.log.debug('loading widget "%s" from the .ui-File', widget_id)
  33. widget = builder.get_object(widget_id)
  34. if not widget:
  35. self.log.error('could not load required widget "%s" from the .ui-File', widget_id)
  36. raise Exception('Widget not found in .ui-File')
  37. return widget