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