]> SALOME platform Git repositories - modules/shaper.git/blob - src/Config/Config_FeatureReader.cpp
Salome HOME
Merge branch 'master' into cgt/devCEA
[modules/shaper.git] / src / Config / Config_FeatureReader.cpp
1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D
2
3 /*
4  * Config_FeatureReader.cpp
5  *
6  *  Created on: Mar 20, 2014
7  *      Author: sbh
8  */
9
10 #include <Config_Keywords.h>
11 #include <Config_Common.h>
12 #include <Config_FeatureMessage.h>
13 #include <Config_AttributeMessage.h>
14 #include <Config_FeatureReader.h>
15 #include <Config_Translator.h>
16 #include <Events_Message.h>
17 #include <Events_Loop.h>
18
19 #include <libxml/parser.h>
20 #include <libxml/tree.h>
21 #include <libxml/xmlstring.h>
22
23 #include <string>
24 #include <algorithm>
25 #include <list>
26
27 #ifdef _DEBUG
28 #include <iostream>
29 #endif
30
31 Config_FeatureReader::Config_FeatureReader(const std::string& theXmlFile,
32                                            const std::string& theLibraryName,
33                                            const char* theEventGenerated)
34     : Config_XMLReader(theXmlFile),
35       myLibraryName(theLibraryName),
36       myEventGenerated(theEventGenerated ? theEventGenerated : Config_FeatureMessage::GUI_EVENT()),
37       myIsProcessWidgets(theEventGenerated != NULL)
38 {
39 }
40
41 Config_FeatureReader::~Config_FeatureReader()
42 {
43 }
44
45 std::list<std::string> Config_FeatureReader::features() const
46 {
47   return myFeatures;
48 }
49
50 void Config_FeatureReader::processNode(xmlNodePtr theNode)
51 {
52   Events_ID aMenuItemEvent = Events_Loop::eventByName(myEventGenerated);
53   if (isNode(theNode, NODE_FEATURE, NULL)) {
54     storeAttribute(theNode, _ID);
55     std::shared_ptr<Config_FeatureMessage>
56       aMessage(new Config_FeatureMessage(aMenuItemEvent, this));
57     fillFeature(theNode, aMessage);
58     myFeatures.push_back(getProperty(theNode, _ID));
59     //If a feature has xml definition for it's widget:
60     aMessage->setUseInput(hasChild(theNode));
61     Events_Loop::loop()->send(aMessage);
62     //The m_last* variables always defined before fillFeature() call. XML is a tree.
63   } else if (isNode(theNode, NODE_WORKBENCH, NODE_GROUP, NULL)) {
64     storeAttribute(theNode, _ID);
65     storeAttribute(theNode, WORKBENCH_DOC, true);
66   } else if (myIsProcessWidgets) {
67     // widgets, like shape_selector or containers, like toolbox
68     if (isAttributeNode(theNode)) {
69       std::shared_ptr<Config_AttributeMessage>
70         aMessage(new Config_AttributeMessage(aMenuItemEvent, this));
71       aMessage->setFeatureId(restoreAttribute(NODE_FEATURE, _ID));
72       std::string anAttributeID = getProperty(theNode, _ID);
73       if (!anAttributeID.empty()) {
74         aMessage->setAttributeId(anAttributeID);
75         aMessage->setObligatory(getBooleanAttribute(theNode, ATTR_OBLIGATORY, true));
76         aMessage->setConcealment(getBooleanAttribute(theNode, ATTR_CONCEALMENT, false));
77
78         std::list<std::pair<std::string, std::string> > aCases;
79         xmlNodePtr aCaseNode =
80           hasParentRecursive(theNode, WDG_SWITCH_CASE, WDG_TOOLBOX_BOX, WDG_OPTIONALBOX, NULL);
81         while(aCaseNode) {
82           std::string aCaseNodeID = getProperty(aCaseNode, _ID);
83           std::string aSwitchNodeID = "";
84           const xmlChar* aName = aCaseNode->name;
85           xmlNodePtr aSwitchNode;
86           if (!xmlStrcmp(aName, (const xmlChar *) WDG_SWITCH_CASE)) {
87             aSwitchNode = hasParentRecursive(aCaseNode, WDG_SWITCH, NULL);
88           }
89           else if (!xmlStrcmp(aName, (const xmlChar *) WDG_TOOLBOX_BOX)) {
90             aSwitchNode = hasParentRecursive(aCaseNode, WDG_TOOLBOX, NULL);
91           }
92           if (!xmlStrcmp(aName, (const xmlChar *) WDG_OPTIONALBOX)) {
93             /// the box is optional, attribute is in case
94             /// if the optional attribute value is not empty
95             aSwitchNode = aCaseNode;
96           }
97           if (aSwitchNode)
98             aSwitchNodeID = getProperty(aSwitchNode, _ID);
99
100           aCases.push_back(std::make_pair(aSwitchNodeID, aCaseNodeID));
101           aCaseNode = hasParentRecursive(aSwitchNode, WDG_SWITCH_CASE,
102                                          WDG_TOOLBOX_BOX, WDG_OPTIONALBOX, NULL);
103         }
104         aMessage->setCases(aCases);
105         Events_Loop::loop()->send(aMessage);
106       }
107     // container pages, like "case" or "box"
108     } else if (isNode(theNode, WDG_OPTIONALBOX, WDG_SWITCH, WDG_SWITCH_CASE,
109                       WDG_TOOLBOX, WDG_TOOLBOX_BOX, NULL)) {
110       storeAttribute(theNode, _ID); // save case:caseId (or box:boxId)
111     }
112   }
113   //Process SOURCE nodes.
114   Config_XMLReader::processNode(theNode);
115 }
116
117 void Config_FeatureReader::cleanup(xmlNodePtr theNode)
118 {
119   if (isNode(theNode, WDG_OPTIONALBOX, WDG_SWITCH, WDG_SWITCH_CASE,
120              WDG_TOOLBOX, WDG_TOOLBOX_BOX, NULL)) {
121     // cleanup id of cases when leave case node
122     cleanupAttribute(theNode, _ID);
123   }
124 }
125
126 bool Config_FeatureReader::processChildren(xmlNodePtr theNode)
127 {
128   bool result = isNode(theNode, NODE_WORKBENCH, NODE_GROUP, NULL);
129   if(!result && myIsProcessWidgets) {
130     result = isNode(theNode, NODE_FEATURE,
131                              WDG_GROUP, WDG_OPTIONALBOX,
132                              WDG_TOOLBOX, WDG_TOOLBOX_BOX,
133                              WDG_SWITCH, WDG_SWITCH_CASE, NULL);
134   }
135   return result;
136 }
137
138 void Config_FeatureReader::fillFeature(xmlNodePtr theFeatureNode,
139   const std::shared_ptr<Config_FeatureMessage>& outFeatureMessage)
140 {
141   std::string anId = getProperty(theFeatureNode, _ID);
142   outFeatureMessage->setId(anId);
143   outFeatureMessage->setPluginLibrary(myLibraryName);
144   outFeatureMessage->setNestedFeatures(getProperty(theFeatureNode, FEATURE_NESTED));
145   outFeatureMessage->setActionsWhenNested(getNormalizedProperty(theFeatureNode,
146                                           FEATURE_WHEN_NESTED));
147   outFeatureMessage->setModal(getBooleanAttribute(theFeatureNode, FEATURE_MODAL, false));
148
149   bool isInternal = getBooleanAttribute(theFeatureNode, ATTR_INTERNAL, false);
150   outFeatureMessage->setInternal(isInternal);
151   if (isInternal) {
152     //Internal feature has no visual representation.
153     return;
154   }
155
156   std::string aText = Config_Translator::translate(anId, getProperty(theFeatureNode, FEATURE_TEXT));
157   outFeatureMessage->setText(aText);
158   std::string aToolTip = Config_Translator::translate(anId,
159                                                       getProperty(theFeatureNode, FEATURE_TOOLTIP));
160   outFeatureMessage->setTooltip(aToolTip);
161   outFeatureMessage->setIcon(getProperty(theFeatureNode, FEATURE_ICON));
162   outFeatureMessage->setKeysequence(getProperty(theFeatureNode, FEATURE_KEYSEQUENCE));
163   outFeatureMessage->setGroupId(restoreAttribute(NODE_GROUP, _ID));
164   outFeatureMessage->setWorkbenchId(restoreAttribute(NODE_WORKBENCH, _ID));
165   // Get document kind of a feature, if empty set workbench's kind (might be empty too)
166   std::string aDocKind = getProperty(theFeatureNode, FEATURE_DOC);
167   if(aDocKind.empty()) {
168     aDocKind = restoreAttribute(NODE_WORKBENCH, WORKBENCH_DOC);
169   }
170   outFeatureMessage->setDocumentKind(aDocKind);
171   bool isAutoPreview = getBooleanAttribute(theFeatureNode, FEATURE_AUTO_PREVIEW, true);
172   outFeatureMessage->setAutoPreview(isAutoPreview);
173 }