ó
ø`9Sc           @   sc   d  d l  Z d  d l Td  d l Td  d l m Z d  d l m Z d g Z d e f d „  ƒ  YZ	 d S(   iÿÿÿÿN(   t   *(   t   gui(   t   txtt   FileSelectionWidgetc           B   sª   e  Z d  Z e ƒ  Z e ƒ  Z d d d „ Z d d „ Z d d „ Z	 d „  Z
 d „  Z d „  Z d „  Z d „  Z d	 „  Z d
 „  Z d „  Z d „  Z d „  Z d „  Z RS(   sô  Widget that displays a file path and allows the user to select the file
    interactively.

    Signals:
      - 'fileOpened': a path to an existing file has been selected by the user,
        or the widget's initialization process (carried out in 'loadSettings')
        has been completed. Use 'getFilePath' to get the file's path. Note:
        if the widget's initialization process fails due to missing settings
        file or non-existent file path, this signal will still be fired, and
        'getFilePath' will return 'None' in that case.
      - 'fileCreated': A non-existent file path has been selected by the
        user. Use 'getFilePath' to get the path, and 'invalidateFilePath' to
        report failure of creation of the requested file.c         C   sé  t  j |  | ƒ | |  _ | |  _ d  |  _ d  |  _ t ƒ  } | d  k	 re t | ƒ } | j	 | ƒ n d  } t
 ƒ  |  _ |  j j t ƒ | j	 |  j ƒ t t j d ƒ d ƒ |  _ |  j j j |  j ƒ |  j j ƒ  | j	 |  j ƒ t t j d ƒ d ƒ |  _ |  j j t ƒ |  j j j |  j ƒ |  j j ƒ  | j	 |  j ƒ t t j d ƒ d ƒ |  _ | d  k	 r}|  j j | ƒ n  |  j j j |  j ƒ | j	 |  j ƒ | j d d d d ƒ |  j | ƒ | d  k	 rå| j |  j ƒ n  d  S(   Nt   newt    t   reloadt   openi    (    t   QWidgett   __init__t   fileNameFiltert   openDialogCaptiont   Nonet   createDialogCaptiont   lastFileDirt   QHBoxLayoutt   QLabelt	   addWidgett	   QLineEditt   filePathEditt   setReadOnlyt   Truet   QPushButtonR   t   loadIcont   createButtont   clickedt   connectt   onCreateButtonClickedt   hidet   reloadButtont
   setEnabledt   Falset   onReloadButtonClickedt
   openButtont
   setToolTipt   onOpenButtonClickedt   setContentsMarginst	   setLayoutt   setBuddy(   t   selft	   labelTextR
   R   t   openButtonToolTipt   parentt   layoutt   filePathLabel(    (    s"   code\common\FileSelectionWidget.pyR	   4   s>    					c         C   s9   | |  _  | d k	 r( |  j j | ƒ n  |  j j ƒ  d S(   sÜ   Display a button that will make it possible to select a non-existent
        file path.

        Whenever such a file path is selected, 'fileCreated' signal will be
        fired instead of the usual 'fileOpened' signal.N(   R   R   R   R"   t   show(   R'   R   t   createButtonToolTip(    (    s"   code\common\FileSelectionWidget.pyt   showCreateButtonk   s    	c         C   s0   | d k	 r |  j j | ƒ n  |  j j ƒ  d S(   sÐ   Display a button that will make it possible to reload the currently
        selected file in one click.

        Reload button will simply fire 'fileOpened' for the currently selected
        file once again.N(   R   R   R"   R-   (   R'   t   reloadButtonToolTip(    (    s"   code\common\FileSelectionWidget.pyt   showReloadButtonx   s    c         C   s   |  j  S(   sA   This may be used with a 'QLabel's 'setBuddy' method, if required.(   R!   (   R'   (    (    s"   code\common\FileSelectionWidget.pyt   getOpenButtonƒ   s    c         C   s,   |  j  j ƒ  } | d k r d St j | ƒ S(   sQ   Return the currently selected file path or 'None' if no file is
        selected.R   N(   R   t   textR   R   t   absolutePath(   R'   t   filePath(    (    s"   code\common\FileSelectionWidget.pyt   getFilePath‡   s    c         C   s£   | d k	 r9 t j | ƒ } t j j | ƒ s9 d } q9 n  | d k	 r] t j j | ƒ |  _ n d |  _ d } |  j j	 | ƒ |  j
 j | d k ƒ |  j j ƒ  d S(   sx  Set the currently selected file path to 'filePath' unless it is
        'None' or represents a non-existent file path, and then fire
        'fileOpened' signal regardless of the given value of 'filePath'.

        If 'filePath' turns out to be valid, then 'getFilePath' will return the
        path if called from the signal handler. Otherwise, it will return
        'None'.R   N(   R   R   R4   t   ost   patht   isfilet   dirnameR   R   t   setTextR   R   t
   fileOpenedt   emit(   R'   R5   (    (    s"   code\common\FileSelectionWidget.pyt   openFile’   s    		c         C   sH   |  j  ƒ  } | d k r+ | j | d ƒ n | j | t j | ƒ ƒ d S(   s@   Save the currently selected file path to a 'QSettings' instance.N(   R6   R   t   setValueR   t   portablePath(   R'   t   settingst   keyR5   (    (    s"   code\common\FileSelectionWidget.pyt   saveSettings¬   s    c         C   s;   | j  | ƒ j ƒ  } | d k r* d } n  |  j | ƒ d S(   sf  Load the file path from a 'QSettings' instance and then fire
        'fileOpened' signal regardles of the loading procedure result.

        If a file path is successfully loaded from the settings file and points
        to an existing file, then 'getFilePath' will return the path if called
        from the signal handler. Otherwise, it will return 'None'.R   N(   t   valuet   toStringR   R>   (   R'   RA   RB   R5   (    (    s"   code\common\FileSelectionWidget.pyt   loadSettings¶   s    		c         C   s$   |  j  j d ƒ |  j j t ƒ d S(   s'   Clear the currently selected file path.R   N(   R   R;   R   R   R   (   R'   (    (    s"   code\common\FileSelectionWidget.pyt   invalidateFilePathÆ   s    c         C   s4   |  j  d k s% t j j |  j  ƒ r) d S|  j  Sd S(   su   Return file directory path suitable for passing to one of the
        'QFileDialog's static file selection functions.t   .N(   R   R   R7   R8   t   isdir(   R'   (    (    s"   code\common\FileSelectionWidget.pyt
   getFileDirÎ   s    %c         C   sH   t  j | ƒ } t j j | ƒ |  _ |  j j | ƒ |  j j	 t
 ƒ d S(   sR   Set the file path, update 'self.lastFileDir' and enable the reload
        button.N(   R   R4   R7   R8   R:   R   R   R;   R   R   R   (   R'   R5   (    (    s"   code\common\FileSelectionWidget.pyt   setFilePath×   s    c         C   sQ   t  j |  |  j |  j ƒ  |  j ƒ } | j ƒ  sM |  j | ƒ |  j j ƒ  n  d  S(   N(	   t   QFileDialogt   getOpenFileNameR   RJ   R
   t   isNullRK   R<   R=   (   R'   R5   (    (    s"   code\common\FileSelectionWidget.pyR#   ã   s
    	c         C   s)   |  j  ƒ  d  k	 s t ‚ |  j j ƒ  d  S(   N(   R6   R   t   AssertionErrorR<   R=   (   R'   (    (    s"   code\common\FileSelectionWidget.pyR    ì   s    c         C   s  t  j |  |  j |  j ƒ  |  j d  t  j ƒ } | j ƒ  r= d  St j	 | ƒ } t
 j j | ƒ s| |  j | ƒ |  j j ƒ  d  St |  ƒ } | j |  j ƒ | j d t j | ƒ ƒ | j d ƒ | j t j ƒ | j t j t j Bƒ | j ƒ  t j k r|  j | ƒ |  j j ƒ  n  d  S(   Ns&   The file selected (%s) already exists.sG   Would you like to open the existing file instead of creating a new one?(   RL   t   getSaveFileNameR   RJ   R
   R   t   DontConfirmOverwriteRN   R   R4   R7   R8   R9   RK   t   fileCreatedR=   t   QMessageBoxt   setWindowTitleR;   t	   quotePatht   setInformativeTextt   setIcont   Warningt   setStandardButtonst   Yest   Not   exec_R<   (   R'   R5   t   msgBox(    (    s"   code\common\FileSelectionWidget.pyR   ò   s*    	N(   t   __name__t
   __module__t   __doc__t
   pyqtSignalR<   RR   R   R	   R/   R1   R2   R6   R>   RC   RF   RG   RJ   RK   R#   R    R   (    (    (    s"   code\common\FileSelectionWidget.pyR      s"   		7				
								(
   t   os.pathR7   t   PyQt4.QtCoret   PyQt4.QtGuit   common.utilsR   R   t   __all__R   R   (    (    (    s"   code\common\FileSelectionWidget.pyt   <module>   s   

	