Salome HOME
updated copyright message
[modules/shaper.git] / src / Config / Config_WidgetAPI.cpp
1 // Copyright (C) 2014-2023  CEA, EDF
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, const std::string theAttributePrefix)
30 {
31   myDoc = xmlParseDoc(BAD_CAST theRawXml.c_str());
32   myCurrentNode = xmlDocGetRootElement(myDoc);
33   myFeatureId = getProperty(_ID);
34   myAttributePrefix = theAttributePrefix;
35 }
36
37 Config_WidgetAPI::~Config_WidgetAPI()
38 {
39   xmlFreeDoc(myDoc);
40 }
41
42 bool Config_WidgetAPI::toNextWidget()
43 {
44   //Skip all non-element node, stop if next node is null
45   xmlNodePtr aNextNode = myCurrentNode;
46   do {
47     aNextNode = aNextNode->next;
48   } while (aNextNode && !isElementNode(aNextNode));
49
50   if (!aNextNode) {
51     toParentWidget();
52     return false;
53   }
54   myCurrentNode = aNextNode;
55   return true;
56 }
57
58 bool Config_WidgetAPI::toChildWidget()
59 {
60   if (myCurrentNode && hasChild(myCurrentNode)) {
61     xmlNodePtr aChildNode = myCurrentNode->children;
62     // it is possible that among child nodes, there is no an element node, so
63     // we should not change the current node until not-zero node is found
64     // otherwise, it may happens that the current node is null and the node tree information
65     // is lost
66     while (aChildNode && !isElementNode(aChildNode)) {
67       aChildNode = aChildNode->next;
68     }
69     if (aChildNode != NULL) {
70       myCurrentNode = aChildNode;
71       return true;
72     }
73   }
74   return false;
75 }
76
77 bool Config_WidgetAPI::toParentWidget()
78 {
79   if (myCurrentNode) {
80     myCurrentNode = myCurrentNode->parent;
81   }
82   return myCurrentNode != NULL;
83 }
84
85 std::string Config_WidgetAPI::widgetType() const
86 {
87   std::string result = "";
88   if (myCurrentNode) {
89     result = std::string((char *) myCurrentNode->name);
90   }
91   return result;
92 }
93
94 bool Config_WidgetAPI::isGroupBoxWidget() const
95 {
96   return isNode(myCurrentNode, WDG_GROUP, WDG_OPTIONALBOX,
97                 NULL);
98 }
99
100 bool Config_WidgetAPI::isPagedWidget() const
101 {
102   return isNode(myCurrentNode, WDG_TOOLBOX, WDG_SWITCH, WDG_RADIOBOX,
103                 NULL);
104 }
105
106 std::string Config_WidgetAPI::getProperty(const char* thePropName) const
107 {
108   return ::getProperty(myCurrentNode, thePropName);
109 }
110
111 bool Config_WidgetAPI::getBooleanAttribute(const char* theAttributeName, bool theDefault) const
112 {
113   return ::getBooleanAttribute(myCurrentNode, theAttributeName, theDefault);
114 }
115
116 std::string Config_WidgetAPI::featureId() const
117 {
118   return myFeatureId;
119 }
120
121 std::string Config_WidgetAPI::widgetId() const
122 {
123   return myAttributePrefix + getProperty(_ID);
124 }
125
126 std::string Config_WidgetAPI::widgetIcon() const
127 {
128   return getProperty(ATTR_ICON);
129 }
130
131 std::string Config_WidgetAPI::widgetLabel() const
132 {
133   return getProperty(ATTR_LABEL);
134 }
135
136 std::string Config_WidgetAPI::widgetTooltip() const
137 {
138   return getProperty(ATTR_TOOLTIP);
139 }