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