
    JelG                     :   d Z ddlZddlZddlZddlZddlmZmZmZ ddl	m
Z
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 ddlmZ dd	lmZ dd
lmZm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,  G d de-      Z. G d de      Z/ G d de-      Z0y)z
Base plugin class
    N)
QByteArrayQtSlot)QCursorQKeySequence)QApplicationQMainWindowQMenuQMessageBox	QShortcutQToolButton)_)get_color_schemeget_font)CONF)	NoDefault)is_text_stringqbytearray_to_str)ima)add_actionscreate_actioncreate_toolbuttonMENU_SEPARATORtoggle_actionsset_menu_icons)APP_STYLESHEET)DockTitleBarSpyderDockWidgetc                   v     e Zd ZdZdZd fd	Zd Z	 	 ddZedfdZ	ddZ
ddZd	 Zd
 ZddZd Zd Z xZS )BasePluginMixinz=Implementation of the basic functionality for Spyder plugins.Nc                     t         t        |           | j                         \  }}| j	                          d| _        |sd| _        | j                  |       y y )NTF)superr    __init__check_compatibility_register_plugin_is_compatible_show_compatibility_message)selfparentr$   message	__class__s       5/usr/lib/python3/dist-packages/spyder/plugins/base.pyr#   zBasePluginMixin.__init__-   sX    ot-/ (,'?'?'A$W"""'D,,W5 #    c                 .    t        j                  |        y)zRegister plugin configuration.N)r   register_pluginr(   s    r,   r%   z BasePluginMixin._register_plugin:   s    T"r-   c                 h    || j                   n|}t        j                  |t        |      ||       y)zSet option in spyder.iniN)recursive_notification)CONF_SECTIONr   setstr)r(   optionvaluesectionr2   s        r,   _set_optionzBasePluginMixin._set_option>   s/     (/$##G#f+u(>	@r-   c                 P    || j                   n|}t        j                  |||      S )zGet option from spyder.ini.)r3   r   get)r(   r6   defaultr8   s       r,   _get_optionzBasePluginMixin._get_optionE   s&    '.$##Gxx11r-   c                 P    || j                   n|}t        j                  ||       y)zRemove option from spyder.ini.N)r3   r   remove_option)r(   r6   r8   s      r,   _remove_optionzBasePluginMixin._remove_optionJ   s#    '.$##G7F+r-   c                 X    | j                   j                         j                  ||       y)z)Show message in main window's status bar.N)main	statusBarshowMessage)r(   r*   timeouts      r,   _show_status_messagez$BasePluginMixin._show_status_messageO   s    		))'7;r-   c                 8   t        |       }|j                  t        j                         |j	                  t        j
                         |j                  d       |j                  |       |j                  t         j                         |j                          y)zShow a compatibility message.zCompatibility CheckN)r   setWindowModalityr   NonModalsetAttributeWA_DeleteOnClosesetWindowTitlesetTextsetStandardButtonsOkshow)r(   r*   
messageBoxs      r,   r'   z+BasePluginMixin._show_compatibility_messageS   sk     &
$$R[[1 3 34!!"787#%%knn5r-   c                     | j                  |       t        j                  t        t        j
                               t        j                          y)ze
        Show message in main window's status bar and change cursor to
        Qt.WaitCursor
        N)rF   r   setOverrideCursorr   r   
WaitCursorprocessEventsr(   r*   s     r,   _starting_long_processz&BasePluginMixin._starting_long_process]   s5    
 	!!'*&&wr}}'=>""$r-   c                 z    t        j                          | j                  |d       t        j                          y)zJ
        Clear main window's status bar and restore mouse cursor.
        i  )rE   N)r   restoreOverrideCursorrF   rU   rV   s     r,   _ending_long_processz$BasePluginMixin._ending_long_processf   s/     	**,!!'4!8""$r-   c                 z    t         j                  j                  t        j                  | j
                              S )z;Return filesystem path to the root directory of the plugin.)ospathdirnameinspectgetfiler+   r0   s    r,   _get_plugin_pathz BasePluginMixin._get_plugin_pathn   s"    wwwt~~>??r-   c                     | j                   8| }|}t        | d      s|}|}| j                  ||      }|j                          |S y)z+Create configuration dialog box page widgetN
