Salome HOME
Merge branch 'Dev_0.6' of newgeom:newgeom into Dev_0.6
[modules/shaper.git] / src / Config / Config_FeatureReader.cpp
1 /*
2  * Config_FeatureReader.cpp
3  *
4  *  Created on: Mar 20, 2014
5  *      Author: sbh
6  */
7
8 #include <Config_Keywords.h>
9 #include <Config_Common.h>
10 #include <Config_FeatureMessage.h>
11 #include <Config_AttributeMessage.h>
12 #include <Config_FeatureReader.h>
13 #include <Events_Message.h>
14 #include <Events_Loop.h>
15
16 #include <libxml/parser.h>
17 #include <libxml/tree.h>
18 #include <libxml/xmlstring.h>
19
20 #include <string>
21 #include <algorithm>
22 #include <list>
23
24 #ifdef _DEBUG
25 #include <iostream>
26 #endif
27
28 Config_FeatureReader::Config_FeatureReader(const std::string& theXmlFile,
29                                            const std::string& theLibraryName,
30                                            const char* theEventGenerated)
31     : Config_XMLReader(theXmlFile),
32       myLibraryName(theLibraryName),
33       myEventGenerated(theEventGenerated ? theEventGenerated : Config_FeatureMessage::GUI_EVENT()),
34       myIsProcessWidgets(theEventGenerated != NULL)
35 {
36 }
37
38 Config_FeatureReader::~Config_FeatureReader()
39 {
40 }
41
42 std::list<std::string> Config_FeatureReader::features() const
43 {
44   return myFeatures;
45 }
46
47 void Config_FeatureReader::processNode(xmlNodePtr theNode)
48 {
49   Events_ID aMenuItemEvent = Events_Loop::eventByName(myEventGenerated);
50   if (isNode(theNode, NODE_FEATURE, NULL)) {
51     storeAttribute(theNode, _ID);
52     std::shared_ptr<Config_FeatureMessage> aMessage(new Config_FeatureMessage(aMenuItemEvent, this));
53     fillFeature(theNode, aMessage);
54     myFeatures.push_back(getProperty(theNode, _ID));
55     //If a feature has xml definition for it's widget:
56     aMessage->setUseInput(hasChild(theNode));
57     Events_Loop::loop()->send(aMessage);
58     //The m_last* variables always defined before fillFeature() call. XML is a tree.
59   } else if (isNode(theNode, NODE_WORKBENCH, NODE_GROUP, NULL)) {
60     storeAttribute(theNode, _ID);
61     storeAttribute(theNode, WORKBENCH_DOC);
62   } else if (myIsProcessWidgets && isWidgetNode(theNode)) {
63     std::shared_ptr<Config_AttributeMessage> aMessage(new Config_AttributeMessage(aMenuItemEvent, this));
64     aMessage->setFeatureId(restoreAttribute(NODE_FEATURE, _ID));
65     std::string anAttributeID = getProperty(theNode, _ID);
66     if (!anAttributeID.empty()) {
67       aMessage->setAttributeId(anAttributeID);
68       aMessage->setObligatory(getBooleanAttribute(theNode, ATTRIBUTE_OBLIGATORY, true));
69       aMessage->setConcealment(getBooleanAttribute(theNode, ATTRIBUTE_CONCEALMENT, false));
70       Events_Loop::loop()->send(aMessage);
71     }
72   }
73   //Process SOURCE, VALIDATOR nodes.
74   Config_XMLReader::processNode(theNode);
75 }
76
77 bool Config_FeatureReader::processChildren(xmlNodePtr theNode)
78 {
79   bool result = isNode(theNode, NODE_WORKBENCH, NODE_GROUP, NULL);
80   if(!result && myIsProcessWidgets) {
81     result = isNode(theNode, NODE_FEATURE, NULL);
82   }
83   return result;
84 }
85
86 void Config_FeatureReader::fillFeature(xmlNodePtr theNode, 
87   const std::shared_ptr<Config_FeatureMessage>& outFeatureMessage)
88 {
89   outFeatureMessage->setId(getProperty(theNode, _ID));
90   outFeatureMessage->setPluginLibrary(myLibraryName);
91   outFeatureMessage->setNestedFeatures(getProperty(theNode, FEATURE_NESTED));
92
93   bool isInternal = getBooleanAttribute(theNode, ATTRIBUTE_INTERNAL, false);
94   outFeatureMessage->setInternal(isInternal);
95   if (isInternal) {
96     //Internal feature has no visual representation.
97     return;
98   }
99   outFeatureMessage->setText(getProperty(theNode, FEATURE_TEXT));
100   outFeatureMessage->setTooltip(getProperty(theNode, FEATURE_TOOLTIP));
101   outFeatureMessage->setIcon(getProperty(theNode, FEATURE_ICON));
102   outFeatureMessage->setKeysequence(getProperty(theNode, FEATURE_KEYSEQUENCE));
103   outFeatureMessage->setGroupId(restoreAttribute(NODE_GROUP, _ID));
104   outFeatureMessage->setWorkbenchId(restoreAttribute(NODE_WORKBENCH, _ID));
105   // Get document kind of a feature, if empty set workbench's kind (might be empty too)
106   std::string aDocKind = getProperty(theNode, WORKBENCH_DOC);
107   if(aDocKind.empty()) {
108     aDocKind = restoreAttribute(NODE_WORKBENCH, WORKBENCH_DOC);
109   }
110   outFeatureMessage->setDocumentKind(aDocKind);
111 }
112
113 void Config_FeatureReader::storeAttribute(xmlNodePtr theNode,
114                                           const char* theNodeAttribute)
115 {
116   std::string aKey = getNodeName(theNode) + ":" + std::string(theNodeAttribute);
117   std::string aValue = getProperty(theNode, theNodeAttribute);
118   if(!aValue.empty()) {
119     myParentAttributes[aKey] = aValue;
120   }
121 }
122
123 std::string Config_FeatureReader::restoreAttribute(xmlNodePtr theNode,
124                                                    const char* theNodeAttribute)
125 {
126   return restoreAttribute(getNodeName(theNode).c_str(), theNodeAttribute);
127 }
128 std::string Config_FeatureReader::restoreAttribute(const char* theNodeName,
129                                                    const char* theNodeAttribute)
130 {
131   std::string aKey = std::string(theNodeName) + ":" + std::string(theNodeAttribute);
132   std::string result = "";
133   if(myParentAttributes.find(aKey) != myParentAttributes.end()) {
134     result = myParentAttributes[aKey];
135   }
136   return result;
137 }