]> SALOME platform Git repositories - modules/shaper.git/blob - src/Config/Config_FeatureReader.cpp
Salome HOME
Merge branch 'Dev_1.1.0' of newgeom:newgeom.git into Dev_1.1.0
[modules/shaper.git] / src / Config / Config_FeatureReader.cpp
1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D
2
3 /*
4  * Config_FeatureReader.cpp
5  *
6  *  Created on: Mar 20, 2014
7  *      Author: sbh
8  */
9
10 #include <Config_Keywords.h>
11 #include <Config_Common.h>
12 #include <Config_FeatureMessage.h>
13 #include <Config_AttributeMessage.h>
14 #include <Config_FeatureReader.h>
15 #include <Events_Message.h>
16 #include <Events_Loop.h>
17
18 #include <libxml/parser.h>
19 #include <libxml/tree.h>
20 #include <libxml/xmlstring.h>
21
22 #include <string>
23 #include <algorithm>
24 #include <list>
25
26 #ifdef _DEBUG
27 #include <iostream>
28 #endif
29
30 Config_FeatureReader::Config_FeatureReader(const std::string& theXmlFile,
31                                            const std::string& theLibraryName,
32                                            const char* theEventGenerated)
33     : Config_XMLReader(theXmlFile),
34       myLibraryName(theLibraryName),
35       myEventGenerated(theEventGenerated ? theEventGenerated : Config_FeatureMessage::GUI_EVENT()),
36       myIsProcessWidgets(theEventGenerated != NULL)
37 {
38 }
39
40 Config_FeatureReader::~Config_FeatureReader()
41 {
42 }
43
44 std::list<std::string> Config_FeatureReader::features() const
45 {
46   return myFeatures;
47 }
48
49 void Config_FeatureReader::processNode(xmlNodePtr theNode)
50 {
51   Events_ID aMenuItemEvent = Events_Loop::eventByName(myEventGenerated);
52   if (isNode(theNode, NODE_FEATURE, NULL)) {
53     storeAttribute(theNode, _ID);
54     std::shared_ptr<Config_FeatureMessage> aMessage(new Config_FeatureMessage(aMenuItemEvent, this));
55     fillFeature(theNode, aMessage);
56     myFeatures.push_back(getProperty(theNode, _ID));
57     //If a feature has xml definition for it's widget:
58     aMessage->setUseInput(hasChild(theNode));
59     Events_Loop::loop()->send(aMessage);
60     //The m_last* variables always defined before fillFeature() call. XML is a tree.
61   } else if (isNode(theNode, NODE_WORKBENCH, NODE_GROUP, NULL)) {
62     storeAttribute(theNode, _ID);
63     storeAttribute(theNode, WORKBENCH_DOC);
64   } else if (myIsProcessWidgets) {
65     // widgets, like shape_selector or containers, like toolbox
66     if (isAttributeNode(theNode)) {
67       std::shared_ptr<Config_AttributeMessage> aMessage(new Config_AttributeMessage(aMenuItemEvent, this));
68       aMessage->setFeatureId(restoreAttribute(NODE_FEATURE, _ID));
69       std::string anAttributeID = getProperty(theNode, _ID);
70       if (!anAttributeID.empty()) {
71         aMessage->setAttributeId(anAttributeID);
72         aMessage->setObligatory(getBooleanAttribute(theNode, ATTR_OBLIGATORY, true));
73         aMessage->setConcealment(getBooleanAttribute(theNode, ATTR_CONCEALMENT, false));
74         // nested "paged" widgets are not allowed, this issue may be resolved here:
75         if (hasParent(theNode, WDG_SWITCH_CASE, WDG_TOOLBOX_BOX, NULL)) {
76           const char* kWdgCase = hasParent(theNode, WDG_SWITCH_CASE, NULL)
77                                  ? WDG_SWITCH_CASE
78                                  : WDG_TOOLBOX_BOX;
79           aMessage->setCaseId(restoreAttribute(kWdgCase, _ID));
80         }
81         Events_Loop::loop()->send(aMessage);
82       }
83     // container pages, like "case" or "box"
84     } else if (isCaseNode(theNode)) {
85       storeAttribute(theNode, _ID); // save case:caseId (or box:boxId)
86     }
87   }
88   //Process SOURCE, VALIDATOR nodes.
89   Config_XMLReader::processNode(theNode);
90 }
91
92 void Config_FeatureReader::cleanup(xmlNodePtr theNode)
93 {
94   if (isCaseNode(theNode)) {
95     // cleanup id of cases when leave case node
96     cleanupAttribute(theNode, _ID);
97   }
98 }
99
100 bool Config_FeatureReader::processChildren(xmlNodePtr theNode)
101 {
102   bool result = isNode(theNode, NODE_WORKBENCH, NODE_GROUP, NULL);
103   if(!result && myIsProcessWidgets) {
104     result = isNode(theNode, NODE_FEATURE, 
105                              WDG_TOOLBOX, WDG_TOOLBOX_BOX,
106                              WDG_SWITCH, WDG_SWITCH_CASE, NULL);
107   }
108   return result;
109 }
110
111 void Config_FeatureReader::fillFeature(xmlNodePtr theFeatureNode,
112   const std::shared_ptr<Config_FeatureMessage>& outFeatureMessage)
113 {
114   outFeatureMessage->setId(getProperty(theFeatureNode, _ID));
115   outFeatureMessage->setPluginLibrary(myLibraryName);
116   outFeatureMessage->setNestedFeatures(getProperty(theFeatureNode, FEATURE_NESTED));
117   outFeatureMessage->setActionsWhenNested(getNormalizedProperty(theFeatureNode, FEATURE_WHEN_NESTED));
118
119   bool isInternal = getBooleanAttribute(theFeatureNode, ATTR_INTERNAL, false);
120   outFeatureMessage->setInternal(isInternal);
121   if (isInternal) {
122     //Internal feature has no visual representation.
123     return;
124   }
125   outFeatureMessage->setText(getProperty(theFeatureNode, FEATURE_TEXT));
126   outFeatureMessage->setTooltip(getProperty(theFeatureNode, FEATURE_TOOLTIP));
127   outFeatureMessage->setIcon(getProperty(theFeatureNode, FEATURE_ICON));
128   outFeatureMessage->setKeysequence(getProperty(theFeatureNode, FEATURE_KEYSEQUENCE));
129   outFeatureMessage->setGroupId(restoreAttribute(NODE_GROUP, _ID));
130   outFeatureMessage->setWorkbenchId(restoreAttribute(NODE_WORKBENCH, _ID));
131   // Get document kind of a feature, if empty set workbench's kind (might be empty too)
132   std::string aDocKind = getProperty(theFeatureNode, FEATURE_DOC);
133   if(aDocKind.empty()) {
134     aDocKind = restoreAttribute(NODE_WORKBENCH, WORKBENCH_DOC);
135   }
136   outFeatureMessage->setDocumentKind(aDocKind);
137 }
138
139 void Config_FeatureReader::storeAttribute(xmlNodePtr theNode,
140                                           const char* theNodeAttribute)
141 {
142   std::string aKey = getNodeName(theNode) + ":" + std::string(theNodeAttribute);
143   std::string aValue = getProperty(theNode, theNodeAttribute);
144   if(!aValue.empty()) {
145     myParentAttributes[aKey] = aValue;
146   }
147 }
148
149 std::string Config_FeatureReader::restoreAttribute(xmlNodePtr theNode,
150                                                    const char* theNodeAttribute)
151 {
152   return restoreAttribute(getNodeName(theNode).c_str(), theNodeAttribute);
153 }
154 std::string Config_FeatureReader::restoreAttribute(const char* theNodeName,
155                                                    const char* theNodeAttribute)
156 {
157   std::string aKey = std::string(theNodeName) + ":" + std::string(theNodeAttribute);
158   std::string result = "";
159   if(myParentAttributes.find(aKey) != myParentAttributes.end()) {
160     result = myParentAttributes[aKey];
161   }
162   return result;
163 }
164
165 bool Config_FeatureReader::cleanupAttribute(xmlNodePtr theNode,
166                                             const char* theNodeAttribute)
167 {
168   return cleanupAttribute(getNodeName(theNode).c_str(), theNodeAttribute);
169 }
170
171 bool Config_FeatureReader::cleanupAttribute(const char* theNodeName,
172                                             const char* theNodeAttribute)
173 {
174   std::string aKey = std::string(theNodeName) + ":" + std::string(theNodeAttribute);
175   bool result = false;
176   std::map<std::string, std::string>::iterator anEntry = myParentAttributes.find(aKey);
177   if( anEntry != myParentAttributes.end()) {
178     myParentAttributes.erase(anEntry);
179     result = true;
180   }
181   return result;
182 }
183