ó
_y]c           @   sp   d  Z  d d l j Z d d l m Z m Z d d d d „ Z d e f d „  ƒ  YZ	 d e j
 f d „  ƒ  YZ d S(	   s'   A navigable completer for the qtconsoleiÿÿÿÿN(   t   QtCoret   QtGuic         C   s1  |  s
 d Sg  } d „  } d „  } g  |  D] } t  t | | ƒ ƒ ^ q) } | r} | \ } }	 d |  | |	 d | | |	 <n  t | d „  | Dƒ ƒ } d }
 d } | rØ d d j d | d	 t |  d
 ƒ ƒ d }
 n  | rd d j d | d	 t |  d
 ƒ ƒ d } n  d |
 d j | ƒ | d } | S(   s#    returnr a string for an html tablet    c         S   s   d |  d S(   Nu   <td>u     </td>(    (   t   text(    (    s8   lib/python2.7/site-packages/qtconsole/completion_html.pyt   <lambda>   R   c         S   s   d |  d S(   Nu   <tr>u   </tr>(    (   R   (    (    s8   lib/python2.7/site-packages/qtconsole/completion_html.pyR      R   u   <td class="inverted">u     </td>c         s   s   |  ] } d  j  | ƒ Vq d S(   u    N(   t   join(   t   .0t   row(    (    s8   lib/python2.7/site-packages/qtconsole/completion_html.pys	   <genexpr>   s    u   <tr>u   <td>u   </td>i    s   </tr>u?   <table class="completion" style="white-space:pre"cellspacing=0>u    u   </table>(   t   listt   mapR   t   len(   t   item_matrixt   selectt   headert   footert	   html_colst   tdst   trsR   t	   tds_itemst   colt   headt   foott   html(    (    s8   lib/python2.7/site-packages/qtconsole/completion_html.pyt   html_tableify   s$    		(!00t   SlidingIntervalc           B   s’   e  Z d  Z d Z d Z d Z d d d d d „ Z e d „  ƒ Z e j	 d „  ƒ Z e d „  ƒ Z
 e d „  ƒ Z e d	 „  ƒ Z e d
 „  ƒ Z RS(   s¶   a bound interval that follows a cursor
    
    internally used to scoll the completion view when the cursor 
    try to go beyond the edges, and show '...' when rows are hidden
    i    i   i   c         C   sH   | |  _  | |  _ d |  _ | |  _ |  j |  j d |  _ | |  _ d S(   s÷   Create a new bounded interval
        
        any value return by this will be bound between maximum and 
        minimum. usual width will be 'width', and sticky_length 
        set when the return  interval should expand to max and min
        i    i   N(   t   _mint   _maxt   _startt   _widtht   _stopt   _sticky_lenght(   t   selft   maximumt   widtht   minimumt   sticky_lenght(    (    s8   lib/python2.7/site-packages/qtconsole/completion_html.pyt   __init__7   s    				c         C   s   |  j  S(   s   current cursor position(   t   _current(   R   (    (    s8   lib/python2.7/site-packages/qtconsole/completion_html.pyt   currentE   s    c         C   sÝ   t  t |  j | ƒ |  j ƒ } | |  _ | |  j k rR | |  _ | |  j |  _ n+ | |  j k  r} | |  _ | |  j |  _ n  t |  j |  j ƒ |  j	 k r« |  j |  _ n  t |  j |  j ƒ |  j	 k rÙ |  j |  _ n  d S(   s   set current cursor positionN(
   t   mint   maxR   R   R%   R   R   R   t   absR   (   R   t   valueR&   (    (    s8   lib/python2.7/site-packages/qtconsole/completion_html.pyR&   J   s    			c         C   s   |  j  S(   s   begiiing of interval to show(   R   (   R   (    (    s8   lib/python2.7/site-packages/qtconsole/completion_html.pyt   start^   s    c         C   s   |  j  S(   s   end of interval to show(   R   (   R   (    (    s8   lib/python2.7/site-packages/qtconsole/completion_html.pyt   stopc   s    c         C   s   |  j  |  j S(   N(   R   R   (   R   (    (    s8   lib/python2.7/site-packages/qtconsole/completion_html.pyR!   h   s    c         C   s   |  j  |  j S(   N(   R&   R+   (   R   (    (    s8   lib/python2.7/site-packages/qtconsole/completion_html.pyt   nthl   s    (   t   __name__t
   __module__t   __doc__R   R   R%   R$   t   propertyR&   t   setterR+   R,   R!   R-   (    (    (    s8   lib/python2.7/site-packages/qtconsole/completion_html.pyR   -   s   t   CompletionHtmlc           B   s¿   e  Z d  Z d Z d Z d Z d Z d Z d Z	 d Z
 d Z d „  Z d „  Z d „  Z d „  Z e d „  ƒ Z d	 „  Z d
 „  Z d „  Z d „  Z d d „ Z e d „ Z d „  Z d „  Z RS(   s7    A widget for tab completion,  navigable by arrow keys i    i   i   c         C   s‚   t  | j t j t j f ƒ s$ t ‚ t t |  ƒ j ƒ  | j |  _	 | |  _
 |  j	 j |  ƒ d |  _ d |  _ |  j |  j	 ƒ d S(   sg    Create a completion widget that is attached to the specified Qt
            text edit widget.
        N(   t
   isinstancet   _controlR   t	   QTextEditt   QPlainTextEditt   AssertionErrort   superR3   R$   t
   _text_editt   _console_widgett   installEventFiltert   Nonet   _sliding_intervalt   _justified_itemst   setFocusProxy(   R   t   console_widget(    (    s8   lib/python2.7/site-packages/qtconsole/completion_html.pyR$   €   s    $			c         C   s7  | |  j  k r| j ƒ  } | t j j k rü| j ƒ  } |  j d k ra | t j j f k ra t	 S|  j d k r£ | t j j f k r£ |  j d |  _ |  j
 ƒ  t S|  j d k rï| t j j t j j f k rÞ |  j ƒ  t S| t j j f k r|  j ƒ  |  j
 ƒ  t S| t j j f k r8|  j ƒ  |  j
 ƒ  t S| t j j f k re|  j ƒ  |  j
 ƒ  t S| t j j f k r’|  j ƒ  |  j
 ƒ  t S| t j j f k r¿|  j ƒ  |  j
 ƒ  t S| t j j f k râ|  j ƒ  t S|  j ƒ  q|  j ƒ  q| t j j k r|  j ƒ  qn  t t |  ƒ j | | ƒ S(   sm    Reimplemented to handle keyboard input and to auto-hide when the
            text edit loses focus.
        i    i   i   (   R:   t   typeR    t   QEventt   KeyPresst   keyt   _consecutive_tabt   Qtt   Key_Tabt   Falset   _update_listt   Truet
   Key_Returnt	   Key_Entert   _complete_currentt   select_rightt   Key_Downt   select_downt	   Key_Rightt   Key_Upt	   select_upt   Key_Leftt   select_leftt
   Key_Escapet   cancel_completiont   FocusOutR9   R3   t   eventFilter(   R   t   objt   eventt   etypeRE   (    (    s8   lib/python2.7/site-packages/qtconsole/completion_html.pyRZ   ‘   sT    $$












c         C   sA   d |  _  d |  _ |  j j ƒ  d |  _ |  j r= d |  _ n  d S(   sÜ   Cancel the completion

        should be called when the completer have to be dismissed

        This reset internal variable, clearing the temporary buffer
        of the console where the completion are shown.
        i    N(   i    i    (   RF   t   _slice_startR;   t   _clear_temporary_buffert   _indexR>   R=   (   R   (    (    s8   lib/python2.7/site-packages/qtconsole/completion_html.pyRX   È   s    				c         C   s—  |  j  \ } } | d } | d } | | k r; | | k sS | | k rf | | k rf |  j d d ƒ n-| d k r~ | d k  s– | d k  r© | d k r© |  j | | ƒ nê | | k rÌ |  j d | d ƒ nÇ | d k  rï |  j | | d ƒ n¤ | | k r|  j | d d ƒ n | d k  r5|  j | d | ƒ n^ d | k rw| | k rwd | k rw| | k rw| | f |  _ n t d | | | | f ƒ ‚ d S(   sÒ  Change the selection index, and make sure it stays in the right range

        A little more complicated than just dooing modulo the number of row columns
        to be sure to cycle through all element.

        horizontaly, the element are maped like this :
        to r <-- a b c d e f --> to g
        to f <-- g h i j k l --> to m
        to l <-- m n o p q r --> to a

        and vertically
        a d g j m p
        b e h k n q
        c f i l o r
        i   i    sa   you'r trying to go where no completion                           have gone before : %d:%d (%d:%d)N(   t   _sizet   _select_indexR`   t   NotImplementedError(   R   R   R   t   nrt   nc(    (    s8   lib/python2.7/site-packages/qtconsole/completion_html.pyRb   â   s&    

000c         C   s-   |  j  |  j } | t |  j ƒ k r) d  S| S(   N(   R^   t
   _slice_lenR
   t   _itemsR=   (   R   t   end(    (    s8   lib/python2.7/site-packages/qtconsole/completion_html.pyt
   _slice_end  s    c         C   s'   |  j  \ } } |  j | d | ƒ d S(   s   move cursor upi   N(   R`   Rb   (   R   t   rt   c(    (    s8   lib/python2.7/site-packages/qtconsole/completion_html.pyRT     s    c         C   s'   |  j  \ } } |  j | d | ƒ d S(   s   move cursor downi   N(   R`   Rb   (   R   Rj   Rk   (    (    s8   lib/python2.7/site-packages/qtconsole/completion_html.pyRQ     s    c         C   s'   |  j  \ } } |  j | | d ƒ d S(   s   move cursor lefti   N(   R`   Rb   (   R   Rj   Rk   (    (    s8   lib/python2.7/site-packages/qtconsole/completion_html.pyRV   !  s    c         C   s'   |  j  \ } } |  j | | d ƒ d S(   s   move cursor righti   N(   R`   Rb   (   R   Rj   Rk   (    (    s8   lib/python2.7/site-packages/qtconsole/completion_html.pyRO   &  s    c   	         s2  | s
 d S| j  t j j d | ƒ| j ƒ  |  _ d |  _ |  j j ƒ  j	 ƒ  } t j
 |  j j ƒ j d ƒ } t t d | | d ƒ ƒ } t j | d d d | ƒ\ } ‰  t t | ƒ d ƒ |  _ | |  _ ˆ  d ˆ  d	 f |  _ | |  _ d |  _ ‡  f d †  } t t | | ƒ ƒ |  _ |  j d t ƒ d S(   se    Shows the completion widget with 'items' at the position specified
            by 'cursor'.
        Nt   ni   t    i
   t   emptyt   displaywidtht   rows_numberst   columns_numbersi    c            s6   g  t  |  ˆ  d ƒ D] \ } } | j | d ƒ ^ q S(   Nt   columns_widthRm   (   t   zipt   ljust(   t   xt   yt   w(   t   ci(    s8   lib/python2.7/site-packages/qtconsole/completion_html.pyR   B  R   t   hilight(   i    i    (   t   movePositionR   t   QTextCursort   Leftt   positiont   _start_positionRF   R:   t   documentt	   textWidtht   QFontMetricsR;   t   fontR!   t   intR(   R   t   compute_item_matrixR   R
   R>   Rg   Ra   t   _old_cursorR`   R   R	   R?   RJ   RI   (	   R   t   cursort   itemst   prefix_lengthR!   t
   char_widthRo   t   items_mt   sjoin(    (   Rx   s8   lib/python2.7/site-packages/qtconsole/completion_html.pyt
   show_items+  s$    				c         C   sî   |  j  d |  j _ d } d } |  j j d k r: d } n  |  j j |  j j k  r[ d } n  |  j |  j j |  j j d !} |  j j	 ƒ  | rª |  j j
 |  j  d f } n d } t | d | d | d | ƒ} |  j j |  j | d t ƒd S(	   sM    update the list of completion and hilight the currently selected completion i    s   ...i   R   R   R   R   N(   R`   R>   R&   R=   R+   R,   R   R?   R;   R_   R-   R   t   _fill_temporary_bufferR…   RK   (   R   Ry   R   R   RŠ   t   selt   strng(    (    s8   lib/python2.7/site-packages/qtconsole/completion_html.pyRJ   I  s     			c         C   sX   |  j  } |  j | d | d } | j ƒ  } | rJ |  j ƒ  j | ƒ n  |  j ƒ  d S(   sB    Perform the completion with the currently selected item.
        i    i   N(   R`   Rg   t   stript   _current_text_cursort
   insertTextRX   (   R   t   it   item(    (    s8   lib/python2.7/site-packages/qtconsole/completion_html.pyRN   e  s    	c         C   sD   |  j  j ƒ  } | j ƒ  |  j k r@ | j |  j t j j ƒ n  | S(   sn    Returns a cursor with text between the start position and the
            current position selected.
        (   R:   t
   textCursorR}   R~   t   setPositionR   R{   t
   KeepAnchor(   R   R†   (    (    s8   lib/python2.7/site-packages/qtconsole/completion_html.pyR‘   o  s
    (    (   i    i    (   i   i   N(   R.   R/   R0   Rg   R`   RF   Ra   R=   R…   R~   R^   Rf   R$   RZ   RX   Rb   R1   Ri   RT   RQ   RV   RO   RŒ   RK   RJ   RN   R‘   (    (    (    s8   lib/python2.7/site-packages/qtconsole/completion_html.pyR3   p   s,   		7		.					
(   R0   t   ipython_genutils.textR   t   qtconsole.qtR    R   R=   R   t   objectR   t   QWidgetR3   (    (    (    s8   lib/python2.7/site-packages/qtconsole/completion_html.pyt   <module>   s
   C