Salome HOME
Merge branch 'master' of newgeom:newgeom.git
[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     boost::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     boost::shared_ptr<Config_AttributeMessage> aMessage(new Config_AttributeMessage(aMenuItemEvent, this));
64     aMessage->setFeatureId(restoreAttribute(NODE_FEATURE, _ID));
65     aMessage->setAttributeId(getProperty(theNode, _ID));
66     aMessage->setObligatory(getBooleanAttribute(theNode, ATTRIBUTE_OBLIGATORY, true));
67     aMessage->setConcealment(getBooleanAttribute(theNode, ATTRIBUTE_CONCEALMENT, false));
68     Events_Loop::loop()->send(aMessage);
69   }
70   //Process SOURCE, VALIDATOR nodes.
71   Config_XMLReader::processNode(theNode);
72 }
73
74 bool Config_FeatureReader::processChildren(xmlNodePtr theNode)
75 {
76   bool result = isNode(theNode, NODE_WORKBENCH, NODE_GROUP, NULL);
77   if(!result && myIsProcessWidgets) {
78     result = isNode(theNode, NODE_FEATURE, NULL);
79   }
80   return result;
81 }
82
83 void Config_FeatureReader::fillFeature(xmlNodePtr theNode, 
84   const boost::shared_ptr<Config_FeatureMessage>& outFeatureMessage)
85 {
86   outFeatureMessage->setId(getProperty(theNode, _ID));
87   outFeatureMessage->setPluginLibrary(myLibraryName);
88   outFeatureMessage->setNestedFeatures(getProperty(theNode, FEATURE_NESTED));
89
90   bool isInternal = getBooleanAttribute(theNode, ATTRIBUTE_INTERNAL, false);
91   outFeatureMessage->setInternal(isInternal);
92   if (isInternal) {
93     //Internal feature has no visual representation.
94     return;
95   }
96   outFeatureMessage->setText(getProperty(theNode, FEATURE_TEXT));
97   outFeatureMessage->setTooltip(getProperty(theNode, FEATURE_TOOLTIP));
98   outFeatureMessage->setIcon(getProperty(theNode, FEATURE_ICON));
99   outFeatureMessage->setKeysequence(getProperty(theNode, FEATURE_KEYSEQUENCE));
100   outFeatureMessage->setGroupId(restoreAttribute(NODE_GROUP, _ID));
101   outFeatureMessage->setWorkbenchId(restoreAttribute(NODE_WORKBENCH, _ID));
102   outFeatureMessage->setDocumentKind(restoreAttribute(NODE_WORKBENCH, WORKBENCH_DOC));
103 }
104
105 void Config_FeatureReader::storeAttribute(xmlNodePtr theNode,
106                                           const char* theNodeAttribute)
107 {
108   std::string aKey = getNodeName(theNode) + ":" + std::string(theNodeAttribute);
109   std::string aValue = getProperty(theNode, theNodeAttribute);
110   if(!aValue.empty()) {
111     myParentAttributes[aKey] = aValue;
112   }
113 }
114
115 std::string Config_FeatureReader::restoreAttribute(xmlNodePtr theNode,
116                                                    const char* theNodeAttribute)
117 {
118   return restoreAttribute(getNodeName(theNode).c_str(), theNodeAttribute);
119 }
120 std::string Config_FeatureReader::restoreAttribute(const char* theNodeName,
121                                                    const char* theNodeAttribute)
122 {
123   std::string aKey = std::string(theNodeName) + ":" + std::string(theNodeAttribute);
124   std::string result = "";
125   if(myParentAttributes.find(aKey) != myParentAttributes.end()) {
126     result = myParentAttributes[aKey];
127   }
128   return result;
129 }