
    Je7                        d Z ddlmZ ddlZddlmZ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mZmZ dd	lmZ dd
lmZmZ ddlmZmZ ddlmZmZ e	eef   Zeeee    ee    ee    f   Z!ee ee!   f   Z" G d de      Z#y)z
Main menu Plugin.
    )OrderedDictN)DictListTupleOptionalUnion)QKeySequence)SpyderAPIError)PLUGIN_REGISTRY)SpyderPluginV2SpyderDockablePluginPlugins_)MENU_SEPARATOR
SpyderMenu)ApplicationMenuApplicationMenus)set_menu_iconsSpyderActionc                       e Zd ZdZeZdZdZed        Zd Z	d Z
d Zd Zd Zd	 Zd
 Zd Z	 ddede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efdZ	 ddedee   fdZdedefdZy)MainMenumainmenuFc                      t        d      S )Nz
Main menusr        @/usr/lib/python3/dist-packages/spyder/plugins/mainmenu/plugin.pyget_namezMainMenu.get_name*   s    r   c                 $    | j                  d      S )Ngenprefs)create_iconselfs    r   get_iconzMainMenu.get_icon.   s    
++r   c                     t        d      S )Nz)Provide main application menu management.r   r"   s    r   get_descriptionzMainMenu.get_description1   s    <==r   c                    t               | _        i | _        | j                  } |t        j
                  t        d              |t        j                  t        d      d        |t        j                  t        d      d        |t        j                  t        d      d        |t        j                  t        d      d        |t        j                  t        d      d       | j                  t        j                        r  |t        j                  t        d	             | j                  t        j                         r  |t        j                   t        d
              |t        j"                  t        d              |t        j$                  t        d              |t        j&                  t        d             y )Nz&Filez&EditFdynamicz&SearchzSour&cez&Runz&Debugz	C&onsolesz	&Projectsz&Toolsz&Viewz&Help)r   _APPLICATION_MENUS_ITEM_QUEUEcreate_application_menur   Filer   EditSearchSourceRunDebugis_plugin_enabledr   IPythonConsoleConsolesProjectsToolsViewHelp)r#   create_app_menus     r   on_initializezMainMenu.on_initialize4   s<   "-- 
 66(--qz:(--qz5I(//9uM(//9uM(,,aiG(..(UK!!'"8"89,55q~F!!'"2"23,55q~F(..(<(--qz:(--qz:r   c                 b    | j                   j                         D ]  }|j                           y N)r*   values_render)r#   menus     r   on_mainwindow_visiblezMainMenu.on_mainwindow_visibleN   s+     ++224 	DLLN	r   c                     |j                         }|D ]h  }t        |dd      r)|j                  |j                  |j                         9|j	                          | j                  |j	                                ij y)z
        Show action shortcuts in menu.

        Parameters
        ----------
        menu: SpyderMenu
            Instance of a spyder menu.
        _shown_shortcutFN)actionsgetattrrC   setShortcutr@   _show_shortcutsr#   r@   menu_actionsactions       r   rG   zMainMenu._show_shortcutsZ   sp     ||~" 
	Fv0%8))5&&v'='=>*$$V[[]3 
	r   c                     |j                         }|D ]f  }t        |dd      r'|j                  |j                  t	                      7|j                          | j                  |j                                gh y)z
        Hide action shortcuts in menu.

        Parameters
        ----------
        menu: SpyderMenu
            Instance of a spyder menu.
        rC   FN)rD   rE   rC   rF   r	   r@   _hide_shortcutsrH   s       r   rL   zMainMenu._hide_shortcutsp   sl     ||~" 
	Fv0%8))5&&|~6*$$V[[]3 
	r   c                 v   t         D ]  }t        j                  |      }t        |t              s)|j                  dk(  r5| j
                  j                         }|j                  j                          m	 |j                  j                           y# t        $ r |j                  j                          Y w xY w)z3Hide options menu when menubar is pressed in macOS.editorN)r   
