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