Salome HOME
refs #98 - Default color for constraints
[modules/shaper.git] / src / Config / Config_FeatureReader.cpp
index 73a5c07fbcfddde72c466e6ea4c84725df7f7743..1f7a65b2dcbc366f278e94c9c37b4d09da2d57ae 100644 (file)
  *      Author: sbh
  */
 
+#include <Config_Keywords.h>
+#include <Config_Common.h>
+#include <Config_FeatureMessage.h>
 #include <Config_FeatureReader.h>
+#include <Events_Message.h>
+#include <Events_Loop.h>
 
-#include <Event_Loop.h>
+#include <libxml/parser.h>
+#include <libxml/tree.h>
+#include <libxml/xmlstring.h>
 
-#include <libxml\parser.h>
-#include <libxml\tree.h>
+#include <string>
+#include <algorithm>
+#include <list>
 
 #ifdef _DEBUG
 #include <iostream>
 #endif
 
-//Hardcoded xml entities
-// * Nodes
-const static char* NODE_WORKBENCH = "workbench";
-const static char* NODE_GROUP = "group";
-const static char* NODE_FEATURE = "feature";
-
-// * Properties
-const static char* _ID = "id";
-//const static char* WORKBENCH_ID = "id";
-//const static char* GROUP_ID = "id";
-//const static char* FEATURE_ID = "id";
-const static char* FEATURE_TEXT = "text";
-const static char* FEATURE_TOOLTIP = "tooltip";
-const static char* FEATURE_ICON = "icon";
-const static char* FEATURE_KEYSEQUENCE = "keysequence";
-
-Config_FeatureReader::Config_FeatureReader(const std::string& theXmlFile)
-    : Config_XMLReader(theXmlFile),
-      myFetchWidgetCfg(false)
-{
-  myLibraryName = "";
-
-#ifdef _DEBUG
-  if (!Event_Loop::loop()) {
-    std::cout << "Config_FeatureReader::importWorkbench: "
-        << "No event loop registered" << std::endl;
-  }
-#endif
-}
-
 Config_FeatureReader::Config_FeatureReader(const std::string& theXmlFile,
-                                           const std::string& theLibraryName)
+                                           const std::string& theLibraryName,
+                                           const char* theEventGenerated)
     : Config_XMLReader(theXmlFile),
       myLibraryName(theLibraryName),
-      myFetchWidgetCfg(false)
+      myEventGenerated(theEventGenerated ? theEventGenerated : EVENT_FEATURE_LOADED)
 {
-#ifdef _DEBUG
-  if (!Event_Loop::loop()) {
-    std::cout << "Config_FeatureReader::importWorkbench: "
-        << "No event loop registered" << std::endl;
-  }
-#endif
 }
 
 Config_FeatureReader::~Config_FeatureReader()
 {
 }
 
