summaryrefslogtreecommitdiff
path: root/silx/gui/qt/_pyside_dynamic.py
diff options
context:
space:
mode:
authorPicca Frédéric-Emmanuel <picca@synchrotron-soleil.fr>2019-05-28 08:16:16 +0200
committerPicca Frédéric-Emmanuel <picca@synchrotron-soleil.fr>2019-05-28 08:16:16 +0200
commita763e5d1b3921b3194f3d4e94ab9de3fbe08bbdd (patch)
tree45d462ed36a5522e9f3b9fde6c4ec4918c2ae8e3 /silx/gui/qt/_pyside_dynamic.py
parentcebdc9244c019224846cb8d2668080fe386a6adc (diff)
New upstream version 0.10.1+dfsg
Diffstat (limited to 'silx/gui/qt/_pyside_dynamic.py')
-rw-r--r--silx/gui/qt/_pyside_dynamic.py54
1 files changed, 47 insertions, 7 deletions
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 <lunaryorn@gmail.com>
# Modifications by Charl Botha <cpbotha@vxlabs.com>
@@ -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 <customwidgets>
+ 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`"""