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