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