
    Je'                         d 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 dZ edd      Z G d	 d
      Z G d d      Z G d de      Z G d de      Zy)z
Spyder API menu widgets.
    N)OptionalUnionTypeVar)QActionQMenu)add_actionsSpyderActionT
SpyderMenu)boundc                       e Zd ZdZdZy)OptionsMenuSectionstop_sectionbottom_sectionN)__name__
__module____qualname__TopBottom     :/usr/lib/python3/dist-packages/spyder/api/widgets/menus.pyr   r      s    
CFr   r   c                       e Zd ZdZdZy)PluginMainWidgetMenuscontext_menuoptions_menuN)r   r   r   ContextOptionsr   r   r   r   r   $   s    GGr   r   c                        e Zd ZdZg Z	 	 d fd	Zd Z	 	 	 	 	 ddedee	ef   de
e   de
e   de
e   d	ed
efdZdefdZd Zd Zd Zd ZddZ xZS )r   zL
    A QMenu subclass to implement additional functionality for Spyder.
    c                    || _         || _        g | _        g | _        i | _        i | _        i | _        d| _        || _        |t        | )  |       nt        | )  ||       | j                  j                  ||| f       t        j                  dk(  r|r| j                  t!        |              | j"                  j%                  | j&                         y )NFdarwin)_parent_title	_sections_actions_actions_mapunintroduced_actions_after_sections_dirtymenu_idsuper__init__MENUSappendsysplatform	addActionr   aboutToShowconnect_render)selfparenttitledynamicr*   	__class__s        r   r,   zSpyderMenu.__init__1   s    $&!!=GV$GUF+

65$/0<<8# NN74=)  .r   c                 j    | j                          g | _        g | _        i | _        i | _        i | _        y)z{
        Remove actions from the menu (including custom references)

        Returns
        -------
        None.
        N)clearr$   r%   r&   r'   r(   r5   s    r   clear_actionszSpyderMenu.clear_actionsJ   s3     	

$&!!r   r5   actionsectionbeforebefore_sectioncheck_beforeomit_idc                    d}t        |t              st        |d      r|j                  }n(t        |t              st        |d      r|j
                  }|s||t        d| d      || j                  j                  ||f       ng }d}	| j                  j                  |d      }
| j                  D ]4  \  }}|
||
k(  rd}	|j                  ||f       |j                  ||f       6 |	s@|r>| j                  j                  |g       }|j                  ||f       || j                  |<   || _        || j                  vr| j                  ||       d| _        || j                  |<   y)a  
        Add action to a given menu section.

        Parameters
        ----------
        action: SpyderAction
            The action to add.
        section: str or None
            The section id in which to insert the `action`.
        before: str
            Make the action appear before the given action identifier.
        before_section: str or None
            Make the item section (if provided) appear before another
            given section.
        check_before: bool
            Check if the `before` action is part of the menu. This is
            necessary to avoid an infinite recursion when adding
            unintroduced actions with this method again.
        omit_id: bool
            If True, then the menu 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
        N	action_idr*   zItem z must declare an id.FT)
