
    Je:                     P   d Z ddlZddlZddlmZ ddlmZmZm	Z	m
Z
mZmZ ddlmZ ddlmZmZmZmZmZmZmZ ddlmZ ddlmZ ddlmZ dd	lmZ dd
lm Z   G d de      Z! G d de!      Z" G d de!      Z# G d de#      Z$ G d de$      Z% G d de$      Z&d Z' G d de$      Z(y)zCustomized combobox widgets.    N)QEventQtQTimerQUrlSignalQSize)QFont)QAction	QComboBox
QCompleter	QLineEditQSizePolicyQToolButtonQToolTip)_)to_text_string)ima)APP_STYLESHEET)IconLineEditc                        e Zd ZdZ eee      Z ee      Z eee      Z		 d Z
d Zd Z fdZd Zd Zd Zd	 Zd
 Zd Zd Z xZS )BaseComboBoxzEditable combo box base classc                     t        j                  | |       | j                  d       | j                  t	        |              | j                         | _        y )NT)r   __init__setEditablesetCompleterr   currentTextselected_textselfparents     ;/usr/lib/python3/dist-packages/spyder/widgets/comboboxes.pyr   zBaseComboBox.__init__3   sD    4(*T*+!--/    c                    t        |t              sy|j                         t        j                  k(  r=|j	                         t
        j                  k(  r| j                  j                  d       yt        j                  | |      S )zKQt Override.

        Filter tab keys and process double tab keys.
        T)
isinstancer   typeKeyPresskeyr   Key_Tabsig_tab_pressedemitr   eventr   r+   s     r!   r+   zBaseComboBox.event:   s\     %(JJLFOO+%))+2K  %%d+tU++r"   c                    |j                         t        j                  k(  s!|j                         t        j                  k(  r2| j	                         r!| j                          | j                          yy|j                         t        j                  k(  r,| j                  | j                         | j                          yt        j                  | |       y)z7Qt Override.

        Handle key press events.
        N)r'   r   
