]> SALOME platform Git repositories - modules/shaper.git/blob - src/Model/Model_PluginManager.cxx
Salome HOME
4c3e3da6d98bed9d746e067c366b167ac0f4480d
[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_Object.h>
9 #include <Model_Document.h>
10 #include <Model_Application.h>
11 #include <Event_Loop.h>
12 #include <Config_FeatureMessage.h>
13 #include <Config_ModuleReader.h>
14
15 using namespace std;
16
17 static Model_PluginManager* myImpl = new Model_PluginManager();
18
19 std::shared_ptr<ModelAPI_Feature> Model_PluginManager::createFeature(
20   string theFeatureID, const bool theAddToDoc)
21 {
22   if (this != myImpl) return myImpl->createFeature(theFeatureID);
23
24   LoadPluginsInfo();
25   if (myPlugins.find(theFeatureID) != myPlugins.end()) {
26     if (myPluginObjs.find(myPlugins[theFeatureID]) == myPluginObjs.end()) {
27       // load plugin library if not yet done
28       myCurrentPluginName = myPlugins[theFeatureID];
29       loadLibrary(myCurrentPluginName);
30     }
31     if (myPluginObjs.find(myCurrentPluginName) != myPluginObjs.end()) {
32       std::shared_ptr<ModelAPI_Feature> aCreated = 
33         myPluginObjs[myCurrentPluginName]->createFeature(theFeatureID, theAddToDoc);
34       return aCreated;
35     }
36   }
37
38   return std::shared_ptr<ModelAPI_Feature>(); // return nothing
39 }
40
41 std::shared_ptr<ModelAPI_Document> Model_PluginManager::rootDocument()
42 {
43   return std::shared_ptr<ModelAPI_Document>(
44     Model_Application::getApplication()->getDocument("root"));
45 }
46
47 shared_ptr<ModelAPI_Document> Model_PluginManager::currentDocument()
48 {
49   if (!myCurrentDoc)
50     myCurrentDoc = rootDocument();
51   return myCurrentDoc;
52 }
53
54 void Model_PluginManager::setCurrentDocument(shared_ptr<ModelAPI_Document> theDoc)
55 {
56   myCurrentDoc = theDoc;
57 }
58
59 Model_PluginManager::Model_PluginManager()
60 {
61   myPluginsInfoLoaded = false;
62   //TODO(sbh): Implement static method to extract event id [SEID]
63   static Event_ID aFeatureEvent = Event_Loop::eventByName("FeatureEvent");
64
65   ModelAPI_PluginManager::SetPluginManager(std::shared_ptr<ModelAPI_PluginManager>(this));
66   // register the configuration reading listener
67   Event_Loop* aLoop = Event_Loop::loop();
68   aLoop->registerListener(this, aFeatureEvent);
69 }
70
71 void Model_PluginManager::processEvent(const Event_Message* theMessage)
72 {
73   const Config_FeatureMessage* aMsg =
74     dynamic_cast<const Config_FeatureMessage*>(theMessage);
75   if (aMsg) {
76     // proccess the plugin info, load plugin
77     if (myPlugins.find(aMsg->id()) == myPlugins.end()) {
78       myPlugins[aMsg->id()] = aMsg->pluginLibrary();
79     }
80   }
81   // plugins information was started to load, so, it will be loaded
82   myPluginsInfoLoaded = true;
83 }
84
85 void Model_PluginManager::LoadPluginsInfo()
86 {
87   if (myPluginsInfoLoaded) // nothing to do
88     return;
89
90   // Read plugins information from XML files
91   Config_ModuleReader aXMLReader;
92   aXMLReader.setAutoImport(true);
93   aXMLReader.readAll();
94 }
95
96 void Model_PluginManager::registerPlugin(ModelAPI_Plugin* thePlugin)
97 {
98   myPluginObjs[myCurrentPluginName] = thePlugin;
99 }