-std::string Config_FeatureReader::featureWidgetCfg(std::string theFeatureName)
+std::list<std::string> Config_FeatureReader::features() const
 {
-  myFetchWidgetCfg = true;
-  readAll();
-  myFetchWidgetCfg = false;
-  return myWidgetCfg;
+  return myFeatures;
 }
 
 void Config_FeatureReader::processNode(xmlNodePtr theNode)
 {
-  static Event_ID aMenuItemEvent = Event_Loop::eventByName("RegisterFeature");
+  Events_ID aMenuItemEvent = Events_Loop::eventByName(myEventGenerated);
   if (isNode(theNode, NODE_FEATURE, NULL)) {
-    if (myFetchWidgetCfg) {
-      xmlBufferPtr buffer = xmlBufferCreate();
-      int size = xmlNodeDump(buffer, theNode->doc, theNode, 0, 1);
-      myWidgetCfg = std::string((char*) buffer->content);
-    } else {
-      Event_Loop* aEvLoop = Event_Loop::loop();
-      Config_FeatureMessage aMessage(aMenuItemEvent, this);
-      fillFeature(theNode, aMessage);
-      aEvLoop->send(aMessage);
-    }
+    Events_Loop* aEvLoop = Events_Loop::loop();
+    boost::shared_ptr<Config_FeatureMessage> aMessage(
+      new Config_FeatureMessage(aMenuItemEvent, this));
+    fillFeature(theNode, aMessage);
+    myFeatures.push_back(getProperty(theNode, _ID));
+    //If a feature has xml definition for it's widget:
+    aMessage->setUseInput(hasChild(theNode));
+    aEvLoop->send(aMessage);
+    //The m_last* variables always defined before fillFeature() call. XML is a tree.
+  } else if (isNode(theNode, NODE_WORKBENCH, NODE_GROUP, NULL)) {
+    storeAttribute(theNode, _ID);
+    storeAttribute(theNode, WORKBENCH_DOC);
   }
-  //The m_last* variables always defined before fillFeature() call. XML is a tree.
-  if (isNode(theNode, NODE_GROUP, NULL)) {
-    myLastGroup = getProperty(theNode, _ID);
+  //Process SOURCE, VALIDATOR nodes.
+  Config_XMLReader::processNode(theNode);
+}
+
+bool Config_FeatureReader::processChildren(xmlNodePtr theNode)
+{
+  return isNode(theNode, NODE_WORKBENCH, NODE_GROUP, NODE_FEATURE, NULL);
+}
+
+void Config_FeatureReader::fillFeature(xmlNodePtr theNode, 
+  const boost::shared_ptr<Config_FeatureMessage>& outFeatureMessage)
+{
+  outFeatureMessage->setId(getProperty(theNode, _ID));
+  outFeatureMessage->setPluginLibrary(myLibraryName);
+  outFeatureMessage->setNestedFeatures(getProperty(theNode, FEATURE_NESTED));
+
+  bool isInternal = isInternalFeature(theNode);
+  outFeatureMessage->setInternal(isInternal);
+  if (isInternal) {
+    //Internal feature has no visual representation.
+    return;
   }
-  if (isNode(theNode, NODE_WORKBENCH, NULL)) {
-    myLastWorkbench = getProperty(theNode, _ID);
+  outFeatureMessage->setText(getProperty(theNode, FEATURE_TEXT));
+  outFeatureMessage->setTooltip(getProperty(theNode, FEATURE_TOOLTIP));
+  outFeatureMessage->setIcon(getProperty(theNode, FEATURE_ICON));
+  outFeatureMessage->setKeysequence(getProperty(theNode, FEATURE_KEYSEQUENCE));
+  outFeatureMessage->setGroupId(restoreAttribute(NODE_GROUP, _ID));
+  outFeatureMessage->setWorkbenchId(restoreAttribute(NODE_WORKBENCH, _ID));
+  outFeatureMessage->setDocumentKind(restoreAttribute(NODE_WORKBENCH, WORKBENCH_DOC));
+}
+
+bool Config_FeatureReader::isInternalFeature(xmlNodePtr theNode)
+{
+  std::string prop = getProperty(theNode, FEATURE_INTERNAL);
+  std::transform(prop.begin(), prop.end(), prop.begin(), ::tolower);
+  if (prop.empty() || prop == "false" || prop == "0") {
+    return false;
   }
+  return true;
 }
 
-bool Config_FeatureReader::processChildren(xmlNodePtr theNode)
+void Config_FeatureReader::storeAttribute(xmlNodePtr theNode,
+                                          const char* theNodeAttribute)
 {
-  return isNode(theNode, NODE_WORKBENCH, NODE_GROUP, NULL);
+  std::string aKey = getNodeName(theNode) + ":" + std::string(theNodeAttribute);
+  std::string aValue = getProperty(theNode, theNodeAttribute);
+  if(!aValue.empty()) {
+    myParentAttributes[aKey] = aValue;
+  }
 }
 
-void Config_FeatureReader::fillFeature(xmlNodePtr theRoot, Config_FeatureMessage& outFtMessage)
+std::string Config_FeatureReader::restoreAttribute(xmlNodePtr theNode,
+                                                   const char* theNodeAttribute)
 {
-  outFtMessage.setId(getProperty(theRoot, _ID));
-  outFtMessage.setText(getProperty(theRoot, FEATURE_TEXT));
-  outFtMessage.setTooltip(getProperty(theRoot, FEATURE_TOOLTIP));
-  outFtMessage.setIcon(getProperty(theRoot, FEATURE_ICON));
-  outFtMessage.setKeysequence(getProperty(theRoot, FEATURE_KEYSEQUENCE));
-  outFtMessage.setGroupId(myLastGroup);
-  outFtMessage.setWorkbenchId(myLastWorkbench);
-  outFtMessage.setPluginLibrary(myLibraryName);
+  return restoreAttribute(getNodeName(theNode).c_str(), theNodeAttribute);
+}
+std::string Config_FeatureReader::restoreAttribute(const char* theNodeName,
+                                                   const char* theNodeAttribute)
+{
+  std::string aKey = std::string(theNodeName) + ":" + std::string(theNodeAttribute);
+  std::string result = "";
+  if(myParentAttributes.find(aKey) != myParentAttributes.end()) {
+    result = myParentAttributes[aKey];
+  }
+  return result;
 }