
    G@dlG                     N   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-          Z0dS )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          |                                            |                                 \  }}|                                  d| _        |sd| _        |                     |           d S d S )NTF)superr    __init__check_compatibility_register_plugin_is_compatible_show_compatibility_message)selfparentr$   message	__class__s       3lib/python3.11/site-packages/spyder/plugins/base.pyr#   zBasePluginMixin.__init__-   s    ot$$--/// (,'?'?'A'A$W"" 	6"'D,,W55555	6 	6    c                 .    t          j        |            dS )zRegister plugin configuration.N)r   register_pluginr(   s    r,   r%   z BasePluginMixin._register_plugin:   s    T"""""r-   Tc                 f    || j         n|}t          j        |t          |          ||           dS )zSet option in spyder.iniN)recursive_notification)CONF_SECTIONr   setstr)r(   optionvaluesectionr2   s        r,   _set_optionzBasePluginMixin._set_option>   sM     (/$##G#f++u(>	@ 	@ 	@ 	@ 	@ 	@r-   c                 D    || 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x111r-   c                 F    || j         n|}t          j        ||           dS )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-   r   c                 `    | j                                                             ||           dS )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                 Z   t          |           }|                    t          j                   |                    t          j                   |                    d           |                    |           |                    t           j	                   |
                                 dS )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   s     &&
$$R[111 3444!!"78887###%%kn555r-   c                     |                      |           t          j        t          t          j                             t          j                     dS )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]   sH    
 	!!'***&wr}'='=>>>"$$$$$r-    c                     t          j                     |                     |d           t          j                     dS )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   sA     	*,,,!!'4!888"$$$$$r-   c                 n    t           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"    wwt~>>???r-   c                     | j         D| }|}t          | d          s|}|}|                      ||          }|                                 |S dS )z+Create configuration dialog box page widgetN
dockwidget)CONFIGWIDGET_CLASShasattr
initialize)r(   dlgmain_windowr)   rB   configwidgets         r,   _create_configwidgetz$BasePluginMixin._create_configwidgetr   sh    ".FD4.. # "2264@@L##%%% /.r-   N)NT)r   )rX   )__name__
__module____qualname____doc___CONF_NAME_MAPr#   r%   r9   r   r=   r@   rF   r'   rW   r[   rb   rk   __classcell__r+   s   @r,   r    r    %   s       GG
 N6 6 6 6 6 6# # # 26+/@ @ @ @ +4T 2 2 2 2
