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