Salome HOME
The ability to register python plugins added
[modules/shaper.git] / src / Model / Model_Session.cpp
1 // File:        Model_Session.cxx
2 // Created:     20 Mar 2014
3 // Author:      Mikhail PONIKAROV
4
5 #include <Model_Session.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_ValidatorMessage.h>
17 #include <Config_ModuleReader.h>
18
19 #include <TDF_CopyTool.hxx>
20 #include <TDF_DataSet.hxx>
21 #include <TDF_RelocationTable.hxx>
22 #include <TDF_ClosureTool.hxx>
23
24 using namespace std;
25
26 static Model_Session* myImpl = new Model_Session();
27
28 // t oredirect all calls to the root document
29 #define ROOT_DOC boost::dynamic_pointer_cast<Model_Document>(moduleDocument())
30
31 bool Model_Session::load(const char* theFileName)
32 {
33   return ROOT_DOC->load(theFileName);
34 }
35
36 bool Model_Session::save(const char* theFileName, std::list<std::string>& theResults)
37 {
38   return ROOT_DOC->save(theFileName, theResults);
39 }
40
41 void Model_Session::startOperation()
42 {
43   ROOT_DOC->startOperation();
44 }
45
46 void Model_Session::finishOperation()
47 {
48   ROOT_DOC->finishOperation();
49   static boost::shared_ptr<Events_Message> aFinishMsg
50     (new Events_Message(Events_Loop::eventByName("FinishOperation")));
51   Events_Loop::loop()->send(aFinishMsg);
52 }
53
54 void Model_Session::abortOperation()
55 {
56   ROOT_DOC->abortOperation();
57   static boost::shared_ptr<Events_Message> anAbortMsg
58     (new Events_Message(Events_Loop::eventByName("AbortOperation")));
59   Events_Loop::loop()->send(anAbortMsg);
60 }
61
62 bool Model_Session::isOperation()
63 {
64   return ROOT_DOC->isOperation();
65 }
66
67 bool Model_Session::isModified()
68 {
69   return ROOT_DOC->isModified();
70 }
71
72 bool Model_Session::canUndo()
73 {
74   return ROOT_DOC->canUndo();
75 }
76
77 void Model_Session::undo()
78 {
79   ROOT_DOC->undo();
80 }
81
82 bool Model_Session::canRedo()
83 {
84   return ROOT_DOC->canRedo();
85 }
86
87 void Model_Session::redo()
88 {
89   ROOT_DOC->redo();
90 }
91
92 FeaturePtr Model_Session::createFeature(string theFeatureID)
93 {
94   if (this != myImpl) {
95     return myImpl->createFeature(theFeatureID);
96   }
97
98   LoadPluginsInfo();
99   if (myPlugins.find(theFeatureID) != myPlugins.end()) {
100     std::pair<std::string, std::string>& aPlugin = myPlugins[theFeatureID]; // plugin and doc kind
101     if (!aPlugin.second.empty() && aPlugin.second != activeDocument()->kind()) {
102       Events_Error::send(
103           string("Feature '") + theFeatureID + "' can be created only in document '"
104               + aPlugin.second + "' by the XML definition");
105       return FeaturePtr();
106     }
107     myCurrentPluginName = aPlugin.first;
108     if (myPluginObjs.find(myCurrentPluginName) == myPluginObjs.end()) {
109       // load plugin library if not yet done
110       Config_ModuleReader::loadPlugin(myCurrentPluginName);
111     }
112     if (myPluginObjs.find(myCurrentPluginName) != myPluginObjs.end()) {
113       FeaturePtr aCreated = myPluginObjs[myCurrentPluginName]->createFeature(theFeatureID);
114       if (!aCreated) {
115         Events_Error::send(
116             string("Can not initialize feature '") + theFeatureID + "' in plugin '"
117                 + myCurrentPluginName + "'");
118       }
119       return aCreated;
120     } else {
121       Events_Error::send(string("Can not load plugin '") + myCurrentPluginName + "'");
122     }
123   } else {
124     Events_Error::send(string("Feature '") + theFeatureID + "' not found in any plugin");
125   }
126
127   return FeaturePtr();  // return nothing
128 }
129
130 boost::shared_ptr<ModelAPI_Document> Model_Session::moduleDocument()
131 {
132   return boost::shared_ptr<ModelAPI_Document>(
133       Model_Application::getApplication()->getDocument("root"));
134 }
135
136 bool Model_Session::hasModuleDocument()
137 {
138   return Model_Application::getApplication()->hasDocument("root");
139 }
140
141 boost::shared_ptr<ModelAPI_Document> Model_Session::activeDocument()
142 {
143   if (!myCurrentDoc || !Model_Application::getApplication()->hasDocument(myCurrentDoc->id()))
144     myCurrentDoc = moduleDocument();
145   return myCurrentDoc;
146 }
147
148 void Model_Session::setActiveDocument(boost::shared_ptr<ModelAPI_Document> theDoc)
149 {
150   if (myCurrentDoc != theDoc) {
151     myCurrentDoc = theDoc;
152     static boost::shared_ptr<Events_Message> aMsg(new Events_Message(Events_Loop::eventByName("CurrentDocumentChanged")));
153     Events_Loop::loop()->send(aMsg);
154   }
155 }
156
157 std::list<boost::shared_ptr<ModelAPI_Document> > Model_Session::allOpenedDocuments()
158 {
159   list<boost::shared_ptr<ModelAPI_Document> > aResult;
160   aResult.push_back(moduleDocument());
161   // add subs recursively
162   list<boost::shared_ptr<ModelAPI_Document> >::iterator aDoc = aResult.begin();
163   for(; aDoc != aResult.end(); aDoc++) {
164     DocumentPtr anAPIDoc = *aDoc;
165     boost::shared_ptr<Model_Document> aDoc = boost::dynamic_pointer_cast<Model_Document>(anAPIDoc);
166     if (aDoc) {
167       std::set<std::string>::const_iterator aSubIter = aDoc->subDocuments().cbegin();
168       for(; aSubIter != aDoc->subDocuments().cend(); aSubIter++) {
169         if (!Model_Application::getApplication()->isLoadByDemand(*aSubIter)) {
170           aResult.push_back(Model_Application::getApplication()->getDocument(*aSubIter));
171         }
172       }
173     }
174   }
175   return aResult;
176 }
177
178 boost::shared_ptr<ModelAPI_Document> Model_Session::copy(
179     boost::shared_ptr<ModelAPI_Document> theSource, std::string theID)
180 {
181   // create a new document
182   boost::shared_ptr<Model_Document> aNew = boost::dynamic_pointer_cast<Model_Document>(
183       Model_Application::getApplication()->getDocument(theID));
184   // make a copy of all labels
185   TDF_Label aSourceRoot = boost::dynamic_pointer_cast<Model_Document>(theSource)->document()->Main()
186       .Father();
187   TDF_Label aTargetRoot = aNew->document()->Main().Father();
188   Handle(TDF_DataSet) aDS = new TDF_DataSet;
189   aDS->AddLabel(aSourceRoot);
190   TDF_ClosureTool::Closure(aDS);
191   Handle(TDF_RelocationTable) aRT = new TDF_RelocationTable;
192   aRT->SetRelocation(aSourceRoot, aTargetRoot);
193   TDF_CopyTool::Copy(aDS, aRT);
194
195   aNew->synchronizeFeatures(false, true);
196   return aNew;
197 }
198
199 Model_Session::Model_Session()
200 {
201   myPluginsInfoLoaded = false;
202   myCheckTransactions = true;
203   ModelAPI_Session::setSession(boost::shared_ptr<ModelAPI_Session>(this));
204   // register the configuration reading listener
205   Events_Loop* aLoop = Events_Loop::loop();
206   static const Events_ID kFeatureEvent = Events_Loop::eventByName("FeatureRegisterEvent");
207   aLoop->registerListener(this, kFeatureEvent);
208   aLoop->registerListener(this, Events_Loop::eventByName(EVENT_OBJECT_CREATED), 0, true);
209   aLoop->registerListener(this, Events_Loop::eventByName(EVENT_OBJECT_UPDATED), 0, true);
210   aLoop->registerListener(this, Events_Loop::eventByName(EVENT_OBJECT_DELETED), 0, true);
211   aLoop->registerListener(this, Events_Loop::eventByName(EVENT_VALIDATOR_LOADED));
212 }
213
214 void Model_Session::processEvent(const boost::shared_ptr<Events_Message>& theMessage)
215 {
216   static const Events_ID kFeatureEvent = Events_Loop::eventByName("FeatureRegisterEvent");
217   static const Events_ID kValidatorEvent = Events_Loop::eventByName(EVENT_VALIDATOR_LOADED);
218   if (theMessage->eventID() == kFeatureEvent) {
219     const boost::shared_ptr<Config_FeatureMessage> aMsg = 
220       boost::dynamic_pointer_cast<Config_FeatureMessage>(theMessage);
221     if (aMsg) {
222       // proccess the plugin info, load plugin
223       if (myPlugins.find(aMsg->id()) == myPlugins.end()) {
224         myPlugins[aMsg->id()] = std::pair<std::string, std::string>(
225           aMsg->pluginLibrary(), aMsg->documentKind());
226       }
227     }
228     // plugins information was started to load, so, it will be loaded
229     myPluginsInfoLoaded = true;
230   } else if (theMessage->eventID() == kValidatorEvent) {
231     boost::shared_ptr<Config_ValidatorMessage> aMsg = 
232       boost::dynamic_pointer_cast<Config_ValidatorMessage>(theMessage);
233     if (aMsg) {
234       if (aMsg->attributeId().empty()) {  // feature validator
235         validators()->assignValidator(aMsg->validatorId(), aMsg->featureId(), aMsg->parameters());
236       } else {  // attribute validator
237         validators()->assignValidator(aMsg->validatorId(), aMsg->featureId(), aMsg->attributeId(),
238                                       aMsg->parameters());
239       }
240     }
241   } else {  // create/update/delete
242     if (myCheckTransactions && !isOperation())
243       Events_Error::send("Modification of data structure outside of the transaction");
244   }
245 }
246
247 void Model_Session::LoadPluginsInfo()
248 {
249   if (myPluginsInfoLoaded)  // nothing to do
250     return;
251
252   // Read plugins information from XML files
253   Config_ModuleReader aXMLReader("FeatureRegisterEvent");
254   aXMLReader.readAll();
255 }
256
257 void Model_Session::registerPlugin(ModelAPI_Plugin* thePlugin)
258 {
259   myPluginObjs[myCurrentPluginName] = thePlugin;
260   static Events_ID EVENT_LOAD = Events_Loop::loop()->eventByName(EVENT_PLUGIN_LOADED);
261   ModelAPI_EventCreator::get()->sendUpdated(ObjectPtr(), EVENT_LOAD);
262   Events_Loop::loop()->flush(EVENT_LOAD);
263 }
264
265 ModelAPI_ValidatorsFactory* Model_Session::validators()
266 {
267   static Model_ValidatorsFactory* aFactory = new Model_ValidatorsFactory;
268   return aFactory;
269 }