Salome HOME
Make circle with radius 0 not crashed: check validity before execution
[modules/shaper.git] / src / Model / Model_PluginManager.cpp
index 05f1586e678f1c1b5ba1df48cd2b668ee71926d8..06893e6fc343c6615bac7b9f097d1cccb56af633 100644 (file)
@@ -9,9 +9,11 @@
 #include <Model_Document.h>
 #include <Model_Application.h>
 #include <Model_Events.h>
+#include <Model_Validator.h>
 #include <Events_Loop.h>
 #include <Events_Error.h>
 #include <Config_FeatureMessage.h>
+#include <Config_ValidatorMessage.h>
 #include <Config_ModuleReader.h>
 
 #include <TDF_CopyTool.hxx>
 #include <TDF_RelocationTable.hxx>
 #include <TDF_ClosureTool.hxx>
 
-
 using namespace std;
 
 static Model_PluginManager* myImpl = new Model_PluginManager();
 
-boost::shared_ptr<ModelAPI_Feature> Model_PluginManager::createFeature(string theFeatureID)
+FeaturePtr Model_PluginManager::createFeature(string theFeatureID)
 {
-  if (this != myImpl) return myImpl->createFeature(theFeatureID);
+  if (this != myImpl)
+    return myImpl->createFeature(theFeatureID);
 
   LoadPluginsInfo();
   if (myPlugins.find(theFeatureID) != myPlugins.end()) {
@@ -36,25 +38,27 @@ boost::shared_ptr<ModelAPI_Feature> Model_PluginManager::createFeature(string th
       Config_ModuleReader::loadLibrary(myCurrentPluginName);
     }
     if (myPluginObjs.find(myCurrentPluginName) != myPluginObjs.end()) {
-      boost::shared_ptr<ModelAPI_Feature> aCreated = 
-        myPluginObjs[myCurrentPluginName]->createFeature(theFeatureID);
+      FeaturePtr aCreated = myPluginObjs[myCurrentPluginName]->createFeature(theFeatureID);
       if (!aCreated) {
-        Events_Error::send(string("Can not initialize feature '") + theFeatureID +
-          "' in plugin '" + myCurrentPluginName + "'");
+        Events_Error::send(
+            string("Can not initialize feature '") + theFeatureID + "' in plugin '"
+                + myCurrentPluginName + "'");
       }
       return aCreated;
     } else {
       Events_Error::send(string("Can not load plugin '") + myCurrentPluginName + "'");
     }
+  } else {
+    Events_Error::send(string("Feature '") + theFeatureID + "' not found in any plugin");
   }
 
-  return boost::shared_ptr<ModelAPI_Feature>(); // return nothing
+  return FeaturePtr();  // return nothing
 }
 
 boost::shared_ptr<ModelAPI_Document> Model_PluginManager::rootDocument()
 {
   return boost::shared_ptr<ModelAPI_Document>(
-    Model_Application::getApplication()->getDocument("root"));
+      Model_Application::getApplication()->getDocument("root"));
 }
 
 bool Model_PluginManager::hasRootDocument()
@@ -72,17 +76,19 @@ boost::shared_ptr<ModelAPI_Document> Model_PluginManager::currentDocument()
 void Model_PluginManager::setCurrentDocument(boost::shared_ptr<ModelAPI_Document> theDoc)
 {
   myCurrentDoc = theDoc;
+  static Events_Message aMsg(Events_Loop::eventByName("CurrentDocumentChanged"));
+  Events_Loop::loop()->send(aMsg);
 }
 
 boost::shared_ptr<ModelAPI_Document> Model_PluginManager::copy(
-  boost::shared_ptr<ModelAPI_Document> theSource, std::string theID) 
+    boost::shared_ptr<ModelAPI_Document> theSource, std::string theID)
 {
   // create a new document
   boost::shared_ptr<Model_Document> aNew = boost::dynamic_pointer_cast<Model_Document>(
-    Model_Application::getApplication()->getDocument(theID));
+      Model_Application::getApplication()->getDocument(theID));
   // make a copy of all labels
-  TDF_Label aSourceRoot = 
-    boost::dynamic_pointer_cast<Model_Document>(theSource)->document()->Main().Father();
+  TDF_Label aSourceRoot = boost::dynamic_pointer_cast<Model_Document>(theSource)->document()->Main()
+      .Father();
   TDF_Label aTargetRoot = aNew->document()->Main().Father();
   Handle(TDF_DataSet) aDS = new TDF_DataSet;
   aDS->AddLabel(aSourceRoot);