, , , ,
< < < <  % % %% % % %@ @ @             r-   r    c                       e Zd ZdZd Zd ZdS )PluginWindowz2MainWindow subclass that contains a Spyder Plugin.c                     t          j        |            || _        |                     t	          t
                               d S rl   )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                            d           | j                                          |                                 }| j                             dt          |                     t          j        | |           | | j         _        d| j         _        dS )zReimplement Qt method.Twindow_geometryN)rw   set_ancestorrB   rd   	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222((555))$///$$&&& $$&&02CH2M2MNNN 	tU+++
 (,$'+$$$r-   N)rm   rn   ro   rp   r#   r    r-   r,   ru   ru      s8        <<0 0 0, , , , ,r-   ru   c                   B    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.
    Nc                    t          t          |                                            d | _        d | _        d| _        d | _        d| _        d | _        t          | t          d          t          j        d                    | _        | 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          | j                  | _        t)          | t          d          t          j        d          t          d          | j                  | _        d S )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__   s   #T**33555  $ !%! $ 01Y<<58Xm5L5LN N N(()ABBB #4[[
 $(  *fII&!!/""(* * * $1$%%;<<,--0$
 $
 $
  ,hKK(###$$)+ + + %2gJJ,''"##)%+ %+ %+!!!r-   c                     |                      dd          r?	 |                                  n# t          $ r Y dS w xY w|                     dd           dS dS )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   sz     ??<.. 	1**,,,,&   OOL%00000	1 	1s   - 
;;c                     |                                  }| j        |                                | _        t          j        dd          r%t          j        dd          } |j        |gdz    dS  |j        | j          dS )zUpdate plugin marginsNrB   use_custom_margincustom_margin   )layoutr   getContentsMarginsr   r;   setContentsMargins)r(   r   margins      r,   _update_marginsz%BasePluginWidgetMixin._update_margins   s     ($*$=$=$?$?D!8F/00 	>Xfo66F%F%xz2222%F%t'<====r-   c                     | j         | j         }n| j        | j        }ndS |                    |                                            dS )z4Update plugin title, i.e. dockwidget or window titleN)rd   r   rL   get_plugin_title)r(   wins     r,   _update_plugin_titlez*BasePluginWidgetMixin._update_plugin_title   sQ    ?&/CC".'CCF4002233333r-   c                    t          |                                 | j                  }|                    | j        j        dz              |                    |j                   |                    |j	                   |
                    |            |                                  |j                            | j                   |j                            | j                   |j                            | j                   |j                            | j                   || _        	 d}d                    | j                  }t1          j        ||| j                  | _        n!# t6          j        t6          j        f$ r Y nw xY w| j        c| j        \t=          t?          | j                  | j        | j                   }| !                    |dd                    | j                             ||j"        fS )z*Add to parent QMainWindow as a dock widget_dwr   zswitch to {})plugin_nameNz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_shownrd   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 7CC 	4>258999T/000'''t&&t'?@@@$$T%?@@@&&t':;;; (()ABBB
	G!(():;;D -gt:>:KM M MDMM+\-GH 	 	 	D	 =$)><66	02 2B""2sN,A,A!-# -# $ $ $ dm$$s   +=E) )FFc                 :    | j                             |            dS )zSwitch to plugin.N)rB   r~   r0   s    r,   _switch_to_pluginz'BasePluginWidgetMixin._switch_to_plugin)  s    	""4(((((r-   c                 L    | j         r| j                             d           dS dS )zDockWidget was closed.FN)r   
setCheckedr0   s    r,   r   z$BasePluginWidgetMixin._plugin_closed-  s6     # 	7$//66666	7 	7r-   Fc                 N    |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  s9     	3 F"7OOF"2OvGG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 6 	6r-   c                                                       } j        dk    rt          d          } j        4t	           | fdt           j                  t          j                  }nt	           | fd          }| _        dS )z/Associate a toggle view action with each plugineditorEditorNc                 .                         |           S rl   toggle_viewcheckedr(   s    r,   <lambda>zBBasePluginWidgetMixin._create_toggle_view_action.<locals>.<lambda>P  s    T5E5Eg5N5N r-   )toggledr   r   c                 .                         |           S rl   r   r   s    r,   r   zBBasePluginWidgetMixin._create_toggle_view_action.<locals>.<lambda>T  s    040@0@0I0I r-   )r   )	r   r3   r   r   r   r   r   WidgetShortcutr   )r(   titleactions   `  r,   _create_toggle_view_actionz0BasePluginWidgetMixin._create_toggle_view_actionI  s    %%''((hKKE=$"4%N%N%N%N&24=&A&A%'%68 8 8FF
 #4 9J 9J 9J 9J K K KF#)   r-   c                 8   | j         | j                                         }|                     dt          |                     | j                                          d| _         | j                            d           | j                            d           dS dS )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/JKKK!'')))$(D! ++E222%11%88888 -,r-   c                    t          |           x| _        }|                    t          j                   |                                 }t          |          r|                     |          }|                    |           |	                    | 
                                           |                    |            |                    |                                            |                     dd          }|rf	 |                    t!                                          t%          |                              d                               n# t(          $ r Y nw xY w|                                  |                     |           | j                            d           | j                            d           |                                 dS )z5Create a QMainWindow instance containing this plugin.rz   rX   )r<   zutf-8FN)ru   r   rJ   r   rK   get_plugin_iconr   get_iconsetWindowIconrL   r   setCentralWidgetresizesizer   restoreGeometryr   fromHexr5   encoder   refresh_pluginr{   rd   setFloatingr}   rP   )r(   windowr   r   s       r,   r   z$BasePluginWidgetMixin._create_windowi  s    *6d););;B/000##%%$ 	'==&&DT"""d3355666%%%diikk""" ??#4b?AA 	&&LL((X)=)=g)F)FGG        	&!!!##E***""5)))s    AE 
