
    "eJ                     h    d Z ddlZddlZddlZddlmZmZ ddlmZm	Z	m
Z
  G d d      Zdadad	dZy)
z:
Qt4's inputhook support function

Author: Christian Boos
    N)QtCoreQtGui)allow_CTRL_Cignore_CTRL_Cstdin_readyc                   &    e Zd ZdZed        Zd Zy)InteractiveShellNc                 J    | j                    |        | _         | j                   S N)	_instance)clss    </usr/lib/python3/dist-packages/pydev_ipython/inputhookqt4.pyinstancezInteractiveShell.instance    s    == ECM}}    c                      y r    )selfargskwargss      r   set_hookzInteractiveShell.set_hook%   s     	r   )__name__
__module____qualname__r   classmethodr   r   r   r   r   r	   r	      s    I r   r	   Fc                 "    |6t         j                  j                         }|t        j                  dg      }t
        j                         }t        |d      r||j                  fS  fd fd}|_        |j                  d|       |fS )aB  Create an input hook for running the Qt4 application event loop.

    Parameters
    ----------
    mgr : an InputHookManager

    app : Qt Application, optional.
        Running application to use.  If not given, we probe Qt for an
        existing application object, and create a new one if none is found.

    Returns
    -------
    A pair consisting of a Qt Application (either the one given or the
    one found or created) and a inputhook.

    Notes
    -----
    We use a custom input hook instead of PyQt4's default one, as it
    interacts better with the readline packages (issue #481).

    The inputhook function works in tandem with a 'pre_prompt_hook'
    which automatically restores the hook as an inputhook in case the
    latter has been temporarily disabled after having intercepted a
    KeyboardInterrupt.
     _inputhook_qt4c                     	 t                t        j                  j                         } | s	 t                y| j	                  t        j
                  j                  d       t               st        j                         }t        j
                         }|j                  j                  |j                         t               s<|j                  d       |j                          |j                          t               s<t                y# t        $ r t!                daj%                          t&        j(                  dk(  ret'        j*                         }t,        sVt/        j0                  dt&        j2                  |t4        j6                  g      at,        j                          nt9        d       Y  t!                dd	lm}  |        t9        d
       j%                          Y xY w# t                w xY w)a  PyOS_InputHook python hook for Qt4.

        Process pending Qt events and if there's no pending keyboard
        input, spend a short slice of time (50ms) running the Qt event
        loop.

        As a Python ctypes callback can't raise an exception, we catch
        the KeyboardInterrupt and temporarily deactivate the hook,
        which will let a *second* CTRL+C be processed normally and go
        back to a clean prompt line.
        r   i,  2   Tposixg{Gz?)r   z0
KeyboardInterrupt - Ctrl-C again for new prompt)	print_excz0Got exception from inputhook_qt4, unregistering.)r   r   QCoreApplicationr   processEvents
QEventLoop	AllEventsr   QTimertimeoutconnectquitstartexec_stopKeyboardInterruptr   
got_kbdintclear_inputhookosnamegetpidsigint_timer	threadingTimerkillsignalSIGINTprint	tracebackr!   )apptimer
event_looppidr!   mgrs        r   inputhook_qt4z+create_inputhook_qt4.<locals>.inputhook_qt4^   sd   D	N))224C@ N f//993?=, #..0
%%joo6%-KKO$$&JJL &-J NE ! 	K OJ! ww'!iik##,??3/2FMM.B$EL &&(IJ	"O+KDE!Ns*   *D CD B(G*3G- 53G*(G- -G9c                 p    t         rt         j                          da t        rj                         day)z'pre_prompt_hook' used to restore the Qt4 input hook

        (in case the latter was temporarily deactivated after a
        CTRL+C)
        NF)r3   cancelr.   set_inputhook)ishellr@   r?   s    r   preprompthook_qt4z/create_inputhook_qt4.<locals>.preprompthook_qt4   s/     !Lm,
r   pre_prompt_hook)	r   r"   r   r   QApplicationr	   hasattrr   r   )r?   r;   iprE   r@   s   `   @r   create_inputhook_qt4rJ   6   s    6 {%%..0;$$cU+C 
	"	"	$Br#$B%%%%
Qf  &BKK!#45r   r   )__doc__r0   r7   r4   pydev_ipython.qt_for_kernelr   r   pydev_ipython.inputhookr   r   r   r	   r.   r3   rJ   r   r   r   <module>rN      s<   " 
   6 L L
 
" 
Nr   