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