]> SALOME platform Git repositories - modules/gui.git/commitdiff
Salome HOME
CCAR: unify all plugins manager in salome_pluginsmanager.py V5_1_plugins
authorcaremoli <caremoli>
Fri, 9 Apr 2010 16:47:39 +0000 (16:47 +0000)
committercaremoli <caremoli>
Fri, 9 Apr 2010 16:47:39 +0000 (16:47 +0000)
add 4 parameters at initialization to parametrize them

src/SalomeApp/SalomeApp_Application.cxx
src/SalomeApp/salome_pluginsmanager.py

index 3ef1725343d26d698f5263ff29421b154a78d516..e840e43b508a4ffc762c4282159bcbd645501924 100644 (file)
@@ -309,7 +309,7 @@ void SalomeApp_Application::createActions()
   // import Python module that manages SALOME plugins
   PyGILState_STATE gstate = PyGILState_Ensure();
   PyObject* pluginsmanager=PyImport_ImportModule((char*)"salome_pluginsmanager");
-  PyObject* res=PyObject_CallMethod( pluginsmanager, (char*)"initialize", (char*)"");
+  PyObject* res=PyObject_CallMethod( pluginsmanager, (char*)"initialize", (char*)"isss",0,"salome","Tools","Plugins");
   if(res==NULL)
     PyErr_Print();
   Py_XDECREF(res);