dockwidget)CONFIGWIDGET_CLASShasattr
initialize)r(   dlgmain_windowr)   rB   configwidgets         r,   _create_configwidgetz$BasePluginMixin._create_configwidgetr   sW    "".FD4. "2264@L##% /r-   N)NT)r   ) )__name__
__module____qualname____doc___CONF_NAME_MAPr#   r%   r9   r   r=   r@   rF   r'   rW   rZ   ra   rj   __classcell__r+   s   @r,   r    r    %   sU    G
 N6# 26+/@ +4T 2
,
<%%@ r-   r    c                       e Zd ZdZd Zd Zy)PluginWindowz2MainWindow subclass that contains a Spyder Plugin.c                 x    t        j                  |        || _        | j                  t	        t
                     y rk   )r	   r#   pluginsetStyleSheetr5   r   )r(   rw   s     r,   r#   zPluginWindow.__init__   s,    T" 	3~./r-   c                    | j                   j                  | j                   j                         | j                   j                  j	                  | j                          | j                   j                  j                  d       | j                   j                          | j                         }| j                   j                  dt        |             t        j                  | |       | | j                   _        d| j                   _        y)zReimplement Qt method.Twindow_geometryN)rw   set_ancestorrB   rc   	setWidget
setVisibleswitch_to_pluginsaveGeometry
set_optionr   r	   
closeEvent_PluginWindow__unsafe__window_undocked_window)r(   eventgeometrys      r,   r   zPluginWindow.closeEvent   s      !1!12((5))$/$$& $$&02CH2MN 	tU+
 (,$'+$r-   N)rm   rn   ro   rp   r#   r    r-   r,   ru   ru      s    <0,r-   ru   c                       e Zd ZdZd fd	Zd Zd Zd Zd Zd Z	 e
       d        Zdd	Zd
 Zd Z e
       d        Z e
       d        Z e
e      d        Zd Zd Zd Zd Zd Zd Zd Z e
e      d        Z xZS )BasePluginWidgetMixinzN
    Implementation of the basic functionality for Spyder plugin widgets.
    c                 |   t         t        |           d | _        d | _        d| _        d | _        d| _        d | _        t        | t        d      t        j                  d            | _        | j                  j                  t        j                          t#        |       | _        d | _        t)        | t        d      t        j                  d      t        d      | j*                        | _        t)        | t        d	      t        d
      t        j                  d      | j.                        | _        t)        | t        d      t        j                  d      t        d      | j2                        | _        t)        | t        d      t        j                  d      t        d      | j6                        | _        y )NFOptionstooloptions)texticonDockdockzDock the pane)r   tip	triggeredUnlock position'Unlock to move pane to another positiondrag_dock_widget)r   r   r   r   UndockundockzUndock the paneClose
