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