Salome HOME
Issue #144 If an operation has valid feature after initialization by preselection...
[modules/shaper.git] / src / Config / Config_WidgetAPI.cpp
1 /*
2  * Config_WidgetAPI.cpp
3  *
4  *  Created on: Apr 1, 2014
5  *      Author: sbh
6  */
7
8 #include <Config_WidgetAPI.h>
9 #include <Config_Keywords.h>
10 #include <Config_Common.h>
11
12 #include <libxml/parser.h>
13 #include <libxml/tree.h>
14
15 #include <string>
16 #include <algorithm>
17
18 Config_WidgetAPI::Config_WidgetAPI(std::string theRawXml)
19 {
20   myDoc = xmlParseDoc(BAD_CAST theRawXml.c_str());
21   myCurrentNode = xmlDocGetRootElement(myDoc);
22 }
23
24 Config_WidgetAPI::~Config_WidgetAPI()
25 {
26   xmlFreeDoc(myDoc);
27 }
28
29 bool Config_WidgetAPI::toNextWidget()
30 {
31   //Skip all non-element node, stop if next node is null
32   xmlNodePtr aNextNode = myCurrentNode;
33   do {
34     aNextNode = aNextNode->next;
35   } while (aNextNode && !isElementNode(aNextNode));
36
37   if (!aNextNode) {
38     toParentWidget();
39     return false;
40   }
41   myCurrentNode = aNextNode;
42   return true;
43 }
44
45 bool Config_WidgetAPI::toChildWidget()
46 {
47   if (myCurrentNode && hasChild(myCurrentNode)) {
48     myCurrentNode = myCurrentNode->children;
49     while (!isElementNode(myCurrentNode)) {
50       myCurrentNode = myCurrentNode->next;
51     }
52     return true;
53   }
54   return false;
55 }
56
57 bool Config_WidgetAPI::toParentWidget()
58 {
59   if (myCurrentNode) {
60     myCurrentNode = myCurrentNode->parent;
61   }
62   return myCurrentNode != NULL;
63 }
64
65 std::string Config_WidgetAPI::widgetType() const
66 {
67   std::string result = "";
68   if (myCurrentNode) {
69     result = std::string((char *) myCurrentNode->name);
70   }
71   return result;
72 }
73
74 bool Config_WidgetAPI::isContainerWidget() const
75 {
76   return isNode(myCurrentNode, WDG_GROUP, WDG_CHECK_GROUP,
77   NULL);
78 }
79
80 bool Config_WidgetAPI::isPagedWidget() const
81 {
82   return isNode(myCurrentNode, WDG_TOOLBOX, WDG_SWITCH,
83   NULL);
84 }
85
86 std::string Config_WidgetAPI::getProperty(const char* thePropName) const
87 {
88   std::string result = "";
89   char* aPropChars = (char*) xmlGetProp(myCurrentNode, BAD_CAST thePropName);
90   if (!aPropChars || aPropChars[0] == 0)
91     return result;
92   result = std::string(aPropChars);
93   return result;
94 }
95
96 bool Config_WidgetAPI::getBooleanAttribute(const char* theAttributeName, bool theDefault) const
97 {
98   std::string prop = getProperty(theAttributeName);
99   std::transform(prop.begin(), prop.end(), prop.begin(), ::tolower);
100   bool result = theDefault;
101   if (prop == "true" || prop == "1") {
102     result = true;
103   } else if (prop == "false" || prop == "0") {
104     result = false;
105   }
106   return result;
107 }
108
109 std::string Config_WidgetAPI::widgetId() const
110 {
111   return getProperty(_ID);
112 }
113
114 std::string Config_WidgetAPI::widgetIcon() const
115 {
116   return getProperty(ANY_WDG_ICON);
117 }
118
119 std::string Config_WidgetAPI::widgetLabel() const
120 {
121   return getProperty(ANY_WDG_LABEL);
122 }
123
124 std::string Config_WidgetAPI::widgetTooltip() const
125 {
126   return getProperty(ANY_WDG_TOOLTIP);
127 }