Key_Return	Key_Enteradd_current_text_if_validselectedhide_completer
Key_Escapeset_current_textr   r   keyPressEventr,   s     r!   r5   zBaseComboBox.keyPressEventJ   s    
 99;"--'599;",,+F--/##% 0 YY[BMM)!!$"4"45!##D%0r"   c                     t         |   |       | j                  j                  |j	                         |j                                y)zO
        Emit a resize signal for widgets that need to adapt its size.
        N)superresizeEventsig_resizedr*   sizeoldSize)r   r+   	__class__s     r!   r8   zBaseComboBox.resizeEventY   s4     	E"ejjlEMMO<r"   c                      y)z`
        Return True if string is valid
        Return None if validation can't be done
        N r   qstrs     r!   is_validzBaseComboBox.is_valida   s    
 	r"   c                 <    | j                   j                  dd       y9Action to be executed when a valid item has been selectedTN)validr*   r   s    r!   r1   zBaseComboBox.selectedh   s    

d#r"   c                    | j                  |      }|dk7  r(| j                  |       | j                  |      }|dk7  r(| j                  d|       | j                  d      }|dk7  rL| j                  |       | j                  dd       |dk7  r| j                  d       y| j                  d       y| j                  d       y)zVAdd text to combo box: add a new item if text is not found in
        combo box items.r       N)findText
removeItem
insertItemsetCurrentIndex)r   textindexs      r!   add_textzBaseComboBox.add_textl   s     d#rkOOE"MM$'E rk 	4 b!B;OOE"OOAr"rz$$Q'$$Q'  #r"   c                 T    | j                         j                  t        |             y)z0Sets the text of the QLineEdit of the QComboBox.N)lineEditsetTextr   r   rO   s     r!   r4   zBaseComboBox.set_current_text   s    t 45r"   c                 F    | j                         }| j                  |       y)z9Add current text to combo box history (convenient method)N)r   rQ   rU   s     r!   add_current_textzBaseComboBox.add_current_text   s    !dr"   c                     | j                  | j                               }|s|| j                          y| j                  | j                         y)z.Add current text to combo box history if validNT)rA   r   rW   r4   r   )r   rE   s     r!   r0   z&BaseComboBox.add_current_text_if_valid   sC    d..01EM!!#!!$"4"45r"   c                 :    | j                  t        g |              y)zHides the completion widget.N)r   r   rF   s    r!   r2   zBaseComboBox.hide_completer   s    *R./r"   )__name__
__module____qualname____doc__r   boolrE   r)   r   r9   r   r+   r5   r8   rA   r1   rQ   r4   rW   r0   r2   __classcell__r<   s   @r!   r   r   "   sc    '4ETlO&K	0, 1=$$&6
60r"   r   c                   4     e Zd ZdZ	 	 ddZd Z fdZ xZS )PatternComboBoxzSearch pattern combo boxc                    t         j                  | |       |r| j                  t        j                         | j                  t        j                  t        j                         || j                  |       || j                  |       ||| _        t        |       | _        | j                  j                  t        j                   d             | j                  j                  t#        d             | j                  j$                  j'                  | j)                         j*                         | j)                         j-                  | j                  t.        j0                         | j)                         j3                  t4              d   | _        | j                  j9                  d       | j)                         j:                  j'                  | j<                         | j?                  |        y )N
clear_textz
Clear textr   F) r   r   setSizeAdjustPolicyr   AdjustToMinimumContentsLengthsetSizePolicyr   	ExpandingFixedaddItems
setToolTipIDr
   clear_actionsetIconr   iconr   	triggeredconnectrS   clear	addActionr   TrailingPositionfindChildrenr   clear_button
setVisibletextChanged_on_text_changedinstallEventFilter)r   r    itemstipadjust_to_minimumid_s         r!   r   zPatternComboBox.__init__   sf   dF+$$Y%L%LM;00+2C2CDMM% ?OOC ?DG
 $DM!!#((<"89$$Q|_5##++DMMO,A,AB!!y99	

 !MMO88EaH 	$$U+##++D,A,AB%r"   c                 v    |r| j                   j                  d       y| j                   j                  d       y)z>Actions to take when text has changed on the line edit widget.TFN)rm   rw   rU   s     r!   ry   z PatternComboBox._on_text_changed   s-    ((.((/r"   c                    |j                         t        j                  k(  rS| j                  j	                  | j                         j                         dz
  | j                  j                                t        | %  ||      S )z
        Event filter for this combobox.

        Notes
        -----
        * Reduce space between clear_action and the right border of lineEdit.
           )
r%   r   Paintrv   moverS   widthyr7   eventFilter)r   widgetr+   r<   s      r!   r   zPatternComboBox.eventFilter   sg     ::<6<<'""%%'",d.?.?.A.A.C w"6511r"   )NNTN)rZ   r[   r\   r]   r   ry   r   r_   r`   s   @r!   rb   rb      s!    "/3-1#&J02 2r"   rb   c                   ,    e Zd ZdZd ZddZd ZddZy)	EditableComboBoxz'
    Editable combo box + Validate
    c                 8   t         j                  | |       t               | _        | j	                         | _        | j                  t        j                         | j                  j                  | j                         t        d      t        d      d| _        y )Nz"Press enter to validate this entryzThis entry is incorrectTF)r   r   r	   fontr   r   re   r   rf   editTextChangedrq   validater   tipsr   s     r!   r   zEditableComboBox.__init__   sw    dF+G	!--/ 	  !H!HI 	$$T]]3AB78:	r"   c                 l    t        j                  | j                  | j                               ||        y)zShow tipN)r   showTextmapToGlobalpos)r   r|   s     r!   show_tipzEditableComboBox.show_tip   s%    $**488:6TBr"   c                 X    t         j                  |        | j                         | _        yrD   N)r   r1   r   r   rF   s    r!   r1   zEditableComboBox.selected   s!    d#!--/r"   c                    | j                   |k(  r"|dk7  r| j                  j                  dd       y| j                  |      }|r<|r| j                  j                  dd       y| j                  j                  dd       yy)zValidate entered pathrI   TNF)r   rE   r*   rA   )r   r@   editingrE   s       r!   r   zEditableComboBox.validate   sh    %$"*JJOOD$'d#

e,

u-	 r"   N)rI   )T)rZ   r[   r\   r]   r   r   r1   r   r>   r"   r!   r   r      s    :C0
.r"   r   c                   v    e Zd ZdZ ee      Zdddej                  fdZ	d Z
d Zd Zd Zdd	Zd
 Zd Zd Zy)PathComboBoxz+
    QComboBox handling path locations
    FNc                 d   t         j                  | |       t        | ||      }|r | j                  t        j
                         nM| j                  t        j                         | j                  t        j                  t        j                         t        d      dd| _        | j                  |       | j                  j                  | j                          | j"                  j                  | j$                         | j&                  j                  |j(                         ||| _        y y )N)
