
    G@d:                     |   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(dS )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        | |           |                     d           |                     t	          |                      |                                 | _        d S )NT)r   __init__setEditablesetCompleterr   currentTextselected_textselfparents     9lib/python3.11/site-packages/spyder/widgets/comboboxes.pyr   zBaseComboBox.__init__3   s_    4(((*T**+++!--//    c                    t          |t                    sdS |                                t          j        k    r>|                                t
          j        k    r| j                            d           dS 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:   sr     %(( 	4JJLLFO++%))++2K2K %%d+++4tU+++r"   c                    |                                 t          j        k    s"|                                 t          j        k    r@|                                 r*|                                  |                                  dS dS |                                 t          j        k    r0|                     | j	                   |                                  dS t          j        | |           dS )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+F--// &##%%%%%& & YY[[BM))!!$"4555!!!!!#D%00000r"   c                     t                                          |           | j                            |                                |                                           dS )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   sL     	E"""ejjllEMMOO<<<<<r"   c                     dS )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                             dd           dS 9Action to be executed when a valid item has been selectedTN)validr*   r   s    r!   r1   zBaseComboBox.selectedh   s    
d#####r"   c                    |                      |          }|dk    r0|                     |           |                      |          }|dk    0|                     d|           |                      d          }|dk    r_|                     |           |                     dd           |dk    r|                     d           dS |                     d           dS |                     d           dS )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##rkkOOE"""MM$''E rkk 	4   b!!B;;OOE"""OOAr"""rzz$$Q'''''$$Q'''''  #####r"   c                 n    |                                                      t          |                     dS )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 455555r"   c                 X    |                                  }|                     |           dS )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                     |                      |                                           }|s||                                  dS |                     | j                   dS )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   s_    d..0011 	6EM!!###4!!$"455555r"   c                 L    |                      t          g |                      dS )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   "   s       ''F4EfTllO&&&K	0 0 0, , , 1 1 1= = = = =  $ $ $$ $ $&6 6 6  
6 6 60 0 0 0 0 0 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 boxNTc                    t                               | |           |r|                     t          j                   |                     t          j        t          j                   || 	                    |           || 
                    |           ||| _        t          |           | _        | j                            t          j        d                     | j        
                    t#          d                     | j        j                            |                                 j                   |                                                     | j        t.          j                   |                                                     t4                    d         | _        | j                            d           |                                 j                            | j                   |                     |            d S )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__   s   dF+++ 	N$$Y%LMMM;0+2CDDDMM%   ?OOC   ?DG
 $DMM!!#(<"8"8999$$Q|__555#++DMMOO,ABBB!!y9	
 	
 	

 !MMOO88EEaH 	$$U+++#++D,ABBB%%%%%r"   c                 v    |r| j                             d           dS | j                             d           dS )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   sD     	0((.....((/////r"   c                 >   |                                 t          j        k    rY| j                            |                                                                 dz
  | 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   s~     ::<<6<''""%%''",d.?.A.A.C.C   ww""65111r"   )NNTN)rZ   r[   r\   r]   r   ry   r   r_   r`   s   @r!   rb   rb      sh        ""/3-1#& #& #& #&J0 0 02 2 2 2 2 2 2 2 2r"   rb   c                   .    e Zd ZdZd Zd	dZd Zd
dZdS )EditableComboBoxz'
    Editable combo box + Validate
    c                 X   t                               | |           t                      | _        |                                 | _        |                     t          j                   | j	        
                    | j                   t          d          t          d          d| _        d S )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__   s    dF+++GG	!--// 	  !HIII 	$$T]333ABB788: :			r"   rI   c                 |    t          j        |                     |                                           ||            dS )zShow tipN)r   showTextmapToGlobalpos)r   r|   s     r!   show_tipzEditableComboBox.show_tip   s3    $**488::66TBBBBBr"   c                 l    t                               |            |                                 | _        dS rD   N)r   r1   r   r   rF   s    r!   r1   zEditableComboBox.selected   s0    d###!--//r"   Tc                    | j         |k    r#|dk    r| j                            dd           dS |                     |          }|r<|r| j                            dd           dS | j                            dd           dS dS )zValidate entered pathrI   TNF)r   rE   r*   rA   )r   r@   editingrE   s       r!   r   zEditableComboBox.validate   s    %%$"**JOOD$'''Fd## 	. .
e,,,,,
u-----		. 	.r"   N)rI   )T)rZ   r[   r\   r]   r   r   r1   r   r>   r"   r!   r   r      sh         : : :C C C C0 0 0
. . . . . .r"   r   c                   t    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dS )PathComboBoxz+
    QComboBox handling path locations
    FNc                 ^   t                               | |           t          | ||          }|r |                     t          j                   nI|                     t          j                   |                     t          j	        t          j
                   t          d          dd| _        |                     |           | j                            | j                   | j                            | j                   | j                            |j                   |	|| _        d S d S )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$   !!$///  /=? ? ?  	I$$Y%?@@@@$$Y%LMMM{4k6GHHH@AA   	""" 	  !EFFF$$T%6777
81222?DGGG ?r"   c                     t          |                                 dd          }|r
 |             t          j        | |           dS )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   sH    dmmoo/A4HH 	KMMMtU+++++r"   c                                                       s-                                 t          j        d fd           t	                                           dd          }|r
 |             t          j         |           dS )z>Handle focus out event restoring the last valid selected path.2   c                  8                          j                  S N)rT   r   )r   r   s   r!   <lambda>z,PathComboBox.focusOutEvent.<locals>.<lambda>-  s    (*:*:4;M*N*N r"   hide_status_iconN)rA   rS   r   
singleShotr   r   focusOutEvent)r   r+   hide_statusr   s   `  @r!   r   zPathComboBox.focusOutEvent'  s     }} 	P}}Hb"N"N"N"N"NOOOdmmoo/A4HH 	KMMMe,,,,,r"   c                 h   t          |                                           }t          j        |dz             }t          d |D                       }t	          ||           }t          t                    }|                                                    |           | 	                    |           |S )"Find available completion options.*c                 :    g | ]}t          j        |          |S r>   )ospisdir.0opts     r!   
<listcomp>z2PathComboBox._complete_options.<locals>.<listcomp>9  s%    ===scinn=s===r"   
r   r   globsortedr   strr   popupsetStyleSheetr   r   rO   opts	completerqsss        r!   _complete_optionszPathComboBox._complete_options5  s    d..0011y$$==d===>>tT**	.!!'',,,)$$$r"   c                    |                                  }t          |          dk    r>|                     |d         t          j        z              |                                  dS |                                                                  dS 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  s}     %%''t99>>!!$q'BF"2333!!!!!NN%%'''''r"   c                 p    ||                                  }t          j        t          |                    S zReturn True if string is valid)r   r   r   r   r?   s     r!   rA   zPathComboBox.is_validM  s0    <##%%Dy--...r"   c                     |                                  | _        | j                            dd           | j                            | j                   dS rC   )r   r   rE   r*   open_dirrF   s    r!   r1   zPathComboBox.selectedS  sK    !--//
d###4-.....r"   c                     |                                  }t          j        |          r"|r |d         t          j        k    r
|dd         }|                     |           dS )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  sb    
 !!9T?? 	!t 	!Bx26!!CRCydr"   c                 n    |                      ||                     |          t          j                   dS )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  s0    
 	e 4 4bnEEEEEr"   r   )rZ   r[   r\   r]   r   r   r   r   	ElideLeftr   r   r   r   r   rA   r1   rW   r   r>   r"   r!   r   r      s          vc{{H27T!",   8, , ,- - -  	( 	( 	(/ / / // / /	 	 	F F F F Fr"   r   c                   "    e Zd ZdZddZddZdS )UrlComboBoxz!
    QComboBox handling urls
    FNc                     t                               | ||           t          |           }|                     |           | j                            | j                   |	|| _        d S d S r   )r   r   r   r   r   
disconnectr   rl   )r   r    r   r~   	line_edits        r!   r   zUrlComboBox.__init__p  sj    dF,>???dOO	###''666?DGGG ?r"   c                 p    ||                                  }t          |                                          S r   )r   r   isValidr?   s     r!   rA   zUrlComboBox.is_validy  s0    <##%%DDzz!!###r"   FNr   )rZ   r[   r\   r]   r   rA   r>   r"   r!   r   r   l  sF            $ $ $ $ $ $r"   r   c                   2    e Zd ZdZ	 	 ddZd	dZd Zd ZdS )
FileComboBoxz'
    QComboBox handling File paths
    NFc                 b   t                               | ||           |r$t          |           }|                     |           |r!|                     t
          j                   d S |                     t
          j                   |                     t          j
        t          j                   d S r   )r   r   r   r   re   r   r   AdjustToContentsOnFirstShowrg   r   rh   ri   )r   r    r   default_line_editr   s        r!   r   zFileComboBox.__init__  s    dF,>??? 	(!$IY'''  	I$$Y%?@@@@@$$Y%JKKK{4k6GHHHHHr"   c                     ||                                  }t          j        t          |                    p 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  sQ    <##%%DN40011 1>$//00 	r"   c                 L   |                                  }t          |          dk    rV|d         }t          j        |          r|t          j        z   }|                     |           |                                  dS |                                 	                                 dS r   )
