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