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