
    G@dQ5                        d Z ddlmZ ddlmZ ddlmZmZmZm	Z	m
Z
 ddl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 ddlmZ ddlmZ ddlmZ ddlm Z  eeef         Z!ee!ee"         ee"         ee"         f         Z# G d d          Z$ G d d          Z% G d d          Z& G d de          Z' G d de          Z(dS )z
Toolbar Container.
    )OrderedDict)SpyderAction)OptionalUnionTupleDictList)QSizeSlot)QActionQWidget)PYSIDE2)SpyderAPIError)_)PluginMainContainer)get_class_values)ApplicationToolbar)ApplicationToolbars)TOOLBAR_REGISTRYc                       e Zd ZdZdS )ToolbarMenustoolbars_menuN)__name__
__module____qualname__ToolbarsMenu     @lib/python3.11/site-packages/spyder/plugins/toolbar/container.pyr   r   $           "LLLr   r   c                       e Zd ZdZdZdS )ToolbarsMenuSectionsmain_sectionsecondary_sectionN)r   r   r   Main	Secondaryr   r   r   r"   r"   (   s        D#IIIr   r"   c                       e Zd ZdZdS )ToolbarActionszshow toolbarsN)r   r   r   ShowToolbarsr   r   r   r(   r(   -   r    r   r(   c                   H    e Zd ZdZed             Zej        d             ZdS )	QActionIDzBWrapper class around QAction that allows to set/get an identifier.c                     | j         S N
_action_idselfs    r   	action_idzQActionID.action_id3   s
    r   c                     || _         d S r-   r.   )r1   acts     r   r2   zQActionID.action_id7   s    r   N)r   r   r   __doc__propertyr2   setterr   r   r   r+   r+   1   sR        LL  X     r   r+   c                   2    e Zd Zd fd	Zd Zd Z e            d             Zd Zd Z	d Z
d	ed
edefdZddZdd	efdZ	 	 	 	 	 dded	ee         dee         dee         dee         defdZ	 dded	ee         fdZd	edefdZd Zd Zd Zd Z xZS )ToolbarContainerNc                     t                                          |||           t                      | _        t                      | _        g | _        g | _        i | _        d S )N)parent)super__init__r   _APPLICATION_TOOLBARS_ADDED_TOOLBARS_toolbarslist_visible_toolbars_ITEMS_QUEUE)r1   namepluginr;   	__class__s       r   r=   zToolbarContainer.__init__=   sX    vf555%0]]"*}}!#r   c                     g }| j         D ])}|                    |                                           *|                     d|           dS )z5Save the name of the visible toolbars in the options.last_visible_toolbarsN)rA   append
objectNameset_confr1   toolbarstoolbars      r   _save_visible_toolbarsz'ToolbarContainer._save_visible_toolbarsH   sU    - 	2 	2GOOG..001111-x88888r   c                     g }| j         D ]A}|                                                                r||vr|                    |           B|| _        dS )zCollect the visible toolbars.N)r@   toggleViewAction	isCheckedrH   rA   rK   s      r   _get_visible_toolbarsz&ToolbarContainer._get_visible_toolbarsP   se    ) 	) 	)G((**4466 )x//(((!)r   c                    |                      d           }|                     d|           |r|                                  n|                                  | j        D ]}|                    |           |                                  dS )zShow/Hide toolbars.toolbars_visibleN)get_confrJ   rN   rR   rA   
setVisibleupdate_actions)r1   valuerM   s      r   _show_toolbarszToolbarContainer._show_toolbarsZ   s     MM"4555(%000 	)''))))&&(((- 	& 	&Gu%%%%r   c                     || j         v r=| j                             |          }|D ]"\  }}}}|                    ||||           !d S d S )N)sectionbeforebefore_section)rB   popadd_item)r1   rM   
toolbar_idpending_itemsitemr[   r\   r]   s           r   _add_missing_toolbar_elementsz.ToolbarContainer._add_missing_toolbar_elementsi   s    *** -11*==M9F @ @5gv~  wv0> ! @ @ @ @ +*@ @r   c                    |                      t          j        t          d          | j                  | _        |                     t          j        t          d                    | _	        | j	        
                    d           d S )NShow toolbars)text	triggeredToolbarszcheckbox-padding)create_actionr(   r)   r   rY   show_toolbars_actioncreate_menur   r   r   setObjectNamer0   s    r   setupzToolbarContainer.setupr   s    $($6$6'?##) %7 %
 %
! "--%jMM
 
 	(();<<<<<r   c                 J   |                      d          }|rt          d          }t          d          }nt          d          }t          d          }| j                            |           | j                            |           | j                            |           d S )NrT   zHide toolbarsre   )rU   r   rj   setText