r   r   r   r   r   r   r4   r2   r   r   )r   r   rO   s      r!   r   zFileComboBox.tab_complete  s     %%''t99>>7Dy %bf}!!$'''!!!!!NN%%'''''r"   c                 h   t          |                                           }t          j        |dz             }t          d |D                       }t	          ||           }t          t                    }|                                                    |           | 	                    |           |S )r   r   c                 b    g | ],}t          j        |          st          j        |          *|-S r>   )r   r   r   r   s     r!   r   z2FileComboBox._complete_options.<locals>.<listcomp>  sD     = = =s)C..=,/JsOO=s = = =r"   r   r   s        r!   r   zFileComboBox._complete_options  s    d..0011y$$ = =d = = = > > tT**	.!!'',,,)$$$r"   )NFFr   )rZ   r[   r\   r]   r   rA   r   r   r>   r"   r!   r   r     sp          8=#(I I I I   ( ( (    r"   r   c                     t          j        |           ot          j        |           d         dv }t          j        |           o&t          j        t          j        | d                    }|p|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     s]    
4  MS\$%7%7%:o%MI4NSZ}0M0M%N%NJ"
"r"   c                   (    e Zd ZdZddZddZd ZdS )	PythonModulesComboBoxz
    QComboBox handling Python modules or packages path
    (i.e. .py, .pyw files *and* directories containing __init__.py)
    FNc                 T    t                               | ||           |	|| _        d S d S r   )r   r   rl   )r   r    r   r~   s       r!   r   zPythonModulesComboBox.__init__  s2    dF,>????DGGG ?r"   c                 f    ||                                  }t          t          |                    S r   )r   r   r   r?   s     r!   rA   zPythonModulesComboBox.is_valid  s.    <##%%D#N4$8$8999r"   c                     t                               |            | j                            |                                            dS 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     sU            
: : : :/ / / / /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 2 2 2 2 2 5 5 5 5 5 5q0 q0 q0 q0 q09 q0 q0 q0h<2 <2 <2 <2 <2l <2 <2 <2~&. &. &. &. &.| &. &. &.RkF kF kF kF kF# kF kF kF\$ $ $ $ $, $ $ $(5 5 5 5 5< 5 5 5p# # #/ / / / /L / / / / /r"   