Salome HOME
Update copyrights
[modules/shaper.git] / src / Config / Config_WidgetAPI.cpp
1 // Copyright (C) 2014-2019  CEA/DEN, EDF R&D
2 //
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License, or (at your option) any later version.
7 //
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11 // Lesser General Public License for more details.
12 //
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
16 //
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
18 //
19
20 #include <Config_WidgetAPI.h>
21 #include <Config_Keywords.h>
22 #include <Config_Common.h>
23
24 #include <libxml/parser.h>
25 #include <libxml/tree.h>
26
27 #include <string>
28
29 Config_WidgetAPI::Config_WidgetAPI(std::string theRawXml)
30 {
31   myDoc = xmlParseDoc(BAD_CAST theRawXml.c_str());
32   myCurrentNode = xmlDocGetRootElement(myDoc);
33   myFeatureId = getProperty(_ID);
34 }
35
36 Config_WidgetAPI::~Config_WidgetAPI()
37 {
38   xmlFreeDoc(myDoc);
39 }
40
41 bool Config_WidgetAPI::toNextWidget()
42 {
43   //Skip all non-element node, stop if next node is null
44   xmlNodePtr aNextNode = myCurrentNode;
45   do {
46     aNextNode = aNextNode->next;
47   } while (aNextNode && !isElementNode(aNextNode));
48
49   if (!aNextNode) {
50     toParentWidget();
51     return false;
52   }
53   myCurrentNode = aNextNode;
54   return true;
55 }
56
57 bool Config_WidgetAPI::toChildWidget()
58 {
59   if (myCurrentNode && hasChild(myCurrentNode)) {
60     xmlNodePtr aChildNode = myCurrentNode->children;
61     // it is possible that among child nodes, there is no an element node, so
62     // we should not change the current node until not-zero node is found
63     // otherwise, it may happens that the current node is null and the node tree information
64     // is lost
65     while (aChildNode && !isElementNode(aChildNode)) {
66       aChildNode = aChildNode->next;
67     }
68     if (aChildNode != NULL) {
69       myCurrentNode = aChildNode;
70       return true;
71     }
72   }
73   return false;
74 }
75
76 bool Config_WidgetAPI::toParentWidget()
77 {
78   if (myCurrentNode) {
79     myCurrentNode = myCurrentNode->parent;
80   }
81   return myCurrentNode != NULL;
82 }
83
84 std::string Config_WidgetAPI::widgetType() const
85 {
86   std::string result = "";
87   if (myCurrentNode) {
88     result = std::string((char *) myCurrentNode->name);
89   }
90   return result;
91 }
92
93 bool Config_WidgetAPI::isGroupBoxWidget() const
94 {
95   return isNode(myCurrentNode, WDG_GROUP, WDG_OPTIONALBOX,
96                 NULL);
97 }
98
99 bool Config_WidgetAPI::isPagedWidget() const
100 {
101   return isNode(myCurrentNode, WDG_TOOLBOX, WDG_SWITCH, WDG_RADIOBOX,
102                 NULL);
103 }
104
105 std::string Config_WidgetAPI::getProperty(const char* thePropName) const
106 {
107   return ::getProperty(myCurrentNode, thePropName);
108 }
109
110 bool Config_WidgetAPI::getBooleanAttribute(const char* theAttributeName, bool theDefault) const
111 {
112   return ::getBooleanAttribute(myCurrentNode, theAttributeName, theDefault);
113 }
114
115 std::string Config_WidgetAPI::featureId() const
116 {
117   return myFeatureId;
118 }
119
120 std::string Config_WidgetAPI::widgetId() const
121 {
122   return getProperty(_ID);
123 }
124
125 std::string Config_WidgetAPI::widgetIcon() const
126 {
127   return getProperty(ATTR_ICON);
128 }
129
130 std::string Config_WidgetAPI::widgetLabel() const
131 {
132   return getProperty(ATTR_LABEL);
133 }
134
135 std::string Config_WidgetAPI::widgetTooltip() const
136 {
137   return getProperty(ATTR_TOOLTIP);
138 }