
    G@d)V                     0   d Z ddlmZmZmZ ddlmZ ddlmZm	Z	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mZ ddlmZmZmZmZ  G d d          Z G d d          Z G d d          Z  G d d          Z! G d de!e ee          Z"dS )z
Spyder API Mixins.
    )AnyOptionalDict)Qt)QSizePolicyQToolBarQWidgetQToolButton)SpyderConfigurationObserver)SpyderAPIError
SpyderMenu)CONF)ima)create_actioncreate_toolbutton)ACTION_REGISTRYMENU_REGISTRYTOOLBAR_REGISTRYTOOLBUTTON_REGISTRYc            	           e Zd ZdZ	 	 	 	 ddZ	 	 ddedee         dee         d	efd
Z	 	 ddee         dee         d	e	eef         fdZ
dS )SpyderToolButtonMixinz=
    Provide methods to create, add and get toolbuttons.
    NTFc                    |rt          |          sd }||		|
| j        }	t          | |d|||||||	|
|| j        | j        d          }||_        |r.|	,|
*t          j        |	|
          }|                    |           |S )z-
        Create a Spyder toolbutton.
        c                     d S N values    9lib/python3.11/site-packages/spyder/api/widgets/mixins.py<lambda>z9SpyderToolButtonMixin.create_toolbutton.<locals>.<lambda>-       D     NT)textshortcuticontiptoggled	triggered	autoraisetext_beside_iconsectionoptionid_plugincontext_nameregister_toolbutton)	callableCONF_SECTIONr   PLUGIN_NAMECONTEXT_NAMEnamer   get
setChecked)selfr5   r#   r%   r&   r'   r(   r)   r*   r+   r,   
toolbuttonr   s                r   r   z'SpyderToolButtonMixin.create_toolbutton%   s      	)8G,, 	)((G6#5+&-#* $
 
 

