]> SALOME platform Git repositories - modules/shaper.git/blob - src/Model/Model_PluginManager.cpp
Salome HOME
00ffa6678d935937f5e678e4d4f0669b0bcae11e
[modules/shaper.git] / src / Model / Model_PluginManager.cpp
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_Data.h>
9 #include <Model_Document.h>
10 #include <Model_Application.h>
11 #include <Model_Events.h>
12 #include <Model_Validator.h>
13 #include <Events_Loop.h>
14 #include <Events_Error.h>
15 #include <Config_FeatureMessage.h>
16 #include <Config_ModuleReader.h>
17
18 #include <TDF_CopyTool.hxx>
19 #include <TDF_DataSet.hxx>
20 #include <TDF_RelocationTable.hxx>
21 #include <TDF_ClosureTool.hxx>
22
23
24 using namespace std;
25
26 static Model_PluginManager* myImpl = new Model_PluginManager();
27
28 FeaturePtr Model_PluginManager::createFeature(string theFeatureID)
29 {
30   if (this != myImpl) return myImpl->createFeature(theFeatureID);
31
32   LoadPluginsInfo();
33   if (myPlugins.find(theFeatureID) != myPlugins.end()) {
34     myCurrentPluginName = myPlugins[theFeatureID];
35     if (myPluginObjs.find(myCurrentPluginName) == myPluginObjs.end()) {
36       // load plugin library if not yet done
37       Config_ModuleReader::loadLibrary(myCurrentPluginName);
38     }
39     if (myPluginObjs.find(myCurrentPluginName) != myPluginObjs.end()) {
40       FeaturePtr aCreated = 
41         myPluginObjs[myCurrentPluginName]->createFeature(theFeatureID);
42       if (!aCreated) {
43         Events_Error::send(string("Can not initialize feature '") + theFeatureID +
44           "' in plugin '" + myCurrentPluginName + "'");
45       }
46       return aCreated;
47     } else {
48       Events_Error::send(string("Can not load plugin '") + myCurrentPluginName + "'");
49     }
50   }
51
52   return FeaturePtr(); // return nothing
53 }
54
55 boost::shared_ptr<ModelAPI_Document> Model_PluginManager::rootDocument()
56 {
57   return boost::shared_ptr<ModelAPI_Document>(
58     Model_Application::getApplication()->getDocument("root"));
59 }
60
61 bool Model_PluginManager::hasRootDocument()
62 {
63   return Model_Application::getApplication()->hasDocument("root");
64 }
65
66 boost::shared_ptr<ModelAPI_Document> Model_PluginManager::currentDocument()
67 {
68   if (!myCurrentDoc || !Model_Application::getApplication()->hasDocument(myCurrentDoc->id()))
69     myCurrentDoc = rootDocument();
70   return myCurrentDoc;
71 }
72
73 void Model_PluginManager::setCurrentDocument(boost::shared_ptr<ModelAPI_Document> theDoc)
74 {
75   myCurrentDoc = theDoc;
76 }
77
78 boost::shared_ptr<ModelAPI_Document> Model_PluginManager::copy(
79   boost::shared_ptr<ModelAPI_Document> theSource, std::string theID) 
80 {
81   // create a new document
82   boost::shared_ptr<Model_Document> aNew = boost::dynamic_pointer_cast<Model_Document>(
83     Model_Application::getApplication()->getDocument(theID));
84   // make a copy of all labels
85   TDF_Label aSourceRoot = 
86     boost::dynamic_pointer_cast<Model_Document>(theSource)->document()->Main().Father();
87   TDF_Label aTargetRoot = aNew->document()->Main().Father();
88   Handle(TDF_DataSet) aDS = new TDF_DataSet;
89   aDS->AddLabel(aSourceRoot);
90   TDF_ClosureTool::Closure(aDS);
91   Handle(TDF_RelocationTable) aRT = new TDF_RelocationTable;
92   aRT->SetRelocation(aSourceRoot, aTargetRoot);
93   TDF_CopyTool::Copy(aDS, aRT);
94
95   aNew->synchronizeFeatures();
96   return aNew;
97 }
98
99 Model_PluginManager::Model_PluginManager()
100 {
101   myPluginsInfoLoaded = false;
102   myCheckTransactions = true;
103   ModelAPI_PluginManager::setPluginManager(boost::shared_ptr<ModelAPI_PluginManager>(this));
104   // register the configuration reading listener
105   Events_Loop* aLoop = Events_Loop::loop();
106   static Events_ID FeatureEvent = Events_Loop::eventByName("FeatureRegisterEvent");
107   aLoop->registerListener(this, FeatureEvent);
108   aLoop->registerListener(this, Events_Loop::eventByName(EVENT_FEATURE_CREATED));
109   aLoop->registerListener(this, Events_Loop::eventByName(EVENT_FEATURE_UPDATED));
110   aLoop->registerListener(this, Events_Loop::eventByName(EVENT_FEATURE_DELETED));
111 }
112
113 void Model_PluginManager::processEvent(const Events_Message* theMessage)
114 {
115   static Events_ID FeatureEvent = Events_Loop::eventByName("FeatureRegisterEvent");
116   if (theMessage->eventID() == FeatureEvent) {
117     const Config_FeatureMessage* aMsg =
118       dynamic_cast<const Config_FeatureMessage*>(theMessage);
119     if (aMsg) {
120       // proccess the plugin info, load plugin
121       if (myPlugins.find(aMsg->id()) == myPlugins.end()) {
122         myPlugins[aMsg->id()] = aMsg->pluginLibrary();
123       }
124     }
125     // plugins information was started to load, so, it will be loaded
126     myPluginsInfoLoaded = true;
127   } else { // create/update/delete
128     if (myCheckTransactions && !rootDocument()->isOperation())
129       Events_Error::send("Modification of data structure outside of the transaction");
130   }
131 }
132
133 void Model_PluginManager::LoadPluginsInfo()
134 {
135   if (myPluginsInfoLoaded) // nothing to do
136     return;
137
138   // Read plugins information from XML files
139   Config_ModuleReader aXMLReader("FeatureRegisterEvent");
140   aXMLReader.readAll();
141 }
142
143 void Model_PluginManager::registerPlugin(ModelAPI_Plugin* thePlugin)
144 {
145   myPluginObjs[myCurrentPluginName] = thePlugin;
146 }
147
148 ModelAPI_ValidatorsFactory* Model_PluginManager::validators()
149 {
150   static Model_ValidatorsFactory* aFactory = new Model_ValidatorsFactory;
151   return aFactory;
152 }