aboutsummaryrefslogtreecommitdiff
path: root/voctogui/lib/uibuilder.py
blob: 53078b0b893d79ede41318fdc821dde248e7cf6f (plain)
  1. import gi
  2. import logging
  3. from gi.repository import Gtk, Gst
  4. class UiBuilder(object):
  5. def __init__(self, uifile):
  6. if not hasattr(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(
  15. 'could find required widget "%s" by ID inside the parent %s',
  16. name,
  17. str(widget)
  18. )
  19. raise Exception('Widget not found in parent')
  20. return widget
  21. def _find_widget_recursive(self, widget, name):
  22. if Gtk.Buildable.get_name(widget) == name:
  23. return widget
  24. if hasattr(widget, 'get_children'):
  25. for child in widget.get_children():
  26. widget = self._find_widget_recursive(child, name)
  27. if widget:
  28. return widget
  29. return None
  30. def get_check_widget(self, widget_id, clone=False):
  31. if clone:
  32. builder = Gtk.Builder()
  33. builder.add_from_file(self.uifile)
  34. else:
  35. builder = self.builder
  36. self.log.debug('loading widget "%s" from the .ui-File', widget_id)
  37. widget = builder.get_object(widget_id)
  38. if not widget:
  39. self.log.error(
  40. 'could not load required widget "%s" from the .ui-File',
  41. widget_id
  42. )
  43. raise Exception('Widget not found in .ui-File')
  44. return widget