Salome HOME
0639f8af389a48465834042e820b0e8917f9a518
[modules/shaper.git] / src / Config / Config_FeatureReader.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_Keywords.h>
21 #include <Config_Common.h>
22 #include <Config_FeatureMessage.h>
23 #include <Config_AttributeMessage.h>
24 #include <Config_FeatureReader.h>
25 #include <Config_Translator.h>
26 #include <Events_Message.h>
27 #include <Events_Loop.h>
28
29 #include <libxml/parser.h>
30 #include <libxml/tree.h>
31 #include <libxml/xmlstring.h>
32
33 #include <string>
34 #include <algorithm>
35 #include <list>
36
37 #ifdef _DEBUG
38 #include <iostream>
39 #endif
40
41 Config_FeatureReader::Config_FeatureReader(const std::string& theXmlFile,
42                                            const std::string& theLibraryName,
43                                            const std::string& theDocSection,
44                                            const char* theEventGenerated)
45     : Config_XMLReader(theXmlFile),
46       myLibraryName(theLibraryName),
47       myLibraryDocSection(theDocSection),
48       myEventGenerated(theEventGenerated ? theEventGenerated : Config_FeatureMessage::GUI_EVENT()),
49       myIsProcessWidgets(theEventGenerated != NULL)
50 {
51 }
52
53 Config_FeatureReader::~Config_FeatureReader()
54 {
55 }
56
57 std::list<std::string> Config_FeatureReader::features() const
58 {
59   return myFeatures;
60 }
61
62 void Config_FeatureReader::processNode(xmlNodePtr theNode)
63 {
64   Events_ID aMenuItemEvent = Events_Loop::eventByName(myEventGenerated);
65   if (isNode(theNode, NODE_FEATURE, NULL)) {
66     storeAttribute(theNode, _ID);
67     std::shared_ptr<Config_FeatureMessage>
68       aMessage(new Config_FeatureMessage(aMenuItemEvent, this));
69     fillFeature(theNode, aMessage);
70     myFeatures.push_back(getProperty(theNode, _ID));
71     //If a feature has xml definition for it's widget:
72     aMessage->setUseInput(hasChild(theNode));
73     Events_Loop::loop()->send(aMessage);
74     //The m_last* variables always defined before fillFeature() call. XML is a tree.
75   } else if (isNode(theNode, NODE_WORKBENCH, NODE_GROUP, NULL)) {
76     storeAttribute(theNode, _ID);
77     storeAttribute(theNode, GROUP_TOOLBAR);
78     storeAttribute(theNode, WORKBENCH_DOC, true);
79   } else if (myIsProcessWidgets) {
80     // widgets, like shape_selector or containers, like toolbox
81     if (isAttributeNode(theNode)) {
82       std::shared_ptr<Config_AttributeMessage>
83         aMessage(new Config_AttributeMessage(aMenuItemEvent, this));
84       aMessage->setFeatureId(restoreAttribute(NODE_FEATURE, _ID));
85       std::string anAttributeID = getProperty(theNode, _ID);
86       if (!anAttributeID.empty()) {
87         aMessage->setAttributeId(anAttributeID);
88         aMessage->setObligatory(getBooleanAttribute(theNode, ATTR_OBLIGATORY, true));
89         bool isConcealment = getBooleanAttribute(theNode, ATTR_CONCEALMENT, false);
90         aMessage->setConcealment(isConcealment);
91         bool isMainArg = isConcealment && getBooleanAttribute(theNode, ATTR_MAIN_ARG, false);
92         aMessage->setMainArgument(isMainArg);
93         aMessage->setGeometricalSelection(getBooleanAttribute(theNode,
94                                                               ATTR_GEOMETRICAL_SELECTION,
95                                                               false));
96
97         std::list<std::pair<std::string, std::string> > aCases;
98         xmlNodePtr aCaseNode = hasParentRecursive(theNode,
99           WDG_SWITCH_CASE, WDG_TOOLBOX_BOX, WDG_OPTIONALBOX, WDG_RADIOBOX_ITEM, NULL);
100         while(aCaseNode) {
101           std::string aCaseNodeID = getProperty(aCaseNode, _ID);
102           std::string aSwitchNodeID = "";
103           const xmlChar* aName = aCaseNode->name;
104           xmlNodePtr aSwitchNode = 0;
105           if (!xmlStrcmp(aName, (const xmlChar *) WDG_SWITCH_CASE)) {
106             aSwitchNode = hasParentRecursive(aCaseNode, WDG_SWITCH, NULL);
107           }
108           else if (!xmlStrcmp(aName, (const xmlChar *) WDG_TOOLBOX_BOX)) {
109             aSwitchNode = hasParentRecursive(aCaseNode, WDG_TOOLBOX, NULL);
110           }
111           else if (!xmlStrcmp(aName, (const xmlChar *)WDG_RADIOBOX_ITEM)) {
112             aSwitchNode = hasParentRecursive(aCaseNode, WDG_RADIOBOX, NULL);
113           }
114           if (!xmlStrcmp(aName, (const xmlChar *) WDG_OPTIONALBOX)) {
115             /// the box is optional, attribute is in case
116             /// if the optional attribute value is not empty
117             aSwitchNode = aCaseNode;
118           }
119           if (aSwitchNode)
120             aSwitchNodeID = getProperty(aSwitchNode, _ID);
121
122           aCases.push_back(std::make_pair(aSwitchNodeID, aCaseNodeID));
123           aCaseNode = hasParentRecursive(aSwitchNode, WDG_SWITCH_CASE,
124             WDG_TOOLBOX_BOX, WDG_OPTIONALBOX, WDG_RADIOBOX_ITEM, NULL);
125         }
126         aMessage->setCases(aCases);
127         Events_Loop::loop()->send(aMessage);
128       }
129     // container pages, like "case" or "box"
130     } else if (isNode(theNode, WDG_OPTIONALBOX, WDG_SWITCH, WDG_SWITCH_CASE,
131                       WDG_TOOLBOX, WDG_TOOLBOX_BOX,
132                       WDG_RADIOBOX, WDG_RADIOBOX_ITEM, NULL)) {
133       storeAttribute(theNode, _ID); // save case:caseId (or box:boxId)
134     }
135   }
136   //Process SOURCE nodes.
137   Config_XMLReader::processNode(theNode);
138 }
139
140 void Config_FeatureReader::cleanup(xmlNodePtr theNode)
141 {
142   if (isNode(theNode, WDG_OPTIONALBOX, WDG_SWITCH, WDG_SWITCH_CASE,
143     WDG_TOOLBOX, WDG_TOOLBOX_BOX, WDG_RADIOBOX_ITEM, WDG_RADIOBOX, NULL)) {
144     // cleanup id of cases when leave case node
145     cleanupAttribute(theNode, _ID);
146   }
147 }
148
149 bool Config_FeatureReader::processChildren(xmlNodePtr theNode)
150 {
151   bool result = isNode(theNode, NODE_WORKBENCH, NODE_GROUP, NULL);
152   if(!result && myIsProcessWidgets) {
153     result = isNode(theNode, NODE_FEATURE,
154                              WDG_GROUP, WDG_OPTIONALBOX,
155                              WDG_TOOLBOX, WDG_TOOLBOX_BOX,
156                              WDG_RADIOBOX, WDG_RADIOBOX_ITEM,
157                              WDG_SWITCH, WDG_SWITCH_CASE, NULL);
158   }
159   return result;
160 }
161
162 void Config_FeatureReader::fillFeature(xmlNodePtr theFeatureNode,
163   const std::shared_ptr<Config_FeatureMessage>& outFeatureMessage)
164 {
165   std::string anId = getProperty(theFeatureNode, _ID);
166   outFeatureMessage->setId(anId);
167   outFeatureMessage->setPluginLibrary(myLibraryName);
168   outFeatureMessage->setNestedFeatures(getProperty(theFeatureNode, FEATURE_NESTED));
169   outFeatureMessage->setActionsWhenNested(getNormalizedProperty(theFeatureNode,
170                                           FEATURE_WHEN_NESTED));
171   outFeatureMessage->setModal(getBooleanAttribute(theFeatureNode, FEATURE_MODAL, false));
172   bool isAutoPreview = getBooleanAttribute(theFeatureNode, FEATURE_AUTO_PREVIEW, true);
173   outFeatureMessage->setAutoPreview(isAutoPreview);
174   outFeatureMessage->setApplyContinue(
175     getBooleanAttribute(theFeatureNode, FEATURE_APPLY_CONTINUE, false));
176
177   bool isInternal = getBooleanAttribute(theFeatureNode, ATTR_INTERNAL, false);
178   outFeatureMessage->setInternal(isInternal);
179
180   std::string aHelpFile = getProperty(theFeatureNode, HELP_FILE);
181   if (!aHelpFile.empty()) {
182     aHelpFile =
183       (myLibraryDocSection.empty() ? myLibraryName : myLibraryDocSection) + "/" + aHelpFile;
184     outFeatureMessage->setHelpFileName(aHelpFile);
185   }
186
187   bool isHideFaces = getBooleanAttribute(theFeatureNode, HIDEFACES_PANEL, false);
188   outFeatureMessage->setHideFacesPanel(isHideFaces);
189
190   bool isConfirmAbort = getBooleanAttribute(theFeatureNode, ABORT_CONFIRMATION, true);
191   outFeatureMessage->setAbortConfirmation(isConfirmAbort);
192
193   //if (isInternal) {
194   //  //Internal feature has no visual representation.
195   //  return;
196   //}
197
198   std::string aText = Config_Translator::translate(anId, getProperty(theFeatureNode, FEATURE_TEXT));
199   outFeatureMessage->setText(aText);
200   bool isTitleInToolbar = getBooleanAttribute(theFeatureNode, FEATURE_TITLE_IN_TOOLBAR, true);
201   outFeatureMessage->setTitleInToolbar(isTitleInToolbar);
202   std::string aToolTip = Config_Translator::translate(
203     anId, getProperty(theFeatureNode, FEATURE_TOOLTIP));
204   outFeatureMessage->setTooltip(aToolTip);
205   outFeatureMessage->setIcon(getProperty(theFeatureNode, FEATURE_ICON));
206   outFeatureMessage->setKeysequence(getProperty(theFeatureNode, FEATURE_KEYSEQUENCE));
207
208   std::string aGroupName = restoreAttribute(NODE_GROUP, _ID);
209   std::string aWBNName = restoreAttribute(NODE_WORKBENCH, _ID);
210   std::string isGroupToolbarId = restoreAttribute(NODE_GROUP, GROUP_TOOLBAR);
211   bool isGroupToolbar = false;
212   if (isGroupToolbarId.length() > 0)
213     isGroupToolbar = (isGroupToolbarId == "yes");
214   outFeatureMessage->setGroupId(aGroupName);
215   outFeatureMessage->setWorkbenchId(aWBNName);
216   outFeatureMessage->setToolBarId(isGroupToolbar ? aGroupName : aWBNName);
217
218   // Get document kind of a feature, if empty set workbench's kind (might be empty too)
219   std::string aDocKind = getProperty(theFeatureNode, FEATURE_DOC);
220   if(aDocKind.empty()) {
221     aDocKind = restoreAttribute(NODE_WORKBENCH, WORKBENCH_DOC);
222   }
223   outFeatureMessage->setDocumentKind(aDocKind);
224 }