close_panezClose the pane)r"   r   r#   _plugin_actionsr   _ismaximized_default_margins
_isvisibleshortcutr   r   r   r   options_buttonsetPopupModer   InstantPopupr
   _options_menu_toggle_view_actionr   _close_window_dock_action_lock_unlock_position_lock_unlock_action_create_window_undock_action_plugin_closed_close_plugin_action)r(   r)   r+   s     r,   r#   zBasePluginWidgetMixin.__init__   sw   #T35  $ !%! $ 01Y<58XXm5LN(()A)AB #4[
 $(  *fI&!/"((* $1$%;<,-00$
  ,hK(##$))+ %2gJ,'"#))%+!r-   c                     | j                  dd      r$	 | j                          | j                  dd       yy# t        $ r Y yw xY w)z
        If this is the first time the plugin is shown, perform actions to
        initialize plugin position in Spyder's window layout.

        Use on_first_registration to define the actions to be run
        by your plugin
        
first_timeTNF)
get_optionon_first_registrationNotImplementedErrorr   r0   s    r,   '_initialize_plugin_in_mainwindow_layoutz=BasePluginWidgetMixin._initialize_plugin_in_mainwindow_layout   sL     ??<.**, OOL%0 / ' s   8 	AAc                    | j                         }| j                  |j                         | _        t        j                  dd      r*t        j                  dd      } |j
                  |gdz    y |j
                  | j                    y)zUpdate plugin marginsNrB   use_custom_margincustom_margin   )layoutr   getContentsMarginsr   r;   setContentsMargins)r(   r   margins      r,   _update_marginsz%BasePluginWidgetMixin._update_margins   su      ($*$=$=$?D!88F/0XXfo6F%F%%xz2%F%%t'<'<=r-   c                     | j                   | j                   }n| j                  | j                  }ny|j                  | j                                y)z4Update plugin title, i.e. dockwidget or window titleN)rc   r   rL   get_plugin_title)r(   wins     r,   _update_plugin_titlez*BasePluginWidgetMixin._update_plugin_title   sF    ??&//C"".''C40023r-   c                 f   t        | j                         | j                        }|j                  | j                  j
                  dz          |j                  |j                         |j                  |j                         |j                  |        | j                          |j                  j                  | j                         |j                  j                  | j                          |j"                  j                  | j$                         |j&                  j                  | j(                         || _        	 d}dj-                  | j.                        }t1        j2                  ||| j.                        | _        | j4                  l| j                  `t=        t?        | j4                        | j                  | j@                        }| jC                  |ddj-                  | j.                               ||jD                  fS # t6        j8                  t6        j:                  f$ r Y w xY w)z*Add to parent QMainWindow as a dock widget_dwr   zswitch to {})plugin_namezSwitch to {})#r   r   rB   setObjectNamer+   rm   setAllowedAreasALLOWED_AREASsetFeaturesFEATURESr|   r   visibilityChangedconnect_visibility_changedtopLevelChanged_on_top_level_changedsig_plugin_closedr   sig_title_bar_shown_on_title_bar_shownrc   formatr3   r   get_shortcutr   configparserNoSectionErrorNoOptionErrorr   r   r~   register_shortcutLOCATION)r(   r   contextnamescs        r,   _create_dockwidgetz(BasePluginWidgetMixin._create_dockwidget  s      5 5 7C 	4>>22589T//0't&&t'?'?@$$T%?%?@&&t':':;  (()A)AB
	G!(():):;D --gt:>:K:KMDM
 ==$)><6		002B""2sN,A,A!!-# $ dmm$$ ++\-G-GH 		s    AH
 
