Salome HOME
Xml --> Widget processing added for "value" (spinbox) entities.
[modules/shaper.git] / src / Model / Model_PluginManager.cxx
1 // File:        Model_PluginManager.cxx
2 // Created:     20 Mar 2014
3 // Author:      Mikhail PONIKAROV
4
5 #include <Model_PluginManager.h>
6 #include <ModelAPI_Feature.h>
7 #include <ModelAPI_Plugin.h>
8 #include <Model_Feature.h>
9 #include <Event_Loop.h>
10 #include <Config_FeatureMessage.h>
11 #include <Config_ModuleReader.h>
12
13 using namespace std;
14
15 static Model_PluginManager* myImpl = new Model_PluginManager();
16
17 boost::shared_ptr<ModelAPI_Feature> Model_PluginManager::createFeature(string theFeatureID)
18 {
19   if (this != myImpl) return myImpl->createFeature(theFeatureID);
20
21   LoadPluginsInfo();
22   if (myPlugins.find(theFeatureID) != myPlugins.end()) {
23     if (myPluginObjs.find(myPlugins[theFeatureID]) == myPluginObjs.end()) {
24       // load plugin library if not yet done
25       myCurrentPluginName = myPlugins[theFeatureID];
26       loadLibrary(myCurrentPluginName);
27     }
28     if (myPluginObjs.find(myCurrentPluginName) != myPluginObjs.end()) {
29       return myPluginObjs[myCurrentPluginName]->createFeature(theFeatureID);
30     }
31   }
32
33   return boost::shared_ptr<ModelAPI_Feature>(); // return nothing
34 }
35
36 Model_PluginManager::Model_PluginManager()
37 {
38   myPluginsInfoLoaded = false;
39   //TODO(sbh): Implement static method to extract event id [SEID]
40   static Event_ID aFeatureEvent = Event_Loop::eventByName("FeatureEvent");
41
42   ModelAPI_PluginManager::SetPluginManager(boost::shared_ptr<ModelAPI_PluginManager>(this));
43   // register the configuration reading listener
44   Event_Loop* aLoop = Event_Loop::loop();
45   aLoop->registerListener(this, aFeatureEvent);
46 }
47
48 void Model_PluginManager::processEvent(const Event_Message* theMessage)
49 {
50   const Config_FeatureMessage* aMsg =
51     dynamic_cast<const Config_FeatureMessage*>(theMessage);
52   if (aMsg) {
53     // proccess the plugin info, load plugin
54     if (myPlugins.find(aMsg->id()) == myPlugins.end()) {
55       myPlugins[aMsg->id()] = aMsg->pluginLibrary(); // TO DO: plugin name must be also imported from XMLs
56     }
57   }
58   // plugins information was started to load, so, it will be loaded
59   myPluginsInfoLoaded = true;
60 }
61
62 void Model_PluginManager::LoadPluginsInfo()
63 {
64   if (myPluginsInfoLoaded) // nothing to do
65     return;
66
67   // Read plugins information from XML files
68   Config_ModuleReader aXMLReader;
69   aXMLReader.setAutoImport(true);
70   aXMLReader.readAll();
71 }
72
73 void Model_PluginManager::registerPlugin(ModelAPI_Plugin* thePlugin)
74 {
75   myPluginObjs[myCurrentPluginName] = thePlugin;
76 }