a
    ÜÄAb  ã                   @   s–   d Z ddgZddl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 dd
l	mZ G dd„ dejƒZG dd„ dejƒZG dd„ dejƒZdS )z*Common components for environment dialogs.ÚLineEditEnvironmentÚEnvironmentActionsDialogé    N)ÚQtCore)ÚQtGui)Ú	QtWidgets)Úwidgets)Úcommon)Údialogsc                       sj   e Zd ZdZe d¡Ze ¡ Ze ¡ Z	e ¡ Z
‡ fdd„Z‡ fdd„Z‡ fdd„Zd	d
„ Zdd„ Z‡  ZS )r   zE
    Custom line edit to handle regex for naming an environment.
    z^[A-Za-z][A-Za-z0-9_-]{0,30}$c                    sR   t ƒ j|i |¤Ž t | j¡| _tj| d| _|  	| j¡ |  
tjjtjj¡ dS )z+Custom line edit for naming an environment.©ÚparentN)ÚsuperÚ__init__r   ZQRegExpValidatorÚVALID_REZ
_validatorr   ZQMenuÚmenuZsetValidatorZsetSizePolicyZQSizePolicyZ	Expanding)ÚselfÚargsÚkwargs©Ú	__class__© úTlib/python3.9/site-packages/anaconda_navigator/widgets/dialogs/environment/common.pyr   "   s
    zLineEditEnvironment.__init__c                    sF   |  ¡ tjjkr:| ¡ tjj@ r:|  ¡ s:|  | 	¡ ¡ dS t
ƒ  |¡S )úOverride Qt method.T)Útyper   ZQEventZMouseButtonPressZbuttonsÚQtZRightButtonZ	isEnabledÚ	show_menuÚposr   Úevent)r   r   r   r   r   r   *   s    ÿþýzLineEditEnvironment.eventc                    sŒ   |  ¡ }| tjj¡rDtj ¡ }| ¡ }| j	 
|¡r||  |¡ dS n8|tjjtjjfv rd| j ¡  n|tjjfv r|| j ¡  tƒ  |¡ dS )r   N)ÚkeyZmatchesr   ZQKeySequenceZPaster   ÚQApplicationÚ	clipboardÚtextr   Z
exactMatchÚsetTextr   r   Z
Key_ReturnZ	Key_EnterÚsig_return_pressedÚemitZ
Key_EscapeÚsig_escape_pressedr   ÚkeyPressEvent)r   r   r   r   r    r   r   r   r%   5   s    


z!LineEditEnvironment.keyPressEventc                 C   sT   | j  ¡  t d| j ¡}|j | j¡ | j  |¡ | j  d¡ | j  	|  
|¡¡ dS )z Show copy menu for channel item.z&CopyTN)r   Úclearr   ZQActionZ	triggeredZconnectÚ	copy_textZ	addActionZ
setEnabledZexec_ZmapToGlobal)r   r   Úcopyr   r   r   r   F   s    
zLineEditEnvironment.show_menuc                 C   s&   t j ¡ }| |  ¡ ¡ | j ¡  dS )z#Copy environment text to clipboard.N)r   r   r   r!   r    Ú
sig_copiedr#   )r   r   r   r   r   r'   O   s    
zLineEditEnvironment.copy_text)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   ZQRegExpr   ÚSignalr"   r$   r)   r   r   r%   r   r'   Ú__classcell__r   r   r   r   r      s   
	c                       st   e Zd ZdZdZe ¡ Zd‡ fdd„	Zddd„Z	dd	„ Z
d
d„ Zdd„ Zedd„ ƒZedd„ ƒZedd„ ƒZ‡  ZS )r   ú0Base dialog with common methods for all dialogs.ià  Nc                    sB   t ƒ j|d d| _d| _d| _d| _|| _d| _d| _d| _	dS )r0   r
   N)
r   r   ÚinfoÚ	_packagesÚ	envs_dirsÚenvironmentsÚapiÚchannelsÚ	text_nameÚlabel_prefix)r   r   r5   r   r   r   r   ]   s    z!EnvironmentActionsDialog.__init__c                 C   sX   |rT|  di ¡}|| _|  d¡| _|d | _|d | _|  d¡| _|  ¡  | j ¡  dS )zASetup the dialog conda information as a result of a conda worker.Zprocessed_infoZpackagesZ__envs_dirs_writableZ__environmentsr6   N)	Úgetr1   r2   r3   r4   r6   ÚrefreshÚsig_setup_readyr#   )r   ZworkerZ
conda_dataÚerrorZ
conda_infor   r   r   Úsetupl   s    

zEnvironmentActionsDialog.setupc                 C   sN   | j  d¡ | jrJ| j ¡ }| | jtjj	d¡}| j 
|¡ | j | j¡ dS )z"Update the location (prefix) text.Ti,  N)Z	button_okZsetDisabledr4   r8   ZfontMetricsZ
elidedTextÚprefixr   r   Z	ElideLeftr!   Z
setToolTip)r   Zfmr>   r   r   r   Úupdate_locationx   s    
z(EnvironmentActionsDialog.update_locationc                 C   s   t ‚dS )z0Update the status of buttons based data entered.N)ÚNotImplementedError)r   r   r   r   r:      s    z EnvironmentActionsDialog.refreshc                 C   sD   | j r| j  ¡ ng }tjdkr4| ¡ }dd„ |D ƒ}t|ƒoB||vS )ze
        Check that an environment has a valid name.
        On Windows is case insensitive.
        Úntc                 S   s   g | ]}|  ¡ ‘qS r   )Úlower)Ú.0Úer   r   r   Ú
<listcomp>   ó    z>EnvironmentActionsDialog.is_valid_env_name.<locals>.<listcomp>)r4   ÚvaluesÚosÚnamerB   Úbool)r   Zenv_nameZ	env_namesr   r   r   Úis_valid_env_name…   s
    
z*EnvironmentActionsDialog.is_valid_env_namec                 C   s$   | D ]}|  tjjtjjB ¡ qdS )z!Align label widgets to the right.N)ZsetAlignmentr   r   Z
AlignRightZAlignVCenter)Zlabel_widgetsZwidgetr   r   r   Úalign_labels‘   s    z%EnvironmentActionsDialog.align_labelsc                 C   s   d}| j r| j  ¡  ¡ }|S )z@Return the content without extra spaces for the name of the env.Ú )r7   r    Ústrip)r   r    r   r   r   rI   —   s    zEnvironmentActionsDialog.namec                 C   s.   | j r$| jr$tj | j d | j¡}n| j}|S )z;Return the full prefix (location) as entered in the dialog.r   )r3   rI   rH   ÚpathÚjoin)r   Úresultr   r   r   r>   Ÿ   s    zEnvironmentActionsDialog.prefix)NN)NNN)r*   r+   r,   r-   ZBASE_DIALOG_WIDTHr   r.   r;   r   r=   r?   r:   rK   ÚstaticmethodrL   ÚpropertyrI   r>   r/   r   r   r   r   r   V   s   
	

c                   @   s   e Zd ZdZdS )ÚOpenIconButtonzButton with "open" icon.N)r*   r+   r,   r-   r   r   r   r   rT   ª   s   rT   )r-   Ú__all__rH   Zqtpyr   r   r   Zanaconda_navigatorr   Zanaconda_navigator.widgetsr   Zglobal_commonsr	   ZLineEditBaser   Z
DialogBaser   Z
IconButtonrT   r   r   r   r   Ú<module>
   s   ?T