#H0/H0c                 :    | j                   j                  |        y)zSwitch to plugin.N)rB   r~   r0   s    r,   _switch_to_pluginz'BasePluginWidgetMixin._switch_to_plugin)  s    		""4(r-   c                 T    | j                   r| j                   j                  d       yy)zDockWidget was closed.FN)r   
setCheckedr0   s    r,   r   z$BasePluginWidgetMixin._plugin_closed-  s&     ##$$//6 $r-   c                 Z    |rd}| j                   }nd}| j                  }t        ||      S )zReturn plugin font.	rich_fontfont)r6   font_size_delta)RICH_FONT_SIZE_DELTAFONT_SIZE_DELTAr   )r(   	rich_textr6   r   s       r,   	_get_fontzBasePluginWidgetMixin._get_font3  s2     F"77OF"22OvGGr-   c                     t        d      )z
        Set plugin font option.

        Note: All plugins in Spyder use a global font. To define a different
        size, the plugin must define a 'FONT_SIZE_DELTA' class variable.
        zrPlugins font is based on the general settings, and cannot be set directly on the plugin.This method is deprecated.)	Exceptionr0   s    r,   set_plugin_fontz%BasePluginWidgetMixin.set_plugin_font>  s      5 6 	6r-   c                      j                         } j                  dk(  rt        d      } j                  <t	         | fdt         j                        t        j                        }| _        yt	         | fd      }| _        y)z/Associate a toggle view action with each plugineditorEditorNc                 &    j                  |       S rk   toggle_viewcheckedr(   s    r,   <lambda>zBBasePluginWidgetMixin._create_toggle_view_action.<locals>.<lambda>P  s    T5E5Eg5N r-   )toggledr   r   c                 &    j                  |       S rk   r   r   s    r,   r   zBBasePluginWidgetMixin._create_toggle_view_action.<locals>.<lambda>T  s    040@0@0I r-   )r   )	r   r3   r   r   r   r   r   WidgetShortcutr   )r(   titleactions   `  r,   _create_toggle_view_actionz0BasePluginWidgetMixin._create_toggle_view_actionI  s    %%'(hKE==$"4%N&24==&A%'%6%68F $*  #4 9J KF#) r-   c                 6   | j                   | j                   j                         }| j                  dt        |             | j                   j	                          d| _         | j
                  j                  d       | j                  j                  d       yy)z5Close QMainWindow instance that contains this plugin.Nrz   F)r   r   r   r   closer   setDisabledr   )r(   r   s     r,   r   z#BasePluginWidgetMixin._close_windowX  s       , ,,99;HOO-/@/JK!!'')$(D! ++E2%%11%8 -r-   c                    t        |       x| _        }|j                  t        j                         | j                         }t        |      r| j                  |      }|j                  |       |j                  | j                                |j                  |        |j                  | j                                | j                  dd      }|rA	 |j                  t!               j#                  t%        |      j'                  d                   | j+                          | j-                  |       | j.                  j1                  d       | j.                  j3                  d       |j5                          y# t(        $ r Y sw xY w)z5Create a QMainWindow instance containing this plugin.rz   rl   )r<   zutf-8FN)ru   r   rJ   r   rK   get_plugin_iconr   get_iconsetWindowIconrL   r   setCentralWidgetresizesizer   restoreGeometryr   fromHexr5   encoder   refresh_pluginr{   rc   setFloatingr}   rP   )r(   windowr   r   s       r,   r   z$BasePluginWidgetMixin._create_windowi  s:    *6d);;B//0##%$==&DT"d3356%diik" ??#4b?A&&L((X)=)=g)FG 	&!##E*""5)  s   A E< <	FFc                 v    |r| j                   j                  d       y| j                   j                  d       y)z9Actions to perform when a plugin is undocked to be moved.TFN)r   r   )r(   	top_levels     r,   r   z+BasePluginWidgetMixin._on_top_level_changed  s/     ++D1++E2r-   c                    | j                   y|rH| j                   j                          | j                         }|| j                  |j	                          | j                   j                         xs | j                  }| j                  rt        | j                  |       |xr || _
        | j                  r| j                          yy)z#Dock widget visibility has changed.N)rc   raise_get_focus_widgetr   setFocus	isVisibler   DISABLE_ACTIONS_WHEN_HIDDENr   r   r   r  )r(   enablewidgetvisibles       r,   r   z)BasePluginWidgetMixin._visibility_changed  s    ??"OO""$**,F!d&;&;&G!//++-B1B1B++4//9 ,W??! r-   c                    | j                   j                          | j                  )t        | j                  | j
                  | j                  g}nt        | j                  g}| j                         |z   | _	        t        | j                   | j                         t        j                  dk(  rt        | j                   d       yy)zRefresh Options menu.NdarwinT)r   clearr   r   r   r   r   r   get_plugin_actionsr   r   sysplatformr   )r(   additional_actionss     r,   _refresh_actionsz&BasePluginWidgetMixin._refresh_actions  s      "   ("0"&":":"&"5"5"&";";"=
 #1"&"3"3"5  $668;MMD&&(<(<=<<8#4--t4 $r-   c                    | j                          | j                         t        | j                  | j                  gz   | _        t        | j                  | j
                         | j                  j                  | j                         | j                  j                  j                  | j                         t        j                  dk(  r2| j                  j                  j                  | j                  fd       | j                   j                  | j"                         | j%                  | j'                                y)zO
        Setup Options menu, create toggle action and connect signals.
        r  c                     t        | d      S )NF)r   )menus    r,   r   z.BasePluginWidgetMixin._setup.<locals>.<lambda>  s    tU+ r-   N)r   r  r   r   r   r   r   r   r   setMenuaboutToShowr   r  r  r  aboutToHidesig_update_plugin_titler   rL   r   r0   s    r,   _setupzBasePluginWidgetMixin._setup  s    
 	'') ##%T55t7J7JKL 	 	D&&(<(<=##D$6$67&&..t/D/DE <<8#**22 .. ,-
 	$$,,T-F-FGD1134r-   c                 @    t        t        j                  dd            S )zGet the current color scheme.
appearanceselected)r   r   r;   r0   s    r,   _get_color_schemez'BasePluginWidgetMixin._get_color_scheme  s    z BCCr-   c                 z    | j                   j                  |        | j                  dk7  r| j                          yy)z0Add dockwidget to the main window and set it up.r   N)rB   add_dockwidgetr3   r!  r0   s    r,   _add_dockwidgetz%BasePluginWidgetMixin._add_dockwidget  s2    		  & (KKM )r-   c                 P    | j                   j                  j                  ||        y)z$Tabify plugin next to a core plugin.N)rB   layoutstabify_plugins)r(   core_plugins     r,   _tabifyzBasePluginWidgetMixin._tabify  s    		((d;r-   c                     t        | j                  j                         t              r| j                  j	                          y| j                  j                          y)z*Show/hide title bar to move/lock position.N)
isinstancerc   titleBarWidgetr   remove_title_barset_title_barr0   s    r,   r   z+BasePluginWidgetMixin._lock_unlock_position  s9    doo446EOO,,.OO))+r-   c                 
   |r| j                   j                  t        d             | j                   j                  t	        j
                  d             dD ])  }t        | j                   |      } |t        d             + y| j                   j                  t        d             | j                   j                  t	        j
                  d             dD ])  }t        | j                   |      } |t        d             + y)	z6Actions to perform when the title bar is shown/hidden.zLock position	lock_open)
setToolTipsetStatusTipz!Lock pane to the current positionr   r   r   N)r   rM   r   setIconr   r   getattr)r(   r  method_namemethods       r,   r   z)BasePluginWidgetMixin._on_title_bar_shown  s     $$,,Q-?@$$,,SXXk-BC= ? !9!9;Gq<=>? $$,,Q/@-AB$$,,SXX6H-IJ= E !9!9;GqBCDEr-   rk   )F)rm   rn   ro   rp   r#   r   r   r   r   r   r   r   r   r   r   r   r   boolr   r   r  r!  r%  r(  r-  r   r   rr   rs   s   @r,   r   r      s    9+v1	>4"%H) 
V7 7
	H	6* 
V9 9  
V < 
$Z3 3" 5*54D<, 
$ZE Er-   r   )1rp   r   r_   r\   r  qtpy.QtCorer   r   r   
qtpy.QtGuir   r   qtpy.QtWidgetsr   r	   r
   r   r   r   spyder.config.baser   spyder.config.guir   r   spyder.config.managerr   spyder.config.userr   spyder.py3compatr   r   spyder.utils.icon_managerr   spyder.utils.qthelpersr   r   r   r   r   r   spyder.utils.stylesheetr   spyder.widgets.dockr   r   objectr    ru   r   r   r-   r,   <module>rI     s   
   	 
 - , ,4 4 ! 8 & ( > )$ $ 3 >Y f Y x,; ,@TEF TEr-   