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