1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D
4 * Config_FeatureReader.cpp
6 * Created on: Mar 20, 2014
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>
19 #include <libxml/parser.h>
20 #include <libxml/tree.h>
21 #include <libxml/xmlstring.h>
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)
41 Config_FeatureReader::~Config_FeatureReader()
45 std::list<std::string> Config_FeatureReader::features() const
50 void Config_FeatureReader::processNode(xmlNodePtr theNode)
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));
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);
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);
89 else if (!xmlStrcmp(aName, (const xmlChar *) WDG_TOOLBOX_BOX)) {
90 aSwitchNode = hasParentRecursive(aCaseNode, WDG_TOOLBOX, NULL);
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;
98 aSwitchNodeID = getProperty(aSwitchNode, _ID);
100 aCases.push_back(std::make_pair(aSwitchNodeID, aCaseNodeID));
101 aCaseNode = hasParentRecursive(aSwitchNode, WDG_SWITCH_CASE,
102 WDG_TOOLBOX_BOX, WDG_OPTIONALBOX, NULL);
104 aMessage->setCases(aCases);
105 Events_Loop::loop()->send(aMessage);
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)
113 //Process SOURCE nodes.
114 Config_XMLReader::processNode(theNode);
117 void Config_FeatureReader::cleanup(xmlNodePtr theNode)
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);
126 bool Config_FeatureReader::processChildren(xmlNodePtr theNode)
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);
138 void Config_FeatureReader::fillFeature(xmlNodePtr theFeatureNode,
139 const std::shared_ptr<Config_FeatureMessage>& outFeatureMessage)
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 bool isAutoPreview = getBooleanAttribute(theFeatureNode, FEATURE_AUTO_PREVIEW, true);
149 outFeatureMessage->setAutoPreview(isAutoPreview);
151 bool isInternal = getBooleanAttribute(theFeatureNode, ATTR_INTERNAL, false);
152 outFeatureMessage->setInternal(isInternal);
154 //Internal feature has no visual representation.
158 std::string aText = Config_Translator::translate(anId, getProperty(theFeatureNode, FEATURE_TEXT));
159 outFeatureMessage->setText(aText);
160 std::string aToolTip = Config_Translator::translate(anId,
161 getProperty(theFeatureNode, FEATURE_TOOLTIP));
162 outFeatureMessage->setTooltip(aToolTip);
163 outFeatureMessage->setIcon(getProperty(theFeatureNode, FEATURE_ICON));
164 outFeatureMessage->setKeysequence(getProperty(theFeatureNode, FEATURE_KEYSEQUENCE));
165 outFeatureMessage->setGroupId(restoreAttribute(NODE_GROUP, _ID));
166 outFeatureMessage->setWorkbenchId(restoreAttribute(NODE_WORKBENCH, _ID));
167 // Get document kind of a feature, if empty set workbench's kind (might be empty too)
168 std::string aDocKind = getProperty(theFeatureNode, FEATURE_DOC);
169 if(aDocKind.empty()) {
170 aDocKind = restoreAttribute(NODE_WORKBENCH, WORKBENCH_DOC);
172 outFeatureMessage->setDocumentKind(aDocKind);