"""
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
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)
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):
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
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:
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))
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()
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)