@@ -98,22 +104,24 @@ boost::shared_ptr<ModelAPI_Document> Model_PluginManager::copy(
 Model_PluginManager::Model_PluginManager()
 {
   myPluginsInfoLoaded = false;
-  ModelAPI_PluginManager::SetPluginManager(boost::shared_ptr<ModelAPI_PluginManager>(this));
+  myCheckTransactions = true;
+  ModelAPI_PluginManager::setPluginManager(boost::shared_ptr<ModelAPI_PluginManager>(this));
   // register the configuration reading listener
   Events_Loop* aLoop = Events_Loop::loop();
-  static Events_ID FeatureEvent = Events_Loop::eventByName("FeatureRegisterEvent");
-  aLoop->registerListener(this, FeatureEvent);
-  aLoop->registerListener(this, Events_Loop::eventByName(EVENT_FEATURE_CREATED));
-  aLoop->registerListener(this, Events_Loop::eventByName(EVENT_FEATURE_UPDATED));
-  aLoop->registerListener(this, Events_Loop::eventByName(EVENT_FEATURE_DELETED));
+  static const Events_ID kFeatureEvent = Events_Loop::eventByName("FeatureRegisterEvent");
+  aLoop->registerListener(this, kFeatureEvent);
+  aLoop->registerListener(this, Events_Loop::eventByName(EVENT_OBJECT_CREATED));
+  aLoop->registerListener(this, Events_Loop::eventByName(EVENT_OBJECT_UPDATED));
+  aLoop->registerListener(this, Events_Loop::eventByName(EVENT_OBJECT_DELETED));
+  aLoop->registerListener(this, Events_Loop::eventByName(EVENT_VALIDATOR_LOADED));
 }
 
 void Model_PluginManager::processEvent(const Events_Message* theMessage)
 {
-  static Events_ID FeatureEvent = Events_Loop::eventByName("FeatureRegisterEvent");
-  if (theMessage->eventID() == FeatureEvent) {
-    const Config_FeatureMessage* aMsg =
-      dynamic_cast<const Config_FeatureMessage*>(theMessage);
+  static const Events_ID kFeatureEvent = Events_Loop::eventByName("FeatureRegisterEvent");
+  static const Events_ID kValidatorEvent = Events_Loop::eventByName(EVENT_VALIDATOR_LOADED);
+  if (theMessage->eventID() == kFeatureEvent) {
+    const Config_FeatureMessage* aMsg = dynamic_cast<const Config_FeatureMessage*>(theMessage);
     if (aMsg) {
       // proccess the plugin info, load plugin
       if (myPlugins.find(aMsg->id()) == myPlugins.end()) {
@@ -122,15 +130,25 @@ void Model_PluginManager::processEvent(const Events_Message* theMessage)
     }
     // plugins information was started to load, so, it will be loaded
     myPluginsInfoLoaded = true;
-  } else { // create/update/delete
-    if (!rootDocument()->isOperation())
+  } else if (theMessage->eventID() == kValidatorEvent) {
+    const Config_ValidatorMessage* aMsg = dynamic_cast<const Config_ValidatorMessage*>(theMessage);
+    if (aMsg) {
+      if (aMsg->attributeId().empty()) {  // feature validator
+        validators()->assignValidator(aMsg->validatorId(), aMsg->featureId(), aMsg->parameters());
+      } else {  // attribute validator
+        validators()->assignValidator(aMsg->validatorId(), aMsg->featureId(), aMsg->attributeId(),
+                                      aMsg->parameters());
+      }
+    }
+  } else {  // create/update/delete
+    if (myCheckTransactions && !rootDocument()->isOperation())
       Events_Error::send("Modification of data structure outside of the transaction");
   }
 }
 
 void Model_PluginManager::LoadPluginsInfo()
 {
-  if (myPluginsInfoLoaded) // nothing to do
+  if (myPluginsInfoLoaded)  // nothing to do
     return;
 
   // Read plugins information from XML files
@@ -142,3 +160,9 @@ void Model_PluginManager::registerPlugin(ModelAPI_Plugin* thePlugin)
 {
   myPluginObjs[myCurrentPluginName] = thePlugin;
 }
+
+ModelAPI_ValidatorsFactory* Model_PluginManager::validators()
+{
+  static Model_ValidatorsFactory* aFactory = new Model_ValidatorsFactory;
+  return aFactory;
+}