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 && isWidgetNode(theNode)) {
65     std::shared_ptr<Config_AttributeMessage> aMessage(new Config_AttributeMessage(aMenuItemEvent, this));
66     aMessage->setFeatureId(restoreAttribute(NODE_FEATURE, _ID));
67     std::string anAttributeID = getProperty(theNode, _ID);
68     if (!anAttributeID.empty()) {
69       aMessage->setAttributeId(anAttributeID);
70       aMessage->setObligatory(getBooleanAttribute(theNode, ATTR_OBLIGATORY, true));
71       aMessage->setConcealment(getBooleanAttribute(theNode, ATTR_CONCEALMENT, false));
72       //aMessage->setCaseId
73       Events_Loop::loop()->send(aMessage);
74     }
75   }
76   //Process SOURCE, VALIDATOR nodes.
77   Config_XMLReader::processNode(theNode);
78 }
79
80 bool Config_FeatureReader::processChildren(xmlNodePtr theNode)
81 {
82   bool result = isNode(theNode, NODE_WORKBENCH, NODE_GROUP, NULL);
83   if(!result && myIsProcessWidgets) {
84     result = isNode(theNode, NODE_FEATURE, NULL);
85   }
86   return result;
87 }
88
89 void Config_FeatureReader::fillFeature(xmlNodePtr theFeatureNode,
90   const std::shared_ptr<Config_FeatureMessage>& outFeatureMessage)
91 {
92   outFeatureMessage->setId(getProperty(theFeatureNode, _ID));
93   outFeatureMessage->setPluginLibrary(myLibraryName);
94   outFeatureMessage->setNestedFeatures(getProperty(theFeatureNode, FEATURE_NESTED));
95   outFeatureMessage->setActionsWhenNested(getNormalizedProperty(theFeatureNode, FEATURE_WHEN_NESTED));
96
97   bool isInternal = getBooleanAttribute(theFeatureNode, ATTR_INTERNAL, false);
98   outFeatureMessage->setInternal(isInternal);
99   if (isInternal) {
100     //Internal feature has no visual representation.
101     return;
102   }
103   outFeatureMessage->setText(getProperty(theFeatureNode, FEATURE_TEXT));
104   outFeatureMessage->setTooltip(getProperty(theFeatureNode, FEATURE_TOOLTIP));
105   outFeatureMessage->setIcon(getProperty(theFeatureNode, FEATURE_ICON));
106   outFeatureMessage->setKeysequence(getProperty(theFeatureNode, FEATURE_KEYSEQUENCE));
107   outFeatureMessage->setGroupId(restoreAttribute(NODE_GROUP, _ID));
108   outFeatureMessage->setWorkbenchId(restoreAttribute(NODE_WORKBENCH, _ID));
109   // Get document kind of a feature, if empty set workbench's kind (might be empty too)
110   std::string aDocKind = getProperty(theFeatureNode, FEATURE_DOC);
111   if(aDocKind.empty()) {
112     aDocKind = restoreAttribute(NODE_WORKBENCH, WORKBENCH_DOC);
113   }
114   outFeatureMessage->setDocumentKind(aDocKind);
115 }
116
117 void Config_FeatureReader::storeAttribute(xmlNodePtr theNode,
118                                           const char* theNodeAttribute)
119 {
120   std::string aKey = getNodeName(theNode) + ":" + std::string(theNodeAttribute);
121   std::string aValue = getProperty(theNode, theNodeAttribute);
122   if(!aValue.empty()) {
123     myParentAttributes[aKey] = aValue;
124   }
125 }
126
127 std::string Config_FeatureReader::restoreAttribute(xmlNodePtr theNode,
128                                                    const char* theNodeAttribute)
129 {
130   return restoreAttribute(getNodeName(theNode).c_str(), theNodeAttribute);
131 }
132 std::string Config_FeatureReader::restoreAttribute(const char* theNodeName,
133                                                    const char* theNodeAttribute)
134 {
135   std::string aKey = std::string(theNodeName) + ":" + std::string(theNodeAttribute);
136   std::string result = "";
137   if(myParentAttributes.find(aKey) != myParentAttributes.end()) {
138     result = myParentAttributes[aKey];
139   }
140   return result;
141 }