E"!E"c                 v    |r| j                             d           dS | j                             d           dS )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  sF      	3++D11111++E22222r-   c                 t   | j         dS |rJ| j                                          |                                 }|| j        |                                 | j                                         p| j        }| j        rt          | j	        |           |o|| _
        | j
        r|                                  dS dS )z#Dock widget visibility has changed.N)rd   raise_get_focus_widgetr   setFocus	isVisibler   DISABLE_ACTIONS_WHEN_HIDDENr   r   r   r  )r(   enablewidgetvisibles       r,   r   z)BasePluginWidgetMixin._visibility_changed  s    ?"F 	"O""$$$**,,F!d&;&G!!!/++--B1B+ 	:4/999 ,W? 	"!!!!!	" 	"r-   c                 R   | j                                          | j        t          | j        | j        | j        g}nt          | j        g}|                                 |z   | _	        t          | j         | j	                   t          j        dk    rt          | j         d           dS dS )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"&";"=
 #1"&"3"5  $6688;MMD&(<===<8##4-t44444 $#r-   c                 >   |                                   |                                 t          | j        | j        gz   | _        t          | j        | j                   | j        	                    | j                   | j        j
                            | j                   t          j        dk    r'| j        j                            | j        fd           | j                            | j                   |                     |                                            dS )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5t7JKL 	 	D&(<===##D$6777&..t/DEEE <8##*22 . , , ,- - -
 	$,,T-FGGGD113344444r-   c                 F    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 BCCCr-   c                 |    | j                             |            | j        dk    r|                                  dS dS )z0Add dockwidget to the main window and set it up.r   N)rB   add_dockwidgetr3   r!  r0   s    r,   _add_dockwidgetz%BasePluginWidgetMixin._add_dockwidget  sA    	  &&& ((KKMMMMM )(r-   c                 F    | j         j                            ||            dS )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                                        t                    r| j                                         dS | j                                         dS )z*Show/hide title bar to move/lock position.N)
isinstancerd   titleBarWidgetr   remove_title_barset_title_barr0   s    r,   r   z+BasePluginWidgetMixin._lock_unlock_position  sV    do4466EE 	,O,,.....O))+++++r-   c                 "   |r| j                             t          d                     | j                             t	          j        d                     dD ]/}t          | j         |          } |t          d                     0dS | j                             t          d                     | j                             t	          j        d                     dD ]/}t          | j         |          } |t          d                     0dS )	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#     	E$,,Q-?-?@@@$,,SXk-B-BCCC= ? ? !9;GGq<==>>>>? ? $,,Q/@-A-ABBB$,,SX6H-I-IJJJ= E E !9;GGqBCCDDDDE Er-   rl   )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+ 9+ 9+ 9+ 9+ 9+v1 1 1	> 	> 	>4 4 4"% "% "%H) ) ) 
TVV7 7 V7
	H 	H 	H 	H	6 	6 	6* * * 
TVV9 9 V9  
TVV  V< 
T$ZZ3 3 Z3" " " 5 5 5*5 5 54D D D  < < <, , , 
T$ZZE E ZE E E 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 4 4 4 4 4 4 4 4 4 4 4 4 4 4 !           8 8 8 8 8 8 8 8 & & & & & & ( ( ( ( ( ( > > > > > > > > ) ) ) ) ) )$ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ 3 2 2 2 2 2 > > > > > > > >Y  Y  Y  Y  Y f Y  Y  Y x, , , , ,; , , ,@TE TE TE TE TEF TE TE TE TE TEr-   