
    Je(                     l   d Z ddlZddlZddlmZ ddlmZ ddlm	Z	m
Z
mZ ddl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 ddlmZ ddlmZ ddlmZ  ej>                  e       Z! G d d      Z" G d d      Z# G d d      Z$ G d de      Z% G d de      Z& G d de      Z' G d de      Z(y)z
Working Directory widget.
    N)getexistingdirectory)QSizeSignalSlot)QSizePolicyQWidget)on_conf_change)_)PluginMainContainer)ApplicationToolbar)get_home_dir)getcwd_or_home)APP_TOOLBAR_STYLESHEET)PathComboBoxc                       e Zd ZdZdZdZdZy)WorkingDirectoryActionsprevious_actionnext_actionbrowse_actionparent_actionN)__name__
__module____qualname__PreviousNextBrowseParent     K/usr/lib/python3/dist-packages/spyder/plugins/workingdirectory/container.pyr   r   &   s     HDFFr   r   c                       e Zd ZdZy)WorkingDirectoryToolbarSectionsmain_sectionN)r   r   r   Mainr   r   r    r"   r"   -   s    Dr   r"   c                       e Zd ZdZy)WorkingDirectoryToolbarItems
path_comboN)r   r   r   r   r   r   r    r&   r&   1   s    Lr   r&   c                       e Zd ZdZy)WorkingDirectoryToolbarworking_directory_toolbarN)r   r   r   IDr   r   r    r)   r)   7   s    	$Br   r)   c                   *     e Zd Z fdZd Zd Z xZS )WorkingDirectoryComboBoxc                 j    t         |   |dt        j                  d       | j	                  d       y )NFT)adjust_to_contentsid_
elide_text   )super__init__r&   r   setMinimumWidthselfparent	__class__s     r    r4   z!WorkingDirectoryComboBox.__init__=   s7    $,99	 	 	
 	S!r   c                     t        dd      S )z6Recommended size when there are toolbars to the right.i  
   )r   r7   s    r    sizeHintz!WorkingDirectoryComboBox.sizeHintH   s    S"~r   c                 B    | j                  | j                                y)z7Set current path as the tooltip of the widget on hover.N)
setToolTipcurrentText)r7   events     r    
enterEventz#WorkingDirectoryComboBox.enterEventL   s    ((*+r   )r   r   r   r4   r=   rB   __classcell__r9   s   @r    r-   r-   ;   s    	",r   r-   c                   "     e Zd ZdZ fdZ xZS )WorkingDirectorySpacerworking_directory_spacerc                     t         |   |       | j                  t        j                  t        j
                         | j                  t        t                     y N)	r3   r4   setSizePolicyr   	Expanding	PreferredsetStyleSheetstrr   r6   s     r    r4   zWorkingDirectorySpacer.__init__T   sD      	;00+2G2GH 	3567r   )r   r   r   r+   r4   rC   rD   s   @r    rF   rF   Q   s    	#B8 8r   rF   c                       e Zd ZdZ ee      Z	 d Zd Z e	d      d        Z
d Z e       dd	       Z e       d
        Z e       d        Z e       d        Zd Z ee       eee       eeee      dd                     Zd ZddZy)WorkingDirectoryContainerz,Container for the working directory toolbar.c                    | j                  dg       | _        d | _        t        d      }t	        | |      | _        t        |       | _        t        |       }| j
                  j                  |       | j
                  j                  |       | j                  j                  | j                  d             | j                  j                         | j                  _        | j                  j                  j                  | j                          | j                  j"                  t$           j                  | j                          | j'                  t(        j*                  t        d      t        d      | j-                  d      | j.                        | _        | j'                  t(        j2                  t        d      t        d      | j-                  d      | j4                        | _        | j'                  t(        j8                  t        d	      t        d	      | j-                  d
      | j:                        }| j'                  t(        j<                  t        d      t        d      | j-                  d      | j>                        }|| j                  ||fD ].  }| jA                  || j
                  tB        jD                         0 y )NhistoryzCurrent working directoryworking_dir_historyBackprevious)texttipicon	triggeredr   nextzBrowse a working directoryDirOpenIconzChange to parent directoryup)section)#get_confrR   	histindexr
   r)   toolbarr-   patheditrF   setWindowTitlesetObjectNamesetMaxCountr@   selected_textopen_dirconnectchdir	activatedrN   create_actionr   r   create_icon_previous_directoryr   r   _next_directoryr   r   _select_directoryr   _parent_directoryadd_item_to_toolbarr"   r$   )r7   titlespacerr   r   items         r    setupzWorkingDirectoryContainer.setupp   sL   }}Y3 -..tU;06'- 	##E*""5)!!$--0E"FG&*mm&?&?&A# 	&&tzz2$,,TZZ8  $11#,,6&	!!*-..  2  
  --#((6&	!!&)** . 
 **#**/0./!!-0,, + 
 **#**/0./!!$',, + 
 T]]M=I 	D$$7<< % 	r   c                    | j                   j                  | j                  d uxr | j                  dkD         | j                  j                  | j                  d uxr% | j                  t	        | j
                        dz
  k         y )Nr      )r   
