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