" 
 	-"v'9&11%%e,,,r"   r5   contextr.   returnc                 Z    || j         n|}|| j        n|}t          j        |||          S )a/  
        Return toolbutton by name, plugin and context.

        Parameters
        ----------
        name: str
            Name of the toolbutton to retrieve.
        context: Optional[str]
            Widget or context identifier under which the toolbutton was stored.
            If None, then `CONTEXT_NAME` is used instead
        plugin: Optional[str]
            Name of the plugin where the toolbutton was defined. If None, then
            `PLUGIN_NAME` is used.

        Returns
        -------
        toolbutton: QToolButton
            The corresponding toolbutton stored under the given `name`,
            `context` and `plugin`.

        Raises
        ------
        KeyError
            If either of `name`, `context` or `plugin` keys do not exist in the
            toolbutton registry.
        )r3   r4   r   get_referencer8   r5   r:   r.   s       r   get_toolbuttonz$SpyderToolButtonMixin.get_toolbuttonM   s;    8 &,^!!'.$##G"0vwGGGr"   c                 X    || j         n|}|| j        n|}t          j        ||          S )a_  
        Return all toolbuttons defined by a context on a given plugin.

        Parameters
        ----------
        context: Optional[str]
            Widget or context identifier under which the toolbuttons were
            stored. If None, then `CONTEXT_NAME` is used instead
        plugin: Optional[str]
            Name of the plugin where the toolbuttons were defined.
            If None, then `PLUGIN_NAME` is used.

        Returns
        -------
        toolbuttons: Dict[str, QToolButton]
            A dictionary that maps string keys to their corresponding
            toolbuttons.
        )r3   r4   r   get_referencesr8   r:   r.   s      r   get_toolbuttonsz%SpyderToolButtonMixin.get_toolbuttonsm   s9    ( &,^!!'.$##G"1&'BBBr"   )	NNNNNTFNNNN)__name__
__module____qualname____doc__r   strr   r
   r?   r   rC   r   r"   r   r   r       s          7;<@;@/3& & & &P BF/3H H3 H# H'}H8CH H H H@ 8<04C Cx} C (C9=c;>N9OC C C C C Cr"   r   c            	           e Zd ZdZ	 	 ddZddZdedefdZ	 	 ddede	e         d	e	e         defd
Z
	 	 dde	e         d	e	e         deeef         fdZdS )SpyderToolbarMixinz:
    Provide methods to create, add and get toolbars.
    Nc                 6    |                     |||           dS )z
        If you provide a `before` action, the action will be placed before this
        one, so the section option will be ignored, since the action will now
        be placed in the same section as the `before` action.
        r+   beforeN)add_item)r8   action_or_widgettoolbarr+   rN   s        r   add_item_to_toolbarz&SpyderToolbarMixin.add_item_to_toolbar   s&     	)76JJJJJr"   c                     t                      }|                    t          j        t          j                   |||_        |S )zG
        Create a stretcher widget to be used in a Qt toolbar.
        )r	   setSizePolicyr   	ExpandingID)r8   r-   	stretchers      r   create_stretcherz#SpyderToolbarMixin.create_stretcher   s;     II	 5{7LMMM?ILr"   r5   r;   c                 f    t          |           }t          j        ||| j        | j                   |S )z*
        Create a Spyder toolbar.
        )r   r   register_referencer3   r4   )r8   r5   rQ   s      r   create_toolbarz!SpyderToolbarMixin.create_toolbar   s;     4..+T4+T->	@ 	@ 	@r"   r:   r.   c                 Z    || j         n|}|| j        n|}t          j        |||          S )a  
        Return toolbar by name, plugin and context.

        Parameters
        ----------
        name: str
            Name of the toolbar to retrieve.
        context: Optional[str]
            Widget or context identifier under which the toolbar was stored.
            If None, then `CONTEXT_NAME` is used instead
        plugin: Optional[str]
            Name of the plugin where the toolbar was defined. If None, then
            `PLUGIN_NAME` is used.

        Returns
        -------
        toolbar: QToolBar
            The corresponding toolbar stored under the given `name`, `context`
            and `plugin`.

        Raises
        ------
        KeyError
            If either of `name`, `context` or `plugin` keys do not exist in the
            toolbar registry.
        )r3   r4   r   r=   r>   s       r   get_toolbarzSpyderToolbarMixin.get_toolbar   s;    8 &,^!!'.$##G-dFGDDDr"   c                 X    || j         n|}|| j        n|}t          j        ||          S )aA  
        Return all toolbars defined by a context on a given plugin.

        Parameters
        ----------
        context: Optional[str]
            Widget or context identifier under which the toolbars were stored.
            If None, then `CONTEXT_NAME` is used instead
        plugin: Optional[str]
            Name of the plugin where the toolbars were defined. If None, then
            `PLUGIN_NAME` is used.

        Returns
        -------
        toolbars: Dict[str, QToolBar]
            A dictionary that maps string keys to their corresponding toolbars.
        )r3   r4   r   rA   rB   s      r   get_toolbarszSpyderToolbarMixin.get_toolbars   s9    & &,^!!'.$##G.vw???r"   rD   r   )rE   rF   rG   rH   rR   rX   rI   r   r[   r   r]   r   r_   r   r"   r   rK   rK      s         FJ#'K K K K	 	 	 	3 8     ?C,0E E Ehsm E$SME5=E E E E@ 59-1@ @HSM @%c]@6:3=6I@ @ @ @ @ @r"   rK   c            	           e Zd ZdZ	 	 ddZddZ	 	 ddedee         dee         defd	Z		 	 ddee         dee         de
eef         fd
ZdS )SpyderMenuMixinz
    Provide methods to create, add and get menus.

    This mixin uses a custom menu object that allows for the creation of
    sections in a simple way.
    Nc                 ~    t          |t                    st          d          |                    |||           dS )z>
        Add a SpyderAction or a QWidget to the menu.
        z'Menu must be an instance of SpyderMenu!rM   N)
isinstancer   r   
add_action)r8   action_or_menumenur+   rN   s        r   add_item_to_menuz SpyderMenuMixin.add_item_to_menu   sD    
 $