setToolTipr   
setEnabled)r1   visible_toolbarsrf   tips       r   rW   zToolbarContainer.update_actions   s    ==);<< 	%_%%DO$$CC_%%DO$$C!))$///!,,S111%%&677777r   r`   titlereturnc                 6   || j         v r"t          d                    |                    t          | |          }||_        |                    |           t          j        ||| j        | j	                   || j         |<   | 
                    ||           |S )a  
        Create an application toolbar and add it to the main window.

        Parameters
        ----------
        toolbar_id: str
            The toolbar unique identifier string.
        title: str
            The localized toolbar title to be displayed.

        Returns
        -------
        spyder.api.widgets.toolbar.ApplicationToolbar
            The created application toolbar.
        #Toolbar with ID "{}" already added!)r>   r   formatr   IDrl   r   register_referencePLUGIN_NAMECONTEXT_NAMErc   )r1   r`   rt   rM   s       r   create_application_toolbarz+ToolbarContainer.create_application_toolbar   s    " 333 5<<ZHHJ J J %T511
j)))+Z!143D	F 	F 	F18":.**7J???r   c                    t          |t                    st          d          |j        }| t          dt	          |           d          || j        v r"t          d                    |                    d}|                    t          ||                     |	                    |           || j        |<   | j
                            |           |r|                    |           |                     ||           dS )a&  
        Add toolbar to application toolbars.

        Parameters
        ----------
        toolbar: spyder.api.widgets.toolbars.ApplicationToolbar
            The application toolbar to add to the `mainwindow`.
        mainwindow: QMainWindow
            The main application window.
        z-Any toolbar must subclass ApplicationToolbar!Nz	Toolbar `z` doesn't have an identifier!rw      )
isinstancer   r   ry   reprr?   rx   setIconSizer
   rl   r@   rH   