setEnabledr_   r   lenrR   r<   s    r    update_actionsz(WorkingDirectoryContainer.update_actions   so    ''NN$&=4>>A+=	?##NN$& 7T\\!2Q!66	
r   rR   )optionc                     || _         y rI   )rR   )r7   values     r    on_history_updatez+WorkingDirectoryContainer.on_history_update   s	    r   c                    t               }| j                  d      rt               }|S | j                  d      rT| j                  d      }t        j                  |      s.| j	                  dd       | j	                  dd       t               }|S )z
        Get the working directory from our config system or return the user
        home directory if none can be found.

        Returns
        -------
        str:
            The initial working directory.
        z%startup/use_project_or_home_directoryzstartup/use_fixed_directoryzstartup/fixed_directoryTF)r   r^   ospisdirset_conf)r7   workdirs     r    _get_init_workdirz+WorkingDirectoryContainer._get_init_workdir   s|     .==@A"nG  ]]89mm$=>G 99W%EtL;UC&.r   Nc                     |T| j                   j                  d       t        | t        d      t	                     }| j                   j                  d       |r| j                  |       yy)z
        Select working directory.

        Parameters
        ----------
        directory: str, optional
            The directory to change to.

        Notes
        -----
        If directory is None, a get directory dialog will be used.
        NFzSelect directoryT)sig_redirect_stdio_requestedemitr   r
   r   rh   )r7   	directorys     r    rn   z+WorkingDirectoryContainer._select_directory   sd     --2259,$% I
 --2248JJy! r   c                 T    | xj                   dz  c_         | j                  dd       y)zSelect the previous directory.rv    Tr   browsing_historyNr_   rh   r<   s    r    rl   z-WorkingDirectoryContainer._previous_directory   "     	!

R$
7r   c                 T    | xj                   dz  c_         | j                  dd       y)zSelect the next directory.rv   r   Tr   Nr   r<   s    r    rm   z)WorkingDirectoryContainer._next_directory   r   r   c                 z    | j                  t        j                  t               t        j                               y)z'Change working directory to parent one.N)rh   r   joinr   pardirr<   s    r    ro   z+WorkingDirectoryContainer._parent_directory   s"     	

388N,cjj9:r   c                 6    | j                   j                         S )z
        Get the current working directory.

        Returns
        -------
        str:
            The current working directory.
        )ra   r@   r<   s    r    get_workdirz%WorkingDirectoryContainer.get_workdir   s     }}((**r   c                 
   |rt        j                  t        |            }|r| j                  | j                     }n|| j                  v r!| j                  j                  |      | _        nm| j                  g | _        n!| j                  d| j                  dz    | _        | j                  j                  |       t        | j                        dz
  | _        	 t        j                  d|        t        j                  |       | j                  j                  |       | j                          |r| j                  j!                  |       yy# t"        $ r( | j                  j%                  | j                         Y yw xY w)a  
        Set `directory` as working directory.

        Parameters
        ----------
        directory: str
            The new working directory.
        browsing_history: bool, optional
            Add the new `directory` to the browsing history. Default is False.
        emit: bool, optional
            Emit a signal when changing the working directory.
            Default is True.
        Nrv   zSetting cwd to )r   abspathrN   rR   r_   indexappendrx   loggerdebugosrh   ra   add_textry   sig_current_directory_changedr   OSErrorpop)r7   r   r   r   s       r    rh   zWorkingDirectoryContainer.chdir	  s)   " C	N3I T^^4I$,,&!\\//	:DN~~%!#||,?T^^a-?@LL	* .2DN		-LL?9+67HHYMM""9-!2277	B  	-LLT^^,	-s   A5E .FFc                     t        | j                  j                               D cg c]&  }t        | j                  j	                  |            ( c}S c c}w )z
        Get the current history list.

        Returns
        -------
        list
            List of string paths.
        )rangera   countrN   itemText)r7   r   s     r    get_historyz%WorkingDirectoryContainer.get_history7  sF     ,,./1uDMM**512 1 	1 1s   +Ac                    | j                  d|       |r| j                  j                  |       || j                         }n6t        j                  d       |}t        j                  |      s
t               }| j                  |       y)z
        Set the current history list.

        Parameters
        ----------
        history: list
            List of string paths.
        cli_workdir: str or None
            Working directory passed on the command line.
        rR   Nz(Setting cwd passed from the command line)
r   ra   addItemsr   r   r   r   r   r   rh   )r7   rR   cli_workdirr   s       r    set_historyz%WorkingDirectoryContainer.set_historyC  sn     	i)MM""7+,,.GLLCD!G 99W%&.

7r   rI   )FT)r   r   r   __doc__r   rN   r   rt   ry   r	   r}   r   r   rn   rl   rm   ro   r   boolrh   r   r   r   r   r    rP   rP   `   s    6 %+3K!8t
 9% &
2 
V" "2 
V8 8
 
V8 8
 
V; ;	+ 
#Y	#t_	#tT)-   )-V
1r   rP   ))r   loggingr   os.pathpathr   qtpy.compatr   qtpy.QtCorer   r   r   qtpy.QtWidgetsr   r   spyder.api.config.decoratorsr	   spyder.api.translationsr
   !spyder.api.widgets.main_containerr   spyder.api.widgets.toolbarsr   spyder.config.baser   spyder.utils.miscr   spyder.utils.stylesheetr   spyder.widgets.comboboxesr   	getLoggerr   r   r   r"   r&   r)   r-   rF   rP   r   r   r    <module>r      s   
  	  - + + / 8 % A : + , : 2 
		8	$
     %0 %,| ,,
8W 
8| 3 |r   