++ 	L !JKKKGGGGGr"   c                     ddl m}  || ||          }|<|                                                    d           |                    |           t          j        ||| j        | j                   |S )a7  
        Create a menu.

        Parameters
        ----------
        name: str
            Unique str identifier.
        text: str or None
            Localized text string.
        icon: QIcon or None
            Icon to use for the menu.

        Return: QMenu
            Return the created menu.
        r   r   )parenttitlemenu_idNT)	spyder.api.widgets.menusr   
menuActionsetIconVisibleInMenusetIconr   rZ   r3   r4   )r8   r5   r#   r%   r   rf   s         r   create_menuzSpyderMenuMixin.create_menu   s      	877777zT4@@@OO224888LL($($*;	= 	= 	=r"   r5   r:   r.   r;   c                 Z    || j         n|}|| j        n|}t          j        |||          S )a  
        Return a menu by name, plugin and context.

        Parameters
        ----------
        name: str
            Name of the menu to retrieve.
        context: Optional[str]
            Widget or context identifier under which the menu was stored.
            If None, then `CONTEXT_NAME` is used instead
        plugin: Optional[str]
            Name of the plugin where the menu was defined. If None, then
            `PLUGIN_NAME` is used.

        Returns
        -------
        menu: SpyderMenu
            The corresponding menu stored under the given `name`, `context`
            and `plugin`.

        Raises
        ------
        KeyError
            If either of `name`, `context` or `plugin` keys do not exist in the
            menu registry.
        )r3   r4   r   r=   r>   s       r   get_menuzSpyderMenuMixin.get_menu  s;    8 &,^!!'.$##G*4AAAr"   c                 X    || j         n|}|| j        n|}t          j        ||          S )a4  
        Return all menus defined by a context on a given plugin.

        Parameters
        ----------
        context: Optional[str]
            Widget or context identifier under which the menus were stored.
            If None, then `CONTEXT_NAME` is used instead
        plugin: Optional[str]
            Name of the plugin where the menus were defined. If None, then
            `PLUGIN_NAME` is used.

        Returns
        -------
        menus: Dict[str, SpyderMenu]
            A dictionary that maps string keys to their corresponding menus.
        )r3   r4   r   rA   rB   s      r   	get_menuszSpyderMenuMixin.get_menus-  s9    & &,^!!'.$##G+FG<<<r"   rD   )rE   rF   rG   rH   rg   rp   rI   r   r   rr   r   rt   r   r"   r   ra   ra      s          >B $H H H H   6 <@)-B BS B8C= B!#B2<B B B B@ 26*.= =# ="3-=37Z3H= = = = = =r"   ra   c                       e Zd ZdZd Zddddddej        dddddddddfdZ	 	 dded	e	e         d
e	e         de
fdZ	 	 dd	e	e         d
e	e         defdZd ZdS )SpyderActionMixinzw
    Provide methods to create, add and get actions in a unified way.

    This mixin uses a custom action object.
    c                     |                      d           	 |                     |                              |           n# t          $ r Y nw xY w|                      d           dS )a  
        This allows to update the state of a togglable action without emitting
        signals.

        This is useful when the global application configuration changes and
        we need to propagate the current state of an action based on those
        changes
        TFN)blockSignals
get_actionr7   r   )r8   action_namer   s      r   _update_action_statez&SpyderActionMixin._update_action_stateL  sz     	$	OOK((33E:::: 	 	 	D	%     s   (A   
AAN FTc                    ||t          d          || }|rt          |          sd }||	|| j        }t          |||||||	|||| j        || j        n||||          }||_        |r|                    |           t          |          |_	        ||_
        ||_        ||_        |
