
    JeQ5                     V   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(y)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y)ToolbarMenustoolbars_menuN)__name__
__module____qualname__ToolbarsMenu     B/usr/lib/python3/dist-packages/spyder/plugins/toolbar/container.pyr   r   $       "Lr   r   c                       e Zd ZdZdZy)ToolbarsMenuSectionsmain_sectionsecondary_sectionN)r   r   r   Main	Secondaryr   r   r   r"   r"   (   s    D#Ir   r"   c                       e Zd ZdZy)ToolbarActionszshow toolbarsN)r   r   r   ShowToolbarsr   r   r   r(   r(   -   r    r   r(   c                   D    e Zd ZdZed        Zej                  d        Zy)	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                     || _         y r-   r.   )r1   acts     r   r2   zQActionID.action_id7   s	    r   N)r   r   r   __doc__propertyr2   setterr   r   r   r+   r+   1   s1    L   r   r+   c                       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 )ToolbarContainerc                     t         |   |||       t               | _        t               | _        g | _        g | _        i | _        y )N)parent)super__init__r   _APPLICATION_TOOLBARS_ADDED_TOOLBARS_toolbarslist_visible_toolbars_ITEMS_QUEUE)r1   namepluginr;   	__class__s       r   r=   zToolbarContainer.__init__=   sC    vf5%0]"*}!#r   c                     g }| j                   D ]!  }|j                  |j                                # | j                  d|       y)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   sB    -- 	2GOOG..01	2 	-x8r   c                     g }| j                   D ]7  }|j                         j                         s"||vs'|j                  |       9 || _        y)zCollect the visible toolbars.N)r@   toggleViewAction	isCheckedrH   rA   rK   s      r   _get_visible_toolbarsz&ToolbarContainer._get_visible_toolbarsP   sO    )) 	)G((*446x/(	)
 "*r   c                     | j                  d       }| j                  d|       |r| j                          n| j                          | j                  D ]  }|j                  |        | j                          y)zShow/Hide toolbars.toolbars_visibleN)get_confrJ   rN   rR   rA   
setVisibleupdate_actions)r1   valuerM   s      r   _show_toolbarszToolbarContainer._show_toolbarsZ   sq     MM"455(%0'')&&(-- 	&Gu%	& 	r   c                     || j                   v r=| j                   j                  |      }|D ]  \  }}}}|j                  ||||        y y )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   sb    *** --11*=M9F @5gv~  wv0> ! @@ +r   c                 
   | j                  t        j                  t        d      | j                        | _        | j                  t        j                  t        d            | _	        | j                  j                  d       y )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   sp    $($6$6''?#)) %7 %
! "--%%jM
 	(();<r   c                 &   | j                  d      }|rt        d      }t        d      }nt        d      }t        d      }| j                  j                  |       | j                  j	                  |       | j
                  j                  |       y )NrT   zHide toolbarsre   )rU   r   rj   setText
setToolTipr   
setEnabled)r1   visible_toolbarsrf   tips       r   rW   zToolbarContainer.update_actions   sz    ==);<_%DO$C_%DO$C!!))$/!!,,S1%%&67r   r`   titlereturnc                 8   || j                   v rt        dj                  |            t        | |      }||_        |j                  |       t        j                  ||| 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J J %T51
j)++Z!1!143D3D	F18"":.**7J?r   c                    t        |t              st        d      |j                  }|t        dt	        |       d      || j
                  v rt        dj                  |            d}|j                  t        ||             |j                  |       || j
                  |<   | j                  j                  |       |r|j                  |       | j                  ||       y)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 ? 
 ZZ
 DM?*GH  --- 5<<ZHJ J E(H56j)+2Z(!!'*!!'***7J?r   c                     || j                   vrt        dj                  |            | j                   j                  |      }| j                  j                  |       |r|j                  |       yy)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   sr     T111 @GG ! ! &&**:6!!'*$$W- r   rb   r[   r\   r]   omit_idc                     || j                   vrA| j                  j                  |g       }|j                  ||||f       || j                  |<   y| j	                  |      }|j                  |||||       y)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AM  $!HI,9Dj)22:>GT76,:G  Mr   item_idc                     || j                   vrt        dj                  |            | j                  |      }|j	                  |       y)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  sO     T777 .55jAC C ..z:G$r   c           	          || j                   vr<t        dj                  |t        | j                   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&  s[     T777 ..4f3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                 f    | j                  d      r| j                          | j                          y)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  s'    ==+,&&(##%r   c                 p   | j                  d      }| j                  d      }|rPi }| j                  D ]  }|||j                         <    g }|D ]  }||v s|j                  ||           || _        n| j                          | j                  D ]  }|j                  |        | j                          y)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3> H& 9=(OOM$$789 &.D"&&(-- 	1G/0	1 	r   c                 N   t         j                  }t         j                  }t        t              }| j
                  j                         D ]Y  \  }}|s	|j                         }t        st        |_
        d| |_        ||v r|n|}| j                  || j                  |       [ y)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0::+,?@#'#7#7#=#=#? 	J 113 (1F$%-j\#:  "%55 !*  %%++# & +	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* 
V @=8!*-2D@'@R.S .2 EIAE@DHL8=#M.9#M4<SM#M 2:##M 19	#M
 9A#M 26#ML JN%C %9A#%&6# 6:L 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      s   
 $ / 5 5 $ +  1 % A - : : 4 L')*hsmXc]HSMIJ# #$ $
# # M* Mr   