isinstancer	   hasattrrE   r   r*   AttributeErrorr%   r.   r&   getr'   r$   _add_sectionr)   )r5   r>   r?   r@   rA   rB   rC   item_idnew_actionsaddedbefore_itemsecactbefore_actionss                 r   
add_actionzSpyderMenu.add_actionY   sn   < fl+wv{/K&&G
+wvy/InnG7?v/A 50D!EFF>MM  '6!23KE++//=K MM /S*sk/A E&&'89""C:./ \!%!:!:!>!>vr!J%%w&784B))&1'DM$..(g~6 %+'"r   rK   c                     || j                   v rl| j                   j                  |      }d }t        | j                        D ]  \  }\  }}||k(  s|} n |#| j                  j                  |       d| _        y y y )NT)r&   pop	enumerater%   r)   )r5   rK   r>   positioni_rP   s          r   remove_actionzSpyderMenu.remove_action   s    d'''&&**73FH(7 8As&= H
 #!!(+" $ (r   c                     | j                   S )z,
        Return the title for menu.
        )r#   r<   s    r   	get_titlezSpyderMenu.get_title   s     {{r   c                     g }| j                   D ]B  }| j                  D ]  \  }}||k(  s|j                  |        |j                  t               D |S )z
        Return a parsed list of menu actions.

        Includes MENU_SEPARATOR taking into account the sections defined.
        )r$   r%   r.   MENU_SEPARATOR)r5   actionsr?   rO   r>   s        r   get_actionszSpyderMenu.get_actions   s[     ~~ 	+G!% +f'>NN6*+ NN>*	+ r   c                 ,    t        | j                        S )z2
        Return a tuple of menu sections.
        )tupler$   r<   s    r   get_sectionszSpyderMenu.get_sections   s     T^^$$r   c                    | j                   ru| j                          | j                  j                         D ]$  \  }}|D ]  \  }}| j	                  |||d        & | j                         }t        | |       d| _         yy)zv
        Create the menu prior to showing it. This takes into account sections
        and location of menus.
        F)r?   r@   rB   N)r)   r;   r'   itemsrR   r_   r   )r5   r@   r^   r?   r>   s        r   r4   zSpyderMenu._render   s    
 ;;JJL $(#<#<#B#B#D G'. GOGVOOFG+1 $ GGG
 &&(Gg&DK r   c                 
   d}|s|| j                   v r:| j                   j                  |      }| j                   j                  ||       d}nF|| j                  |<   | j                   j	                  |       n| j                   j	                  |       | j                  j                  |d      }||sd|| j                   v r| j                   j                  |       | j                   j                  |      }| j                   j                  |dz   |       y|| j                   v r| j                   j                  |       | j                   j                  |      }|dk(  r|n|dz
  }| j                   j                  ||       yy)z
        Add a new section to the list of sections in this menu.

        Parameters
        ----------
        before_section: str or None
            Make `section` appear before another one.
        FNT   r   )r$   indexinsertr(   r.   rT   remove)r5   r?   rA   inserted_before_otheridxafter_sectionrg   s          r   rJ   zSpyderMenu._add_section   sQ    !&%/ nn**>:%%c73(,%
 8?$$^4
 %%g.NN!!'* ,,00$?$( dnn,NN))'2,,];%%eai9 !DNN2NN))-8nn**73!QhccAg%%c=9# %r   )NNTN)NNNTF)N)r   r   r   __doc__r-   r,   r=   r
   r   r	   r   strboolrR   rY   r[   r_   rb   r4   rJ   __classcell__)r9   s   @r   r   r   +   s     E8</2"" -1+/37(,#(E, E, q1E,$SME, $C=E, $,C=	E,
 "&E, !E,N#S #% &5:r   c                       e Zd ZdZd Zy)MainWidgetMenuzA
    This menu fixes the bottom section of the options menu.
    c                    | j                   r| j                          t        j                  }g }| j                  D ]H  }| j
                  D ]"  \  }}||k(  s||k7  s|j                  |       $ |j                  t               J | j
                  D ]  \  }}||k(  s|j                  |        t        | |       d| _         yy)z
        Create the menu prior to showing it. This takes into account sections
        and location of menus. It also hides consecutive separators if found.
        FN)	r)   r;   r   r   r$   r%   r.   r]   r   )r5   bottomr^   r?   rO   r>   s         r   r4   zMainWidgetMenu._render  s    
 ;;JJL(//FG>> /%)]] /MS&g~#-v./ ~./ "& +f&=NN6*+ g&DK# r   N)r   r   r   rm   r4   r   r   r   rr   rr     s     r   rr   )rm   r/   typingr   r   r   qtpy.QtWidgetsr   r   spyder.utils.qthelpersr   r	   r]   r
   r   r   r   rr   r   r   r   <module>rx      sc   
  + + * =
  C|$ 
 f: f:R Z  r   