]> SALOME platform Git repositories - modules/shaper.git/blob - src/Model/Model_Session.cpp
Salome HOME
Issue #126: Activate a part on current document changed event
[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     myCurrentPluginName = myPlugins[theFeatureID];
94     if (myPluginObjs.find(myCurrentPluginName) == myPluginObjs.end()) {
95       // load plugin library if not yet done
96       Config_ModuleReader::loadLibrary(myCurrentPluginName);
97     }
98     if (myPluginObjs.find(myCurrentPluginName) != myPluginObjs.end()) {
99       FeaturePtr aCreated = myPluginObjs[myCurrentPluginName]->createFeature(theFeatureID);
100       if (!aCreated) {
101         Events_Error::send(
102             string("Can not initialize feature '") + theFeatureID + "' in plugin '"
103                 + myCurrentPluginName + "'");
104       }
105       return aCreated;
106     } else {
107       Events_Error::send(string("Can not load plugin '") + myCurrentPluginName + "'");
108     }
109   } else {
110     Events_Error::send(string("Feature '") + theFeatureID + "' not found in any plugin");
111   }
112
113   return FeaturePtr();  // return nothing
114 }
115
116 boost::shared_ptr<ModelAPI_Document> Model_Session::moduleDocument()
117 {
118   return boost::shared_ptr<ModelAPI_Document>(
119       Model_Application::getApplication()->getDocument("root"));
120 }
121
122 bool Model_Session::hasModuleDocument()
123 {
124   return Model_Application::getApplication()->hasDocument("root");
125 }
126
127 boost::shared_ptr<ModelAPI_Document> Model_Session::activeDocument()
128 {
129   if (!myCurrentDoc || !Model_Application::getApplication()->hasDocument(myCurrentDoc->id()))
130     myCurrentDoc = moduleDocument();
131   return myCurrentDoc;
132 }
133
134 void Model_Session::setActiveDocument(boost::shared_ptr<ModelAPI_Document> theDoc)
135 {
136   if (myCurrentDoc != theDoc) {
137     myCurrentDoc = theDoc;
138     static Events_Message aMsg(Events_Loop::eventByName("CurrentDocumentChanged"));
139     Events_Loop::loop()->send(aMsg);
140   }
141 }
142
143 boost::shared_ptr<ModelAPI_Document> Model_Session::copy(
144     boost::shared_ptr<ModelAPI_Document> theSource, std::string theID)
145 {
146   // create a new document
147   boost::shared_ptr<Model_Document> aNew = boost::dynamic_pointer_cast<Model_Document>(
148       Model_Application::getApplication()->getDocument(theID));
149   // make a copy of all labels
150   TDF_Label aSourceRoot = boost::dynamic_pointer_cast<Model_Document>(theSource)->document()->Main()
151       .Father();
152   TDF_Label aTargetRoot = aNew->document()->Main().Father();
153   Handle(TDF_DataSet) aDS = new TDF_DataSet;
154   aDS->AddLabel(aSourceRoot);
155   TDF_ClosureTool::Closure(aDS);
156   Handle(TDF_RelocationTable) aRT = new TDF_RelocationTable;
157   aRT->SetRelocation(aSourceRoot, aTargetRoot);
158   TDF_CopyTool::Copy(aDS, aRT);
159
160   aNew->synchronizeFeatures();
161   return aNew;
162 }
163
164 Model_Session::Model_Session()
165 {
166   myPluginsInfoLoaded = false;
167   myCheckTransactions = true;
168   ModelAPI_Session::setSession(boost::shared_ptr<ModelAPI_Session>(this));
169   // register the configuration reading listener
170   Events_Loop* aLoop = Events_Loop::loop();
171   static const Events_ID kFeatureEvent = Events_Loop::eventByName("FeatureRegisterEvent");
172   aLoop->registerListener(this, kFeatureEvent);
173   aLoop->registerListener(this, Events_Loop::eventByName(EVENT_OBJECT_CREATED));
174   aLoop->registerListener(this, Events_Loop::eventByName(EVENT_OBJECT_UPDATED));
175   aLoop->registerListener(this, Events_Loop::eventByName(EVENT_OBJECT_DELETED));
176   aLoop->registerListener(this, Events_Loop::eventByName(EVENT_VALIDATOR_LOADED));
177 }
178
179 void Model_Session::processEvent(const Events_Message* theMessage)
180 {
181   static const Events_ID kFeatureEvent = Events_Loop::eventByName("FeatureRegisterEvent");
182   static const Events_ID kValidatorEvent = Events_Loop::eventByName(EVENT_VALIDATOR_LOADED);
183   if (theMessage->eventID() == kFeatureEvent) {
184     const Config_FeatureMessage* aMsg = dynamic_cast<const Config_FeatureMessage*>(theMessage);
185     if (aMsg) {
186       // proccess the plugin info, load plugin
187       if (myPlugins.find(aMsg->id()) == myPlugins.end()) {
188         myPlugins[aMsg->id()] = aMsg->pluginLibrary();
189       }
190     }
191     // plugins information was started to load, so, it will be loaded
192     myPluginsInfoLoaded = true;
193   } else if (theMessage->eventID() == kValidatorEvent) {
194     const Config_ValidatorMessage* aMsg = dynamic_cast<const Config_ValidatorMessage*>(theMessage);
195     if (aMsg) {
196       if (aMsg->attributeId().empty()) {  // feature validator
197         validators()->assignValidator(aMsg->validatorId(), aMsg->featureId(), aMsg->parameters());
198       } else {  // attribute validator
199         validators()->assignValidator(aMsg->validatorId(), aMsg->featureId(), aMsg->attributeId(),
200                                       aMsg->parameters());
201       }
202     }
203   } else {  // create/update/delete
204     if (myCheckTransactions && !isOperation())
205       Events_Error::send("Modification of data structure outside of the transaction");
206   }
207 }
208
209 void Model_Session::LoadPluginsInfo()
210 {
211   if (myPluginsInfoLoaded)  // nothing to do
212     return;
213
214   // Read plugins information from XML files
215   Config_ModuleReader aXMLReader("FeatureRegisterEvent");
216   aXMLReader.readAll();
217 }
218
219 void Model_Session::registerPlugin(ModelAPI_Plugin* thePlugin)
220 {
221   myPluginObjs[myCurrentPluginName] = thePlugin;
222   static Events_ID EVENT_LOAD = Events_Loop::loop()->eventByName(EVENT_PLUGIN_LOADED);
223   ModelAPI_EventCreator::get()->sendUpdated(ObjectPtr(), EVENT_LOAD);
224   Events_Loop::loop()->flush(EVENT_LOAD);
225 }
226
227 ModelAPI_ValidatorsFactory* Model_Session::validators()
228 {
229   static Model_ValidatorsFactory* aFactory = new Model_ValidatorsFactory;
230   return aFactory;
231 }