*|r|                    |
           nB|rt          d          n0|r.|,|*t          j        ||          }|                    |           |S )a  
        name: str
            unique identifiable name for the action
        text: str
           Localized text for the action
        icon: QIcon,
            Icon for the action when applied to menu or toolbutton.
        icon_text: str
            Icon for text in toolbars. If True, this will also disable
            the tooltip on this toolbutton if part of a toolbar.
        tip: str
            Tooltip to define for action on menu or toolbar.
        toggled: Optional[Union[Callable, bool]]
            If True, then the action modifies the configuration option on the
            section specified. Otherwise, it should be a callable to use
            when toggling this action. If None, then the action does not
            behave like a checkbox.
        triggered: callable
            The callable to use when triggering this action.
        shortcut_context: str
            Set the `str` context of the shortcut.
        context: Qt.ShortcutContext
            Set the context for the shortcut.
        initial: object
            Sets the initial state of a togglable action. This does not emit
            the toggled signal.
        section: Optional[str]
            Name of the configuration section whose option is going to be
            modified. If None, and `option` is not None, then it defaults to
            the class `CONF_SECTION` attribute.
        option: ConfigurationKey
            Name of the configuration option whose value is reflected and
            affected by the action.
        register_shortcut: bool, optional
            If True, main window will expose the shortcut in Preferences.
            The default value is `False`.
        parent: QWidget (None)
            Define the parent of the widget. Use `self` if not provided.
        register_action: bool, optional
            If True, the action will be registered and searchable.
            The default value is `True`.
        overwrite: bool, optional
            If True, in case of action overwriting no warning will be shown.
            The default value is `False`
        context_name: Optional[str]
            Name of the context that holds the action in case of registration.
            The combination of `name` and `context_name` is unique so trying
            to register an action with the same `name` and `context_name` will
            cause a warning unless `overwrite` is set to `True`.
        menurole: QAction.MenuRole, optional
            Menu role for the action (it only has effect on macOS).

        Notes
        -----
        There is no need to set shortcuts right now. We only create actions
        with this (and similar methods) and these are then exposed as possible
        shortcuts on plugin registration in the main window with the
        register_shortcut argument.

        If icon_text is True, this will also disable the tooltip.

        If a shortcut is found in the default config then it is assigned,
        otherwise it's left blank for the user to define one for it.
        Nz8Action must provide the toggled or triggered parameters!c                     d S r   r   r   s    r   r    z1SpyderActionMixin.create_action.<locals>.<lambda>  r!   r"   )r#   r%   r&   r'   r(   r:   r+   r,   r-   r.   r/   register_action	overwritemenurolez3Initial values can only apply to togglable actions!)r   r1   r2   r   r3   r4   r5   setIconTextboolr*   shortcut_contextregister_shortcutr&   r7   r   r6   )r8   r5   r#   r%   	icon_textr&   r'   r(   r   r:   initialr   r+   r,   ri   r   r   r/   r   actionr   s                        r   r   zSpyderActionMixin.create_action`  s   L  J   >F 	)8G,, 	)((G6#5+#%1%9!!|+!
 
 