get_plugin
isinstancer   CONF_SECTIONrN   get_current_editorstackr@   hideoptions_menuAttributeError_options_menu)r#   plugin_nameplugin_instanceeditorstacks       r   _hide_options_menuszMainMenu._hide_options_menus   s    * 	=K-88EO/+?@"//8;"&++"E"E"GK$$))+='4499;	= * ='55::<=s   5B#B87B8c                 z    t         j                  dk(  r j                  D ]  } j                  |   }||j                  j	                  |f fd	       |j
                  j	                  |f fd	       |j                  j	                  |fd       |j                  j	                   j                          yy)zSetup menus.darwinNc                 &    j                  |       S r=   rG   r@   r#   s    r   <lambda>z'MainMenu._setup_menus.<locals>.<lambda>       $*>*>t*D r   c                 &    j                  |       S r=   rL   r_   s    r   r`   z'MainMenu._setup_menus.<locals>.<lambda>   ra   r   c                     t        | d      S NFr   r@   s    r   r`   z'MainMenu._setup_menus.<locals>.<lambda>   s    .u*E r   )sysplatformr*   aboutToShowconnectaboutToHiderZ   r#   menu_idr@   s   `  r   _setup_menuszMainMenu._setup_menus   s     <<8#22 	G..w7#$$,,$(DF$$,,$(DF$$,,$(EG$$,,T-E-EF	G $r   rn   titler)   c                     | j                   v rt        dj                  |            t         j                  ||      }||_        | j                   |<    j                  j                         j                  |       t        j                  dk(  r|j                  j                  |f fd	       |j                  j                  |f fd	       |j                  j                  |fd       |j                  j                   j                         | j                  v r? j                  j                  |      }|D ]  }|\  }}}	}
 j!                  ||||	|
       ! |S )z
        Create a Spyder application menu.

        Parameters
        ----------
        menu_id: str
            The menu unique identifier string.
        title: str
            The localized menu title to be displayed.
        z Menu with id "{}" already added!r(   r\   c                 &    j                  |       S r=   r^   r_   s    r   r`   z2MainMenu.create_application_menu.<locals>.<lambda>       $"6"6t"< r   c                 &    j                  |       S r=   rc   r_   s    r   r`   z2MainMenu.create_application_menu.<locals>.<lambda>   rs   r   c                     t        | d      S re   rf   rg   s    r   r`   z2MainMenu.create_application_menu.<locals>.<lambda>   s    .u"= r   )rn   sectionbeforebefore_section)r*   r
   formatr   mainrn   menuBaraddMenurh   ri   rj   rk   rl   rZ   r+   popadd_item_to_application_menu)r#   rn   rp   r)   r@   pending_itemspendingitemrv   before_itemrx   s   `          r   r,   z MainMenu.create_application_menu   sY    d--- 299'BD D tyy%A+/(		##D) <<8#$$ <>$$ <>$$ =?$$T%=%=>d&&& ,,009M( G07.wn11'7&~ 2 GG r   Nr   rv   rw   rx   omit_idc           
         t        |t        t        f      s|st        d      t        j
                  | j                  j                  t        j                  | j                  j                  t        j                  | j                  j                  t        j                  | j                  j                  t        j                  | j                  j                  i}||v r,||   }|j!                  t"               |j!                  |       y|| j$                  vrA| j&                  j)                  |g       }	|	j!                  ||||f       |	| j&                  |<   y| j+                  |      }
|
j-                  |||||       y)a  
        Add action or widget `item` to given application menu `section`.

        Parameters
        ----------
        item: SpyderAction or SpyderMenu
            The item to add to the `menu`.
        menu_id: str or None
            The application menu unique string identifier.
        section: str or None
            The section id in which to insert the `item` on the `menu`.
        before: str
            Make the item appear before the given object identifier.
        before_section: Section or None
            Make the item section (if provided) appear before another
            given section.
        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

        Notes
        -----
        Must provide a `menu` or a `menu_id`.
        zDA menu only accepts items objects of type SpyderAction or SpyderMenu)rv   rw   rx   r   N)rP   r   r   r
   r   r.   _mainedit_menu_actionsr/   search_menu_actionsr0   source_menu_actionsr1   run_menu_actionsr2   debug_menu_actionsappendr   r*   r+   getget_application_menu
