a
    §ˆcÇ  ã                   @   s„   d Z ddlmZ ddlZddlZddlmZ ddlmZ edd„ ƒZ	edd	d
„ƒZ
edd„ ƒZeddd„ƒZddd„Zddd„ZdS )z Tools for testing. é    )ÚcontextmanagerN)ÚQTimer)Úget_app_qt4c                  c   s    t ƒ } dV  |  ¡  |  ¡  dS )z? Post and process the Qt events at the exit of the code block. N)r   ZsendPostedEventsZprocessEvents)Úapp© r   ú]/home/yuhw/wc/wire-cell-python/wcpy/lib/python3.9/site-packages/pyface/ui/qt4/util/testing.pyÚ
event_loop   s    r   ç      ð?c                 c   sx   t ƒ }tƒ }| d¡ | t|d ƒ¡ |j |j¡ | j |j¡ dV  | 	¡  | 
¡  | ¡ std}t| | ¡ƒ‚dS )z` Context manager that executes the event loop so that we can safely
    delete a Qt widget.
    Tiè  Nz-Could not destroy widget before timeout: {!r})r   r   ZsetSingleShotZsetIntervalÚroundÚtimeoutÚconnectÚquitZ	destroyedÚstartÚexec_ZisActiveÚAssertionErrorÚformat)Úwidgetr   r   ZtimerÚmsgr   r   r   Údelete_widget"   s    
r   c                 c   s>   | du r4t tjdƒ}z|V  W | ¡  q:| ¡  0 n| V  dS )z? If 'stream' is None, provide a temporary handle to /dev/null. NÚw)ÚopenÚosÚdevnullÚclose)ÚstreamÚoutr   r   r   Ú_convert_none_to_null_handle;   s    r   c              
   c   s¢   t | ƒ�†} t |ƒ�^}tj}| ¡  tj}| ¡  z"| t_|t_dV  W |t_|t_n|t_|t_0 W d  ƒ n1 sv0    Y  W d  ƒ n1 s”0    Y  dS )z= Re-direct the stderr and stdout streams while in the block. N)r   ÚsysÚstderrÚflushÚstdout)r   ÚerrZ_old_stderrZ_old_stdoutr   r   r   Úsilence_outputI   s    

ÿr"   c                 C   sP   |d }|dkrt ƒ  t d| | ƒ |  ¡ D ]}t||d� q,|dkrLt ƒ  dS )a-   Debugging helper to print out the Qt widget tree starting at a
    particular `widget`.

    Parameters
    ----------
    widget : QObject
        The root widget in the tree to print.
    level : int
        The current level in the tree. Used internally for displaying the
        tree level.
    é   r   ú )ÚlevelN)ÚprintÚchildrenÚprint_qt_widget_tree)r   r%   Úchildr   r   r   r(   ^   s    r(   c                 C   sR   |du rdd„ }t | |ƒr&|| ƒr&| S |  ¡ D ]}t|||d�}|r.|  S q.dS )aa  Recursively walks the Qt widget tree from Qt widget `start` until it
    finds a widget of type `type_` (a QWidget subclass) that
    satisfies the provided `test` method.

    Parameters
    ----------
    start : QWidget
        The widget from which to start walking the tree
    type_ : type
        A subclass of QWidget to use for an initial type filter while
        walking the tree
    test : callable
        A filter function that takes one argument (the current widget being
        evaluated) and returns either True or False to determine if the
        widget matches the required criteria.
    Nc                 S   s   dS )NTr   )r   r   r   r   Ú<lambda>†   ó    z find_qt_widget.<locals>.<lambda>)Útest)Ú
isinstancer'   Úfind_qt_widget)r   Útype_r,   r)   r   r   r   r   r.   t   s    

r.   )r	   )NN)r   )N)Ú__doc__Ú
contextlibr   r   r   Zpyface.qt.QtCorer   Zpyface.util.guisupportr   r   r   r   r"   r(   r.   r   r   r   r   Ú<module>   s   


