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