Salome HOME
Initial version of redesign of working with results
[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   } else {
51       Events_Error::send(string("Feature '") + theFeatureID + "' not found in any plugin");
52   }
53
54   return FeaturePtr(); // return nothing
55 }
56
57 boost::shared_ptr<ModelAPI_Document> Model_PluginManager::rootDocument()
58 {
59   return boost::shared_ptr<ModelAPI_Document>(
60     Model_Application::getApplication()->getDocument("root"));
61 }
62
63 bool Model_PluginManager::hasRootDocument()
64 {
65   return Model_Application::getApplication()->hasDocument("root");
66 }
67
68 boost::shared_ptr<ModelAPI_Document> Model_PluginManager::currentDocument()
69 {
70   if (!myCurrentDoc || !Model_Application::getApplication()->hasDocument(myCurrentDoc->id()))
71     myCurrentDoc = rootDocument();
72   return myCurrentDoc;
73 }
74
75 void Model_PluginManager::setCurrentDocument(boost::shared_ptr<ModelAPI_Document> theDoc)
76 {
77   myCurrentDoc = theDoc;
78   static Events_Message aMsg(Events_Loop::eventByName("CurrentDocumentChanged"));
79   Events_Loop::loop()->send(aMsg);
80 }
81
82 boost::shared_ptr<ModelAPI_Document> Model_PluginManager::copy(
83   boost::shared_ptr<ModelAPI_Document> theSource, std::string theID) 
84 {
85   // create a new document
86   boost::shared_ptr<Model_Document> aNew = boost::dynamic_pointer_cast<Model_Document>(
87     Model_Application::getApplication()->getDocument(theID));
88   // make a copy of all labels
89   TDF_Label aSourceRoot = 
90     boost::dynamic_pointer_cast<Model_Document>(theSource)->document()->Main().Father();
91   TDF_Label aTargetRoot = aNew->document()->Main().Father();
92   Handle(TDF_DataSet) aDS = new TDF_DataSet;
93   aDS->AddLabel(aSourceRoot);
94   TDF_ClosureTool::Closure(aDS);
95   Handle(TDF_RelocationTable) aRT = new TDF_RelocationTable;
96   aRT->SetRelocation(aSourceRoot, aTargetRoot);
97   TDF_CopyTool::Copy(aDS, aRT);
98
99   aNew->synchronizeFeatures();
100   return aNew;
101 }
102
103 Model_PluginManager::Model_PluginManager()
104 {
105   myPluginsInfoLoaded = false;
106   myCheckTransactions = true;
107   ModelAPI_PluginManager::setPluginManager(boost::shared_ptr<ModelAPI_PluginManager>(this));
108   // register the configuration reading listener
109   Events_Loop* aLoop = Events_Loop::loop();
110   static Events_ID FeatureEvent = Events_Loop::eventByName("FeatureRegisterEvent");
111   aLoop->registerListener(this, FeatureEvent);
112   aLoop->registerListener(this, Events_Loop::eventByName(EVENT_OBJECT_CREATED));
113   aLoop->registerListener(this, Events_Loop::eventByName(EVENT_OBJECT_UPDATED));
114   aLoop->registerListener(this, Events_Loop::eventByName(EVENT_OBJECT_DELETED));
115 }
116
117 void Model_PluginManager::processEvent(const Events_Message* theMessage)
118 {
119   static Events_ID FeatureEvent = Events_Loop::eventByName("FeatureRegisterEvent");
120   if (theMessage->eventID() == FeatureEvent) {
121     const Config_FeatureMessage* aMsg =
122       dynamic_cast<const Config_FeatureMessage*>(theMessage);
123     if (aMsg) {
124       // proccess the plugin info, load plugin
125       if (myPlugins.find(aMsg->id()) == myPlugins.end()) {
126         myPlugins[aMsg->id()] = aMsg->pluginLibrary();
127       }
128     }
129     // plugins information was started to load, so, it will be loaded
130     myPluginsInfoLoaded = true;
131   } else { // create/update/delete
132     if (myCheckTransactions && !rootDocument()->isOperation())
133       Events_Error::send("Modification of data structure outside of the transaction");
134   }
135 }
136
137 void Model_PluginManager::LoadPluginsInfo()
138 {
139   if (myPluginsInfoLoaded) // nothing to do
140     return;
141
142   // Read plugins information from XML files
143   Config_ModuleReader aXMLReader("FeatureRegisterEvent");
144   aXMLReader.readAll();
145 }
146
147 void Model_PluginManager::registerPlugin(ModelAPI_Plugin* thePlugin)
148 {
149   myPluginObjs[myCurrentPluginName] = thePlugin;
150 }
151
152 ModelAPI_ValidatorsFactory* Model_PluginManager::validators()
153 {
154   static Model_ValidatorsFactory* aFactory = new Model_ValidatorsFactory;
155   return aFactory;
156 }