index 0a7f4167aa7a47b9c38ee44070a06f11938da086..65259ae4b2e935bde8301e1028634a7b893604a2 100644 (file)
@@ -1,17 +1,21 @@
 """
 This module is imported from C++ SalomeApp_Application
-and initialized (call to initialize function)
+and initialized (call to initialize function with 4 parameters)
+module : 0 if it's plugins manager at the application level 1 if it is at the module level
+name : the name of the plugins manager. This name is used to build the name of the plugins files
+basemenuname : the name of the menu into we want to add the menu of the plugins ("Tools" for example)
+menuname : the name of plugins menu
 
-A plugins manager is created at import (singleton object).
+A plugins manager is created at when calling initialize.
 
-The plugins manager creates a submenu Plugins in the Tools menu.
+The plugins manager creates a submenu <menuname> in the <basemenuname> menu.
 
 The plugins manager searches in $HOME/.salome/Plugins, $HOME/$APPLI/Plugins, $SALOME_PLUGINS_PATH directories
-files named salome_plugins.py and executes them.
+files named <name>_plugins.py and executes them.
 
 These files should contain python code that register functions into the plugins manager.
 
-salome_plugins.py example::
+Example of a plugins manager with name salome. It searches files with name salome_plugins.py (example follows)::
 
   import salome_pluginsmanager
 
@@ -21,7 +25,7 @@ salome_plugins.py example::
 
   salome_pluginsmanager.AddFunction('About plugins','About SALOME pluginmanager',about)
 
-First import the python module salome_pluginsmanager
+First you need to import the python module salome_pluginsmanager
 Second write a function with one argument context (it's an object with 3 attributes)
 Third register the function with a call to AddFunction (entry in menu plugins, tooltip, function)
 
@@ -50,8 +54,13 @@ sgPyQt = SalomePyQt.SalomePyQt()
 import libSALOME_Swig
 sg = libSALOME_Swig.SALOMEGUI_Swig()
 
-def initialize():
-  plugins.activate()
+plugins={}
+current_plugins_manager=None
+
+def initialize(module,name,basemenuname,menuname):
+  if plugins.has_key(name):return
+  plugins[name]=PluginsManager(module,name,basemenuname,menuname)
+  plugins[name].activate()
 
 class Context:
     def __init__(self,sg):
@@ -59,8 +68,12 @@ class Context:
         self.studyId=salome.sg.getActiveStudyId()
         self.study= salome.myStudyManager.GetStudyByID(self.studyId)
 
-class Plugins:
-    def __init__(self):
+class PluginsManager:
+    def __init__(self,module,name,basemenuname,menuname):
+        self.name=name
+        self.basemenuname=basemenuname
+        self.menuname=menuname
+        self.module=module
         self.registry={}
         self.handlers={}
         self.lasttime=0
@@ -83,20 +96,20 @@ class Plugins:
           for directory in pluginspath.split(SEP):
             self.plugindirs.append(directory)
 
-        basemenu=sgPyQt.getPopupMenu("Tools")
-        self.menu=QtGui.QMenu("Plugins",basemenu)
-        basemenu.addMenu(self.menu)
-
-        #a=QtGui.QAction("Plugins",basemenu)
-        #a.setMenu(self.menu)
-        #mid=sgPyQt.createMenu(a,"Tools")
+        if self.module:
+          self.menu=QtGui.QMenu(self.menuname)
+          mid=sgPyQt.createMenu(self.menu.menuAction(),self.basemenuname)
+        else:
+          self.basemenu=sgPyQt.getPopupMenu(self.basemenuname)
+          self.menu=QtGui.QMenu(self.menuname,self.basemenu)
+          self.basemenu.addMenu(self.menu)
 
         self.menu.connect(self.menu,QtCore.SIGNAL("aboutToShow()"),self.importPlugins)
 
-    def AddFunction(self,name,description,script,script_type=None):
+    def AddFunction(self,name,description,script):
         """ Add a plugin function
         """
-        self.registry[name]=script,description,script_type
+        self.registry[name]=script,description
 
         def handler(obj=self,script=script):
           try:
@@ -108,21 +121,21 @@ class Plugins:
         self.handlers[name]=handler
 
     def importPlugins(self):
-        """Import the salome_plugins module that contains plugins definition """
-        if self.lasttime ==0:
-          studyId=sg.getActiveStudyId()
-          if studyId == 0:
-             self.menu.clear()
-             self.menu.addAction("No active study : open or load a new one")
-             return
-          else:
-             salome.salome_init(embedded=1)
+        """Execute the salome_plugins file that contains plugins definition """
+        studyId=sg.getActiveStudyId()
+        if studyId == 0:
+          self.menu.clear()
+          #self.menu.addAction("No active study : open or load a new one")
+          return
+        elif self.lasttime ==0:
+          salome.salome_init(embedded=1)
 
         lasttime=0
 
         plugins_files=[]
+        plugins_file_name=self.name+"_plugins.py"
         for directory in self.plugindirs:
-          plugins_file = os.path.join(directory,"salome_plugins.py")
+          plugins_file = os.path.join(directory,plugins_file_name)
           if os.path.isfile(plugins_file):
             plugins_files.append((directory,plugins_file))
             lasttime=max(lasttime,os.path.getmtime(plugins_file))
@@ -137,16 +150,18 @@ class Plugins:
           return
 
         if self.plugins_files != plugins_files or lasttime > self.lasttime:
+          global current_plugins_manager
+          current_plugins_manager=self
           self.registry.clear()
           self.handlers.clear()
           self.lasttime=lasttime
-          for directory,salome_plugins_file in plugins_files:
+          for directory,plugins_file in plugins_files:
             if directory not in sys.path:
               sys.path.insert(0,directory)
             try:
-              execfile(salome_plugins_file,globals(),{})
+              execfile(plugins_file,globals(),{})
             except:
-              print "Error while loading plugins from file:",salome_plugins_file
+              print "Error while loading plugins from file:",plugins_file
               traceback.print_exc()
 
           self.updateMenu()
@@ -156,19 +171,17 @@ class Plugins:
         self.menu.clear()
         for name,handler in self.handlers.items():
           act=self.menu.addAction(name,handler)
+          act.setStatusTip(self.registry[name][1])
 
     def activate(self):
         """Add the Plugins menu to the Tools menu"""
-        #tools_menu= sgPyQt.getPopupMenu(SalomePyQt.Tools)
-        #tools_menu.addMenu(self.menu)
 
     def deactivate(self):
         """Remove the Plugins menu from Tools menu (not implemented)"""
 
-plugins=Plugins()
 
-def AddFunction(name,description,script,script_type=None):
+def AddFunction(name,description,script):
    """ Add a plugin function
        Called by a user to register a function (script)
    """
-   return plugins.AddFunction(name,description,script,script_type)
+   return current_plugins_manager.AddFunction(name,description,script)