aboutsummaryrefslogtreecommitdiff
path: root/voctogui/lib/uibuilder.py
blob: 8776480070bdd1167e39e6c8335f4f8d881bcc40 (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 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