$  	*y)))"&y//"2#4 
 K!!'**** K$IK K KK  -&6+= HWf55E%%e,,,r"   r5   r:   r.   r;   c                 Z    || j         n|}|| j        n|}t          j        |||          S )a  
        Return an action by name, context and plugin.

        Parameters
        ----------
        name: str
            Name of the action to retrieve.
        context: Optional[str]
            Widget or context identifier under which the action was stored.
            If None, then `CONTEXT_NAME` is used instead
        plugin: Optional[str]
            Name of the plugin where the action was defined. If None, then
            `PLUGIN_NAME` is used.

        Returns
        -------
        action: SpyderAction
            The corresponding action stored under the given `name`, `context`
            and `plugin`.

        Raises
        ------
        KeyError
            If either of `name`, `context` or `plugin` keys do not exist in the
            action registry.
        )r3   r4   r   r=   r>   s       r   ry   zSpyderActionMixin.get_action  s;    8 &,^!!'.$##G,T67CCCr"   c                 X    || j         n|}|| j        n|}t          j        ||          S )a  
        Return all actions defined by a context on a given plugin.

        Parameters
        ----------
        context: Optional[str]
            Widget or context identifier under which the actions were stored.
            If None, then `CONTEXT_NAME` is used instead
        plugin: Optional[str]
            Name of the plugin where the actions were defined. If None, then
            `PLUGIN_NAME` is used.

        Returns
        -------
        actions: Dict[str, SpyderAction]
            A dictionary that maps string keys to their corresponding actions.

        Notes
        -----
        1. Actions should be created once. Creating new actions on menu popup
           is *highly* discouraged.
        2. Actions can be created directly on a PluginMainWidget or
           PluginMainContainer subclass. Child widgets can also create
           actions, but they need to subclass SpyderWidgetMixin.
        3. PluginMainWidget or PluginMainContainer will collect any actions
           defined in subwidgets (if defined) and expose them in the
           get_actions method at the plugin level.
        4. Any action created this way is now exposed as a possible shortcut
           automatically without manual shortcut registration.
           If an option is found in the config system then it is assigned,
           otherwise it's left with an empty shortcut.
        5. There is no need to override this method.
        )r3   r4   r   rA   rB   s      r   get_actionszSpyderActionMixin.get_actions  s:    F &,^!!'.$##G-fg>>>r"   c                      t          d          )z
        Update the state of exposed actions.

        Exposed actions are actions created by the `create_action` method.
        r|   )NotImplementedError)r8   optionss     r   update_actionsz SpyderActionMixin.update_actions&  s     ""%%%r"   rD   )rE   rF   rG   rH   r{   r   WidgetWithChildrenShortcutr   rI   r   r   ry   dictr   r   r   r"   r   rv   rv   E  s        ! ! !( .2RT"dT ;T(-tD!45#'$| | | || >B+/D Ds DXc] D#C=D47D D D DB 48,0%? %?8C= %?$SM%?59%? %? %? %?N& & & & &r"   rv   c                   N     e Zd ZdZdZdZd fd	Zed             Zd Z	d Z
 xZS )SpyderWidgetMixinag  
    Basic functionality for all Spyder widgets and Qt items.

    This mixin does not include toolbar handling as that is limited to the
    application with the coreui plugin or the PluginMainWidget for dockable
    plugins.

    This provides a simple management of widget options, as well as Qt helpers
    for defining the actions a widget provides.
    Nc           	          dD ]B}t          | |d           /t          ||          rt          | |t          ||                     Ct                                                       d S )N)r2   r3   )getattrhasattrsetattrsuper__init__)r8   class_parentattr	__class__s      r   r   zSpyderWidgetMixin.__init__C  sr    3 	E 	EDtT4((0<.. ED$d(C(CDDDr"   c                 *    t          j        |           S )zG
        Create an icon by name using the spyder Icon manager.
        )r   r%   )r5   s    r   create_iconzSpyderWidgetMixin.create_iconL  s    
 x~~r"   c                     dS )z
        Update stylesheet and style of the widget.

        This method will be called recursively on all widgets on theme change.
        Nr   r8   s    r   update_stylezSpyderWidgetMixin.update_styleS  s	     	r"   c                     dS )z
        Update localization of the widget.

        This method will be called recursively on all widgets on language
        change.
        Nr   r   s    r   update_translationz$SpyderWidgetMixin.update_translation[  s	     	r"   r   )rE   rF   rG   rH   r3   r4   r   staticmethodr   r   r   __classcell__)r   s   @r   r   r   /  s        	 	 K L        \        r"   r   N)#rH   typingr   r   r   qtpy.QtCorer   qtpy.QtWidgetsr   r   r	   r
   spyder.api.config.mixinsr   spyder.api.exceptionsr   rl   r   spyder.config.managerr   spyder.utils.icon_managerr   spyder.utils.qthelpersr   r   spyder.utils.registriesr   r   r   r   r   rK   ra   rv   r   r   r"   r   <module>r      s5   
 ' & & & & & & & & &       F F F F F F F F F F F F A @ @ @ @ @ 0 0 0 0 0 0 / / / / / / & & & & & & ) ) ) ) ) ) C C C C C C C CK K K K K K K K K K K KcC cC cC cC cC cC cC cCLW@ W@ W@ W@ W@ W@ W@ W@tb= b= b= b= b= b= b= b=Jg& g& g& g& g& g& g& g&T3 3 3 3 3)?35J3 3 3 3 3r"   