Salome HOME
Make circle with radius 0 not crashed: check validity before execution
[modules/shaper.git] / src / Model / Model_PluginManager.cpp
1 // File:        Model_PluginManager.cxx
2 // Created:     20 Mar 2014
3 // Author:      Mikhail PONIKAROV
4
5 #include <Model_PluginManager.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_PluginManager* myImpl = new Model_PluginManager();
27
28 FeaturePtr Model_PluginManager::createFeature(string theFeatureID)
29 {
30   if (this != myImpl)
31     return myImpl->createFeature(theFeatureID);
32
33   LoadPluginsInfo();
34   if (myPlugins.find(theFeatureID) != myPlugins.end()) {
35     myCurrentPluginName = myPlugins[theFeatureID];
36     if (myPluginObjs.find(myCurrentPluginName) == myPluginObjs.end()) {
37       // load plugin library if not yet done
38       Config_ModuleReader::loadLibrary(myCurrentPluginName);
39     }
40     if (myPluginObjs.find(myCurrentPluginName) != myPluginObjs.end()) {
41       FeaturePtr aCreated = myPluginObjs[myCurrentPluginName]->createFeature(theFeatureID);
42       if (!aCreated) {
43         Events_Error::send(
44             string("Can not initialize feature '") + theFeatureID + "' in plugin '"
45                 + myCurrentPluginName + "'");
46       }
47       return aCreated;
48     } else {
49       Events_Error::send(string("Can not load plugin '") + myCurrentPluginName + "'");
50     }
51   } else {
52     Events_Error::send(string("Feature '") + theFeatureID + "' not found in any plugin");
53   }
54
55   return FeaturePtr();  // return nothing
56 }
57
58 boost::shared_ptr<ModelAPI_Document> Model_PluginManager::rootDocument()
59 {
60   return boost::shared_ptr<ModelAPI_Document>(
61       Model_Application::getApplication()->getDocument("root"));
62 }
63
64 bool Model_PluginManager::hasRootDocument()
65 {
66   return Model_Application::getApplication()->hasDocument("root");
67 }
68
69 boost::shared_ptr<ModelAPI_Document> Model_PluginManager::currentDocument()
70 {
71   if (!myCurrentDoc || !Model_Application::getApplication()->hasDocument(myCurrentDoc->id()))
72     myCurrentDoc = rootDocument();
73   return myCurrentDoc;
74 }
75
76 void Model_PluginManager::setCurrentDocument(boost::shared_ptr<ModelAPI_Document> theDoc)
77 {
78   myCurrentDoc = theDoc;
79   static Events_Message aMsg(Events_Loop::eventByName("CurrentDocumentChanged"));
80   Events_Loop::loop()->send(aMsg);
81 }
82
83 boost::shared_ptr<ModelAPI_Document> Model_PluginManager::copy(
84     boost::shared_ptr<ModelAPI_Document> theSource, std::string theID)
85 {
86   // create a new document
87   boost::shared_ptr<Model_Document> aNew = boost::dynamic_pointer_cast<Model_Document>(
88       Model_Application::getApplication()->getDocument(theID));
89   // make a copy of all labels
90   TDF_Label aSourceRoot = boost::dynamic_pointer_cast<Model_Document>(theSource)->document()->Main()
91       .Father();
92   TDF_Label aTargetRoot = aNew->document()->Main().Father();
93   Handle(TDF_DataSet) aDS = new TDF_DataSet;
94   aDS->AddLabel(aSourceRoot);
95   TDF_ClosureTool::Closure(aDS);
96   Handle(TDF_RelocationTable) aRT = new TDF_RelocationTable;
97   aRT->SetRelocation(aSourceRoot, aTargetRoot);
98   TDF_CopyTool::Copy(aDS, aRT);
99
100   aNew->synchronizeFeatures();
101   return aNew;
102 }
103
104 Model_PluginManager::Model_PluginManager()
105 {
106   myPluginsInfoLoaded = false;
107   myCheckTransactions = true;
108   ModelAPI_PluginManager::setPluginManager(boost::shared_ptr<ModelAPI_PluginManager>(this));
109   // register the configuration reading listener
110   Events_Loop* aLoop = Events_Loop::loop();
111   static const Events_ID kFeatureEvent = Events_Loop::eventByName("FeatureRegisterEvent");
112   aLoop->registerListener(this, kFeatureEvent);
113   aLoop->registerListener(this, Events_Loop::eventByName(EVENT_OBJECT_CREATED));
114   aLoop->registerListener(this, Events_Loop::eventByName(EVENT_OBJECT_UPDATED));
115   aLoop->registerListener(this, Events_Loop::eventByName(EVENT_OBJECT_DELETED));
116   aLoop->registerListener(this, Events_Loop::eventByName(EVENT_VALIDATOR_LOADED));
117 }
118
119 void Model_PluginManager::processEvent(const Events_Message* theMessage)
120 {
121   static const Events_ID kFeatureEvent = Events_Loop::eventByName("FeatureRegisterEvent");
122   static const Events_ID kValidatorEvent = Events_Loop::eventByName(EVENT_VALIDATOR_LOADED);
123   if (theMessage->eventID() == kFeatureEvent) {
124     const Config_FeatureMessage* aMsg = dynamic_cast<const Config_FeatureMessage*>(theMessage);
125     if (aMsg) {
126       // proccess the plugin info, load plugin
127       if (myPlugins.find(aMsg->id()) == myPlugins.end()) {
128         myPlugins[aMsg->id()] = aMsg->pluginLibrary();
129       }
130     }
131     // plugins information was started to load, so, it will be loaded
132     myPluginsInfoLoaded = true;
133   } else if (theMessage->eventID() == kValidatorEvent) {
134     const Config_ValidatorMessage* aMsg = dynamic_cast<const Config_ValidatorMessage*>(theMessage);
135     if (aMsg) {
136       if (aMsg->attributeId().empty()) {  // feature validator
137         validators()->assignValidator(aMsg->validatorId(), aMsg->featureId(), aMsg->parameters());
138       } else {  // attribute validator
139         validators()->assignValidator(aMsg->validatorId(), aMsg->featureId(), aMsg->attributeId(),
140                                       aMsg->parameters());
141       }
142     }
143   } else {  // create/update/delete
144     if (myCheckTransactions && !rootDocument()->isOperation())
145       Events_Error::send("Modification of data structure outside of the transaction");
146   }
147 }
148
149 void Model_PluginManager::LoadPluginsInfo()
150 {
151   if (myPluginsInfoLoaded)  // nothing to do
152     return;
153
154   // Read plugins information from XML files
155   Config_ModuleReader aXMLReader("FeatureRegisterEvent");
156   aXMLReader.readAll();
157 }
158
159 void Model_PluginManager::registerPlugin(ModelAPI_Plugin* thePlugin)
160 {
161   myPluginObjs[myCurrentPluginName] = thePlugin;
162 }
163
164 ModelAPI_ValidatorsFactory* Model_PluginManager::validators()
165 {
166   static Model_ValidatorsFactory* aFactory = new Model_ValidatorsFactory;
167   return aFactory;
168 }