add_action)r#   r   rn   rv   rw   rx   r   app_menu_actionsrD   pending_menu_itemsr@   s              r   r~   z%MainMenu.add_item_to_application_menu   sG   > $z :;G  "> ? ? !!4::#?#?##TZZ%C%C##TZZ%C%C  $**"="=""DJJ$A$A
 &&&w/GNN>*NN4 d555%)%5%5%9%9'2%F""))4&+9+; <,>  )009gf/=w   Pr   c                     || j                   v rS| j                   j                  |      }| j                  j                         j	                  |j                                yy)z
        Remove a Spyder application menu.

        Parameters
        ----------
        menu_id: str
            The menu unique identifier string.
        N)r*   r}   rz   r{   removeAction
menuActionrm   s      r   remove_application_menuz MainMenu.remove_application_menu  sO     d---**..w7DII,,T__->? .r   item_idc                    || j                   vrt        dj                  |            t        j                  | j
                  j                  | j
                  j                  ft        j                  | j
                  j                  | j
                  j                  ft        j                  | j
                  j                  | j
                  j                  ft        j                  | j
                  j                  | j
                  j                   ft        j"                  | j
                  j$                  | j
                  j&                  fi}t        j                  | j
                  j                  t        j                  | j
                  j                  t        j                  | j
                  j                  t        j                  | j
                  j                   t        j"                  | j
                  j&                  i}| j)                  |      }||v r||   }||   }d}t+        |      D ]d  \  }}	d}
t-        |	t.              st1        |	d      r|	j2                  }
n(t-        |	t4              st1        |	d      r|	j6                  }
|
\|
|k(  sb|} n |#|j9                  |       |j;                  |       yy|j;                  |       y)a  
        Remove action or widget from given application menu by id.

        Parameters
        ----------
        item_id: str
            The item identifier to remove from the given menu.
        menu_id: str or None
            The application menu unique string identifier.
        z{} is not a valid menu_idN	action_idrn   )r*   r
   ry   r   r.   r   r   	edit_menur/   r   search_menur0   r   source_menur1   r   run_menur2   r   
debug_menur   	enumeraterP   r   hasattrr   r   rn   r}   remove_action)r#   r   rn   r   	app_menusr@   rD   positionirJ   this_item_ids              r   !remove_item_from_application_menuz*MainMenu.remove_item_from_application_menu  sF    $111 !<!C!CG!LMM
 !!

,,djj.B.B$D##

..

0F0F&H##

..

0F0F&H  

++TZZ-@-@#B""

--tzz/D/D%F
 !!4::#7#7##TZZ%;%;##TZZ%;%;  $**"5"5""DJJ$9$9
	 ((1&&&w/GW%DH&w/ 
	6#v|44#)#3#3L 4	2#)>>L+0G H
 #H%""7+ $ w'r   returnc           	          || j                   vr<t        dj                  |t        | j                   j	                                           | j                   |   S )z
        Return an application menu by menu unique id.

        Parameters
        ----------
        menu_id: ApplicationMenu
            The menu unique identifier string.
        z:Application menu "{0}" not found! Available menus are: {1})r*   r
   ry   listkeys)r#   rn   s     r   r   zMainMenu.get_application_menuX  s[     $111 !!'T$"9"9">">"@A"C  &&w//r   )T)NNNNFr=   )__name__
__module____qualname__NAMErQ   	CONF_FILECAN_BE_DISABLEDstaticmethodr   r$   r&   r;   rA   rG   rL   rZ   ro   strboolr,   ItemTyper   r~   r   r   r   r   r   r   r   r   r   $   s   DLIO ,>;4,,= G$ 15)s )3 ))-)X ?C>B=AEI5::P :P.6sm:P.6sm:P .6c]:P 6>c]	:P
 /3:Px@s @ DH;( ;(3;C=;(z0C 0J 0r   r   )$__doc__collectionsr   rh   typingr   r   r   r   r   
qtpy.QtGuir	   spyder.api.exceptionsr
   'spyder.api.plugin_registration.registryr   spyder.api.pluginsr   r   r   spyder.api.translationsr   spyder.api.widgets.menusr   r   spyder.plugins.mainmenu.apir   r   spyder.utils.qthelpersr   r   r   r   ItemSectionBefore	ItemQueuer   r   r   r   <module>r      s   
 $ 
 5 5 $ 1 C L L % ? I ? z)*hsmXc]HSM9; d,--.	D0~ D0r   