Salome HOME
Issue #1649: Added options to create plane by coincident point;
[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> aMessage(new Config_FeatureMessage(aMenuItemEvent, this));
55     fillFeature(theNode, aMessage);
56     myFeatures.push_back(getProperty(theNode, _ID));
57     //If a feature has xml definition for it's widget:
58     aMessage->setUseInput(hasChild(theNode));
59     Events_Loop::loop()->send(aMessage);
60     //The m_last* variables always defined before fillFeature() call. XML is a tree.
61   } else if (isNode(theNode, NODE_WORKBENCH, NODE_GROUP, NULL)) {
62     storeAttribute(theNode, _ID);
63     storeAttribute(theNode, WORKBENCH_DOC, true);
64   } else if (myIsProcessWidgets) {
65     // widgets, like shape_selector or containers, like toolbox
66     if (isAttributeNode(theNode)) {
67       std::shared_ptr<Config_AttributeMessage> aMessage(new Config_AttributeMessage(aMenuItemEvent, this));
68       aMessage->setFeatureId(restoreAttribute(NODE_FEATURE, _ID));
69       std::string anAttributeID = getProperty(theNode, _ID);
70       if (!anAttributeID.empty()) {
71         aMessage->setAttributeId(anAttributeID);
72         aMessage->setObligatory(getBooleanAttribute(theNode, ATTR_OBLIGATORY, true));
73         aMessage->setConcealment(getBooleanAttribute(theNode, ATTR_CONCEALMENT, false));
74
75         std::list<std::pair<std::string, std::string> > aCases;
76         xmlNodePtr aCaseNode = hasParentRecursive(theNode, WDG_SWITCH_CASE, WDG_TOOLBOX_BOX, WDG_OPTIONALBOX, NULL);
77         while(aCaseNode) {
78           std::string aCaseNodeID = getProperty(aCaseNode, _ID);
79           std::string aSwitchNodeID = "";
80           const xmlChar* aName = aCaseNode->name;
81           xmlNodePtr aSwitchNode;
82           if (!xmlStrcmp(aName, (const xmlChar *) WDG_SWITCH_CASE)) {
83             aSwitchNode = hasParentRecursive(aCaseNode, WDG_SWITCH, NULL);
84           }
85           else if (!xmlStrcmp(aName, (const xmlChar *) WDG_TOOLBOX_BOX)) {
86             aSwitchNode = hasParentRecursive(aCaseNode, WDG_TOOLBOX, NULL);
87           }
88           if (!xmlStrcmp(aName, (const xmlChar *) WDG_OPTIONALBOX)) {
89             /// the box is optional, attribute is in case if the optional attribute value is not empty
90             aSwitchNode = aCaseNode;
91           }
92           if (aSwitchNode)
93             aSwitchNodeID = getProperty(aSwitchNode, _ID);
94
95           aCases.push_back(std::make_pair(aSwitchNodeID, aCaseNodeID));
96           aCaseNode = hasParentRecursive(aSwitchNode, WDG_SWITCH_CASE, WDG_TOOLBOX_BOX, WDG_OPTIONALBOX, NULL);
97         }
98         aMessage->setCases(aCases);
99         Events_Loop::loop()->send(aMessage);
100       }
101     // container pages, like "case" or "box"
102     } else if (isNode(theNode, WDG_OPTIONALBOX, WDG_SWITCH, WDG_SWITCH_CASE,
103                       WDG_TOOLBOX, WDG_TOOLBOX_BOX, NULL)) {
104       storeAttribute(theNode, _ID); // save case:caseId (or box:boxId)
105     }
106   }
107   //Process SOURCE nodes.
108   Config_XMLReader::processNode(theNode);
109 }
110
111 void Config_FeatureReader::cleanup(xmlNodePtr theNode)
112 {
113   if (isNode(theNode, WDG_OPTIONALBOX, WDG_SWITCH, WDG_SWITCH_CASE,
114              WDG_TOOLBOX, WDG_TOOLBOX_BOX, NULL)) {
115     // cleanup id of cases when leave case node
116     cleanupAttribute(theNode, _ID);
117   }
118 }
119
120 bool Config_FeatureReader::processChildren(xmlNodePtr theNode)
121 {
122   bool result = isNode(theNode, NODE_WORKBENCH, NODE_GROUP, NULL);
123   if(!result && myIsProcessWidgets) {
124     result = isNode(theNode, NODE_FEATURE, 
125                              WDG_GROUP, WDG_OPTIONALBOX,
126                              WDG_TOOLBOX, WDG_TOOLBOX_BOX,
127                              WDG_SWITCH, WDG_SWITCH_CASE, NULL);
128   }
129   return result;
130 }
131
132 void Config_FeatureReader::fillFeature(xmlNodePtr theFeatureNode,
133   const std::shared_ptr<Config_FeatureMessage>& outFeatureMessage)
134 {
135   outFeatureMessage->setId(getProperty(theFeatureNode, _ID));
136   outFeatureMessage->setPluginLibrary(myLibraryName);
137   outFeatureMessage->setNestedFeatures(getProperty(theFeatureNode, FEATURE_NESTED));
138   outFeatureMessage->setActionsWhenNested(getNormalizedProperty(theFeatureNode, FEATURE_WHEN_NESTED));
139   outFeatureMessage->setModal(getBooleanAttribute(theFeatureNode, FEATURE_MODAL, false));
140
141   bool isInternal = getBooleanAttribute(theFeatureNode, ATTR_INTERNAL, false);
142   outFeatureMessage->setInternal(isInternal);
143   if (isInternal) {
144     //Internal feature has no visual representation.
145     return;
146   }
147   
148   outFeatureMessage->setText(getProperty(theFeatureNode, FEATURE_TEXT));
149   outFeatureMessage->setTooltip(getProperty(theFeatureNode, FEATURE_TOOLTIP));
150   outFeatureMessage->setIcon(getProperty(theFeatureNode, FEATURE_ICON));
151   outFeatureMessage->setKeysequence(getProperty(theFeatureNode, FEATURE_KEYSEQUENCE));
152   outFeatureMessage->setGroupId(restoreAttribute(NODE_GROUP, _ID));
153   outFeatureMessage->setWorkbenchId(restoreAttribute(NODE_WORKBENCH, _ID));
154   // Get document kind of a feature, if empty set workbench's kind (might be empty too)
155   std::string aDocKind = getProperty(theFeatureNode, FEATURE_DOC);
156   if(aDocKind.empty()) {
157     aDocKind = restoreAttribute(NODE_WORKBENCH, WORKBENCH_DOC);
158   }
159   outFeatureMessage->setDocumentKind(aDocKind);
160   bool isAutoPreview = getBooleanAttribute(theFeatureNode, FEATURE_AUTO_PREVIEW, true);
161   outFeatureMessage->setAutoPreview(isAutoPreview);
162 }