From: caremoli Date: Fri, 9 Apr 2010 16:47:39 +0000 (+0000) Subject: CCAR: unify all plugins manager in salome_pluginsmanager.py X-Git-Url: http://git.salome-platform.org/gitweb/?a=commitdiff_plain;h=refs%2Fheads%2FV5_1_plugins;p=modules%2Fgui.git CCAR: unify all plugins manager in salome_pluginsmanager.py add 4 parameters at initialization to parametrize them --- diff --git a/src/SalomeApp/SalomeApp_Application.cxx b/src/SalomeApp/SalomeApp_Application.cxx index 3ef172534..e840e43b5 100644 --- a/src/SalomeApp/SalomeApp_Application.cxx +++ b/src/SalomeApp/SalomeApp_Application.cxx @@ -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); diff --git a/src/SalomeApp/salome_pluginsmanager.py b/src/SalomeApp/salome_pluginsmanager.py index 0a7f4167a..65259ae4b 100644 --- a/src/SalomeApp/salome_pluginsmanager.py +++ b/src/SalomeApp/salome_pluginsmanager.py @@ -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 in the 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 _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)