addToolBarrc   )r1   rM   
mainwindowr`   iconsizes        r   add_application_toolbarz(ToolbarContainer.add_application_toolbar   s,    '#566 	 ?  
 Z
 HDMMHHH   --- 5<<ZHHJ J J E(H55666j)))+2Z(!!'*** 	+!!'*****7J?????r   c                     || j         vr"t          d                    |                    | j                             |          }| j                            |           |r|                    |           dS dS )a  
        Remove toolbar from application toolbars.

        Parameters
        ----------
        toolbar: str
            The application toolbar to remove from the `mainwindow`.
        mainwindow: QMainWindow
            The main application window.
        z.Toolbar with ID "{}" is not in the main windowN)r?   r   rx   r^   r@   removeremoveToolBar)r1   r`   r   rM   s       r   remove_application_toolbarz+ToolbarContainer.remove_application_toolbar   s     T111 @GG   ! ! ! &**:66!!'*** 	.$$W-----	. 	.r   Frb   r[   r\   r]   omit_idc                     || j         vr@| j                            |g           }|                    ||||f           || j        |<   dS |                     |          }|                    |||||           dS )ap  
        Add action or widget `item` to given application toolbar `section`.

        Parameters
        ----------
        item: SpyderAction or QWidget
            The item to add to the `toolbar`.
        toolbar_id: str or None
            The application toolbar unique string identifier.
        section: str or None
            The section id in which to insert the `item` on the `toolbar`.
        before: str or None
            Make the item appear before another given item.
        before_section: str or None
            Make the item defined section appear before another given section
            (the section must be already defined).
        omit_id: bool
            If True, then the toolbar will check if the item to add declares an
            id, False otherwise. This flag exists only for items added on
            Spyder 4 plugins. Default: False
        )r[   r\   r]   r   N)r>   rB   getrH   get_application_toolbarr_   )	r1   rb   r`   r[   r\   r]   r   ra   rM   s	            r   add_item_to_application_toolbarz0ToolbarContainer.add_item_to_application_toolbar   s    8 T777 -11*bAAM  $!HIII,9Dj)))22:>>GT76,:G  M M M M Mr   item_idc                     || j         vr"t          d                    |                    |                     |          }|                    |           dS )a  
        Remove action or widget from given application toolbar by id.

        Parameters
        ----------
        item: str
            The item to remove from the `toolbar`.
        toolbar_id: str or None
            The application toolbar unique string identifier.
        z{} is not a valid toolbar_idN)r>   r   rx   r   remove_item)r1   r   r`   rM   s       r   $remove_item_from_application_toolbarz5ToolbarContainer.remove_item_from_application_toolbar  sg     T777 .55jAAC C C ..z::G$$$$$r   c           	          || j         vrGt          d                    |t          | j                                                                       | j         |         S )a,  
        Return an application toolbar by toolbar_id.

        Parameters
        ----------
        toolbar_id: str
            The toolbar unique string identifier.

        Returns
        -------
        spyder.api.widgets.toolbars.ApplicationToolbar
            The application toolbar.
        z@Application toolbar "{0}" not found! Available toolbars are: {1})r>   r   rx   listkeys)r1   r`   s     r   r   z(ToolbarContainer.get_application_toolbar&  se     T777 ..4f388::;;/ /   )*55r   c                     | j         S )z
        Return all created application toolbars.

        Returns
        -------
        list
            The list of all the added application toolbars.
        )r@   r0   s    r   get_application_toolbarsz)ToolbarContainer.get_application_toolbars?  s     !!r   c                     |                      d          r|                                  |                                  dS )z8Save the last visible toolbars state in our preferences.rT   N)rU   rR   rN   r0   s    r   save_last_visible_toolbarsz+ToolbarContainer.save_last_visible_toolbarsJ  sA    ==+,, 	)&&(((##%%%%%r   c                    |                      d          }|                      d          }|rQi }| j        D ]}|||                                <   g }|D ]!}||v r|                    ||                    "|| _        n|                                  | j        D ]}|                    |           |                                  dS )z4Load the last visible toolbars from our preferences.rG   rT   N)rU   r@   rI   rH   rA   rR   rV   rW   )r1   toolbars_namesrT   toolbars_dictrM   rL   rC   s          r   load_last_visible_toolbarsz+ToolbarContainer.load_last_visible_toolbarsP  s    '>??==);<< 	)M- > >6=g002233H& 9 9=((OOM$$7888%-D""&&(((- 	1 	1G/0000r   c                 L   t           j        }t           j        }t          t                    }| j                                        D ]]\  }}|rV|                                }t          st          |_
        d| |_        ||v r|n|}|                     || j        |           ^dS )zN
        Populate the toolbars menu inside the view application menu.
        toolbar_)menur[   N)r"   r%   r&   r   r   r?   itemsrP   r   r+   rE   r2   add_item_to_menur   )r1   r#   r$   default_toolbarsr`   rM   actionr[   s           r   create_toolbars_menuz%ToolbarContainer.create_toolbars_menuh  s     ,00:+,?@@#'#7#=#=#?#? 	 	J  1133 
1 (1F$#:j#:#:  "%555 !L*  %%+# &   +	 	r   r-   )NNNNF)r   r   r   r=   rN   rR   r   rY   rc   rm   rW   strr   r}   r   r   ToolbarItemr   boolr   r   r   r   r   r   r   __classcell__)rE   s   @r   r9   r9   <   s%            9 9 9* * * 
TVV  V@ @ @= = =8 8 8!*-2D   @'@ '@ '@ '@R. .S . . . .2 EIAE@DHL8=#M #M.9#M4<SM#M 2:##M 19	#M
 9A#M 26#M #M #M #ML JN% %C %9A#% % % %&6# 6:L 6 6 6 62	" 	" 	"& & &  0! ! ! ! ! ! !r   r9   N))r5   collectionsr   spyder.utils.qthelpersr   typingr   r   r   r   r	   qtpy.QtCorer
   r   qtpy.QtWidgetsr   r   qtpyr   spyder.api.exceptionsr   spyder.api.translationsr   !spyder.api.widgets.main_containerr   spyder.api.utilsr   spyder.api.widgets.toolbarsr   spyder.plugins.toolbar.apir   spyder.utils.registriesr   r   r   ItemInfor   r"   r(   r+   r9   r   r   r   <module>r      s4   
 $ # # # # # / / / / / / 5 5 5 5 5 5 5 5 5 5 5 5 5 5 $ # # # # # # # + + + + + + + +       1 0 0 0 0 0 % % % % % % A A A A A A - - - - - - : : : : : : : : : : : : 4 4 4 4 4 4 L')*hsmXc]HSMIJ# # # # # # # #$ $ $ $ $ $ $ $
# # # # # # # #       M M M M M* M M M M Mr   