3
7ßL]è  ã               @   s�   d Z ddlm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. é    )Úprint_function)ÚcontextmanagerN)ÚQTimer)Úget_app_qt4c              c   s    t ƒ } dV  | jƒ  | jƒ  dS )z? Post and process the Qt events at the exit of the code block. N)r   ZsendPostedEventsZprocessEvents)Úapp© r   ú>/tmp/pip-install-5sebvq2j/pyface/pyface/ui/qt4/util/testing.pyÚ
event_loop   s    r	   ç      ð?c             c   st   t ƒ }tƒ }|jdƒ |j|d ƒ |jj|jƒ | jj|jƒ dV  |jƒ  |j	ƒ  |j
ƒ spd}t|j| ƒƒ‚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ÚtimeoutÚconnectÚquitZ	destroyedÚstartÚexec_ZisActiveÚAssertionErrorÚformat)Úwidgetr   r   ZtimerÚmsgr   r   r   Údelete_widget!   s    
r   c          
   c   s6   | dkr,t tjdƒ}z
|V  W d|jƒ  X 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   sn   t | ƒ�\} t |ƒ�H}tj}|jƒ  tj}|jƒ  z| t_|t_dV  W d|t_|t_X W dQ R X W dQ R X 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_outputH   s    


r"   c             C   sT   |d }|dkrt ƒ  t d| | ƒ x| jƒ D ]}t||d� q.W |dkrPt ƒ  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krdd„ }t | |ƒr&|| ƒr&| S x&| jƒ D ]}t|||d�}|r0|S q0W 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>…   s    z find_qt_widget.<locals>.<lambda>)Útest)Ú
isinstancer'   Úfind_qt_widget)r   Útype_r+   r)   r   r   r   r   r-   s   s    
r-   )r
   )NN)r   )N)Ú__doc__Ú
__future__r   Ú
contextlibr   r   r   Zpyface.qt.QtCorer   Zpyface.util.guisupportr   r	   r   r   r"   r(   r-   r   r   r   r   Ú<module>
   s   
