
,]c        	   @   s4  d  Z  d d l Z d d l j Z d d l Z d d l Z d d l m Z m	 Z	 d d l
 m Z m Z m Z m Z m Z m Z m Z m Z m Z d d l m Z m Z m Z m Z d d l m Z m Z m Z m Z m Z m Z m  Z  m! Z! 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/ m0 Z0 d d l1 m2 Z2 m3 Z3 d d  Z4 d e f d     YZ5 e6 d d  Z7 d   Z8 d a: d   Z; d   Z< d   Z= d d  Z> d   Z? d   Z@ d   ZA d d d d d d e6 eB d  ZC e6 eB d d  ZD d   ZE d d d d d d d e jF d  ZG d   ZH d d   ZI d!   ZJ d"   ZK d d d#  ZL d$   ZM d d d%  ZN g  d&  ZO d' e f d(     YZP d)   ZQ d* e f d+     YZR d, e$ f d-     YZS d.   ZT d/   ZU d d0  ZV d ZW eX d1 k r0eT   n  d S(2   s   Qt utilitiesiN(   t   to_qvariantt   from_qvariant(	   t   QEventt   QLibraryInfot   QLocalet   QObjectt   Qtt   QTimert   QTranslatort   Signalt   Slot(   t   QIcont	   QKeyEventt   QKeySequencet   QPixmap(   t   QActiont   QApplicationt   QHBoxLayoutt   QLabelt	   QLineEditt   QMenut   QStylet   QToolBart   QToolButtont   QVBoxLayoutt   QWidget(   t   get_image_patht   running_in_mac_app(   t   get_shortcut(   t   programs(   t   icon_manager(   t   get_icont   get_std_icon(   t   is_text_stringt   to_text_strings   not_found.pngc         C   s)   t    } | j t t |  |    | S(   s#   Return image inside a QLabel object(   R   t	   setPixmapR   R   (   t   namet   defaultt   label(    (    s5   lib/python2.7/site-packages/spyder/utils/qthelpers.pyt   get_image_label,   s    	t   MacApplicationc           B   s,   e  Z d  Z e e  Z d   Z d   Z RS(   s;   Subclass to be able to open external files with our Mac appc         G   s   t  j |  |  d  S(   N(   R   t   __init__(   t   selft   args(    (    s5   lib/python2.7/site-packages/spyder/utils/qthelpers.pyR)   7   s    c         C   sJ   | j    t j k r: t | j    } |  j j |  n  t j |  |  S(   N(	   t   typeR   t   FileOpent   strt   filet   sig_open_external_filet   emitR   t   event(   R*   R2   t   fname(    (    s5   lib/python2.7/site-packages/spyder/utils/qthelpers.pyR2   :   s    (   t   __name__t
   __module__t   __doc__R	   R.   R0   R)   R2   (    (    (    s5   lib/python2.7/site-packages/spyder/utils/qthelpers.pyR(   3   s   	i   c         C   s   t    r t } n t } | j   } | d k rO | d g  } | j d  n  |  rb t |  n  t j j	 d d  } | d k	 r t
 |  } | j j | j  | j | d  n  | S(   s   
    Return QApplication instance
    Creates it if it doesn't already exist
    
    test_time: Time to maintain open the application when testing. It's given
    in seconds
    t   Spydert   TEST_CI_WIDGETSi  N(   R   R(   R   t   instancet   Nonet   setApplicationNamet   install_translatort   ost   environt   getR   t   timeoutt   connectt   quitt   start(   t	   translatet	   test_timet   SpyderApplicationt   appt   test_cit   timer_shutdown(    (    s5   lib/python2.7/site-packages/spyder/utils/qthelpers.pyt   qapplicationA   s    		c         C   s@   t  j d k r4 t j d |   r) d |  Sd |  Sn d |  Sd S(   sB   Select the right file uri scheme according to the operating systemt   nts
   ^[a-zA-Z]:s   file:///s   file://N(   R=   R$   t   ret   search(   R3   (    (    s5   lib/python2.7/site-packages/spyder/utils/qthelpers.pyt   file_uria   s
    c         C   so   t  d k rO t   } | j d t j   j   t j t j	   rO | a  qO n  t  d k	 rk |  j
 t   n  d S(   s2   Install Qt translator to the QApplication instancet   qt_N(   t   QT_TRANSLATORR:   R   t   loadR   t   systemR$   R   t   locationt   TranslationsPatht   installTranslator(   t   qappt   qt_translator(    (    s5   lib/python2.7/site-packages/spyder/utils/qthelpers.pyR<   o   s    	c         C   s)   t  t |   } t t j |  d t  S(   s   Return keybindingi    (   t   getattrR   R   t   keyBindingsR.   (   t   attrt   ks(    (    s5   lib/python2.7/site-packages/spyder/utils/qthelpers.pyt
   keybinding{   s    c         C   s   |  j  d  rT t j d k rG |  j  d  r: |  d }  qQ |  d }  qT |  d }  n  |  j d t j  }  t j |   r | d  k s t j |   d | k r |  Sn  d  S(	   Ns   file://RK   s   file:///i   i   i   s   %5Ci   (	   t
   startswithR=   R$   t   replacet   sept   ospt   existsR:   t   splitext(   t   patht   extlist(    (    s5   lib/python2.7/site-packages/spyder/utils/qthelpers.pyt   _process_mime_path   s    %c         C   s   g  } |  j    rc x |  j   D]= } t t | j    |  } | d k	 r | j |  q q Wn] |  j   r xN t |  j    j	   D]1 } t | |  } | d k	 r | j |  q q Wn  | r | Sd S(   sR   
    Extract url list from MIME data
    extlist: for example ('.py', '.pyw')
    N(
   t   hasUrlst   urlsRe   R"   t   toStringR:   t   appendt   hasTextt   textt
   splitlines(   t   sourceRd   t   pathlistt   urlRc   t   rawpath(    (    s5   lib/python2.7/site-packages/spyder/utils/qthelpers.pyt   mimedata2url   s    c         C   s:   |  j    |  j   |  j   |  j   |  j   |  j   f S(   s'   Convert QKeyEvent instance into a tuple(   R,   t   keyt	   modifiersRk   t   isAutoRepeatt   count(   R2   (    (    s5   lib/python2.7/site-packages/spyder/utils/qthelpers.pyt   keyevent2tuple   s    $c         C   s
   t  |    S(   s'   Convert tuple into a QKeyEvent instance(   R   (   t
   past_event(    (    s5   lib/python2.7/site-packages/spyder/utils/qthelpers.pyt   tuple2keyevent   s    c         C   s   t  |  t  r6 |  \ } } } } } } t |   }  n$ |  j   } |  j   } |  j   } | t j @} | t j @} |  | | | | f S(   N(	   t
   isinstancet   tupleRx   Rk   Rs   Rr   R   t   ControlModifiert   ShiftModifier(   R2   t   _Rr   Rs   Rk   t   ctrlt   shift(    (    s5   lib/python2.7/site-packages/spyder/utils/qthelpers.pyt   restore_keyevent   s    c	   
      C   s*  t  |   }	 | d k	 r( |	 j |  n  | d k	 r_ t |  rO t |  } n  |	 j |  n  | d k	 sw | d k	 r |	 j | d k r | n |  n  | r |	 j t j	  n  |	 j
 |  | d k	 r |	 j j |  n  | d k	 r
|	 j j |  |	 j t  n  | d k	 r&|	 j |  n  |	 S(   s   Create a QToolButtonN(   R   R:   t   setTextR!   R   t   setIcont
   setToolTipt   setToolButtonStyleR   t   ToolButtonTextBesideIcont   setAutoRaiset   clickedRA   t   toggledt   setCheckablet   Truet   setShortcut(
   t   parentRk   t   shortcutt   icont   tipR   t	   triggeredt	   autoraiset   text_beside_icont   button(    (    s5   lib/python2.7/site-packages/spyder/utils/qthelpers.pyt   create_toolbutton   s(    "c         C   s^   | d k r |  j   } n  t |  } | j |   | j |  | rZ | j t j  n  | S(   s3   Create a QToolButton directly from a QAction objectN(   R:   R   R   t   setDefaultActionR   R   R   R   (   t   actionR   R   R   R   (    (    s5   lib/python2.7/site-packages/spyder/utils/qthelpers.pyt   action2button   s    c         C   s@   |  d k	 r< x- |  D]" } | d k	 r | j |  q q Wn  d S(   s   Enable/disable actionsN(   R:   t
   setEnabled(   t   actionst   enableR   (    (    s5   lib/python2.7/site-packages/spyder/utils/qthelpers.pyt   toggle_actions   s    c
         C   s  t  | |   }
 | d k	 r. |
 j j |  n  | d k	 rZ |
 j j |  |
 j t  n  | d k	 r t |  r t |  } n  |
 j	 |  n  | d k	 r |
 j
 |  |
 j |  n  | d k	 r |
 j t |   n  | d k	 r |
 j |  n  t j d k rod |
 _ |	 t j k rC| d k	 r7| |
 _ qld |
 _ q| d k	 r_|
 j |  n  |
 j |	  n) | d k	 r|
 j |  n  |
 j |	  |
 S(   s   Create a QActiont   darwint   missingN(   t   SpyderActionR:   R   RA   R   R   R   R!   R   R   R   t   setStatusTipt   setDataR    t   setMenuRolet   syst   platformt   _shown_shortcutR   t   WidgetShortcutR   t   setShortcutContext(   R   Rk   R   R   R   R   R   t   datat   menurolet   contextR   (    (    s5   lib/python2.7/site-packages/spyder/utils/qthelpers.pyt   create_action   s<    	c         C   s.   |  j  |  j   d t d | d |   d S(   s>   Add the shortcut associated with a given action to its tooltips    (%s)R   R$   N(   R   t   toolTipR   (   R   R   R$   (    (    s5   lib/python2.7/site-packages/spyder/utils/qthelpers.pyt   add_shortcut_to_tooltip  s    c         C   s  d } t |  j    } | r@ | d } | j   r@ d } q@ n  xD| D]<} | d k r | d k	 r | d k r~ |  j   q}|  j |  n t | t  r | d k r |  j |  q}|  j	 | |  n t | t
  r}t | t  r3t |  t  s	t |  t  r3y | j } Wq0t k
 r,qG q0Xq3n  | d k rjy |  j |  Wqzt k
 rfqG qzXq}|  j | |  n  | } qG Wd S(   s%   Add actions to a QMenu or a QToolBar.iN(   R:   t   listR   t   isSeparatort   addSeparatort   insertSeparatorRy   R   t   addMenut
   insertMenuR   R   R   t   no_icon_actiont   RuntimeErrort	   addActiont   insertAction(   t   targetR   t   insert_beforet   previous_actiont   target_actionsR   (    (    s5   lib/python2.7/site-packages/spyder/utils/qthelpers.pyt   add_actions  s:    

c         C   s   t  |  j d t j  t  S(   s$   Get QTreeWidgetItem user role stringi    (   R   R   R   t   UserRoleR"   (   t   item(    (    s5   lib/python2.7/site-packages/spyder/utils/qthelpers.pyt   get_item_user_textE  s    c         C   s    |  j  d t j t |   d S(   s$   Set QTreeWidgetItem user role stringi    N(   R   R   R   R    (   R   Rk   (    (    s5   lib/python2.7/site-packages/spyder/utils/qthelpers.pyt   set_item_user_textJ  s    c      	      s7   t      f d    } t |  | d | d | d | S(   s   Create bookmark actionc              s   t  j    S(   N(   R   t
   start_file(    (   Ro   (    s5   lib/python2.7/site-packages/spyder/utils/qthelpers.pyt   open_urlR  s    R   R   R   (   R
   R   (   R   Ro   t   titleR   R   R   (    (   Ro   s5   lib/python2.7/site-packages/spyder/utils/qthelpers.pyt   create_bookmark_actionO  s    c         C   sy   g  } xl | D]d \ } } } t  } | d k rI t j |  sI t } qI n  | r t |  | |  } | j |  q q W| S(   st   
    Create bookmark actions depending on module installation:
    bookmarks = ((module_name, url, title), ...)
    t	   winpython(   R   R   t   is_module_installedt   FalseR   Ri   (   R   t	   bookmarksR   Rr   Ro   R   t
   create_actt   act(    (    s5   lib/python2.7/site-packages/spyder/utils/qthelpers.pyt   create_module_bookmark_actionsZ  s    c            s   t  |  r t |  } n  t j d k r? | d k	 r? |   n  t j    } | d k	 r| t |  | d | d   f d   Sd S(   s   Create action to run a programRK   R   R   c              s   t  j    S(   N(   R   t   run_program(    (   R$   (    s5   lib/python2.7/site-packages/spyder/utils/qthelpers.pyt   <lambda>v  t    N(   R!   R   R=   R$   R:   R   t   find_programR   (   R   Rk   R$   R   t   nt_nameRc   (    (   R$   s5   lib/python2.7/site-packages/spyder/utils/qthelpers.pyt   create_program_actionm  s    	c      	      sY   t  |  r t |  } n  t j    rU t |  | d | d     f d   Sd S(   s.   Create action to run a GUI based Python scriptR   R   c              s   t  j      S(   N(   R   t   run_python_script(    (   R+   t   modulet   package(    s5   lib/python2.7/site-packages/spyder/utils/qthelpers.pyR     s    N(   R!   R   R   t   python_script_existsR   (   R   Rk   R   R   R   R+   (    (   R+   R   R   s5   lib/python2.7/site-packages/spyder/utils/qthelpers.pyt   create_python_script_actiony  s
    t   DialogManagerc           B   s2   e  Z d  Z d   Z d   Z d   Z d   Z RS(   s   
    Object that keep references to non-modal dialog boxes for another QObject,
    typically a QMainWindow or any kind of QWidget
    c         C   s   t  j |   i  |  _ d  S(   N(   R   R)   t   dialogs(   R*   (    (    s5   lib/python2.7/site-packages/spyder/utils/qthelpers.pyR)     s    c            s   x t    j j    D]B } t | j    t | j    k r | j   | j   Pq q W| j   |   j t |  <| j j	 t |    f d   | j
 j	 t |    f d   d S(   sY   Generic method to show a non-modal dialog and keep reference
        to the Qt C++ objectc            s     j  |   S(   N(   t   dialog_finished(   t   eid(   R*   (    s5   lib/python2.7/site-packages/spyder/utils/qthelpers.pyR     R   c            s     j  |   S(   N(   R   (   R   (   R*   (    s5   lib/python2.7/site-packages/spyder/utils/qthelpers.pyR     R   N(   R   R   t   valuesR"   t   windowTitlet   showt   raise_t   idt   acceptedRA   t   rejected(   R*   t   dialogt   dlg(    (   R*   s5   lib/python2.7/site-packages/spyder/utils/qthelpers.pyR     s    


		c         C   s   |  j  j |  S(   s   Manage non-modal dialog boxes(   R   t   pop(   R*   t	   dialog_id(    (    s5   lib/python2.7/site-packages/spyder/utils/qthelpers.pyR     s    c         C   s.   x' t  |  j j    D] } | j   q Wd S(   s   Close all opened dialog boxesN(   R   R   R   t   reject(   R*   R   (    (    s5   lib/python2.7/site-packages/spyder/utils/qthelpers.pyt	   close_all  s    (   R4   R5   R6   R)   R   R   R   (    (    (    s5   lib/python2.7/site-packages/spyder/utils/qthelpers.pyR     s
   			c         C   sI   t  j |   d } | j d  r/ | d } n  t d | t j d   S(   s   Return file type iconi   t   .s   %s.pngt   FileIcon(   R`   Rb   R]   R   t   imaR   (   R3   t   ext(    (    s5   lib/python2.7/site-packages/spyder/utils/qthelpers.pyt   get_filetype_icon  s    R   c           B   s/   e  Z d  Z d   Z d   Z e d    Z RS(   s>   Spyder QAction class wrapper to handle cross platform patches.c         O   s   t  t |   j | |   d |  _ t j d k r t | |   |  _ |  j j t	    |  j j
 j |  j
  |  j j j |  j  |  j j j |  j  |  j j j |  j  n	 |  |  _ d S(   s>   Spyder QAction class wrapper to handle cross platform patches.R   N(   t   superR   R)   R:   t   _action_no_iconR   R   R   R   R   R   RA   R   t   changedt   hovered(   R*   R+   t   kwargs(    (    s5   lib/python2.7/site-packages/spyder/utils/qthelpers.pyR)     s    	c            sT   t  t   j     t   d  rL  d k rL     f d   } | S  Sd S(	   sA   Intercept method calls and apply to both actions, except signals.t   __call__R   R   R   R   c             sT     |  |   }  d k rP  j  d } t t |  j   } | |  |   n  | S(   NR   R   (   R   (   t   __dict__R   R   t   __getattribute__(   R+   R   t   resultt   action_no_icont   attr_no_icon(   RZ   R$   R*   (    s5   lib/python2.7/site-packages/spyder/utils/qthelpers.pyt   newfunc  s    N(   R   R   R   R   (   R   R   R   t   hasattr(   R*   R$   R   (    (   RZ   R$   R*   s5   lib/python2.7/site-packages/spyder/utils/qthelpers.pyR     s    	c         C   s   |  j  S(   s"   Return the action without an Icon.(   R   (   R*   (    (    s5   lib/python2.7/site-packages/spyder/utils/qthelpers.pyR     s    (   R4   R5   R6   R)   R   t   propertyR   (    (    (    s5   lib/python2.7/site-packages/spyder/utils/qthelpers.pyR     s   		t   ShowStdIconsc           B   s   e  Z d  Z d   Z RS(   s'   
    Dialog showing standard icons
    c   
      C   s1  t  j |  |  t   } d } d } x t t  D] } | j d  r2 | d k r_ t   } n  t   } t |  } t   }	 |	 j	 | j
 d d   | j |	  | j t | j d d    | j |  | d | } | d k r | j |  q q2 q2 W|  j |  |  j d  |  j t d   d  S(	   Ni   i    t   SP_i    R   i   s   Standard Platform Iconst   TitleBarMenuButton(   R   R)   R   t   dirR   R]   R   R    R   R#   t   pixmapt	   addWidgetR   R^   t	   addLayoutt	   setLayoutt   setWindowTitlet   setWindowIcon(
   R*   R   t   layoutt   row_nbt   cindext   childt
   col_layoutt   icon_layoutR   R&   (    (    s5   lib/python2.7/site-packages/spyder/utils/qthelpers.pyR)     s*    			(   R4   R5   R6   R)   (    (    (    s5   lib/python2.7/site-packages/spyder/utils/qthelpers.pyR     s   c          C   s6   t    }  t d  } | j   t j |  j    d S(   s!   
    Show all standard Icons
    N(   RJ   R   R:   R   R   t   exitt   exec_(   RG   R   (    (    s5   lib/python2.7/site-packages/spyder/utils/qthelpers.pyt   show_std_icons  s    	
c         C   s   i	 d d 6d d 6d d 6d d	 6d d 6d d 6d d 6d d 6d  d 6} t    j   j d  } d t j | f } | | k r | | \ } } |  j   j   } | | | } t | d  Sd S(!   s  
    Return a spacing (int) or None if we don't have the appropriate metrics
    to calculate the spacing.

    We're trying to adapt the spacing below the tools_layout spacing so that
    the main_widget has the same vertical position as the editor widgets
    (which have tabs above).

    The required spacing is

        spacing = tabbar_height - tools_height + offset

    where the tabbar_heights were empirically determined for a combination of
    operating systems and styles. Offsets were manually adjusted, so that the
    heights of main_widgets and editor widgets match. This is probably
    caused by a still not understood element of the layout and style metrics.
    i    i    s	   nt.fusioni   i   s   nt.windowsvistai   s   nt.windowsxps
   nt.windowsi   is   posix.breezei&   is   posix.oxygeni   s   posix.qtcurvei   s   posix.windowss   posix.fusionR$   s   %s.%sN(   i    i    (   i   i   (   i   i    (   i   i   (   i   i(   i&   i(   i   i    (   i   i    (   i    i    (   RJ   t   styleR   R=   R$   t   sizeHintt   heightt   max(   t   tools_layoutt   metricst
   style_nameRr   t   tabbar_heightt   offsett   tools_heightt   spacing(    (    s5   lib/python2.7/site-packages/spyder/utils/qthelpers.pyt   calc_tools_spacing  s"    
c         C   st   t    } | j d d d d  t |   } | d k	 rG | j |  n  | j |   | d k	 rp | j |  n  | S(   s  
    Returns a layout for a set of controls above a main widget. This is a
    standard layout for many plugin panes (even though, it's currently
    more often applied not to the pane itself but with in the one widget
    contained in the pane.

    tools_layout: a layout containing the top toolbar
    main_widget: the main widget. Can be None, if you want to add this
        manually later on.
    i    N(   R   t   setContentsMarginsR  R:   t
   setSpacingR   R   (   R  t   main_widgetR  R  (    (    s5   lib/python2.7/site-packages/spyder/utils/qthelpers.pyt   create_plugin_layout'  s    	t   __main__(Y   R6   R=   t   os.pathRc   R`   RL   R   t   qtpy.compatR    R   t   qtpy.QtCoreR   R   R   R   R   R   R   R	   R
   t
   qtpy.QtGuiR   R   R   R   t   qtpy.QtWidgetsR   R   R   R   R   R   R   R   R   R   R   t   spyder.config.baseR   R   t   spyder.config.guiR   t   spyder.utilsR   R   R   t   spyder.utils.icon_managerR   R    t   spyder.py3compatR!   R"   R'   R(   R   RJ   RN   R:   RP   R<   R\   Re   Rq   Rv   Rx   R   R   R   R   R   t   WindowShortcutR   R   R   R   R   R   R   R   R   R   R   R   R   R  R  R  t   MENU_SEPARATORR4   (    (    (    s5   lib/python2.7/site-packages/spyder/utils/qthelpers.pyt   <module>   sh   @"L 									+	&			
$	*	
	(