From a763e5d1b3921b3194f3d4e94ab9de3fbe08bbdd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Picca=20Fr=C3=A9d=C3=A9ric-Emmanuel?= Date: Tue, 28 May 2019 08:16:16 +0200 Subject: New upstream version 0.10.1+dfsg --- silx/gui/qt/_pyside_dynamic.py | 54 ++++++++++++++++++++++++++++++++++++------ 1 file changed, 47 insertions(+), 7 deletions(-) (limited to 'silx/gui/qt') diff --git a/silx/gui/qt/_pyside_dynamic.py b/silx/gui/qt/_pyside_dynamic.py index 13d1a9d..6013416 100644 --- a/silx/gui/qt/_pyside_dynamic.py +++ b/silx/gui/qt/_pyside_dynamic.py @@ -1,6 +1,7 @@ # -*- coding: utf-8 -*- # Taken from: https://gist.github.com/cpbotha/1b42a20c8f3eb9bb7cb8 +# Plus: https://github.com/spyder-ide/qtpy/commit/001a862c401d757feb63025f88dbb4601d353c84 # Copyright (c) 2011 Sebastian Wiesner # Modifications by Charl Botha @@ -83,7 +84,9 @@ class UiLoader(QUiLoader): QUiLoader.__init__(self, baseinstance) self.baseinstance = baseinstance - self.customWidgets = customWidgets + self.customWidgets = {} + self.uifile = None + self.customWidgets.update(customWidgets) def createWidget(self, class_name, parent=None, name=''): """ @@ -107,13 +110,15 @@ class UiLoader(QUiLoader): # this will raise KeyError if the user has not supplied the # relevant class_name in the dictionary, or TypeError, if # customWidgets is None - try: - widget = self.customWidgets[class_name](parent) - - except (TypeError, KeyError): + if class_name not in self.customWidgets: raise Exception('No custom widget ' + class_name + ' found in customWidgets param of' + - 'UiLoader __init__.') + 'UiFile %s.' % self.uifile) + try: + widget = self.customWidgets[class_name](parent) + except Exception: + _logger.error("Fail to instanciate widget %s from file %s", class_name, self.uifile) + raise if self.baseinstance: # set an attribute for the new child widget on the base @@ -126,6 +131,42 @@ class UiLoader(QUiLoader): return widget + def _parse_custom_widgets(self, ui_file): + """ + This function is used to parse a ui file and look for the + section, then automatically load all the custom widget classes. + """ + import importlib + from xml.etree.ElementTree import ElementTree + + # Parse the UI file + etree = ElementTree() + ui = etree.parse(ui_file) + + # Get the customwidgets section + custom_widgets = ui.find('customwidgets') + + if custom_widgets is None: + return + + custom_widget_classes = {} + + for custom_widget in custom_widgets.getchildren(): + + cw_class = custom_widget.find('class').text + cw_header = custom_widget.find('header').text + + module = importlib.import_module(cw_header) + + custom_widget_classes[cw_class] = getattr(module, cw_class) + + self.customWidgets.update(custom_widget_classes) + + def load(self, uifile): + self._parse_custom_widgets(uifile) + self.uifile = uifile + return QUiLoader.load(self, uifile) + if "PySide2.QtCore" in sys.modules: @@ -155,7 +196,6 @@ if "PySide2.QtCore" in sys.modules: orientation = Property("Qt::Orientation", getOrientation, setOrientation) - CUSTOM_WIDGETS = {"Line": _Line} """Default custom widgets for `loadUi`""" -- cgit v1.2.3