B
    W\$                 @   s   d dl Z ddlmZmZmZmZ ddlmZ er:d dlT nperHd dl	T nbdgZ
erld dlmZ d dlmZ nerd dlmZ d dlmZ G d	d
 d
eZdd ZdddZdS )    N   )PYSIDEPYSIDE2PYQT4PYQT5)	QComboBox)*loadUi)QMetaObject)	QUiLoaderc               @   s$   e Zd ZdZdddZd	ddZdS )
UiLoadera  
        Subclass of :class:`~PySide.QtUiTools.QUiLoader` to create the user
        interface in a base instance.

        Unlike :class:`~PySide.QtUiTools.QUiLoader` itself this class does not
        create a new instance of the top-level widget, but creates the user
        interface in an existing instance of the top-level class if needed.

        This mimics the behaviour of :func:`PyQt4.uic.loadUi`.
        Nc             C   s,   t | | || _|dkr"i | _n|| _dS )ah  
            Create a loader for the given ``baseinstance``.

            The user interface is created in ``baseinstance``, which must be an
            instance of the top-level class in the user interface to load, or a
            subclass thereof.

            ``customWidgets`` is a dictionary mapping from class name to class
            object for custom widgets. Usually, this should be done by calling
            registerCustomWidget on the QUiLoader, but with PySide 1.1.2 on
            Ubuntu 12.04 x86_64 this causes a segfault.

            ``parent`` is the parent object of this loader.
            N)r   __init__baseinstancecustomWidgets)selfr   r    r   'lib/python3.7/site-packages/qtpy/uic.pyr   d   s
    zUiLoader.__init__ c             C   s   |dkr| j r| j S ||  ks(|dkr:t| |||}n8y| j| |}W n$ tk
rp   td| d Y nX | j rt| j || |S dS )z
            Function that is called for each widget defined in ui file,
            overridden here to populate baseinstance instead.
            NZLinezNo custom widget z found in customWidgets)r   ZavailableWidgetsr   createWidgetr   KeyError	Exceptionsetattr)r   
class_nameparentnamewidgetr   r   r   r   }   s    zUiLoader.createWidget)N)Nr   )__name__
__module____qualname____doc__r   r   r   r   r   r   r   X   s   

r   c             C   s   ddl }ddl}ddlm} | }|| }|d}|dkrBi S i }x@| D ]4}|dj}	|dj}
||
}t	||	||	< qPW |S )z
        This function is used to parse a ui file and look for the <customwidgets>
        section, then automatically load all the custom widget classes.
        r   N)ElementTreeZcustomwidgetsclassheader)
sys	importlibZxml.etree.ElementTreer    parsefindZgetchildrentextimport_modulegetattr)Zui_filer#   r$   r    ZetreeZuiZcustom_widgetsZcustom_widget_classesZcustom_widgetZcw_classZ	cw_headermoduler   r   r   _get_custom_widgets   s    


r+   c             C   s<   t | }t||}|dk	r$|| || }t| |S )aj  
        Dynamically load a user interface from the given ``uifile``.

        ``uifile`` is a string containing a file name of the UI file to load.

        If ``baseinstance`` is ``None``, the a new instance of the top-level
        widget will be created. Otherwise, the user interface is created within
        the given ``baseinstance``. In this case ``baseinstance`` must be an
        instance of the top-level widget class in the UI file to load, or a
        subclass thereof. In other words, if you've created a ``QMainWindow``
        interface in the designer, ``baseinstance`` must be a ``QMainWindow``
        or a subclass thereof, too. You cannot load a ``QMainWindow`` UI file
        with a plain :class:`~PySide.QtGui.QWidget` as ``baseinstance``.

        :method:`~PySide.QtCore.QMetaObject.connectSlotsByName()` is called on
        the created user interface, so you can implemented your slots according
        to its conventions in your widget class.

        Return ``baseinstance``, if ``baseinstance`` is not ``None``. Otherwise
        return the newly created instance of the user interface.
        N)r+   r   ZsetWorkingDirectoryloadr
   ZconnectSlotsByName)Zuifiler   ZworkingDirectoryr   loaderr   r   r   r   r	      s    



)NN)osr   r   r   r   r   Z	QtWidgetsr   Z	PyQt5.uicZ	PyQt4.uic__all__ZPySide.QtCorer
   ZPySide.QtUiToolsr   ZPySide2.QtCoreZPySide2.QtUiToolsr   r+   r	   r   r   r   r   <module>   s    

@J!