Salome HOME
Merge branch 'master' of newgeom:newgeom
[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_FeatureReader.h>
12 #include <Events_Message.h>
13 #include <Events_Loop.h>
14
15 #include <libxml/parser.h>
16 #include <libxml/tree.h>
17 #include <libxml/xmlstring.h>
18
19 #include <string>
20 #include <algorithm>
21
22 #ifdef _DEBUG
23 #include <iostream>
24 #endif
25
26
27 Config_FeatureReader::Config_FeatureReader(const std::string& theXmlFile,
28                                            const std::string& theLibraryName,
29                                            const char* theEventGenerated)
30     : Config_XMLReader(theXmlFile),
31       myLibraryName(theLibraryName),
32       myEventGenerated(theEventGenerated ? theEventGenerated : EVENT_FEATURE_LOADED)
33 {
34 }
35
36 Config_FeatureReader::~Config_FeatureReader()
37 {
38 }
39
40 std::list<std::string> Config_FeatureReader::features() const
41 {
42   return myFeatures;
43 }
44
45 void Config_FeatureReader::processNode(xmlNodePtr theNode)
46 {
47   Events_ID aMenuItemEvent = Events_Loop::eventByName(myEventGenerated);
48   if (isNode(theNode, NODE_FEATURE, NULL)) {
49     Events_Loop* aEvLoop = Events_Loop::loop();
50     Config_FeatureMessage aMessage(aMenuItemEvent, this);
51     fillFeature(theNode, aMessage);
52     myFeatures.push_back(getProperty(theNode, _ID));
53     //If a feature has xml definition for it's widget:
54     aMessage.setUseInput(hasChild(theNode));
55     aEvLoop->send(aMessage);
56   }
57   //The m_last* variables always defined before fillFeature() call. XML is a tree.
58   if (isNode(theNode, NODE_GROUP, NULL)) {
59     myLastGroup = getProperty(theNode, _ID);
60   }
61   if (isNode(theNode, NODE_WORKBENCH, NULL)) {
62     myLastWorkbench = getProperty(theNode, _ID);
63   }
64   //Process SOURCE nodes.
65   Config_XMLReader::processNode(theNode);
66 }
67
68 bool Config_FeatureReader::processChildren(xmlNodePtr theNode)
69 {
70   return isNode(theNode, NODE_WORKBENCH, NODE_GROUP, NULL);
71 }
72
73 void Config_FeatureReader::fillFeature(xmlNodePtr theRoot, Config_FeatureMessage& outFtMessage)
74 {
75   outFtMessage.setId(getProperty(theRoot, _ID));
76   outFtMessage.setPluginLibrary(myLibraryName);
77   outFtMessage.setNestedFeatures(getProperty(theRoot, FEATURE_NESTED));
78   bool isFtInternal = isInternalFeature(theRoot);
79   outFtMessage.setInternal(isFtInternal);
80   if(isFtInternal) {
81     //Internal feature has no visual representation.
82     return;
83   }
84   outFtMessage.setText(getProperty(theRoot, FEATURE_TEXT));
85   outFtMessage.setTooltip(getProperty(theRoot, FEATURE_TOOLTIP));
86   outFtMessage.setIcon(getProperty(theRoot, FEATURE_ICON));
87   outFtMessage.setKeysequence(getProperty(theRoot, FEATURE_KEYSEQUENCE));
88   outFtMessage.setGroupId(myLastGroup);
89   outFtMessage.setWorkbenchId(myLastWorkbench);
90 }
91
92 bool Config_FeatureReader::isInternalFeature(xmlNodePtr theRoot)
93 {
94   std::string prop = getProperty(theRoot, FEATURE_INTERNAL);
95   std::transform(prop.begin(), prop.end(), prop.begin(), ::tolower);
96   if(prop.empty() || prop == "false" || prop == "0") {
97     return false;
98   }
99   return true;
100 }