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