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