changeset 1366:584d45bb36d9 frontends_multi_profiles

quick_frontends(menus): added MenuContainer.replace method + addMenuHook helper method in QuickMenusManager
author Goffi <goffi@goffi.org>
date Wed, 18 Mar 2015 10:39:22 +0100
parents ba87b940f07a
children f71a0fc26886 45732656b6ec
files frontends/src/quick_frontend/quick_menus.py
diffstat 1 files changed, 26 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- a/frontends/src/quick_frontend/quick_menus.py	Tue Mar 17 19:33:05 2015 +0100
+++ b/frontends/src/quick_frontend/quick_menus.py	Wed Mar 18 10:39:22 2015 +0100
@@ -219,7 +219,16 @@
                 yield child
 
     def append(self, item):
+        """add an item at the end of current ones
+
+        @param item: instance of MenuBase (must be unique in container)
+        """
         assert isinstance(item, MenuItem) or isinstance(item, MenuContainer)
+        assert item.canonical not in self._items
+        self._items[item.canonical] = item
+
+    def replace(self, item):
+        """add an item at the end of current ones or replace an existing one"""
         self._items[item.canonical] = item
 
 
@@ -391,6 +400,23 @@
             else:
                 self.addMenu(type_, path, path_i18n, id_=id_, extra=extra, top_extra=top_extra)
 
+    def addMenuHook(self, type_, path, path_i18n=None, extra=None, top_extra=None, callback=None):
+        """Helper method to add a menu hook
+
+        Menu hooks are local menus which override menu given by backend
+        @param type_(unicode): same as in [sat.core.sat_main.SAT.importMenu]
+        @param path(list[unicode]):  same as in [sat.core.sat_main.SAT.importMenu]
+        @param path_i18n(list[unicode], None):  translated menu path (same lenght as path), or None to get deferred translation
+        @param extra(dict[unicode, unicode], None): same as in [addMenus]
+        @param top_extra: same as in [_createCategories]
+        @param callback(callable): local callback (mutually exclusive with id_)
+        """
+        if path_i18n is None:
+            path_i18n = self._getPathI18n(path)
+        menu_item = MenuHook(type_, path[-1], path_i18n[-1], callback=callback, extra=extra)
+        self.addMenuItem(type_, path[:-1], menu_item, path_i18n[:-1], top_extra)
+        log.info(u"Menu hook set on {path} ({type_})".format(path=path, type_=type_))
+
     def addCategory(self, type_, path, path_i18n=None, extra=None, top_extra=None):
         """Create a category with all parents, and set extra on the last one