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