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