elide_textellipsis_placez!Press enter to validate this pathrI   r   )r   r   r   re   r   AdjustToContentsrf   rg   r   rh   ri   r   r   setLineEdithighlightedrq   add_tooltip_to_highlighted_itemr)   tab_completerE   update_statusrl   )r   r    adjust_to_contentsr~   r   r   lineedits          r!   r   zPathComboBox.__init__  s    !!$/  /=? $$Y%?%?@$$Y%L%LM{44k6G6GH@A 	" 	  !E!EF$$T%6%67

8112?DG r"   c                 x    t        | j                         dd      }|r |        t        j                  | |       y)z;Handle focus in event restoring to display the status icon.show_status_iconN)getattrrS   r   focusInEvent)r   r+   show_statuss      r!   r   zPathComboBox.focusInEvent   s/    dmmo/A4HMtU+r"   c                       j                         s* j                         t        j                  d fd       t	         j                         dd      }|r |        t        j                   |       y)z>Handle focus out event restoring the last valid selected path.2   c                  :     j                  j                        S N)rT   r   )r   r   s   r!   <lambda>z,PathComboBox.focusOutEvent.<locals>.<lambda>-  s    (*:*:4;M;M*N r"   hide_status_iconN)rA   rS   r   
singleShotr   r   focusOutEvent)r   r+   hide_statusr   s   `  @r!   r   zPathComboBox.focusOutEvent'  sW     }}}}Hb"NOdmmo/A4HMe,r"   c                 f   t        | j                               }t        j                  |dz         }t        |D cg c]  }t	        j
                  |      s| c}      }t        ||       }t        t              }|j                         j                  |       | j                  |       |S c c}w z"Find available completion options.*)r   r   globsortedospisdirr   strr   popupsetStyleSheetr   r   rO   optsopt	completerqsss         r!   _complete_optionszPathComboBox._complete_options5  s    d..01yy$d=sciins=>tT*	.!'',)$ >s   B.B.c                     | j                         }t        |      dk(  r6| j                  |d   t        j                  z          | j                          y| j                         j                          yzU
        If there is a single option available one tab completes the option.
        rJ   r   N)r   lenr4   ossepr2   r   complete)r   r   s     r!   r   zPathComboBox.tab_completeB  sW     %%'t9>!!$q'BFF"23!NN%%'r"   c                 b    || j                         }t        j                  t        |            S zReturn True if string is valid)r   r   r   r   r?   s     r!   rA   zPathComboBox.is_validM  s)    <##%Dyy-..r"   c                     | j                         | _        | j                  j                  dd       | j                  j                  | j                         yrC   )r   r   rE   r*   open_dirrF   s    r!   r1   zPathComboBox.selectedS  s=    !--/

d#4--.r"   c                     | j                         }t        j                  |      r|r|d   t        j                  k(  r|dd }| j                  |       y)z
        Add current text to combo box history (convenient method).
        If path ends in os separator ("" windows, "/" unix) remove it.
        rH   N)r   r   r   r   r   rQ   rU   s     r!   rW   zPathComboBox.add_current_textY  sF    
 !99T?tBx266!CRydr"   c                 d    | j                  || j                  |      t        j                         y)zt
        Add a tooltip showing the full path of the currently highlighted item
        of the PathComboBox.
        N)setItemDataitemTextr   ToolTipRole)r   rP   s     r!   r   z,PathComboBox.add_tooltip_to_highlighted_itemd  s#    
 	e 4bnnEr"   r   )rZ   r[   r\   r]   r   r   r   r   	ElideLeftr   r   r   r   r   rA   r1   rW   r   r>   r"   r!   r   r      sN     c{H27T!",,8,-	(//	Fr"   r   c                        e Zd ZdZddZddZy)UrlComboBoxz!
    QComboBox handling urls
    Nc                     t         j                  | ||       t        |       }| j                  |       | j                  j                  | j                         ||| _        y y r   )r   r   r   r   r   
disconnectr   rl   )r   r    r   r~   	line_edits        r!   r   zUrlComboBox.__init__p  sU    dF,>?dO	#''6?DG r"   c                 X    || j                         }t        |      j                         S r   )r   r   isValidr?   s     r!   rA   zUrlComboBox.is_validy  s(    <##%DDz!!##r"   FNr   )rZ   r[   r\   r]   r   rA   r>   r"   r!   r   r   l  s    $r"   r   c                   0    e Zd ZdZ	 	 ddZddZd Zd Zy)	FileComboBoxz'
    QComboBox handling File paths
    Nc                 L   t         j                  | ||       |rt        |       }| j                  |       |r | j	                  t
        j                         y | j	                  t
        j                         | j                  t        j                  t        j                         y r   )r   r   r   r   re   r   r   AdjustToContentsOnFirstShowrg   r   rh   ri   )r   r    r   default_line_editr   s        r!   r   zFileComboBox.__init__  sx    dF,>?!$IY' $$Y%?%?@$$Y%J%JK{44k6G6GHr"   c                     || j                         }t        j                  t        |            xs t        j                  t        |            }|S )zReturn True if string is valid.)r   r   isfiler   r   )r   r@   rE   s      r!   rA   zFileComboBox.is_valid  sF    <##%DN401 1>$/0 	r"   c                    | j                         }t        |      dk(  rO|d   }t        j                  |      r|t        j
                  z   }| j                  |       | j                          y| j                         j                          yr   )
r   r   r   r   r   r   r4   r2   r   r   )r   r   rO   s      r!   r   zFileComboBox.tab_complete  sk     %%'t9>7Dyybff}!!$'!NN%%'r"   c                    t        | j                               }t        j                  |dz         }t        |D cg c].  }t	        j
                  |      st	        j                  |      r|0 c}      }t        ||       }t        t              }|j                         j                  |       | j                  |       |S c c}w r   )r   r   r   r   r   r   r   r   r   r   r   r   r   r   s         r!   r   zFileComboBox._complete_options  s    d..01yy$d =s))C.CJJsO  = > tT*	.!'',)$=s   3C)NFFr   )rZ   r[   r\   r]   r   rA   r   r   r>   r"   r!   r   r     s$     8=#(I(r"   r   c                     t        j                  |       xr t        j                  |       d   dv }t        j                  |       xr) t        j                  t        j                  | d            }|xs |S )z.Return True if path is a Python module/packagerJ   )z.pyz.pywz__init__.py)r   r   splitextr   join)path	is_module
is_packages      r!   is_module_or_packager     sY    

4 MS\\$%7%:o%MI4NSZZ}0M%NJ"
"r"   c                   &    e Zd ZdZddZddZd Zy)PythonModulesComboBoxz
    QComboBox handling Python modules or packages path
    (i.e. .py, .pyw files *and* directories containing __init__.py)
    Nc                 F    t         j                  | ||       ||| _        y y r   )r   r   rl   )r   r    r   r~   s       r!   r   zPythonModulesComboBox.__init__  s&    dF,>??DG r"   c                 N    || j                         }t        t        |            S r   )r   r   r   r?   s     r!   rA   zPythonModulesComboBox.is_valid  s%    <##%D#N4$899r"   c                     t         j                  |        | j                  j                  | j	                                yr   )r   r1   r   r*   r   rF   s    r!   r1   zPythonModulesComboBox.selected  s+    !!$'4++-.r"   r   r   )rZ   r[   r\   r]   r   rA   r1   r>   r"   r!   r   r     s    
:/r"   r   ))r]   r   r   os.pathr   r   qtpy.QtCorer   r   r   r   r   r   
qtpy.QtGuir	   qtpy.QtWidgetsr
   r   r   r   r   r   r   spyder.config.baser   spyder.py3compatr   spyder.utils.icon_managerr   spyder.utils.stylesheetr   spyder.widgets.helperwidgetsr   r   rb   r   r   r   r   r   r   r>   r"   r!   <module>r      s    #  	  @ ? @ @ @ ! + ) 2 5q09 q0h<2l <2~&.| &.RkF# kF\$, $(5< 5p#/L /r"   