]> SALOME platform Git repositories - modules/shaper.git/blob - src/Model/Model_PluginManager.cpp
Salome HOME
Attaching a debug OCAF browser plugin
[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   static Events_Message aMsg(Events_Loop::eventByName("CurrentDocumentChanged"));
77   Events_Loop::loop()->send(aMsg);
78 }
79
80 boost::shared_ptr<ModelAPI_Document> Model_PluginManager::copy(
81   boost::shared_ptr<ModelAPI_Document> theSource, std::string theID) 
82 {
83   // create a new document
84   boost::shared_ptr<Model_Document> aNew = boost::dynamic_pointer_cast<Model_Document>(
85     Model_Application::getApplication()->getDocument(theID));
86   // make a copy of all labels
87   TDF_Label aSourceRoot = 
88     boost::dynamic_pointer_cast<Model_Document>(theSource)->document()->Main().Father();
89   TDF_Label aTargetRoot = aNew->document()->Main().Father();
90   Handle(TDF_DataSet) aDS = new TDF_DataSet;
91   aDS->AddLabel(aSourceRoot);
92   TDF_ClosureTool::Closure(aDS);
93   Handle(TDF_RelocationTable) aRT = new TDF_RelocationTable;
94   aRT->SetRelocation(aSourceRoot, aTargetRoot);
95   TDF_CopyTool::Copy(aDS, aRT);
96
97   aNew->synchronizeFeatures();
98   return aNew;
99 }
100
101 Model_PluginManager::Model_PluginManager()
102 {
103   myPluginsInfoLoaded = false;
104   myCheckTransactions = true;
105   ModelAPI_PluginManager::setPluginManager(boost::shared_ptr<ModelAPI_PluginManager>(this));
106   // register the configuration reading listener
107   Events_Loop* aLoop = Events_Loop::loop();
108   static Events_ID FeatureEvent = Events_Loop::eventByName("FeatureRegisterEvent");
109   aLoop->registerListener(this, FeatureEvent);
110   aLoop->registerListener(this, Events_Loop::eventByName(EVENT_FEATURE_CREATED));
111   aLoop->registerListener(this, Events_Loop::eventByName(EVENT_FEATURE_UPDATED));
112   aLoop->registerListener(this, Events_Loop::eventByName(EVENT_FEATURE_DELETED));
113 }
114
115 void Model_PluginManager::processEvent(const Events_Message* theMessage)
116 {
117   static Events_ID FeatureEvent = Events_Loop::eventByName("FeatureRegisterEvent");
118   if (theMessage->eventID() == FeatureEvent) {
119     const Config_FeatureMessage* aMsg =
120       dynamic_cast<const Config_FeatureMessage*>(theMessage);
121     if (aMsg) {
122       // proccess the plugin info, load plugin
123       if (myPlugins.find(aMsg->id()) == myPlugins.end()) {
124         myPlugins[aMsg->id()] = aMsg->pluginLibrary();
125       }
126     }
127     // plugins information was started to load, so, it will be loaded
128     myPluginsInfoLoaded = true;
129   } else { // create/update/delete
130     if (myCheckTransactions && !rootDocument()->isOperation())
131       Events_Error::send("Modification of data structure outside of the transaction");
132   }
133 }
134
135 void Model_PluginManager::LoadPluginsInfo()
136 {
137   if (myPluginsInfoLoaded) // nothing to do
138     return;
139
140   // Read plugins information from XML files
141   Config_ModuleReader aXMLReader("FeatureRegisterEvent");
142   aXMLReader.readAll();
143 }
144
145 void Model_PluginManager::registerPlugin(ModelAPI_Plugin* thePlugin)
146 {
147   myPluginObjs[myCurrentPluginName] = thePlugin;
148 }
149
150 ModelAPI_ValidatorsFactory* Model_PluginManager::validators()
151 {
152   static Model_ValidatorsFactory* aFactory = new Model_ValidatorsFactory;
153   return aFactory;
154 }