1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D
4 * ModuleBase_WidgetFactory.cpp
6 * Created on: Apr 3, 2014
10 #include <ModuleBase_WidgetFactory.h>
12 #include <ModuleBase_Operation.h>
13 #include <ModuleBase_OperationDescription.h>
14 #include <ModuleBase_WidgetEditor.h>
15 #include <ModuleBase_WidgetSwitch.h>
16 #include <ModuleBase_WidgetShapeSelector.h>
17 #include <ModuleBase_WidgetDoubleValue.h>
18 #include <ModuleBase_WidgetIntValue.h>
19 #include <ModuleBase_WidgetBoolValue.h>
20 #include <ModuleBase_WidgetFileSelector.h>
21 #include <ModuleBase_WidgetChoice.h>
22 #include <ModuleBase_IWorkshop.h>
23 #include <ModuleBase_IModule.h>
24 #include <ModuleBase_Tools.h>
25 #include <ModuleBase_WidgetLineEdit.h>
26 #include <ModuleBase_WidgetMultiSelector.h>
27 #include <ModuleBase_WidgetLabel.h>
28 #include <ModuleBase_WidgetErrorLabel.h>
29 #include <ModuleBase_WidgetToolbox.h>
30 #include <ModuleBase_PageBase.h>
31 #include <ModuleBase_PageGroupBox.h>
32 #include <ModuleBase_WidgetCheckGroupBox.h>
33 #include <ModuleBase_PageWidget.h>
34 #include <ModuleBase_WidgetExprEditor.h>
35 #include <ModuleBase_WidgetCreatorFactory.h>
37 #include <ModelAPI_Validator.h>
38 #include <ModelAPI_Session.h>
40 #include <Config_Keywords.h>
41 #include <Config_WidgetAPI.h>
44 #include <QHBoxLayout>
45 #include <QGridLayout>
47 #include <QMetaProperty>
60 ModuleBase_WidgetFactory::ModuleBase_WidgetFactory(const std::string& theXmlRepresentation,
61 ModuleBase_IWorkshop* theWorkshop)
62 : myWorkshop(theWorkshop)
64 myWidgetApi = new Config_WidgetAPI(theXmlRepresentation);
67 ModuleBase_WidgetFactory::~ModuleBase_WidgetFactory()
72 void ModuleBase_WidgetFactory::createWidget(ModuleBase_PageBase* thePage)
74 std::string aWType = myWidgetApi->widgetType();
75 if (aWType == NODE_FEATURE) {
76 // if XML definition of the feature contains the next key, the widgets should not be created,
77 // but a specific panel should be made. However, to provide persistent of the panel values,
78 // we need to get into the panel the feature of the operation. As a result this panel should
79 // be created after the feature creating(create operation). The method setPanel() of this
80 // class is used for this. Here, we just return to avoid the widgets creation.
81 std::string aPanelName = myWidgetApi->getProperty(PROPERTY_PANEL_ID);
82 if (!aPanelName.empty())
86 if (!myWidgetApi->toChildWidget())
89 do { //Iterate over each node
90 std::string aWdgType = myWidgetApi->widgetType();
91 // Create PageGroup TODO: extract
92 if (myWidgetApi->isGroupBoxWidget() ||
93 ModuleBase_WidgetCreatorFactory::get()->hasPageWidget(aWdgType)) {
95 //if current widget is groupbox (container) process it's children recursively
96 ModuleBase_PageBase* aPage = createPageByType(aWdgType, thePage->pageWidget());
99 thePage->addPageWidget(aPage);
101 // Create a ModelWidget
102 ModuleBase_ModelWidget* aWidget = createWidgetByType(aWdgType, thePage->pageWidget());
104 if (!myWidgetApi->getBooleanAttribute(ATTR_INTERNAL, false)) {
105 thePage->addModelWidget(aWidget);
107 aWidget->setVisible(false);
110 // Create PagedContainer TODO: extract
111 if (myWidgetApi->isPagedWidget()) {
112 //If current widget is toolbox or switch-casebox then fetch all
113 //it's pages recursively and setup into the widget.
114 myWidgetApi->toChildWidget();
116 QString aPageName = qs(myWidgetApi->getProperty(CONTAINER_PAGE_NAME));
117 QString aCaseId = qs(myWidgetApi->getProperty(_ID));
118 ModuleBase_PageBase* aPage = new ModuleBase_PageWidget(aWidget);
120 if (aWdgType == WDG_SWITCH || aWdgType == WDG_TOOLBOX) {
121 ModuleBase_PagedContainer* aContainer = qobject_cast<ModuleBase_PagedContainer*>(aWidget);
123 QString anIconPath = qs( myWidgetApi->getProperty( CONTAINER_PAGE_ICON ) );
124 QPixmap anIcon( anIconPath );
125 aContainer->addPage( aPage, aPageName, aCaseId, anIcon );
127 } while (myWidgetApi->toNextWidget());
130 } while (myWidgetApi->toNextWidget());
132 thePage->alignToTop();
135 void ModuleBase_WidgetFactory::createPanel(ModuleBase_PageBase* thePage,
136 const FeaturePtr& theFeature)
138 std::string aPanelName = myWidgetApi->getProperty(PROPERTY_PANEL_ID);
139 if (!aPanelName.empty() && ModuleBase_WidgetCreatorFactory::get()->hasPanelWidget(aPanelName)) {
140 QWidget* aPanel = ModuleBase_WidgetCreatorFactory::get()->createPanelByType(aPanelName,
141 thePage->pageWidget(), theFeature);
142 thePage->addWidget(aPanel);
143 thePage->alignToTop();
147 void ModuleBase_WidgetFactory::createWidget(ModuleBase_PageBase* thePage,
148 const std::string& theWidgetId)
151 moveToWidgetId(theWidgetId, aFound);
153 std::string aWdgType = myWidgetApi->widgetType();
155 // Create a ModelWidget
156 ModuleBase_ModelWidget* aWidget = createWidgetByType(aWdgType, thePage->pageWidget());
158 if (!myWidgetApi->getBooleanAttribute(ATTR_INTERNAL, false)) {
159 thePage->addModelWidget(aWidget);
162 aWidget->setVisible(false);
166 thePage->alignToTop();
169 void ModuleBase_WidgetFactory::getAttributeTitle(const std::string& theAttributeId,
170 std::string& theTitle)
173 moveToWidgetId(theAttributeId, aFound);
175 theTitle = QString::fromStdString(myWidgetApi->widgetLabel()).toStdString().c_str();
176 if (theTitle.empty())
177 theTitle = QString::fromStdString(myWidgetApi->getProperty(CONTAINER_PAGE_NAME)).toStdString().c_str();
181 void ModuleBase_WidgetFactory::getGreedAttribute(std::string& theAttributeId)
183 if (!theAttributeId.empty())
186 if (!myWidgetApi->toChildWidget())
189 do { //Iterate over each node
190 std::string aWdgType = myWidgetApi->widgetType();
191 // Find title under PageGroup
192 if (myWidgetApi->isGroupBoxWidget() ||
193 ModuleBase_WidgetCreatorFactory::get()->hasPageWidget(aWdgType)) {
194 getGreedAttribute(theAttributeId);
198 std::string anAttributeId = myWidgetApi->widgetId();
199 if (myWidgetApi->getBooleanAttribute(ATTR_GREED, false))
200 theAttributeId = anAttributeId;
201 if (theAttributeId.empty() && myWidgetApi->isPagedWidget()) {
202 //If current widget is toolbox or switch-casebox then fetch all
203 //it's pages recursively and setup into the widget.
204 myWidgetApi->toChildWidget();
206 getGreedAttribute(theAttributeId);
207 } while (theAttributeId.empty() && myWidgetApi->toNextWidget());
210 } while (theAttributeId.empty() && myWidgetApi->toNextWidget());
213 void ModuleBase_WidgetFactory::moveToWidgetId(const std::string& theWidgetId, bool& theFound)
218 if (!myWidgetApi->toChildWidget())
221 do { //Iterate over each node
222 std::string aWdgType = myWidgetApi->widgetType();
223 // Find title under PageGroup
224 if (myWidgetApi->isGroupBoxWidget() ||
225 ModuleBase_WidgetCreatorFactory::get()->hasPageWidget(aWdgType)) {
226 moveToWidgetId(theWidgetId, theFound);
230 std::string anAttributeId = myWidgetApi->widgetId();
231 theFound = anAttributeId == theWidgetId;
232 if (!theFound && myWidgetApi->isPagedWidget()) {
233 //If current widget is toolbox or switch-casebox then fetch all
234 //it's pages recursively and setup into the widget.
235 myWidgetApi->toChildWidget();
237 moveToWidgetId(theWidgetId, theFound);
238 } while (!theFound && myWidgetApi->toNextWidget());
241 } while (!theFound && myWidgetApi->toNextWidget());
244 ModuleBase_PageBase* ModuleBase_WidgetFactory::createPageByType(const std::string& theType,
247 ModuleBase_PageBase* aResult = NULL;
249 if (theType == WDG_GROUP) {
250 QString aGroupName = qs(myWidgetApi->getProperty(CONTAINER_PAGE_NAME));
251 ModuleBase_PageGroupBox* aPage = new ModuleBase_PageGroupBox(theParent);
252 aPage->setTitle(aGroupName);
255 else if (theType == WDG_CHECK_GROUP) {
256 QString aGroupName = qs(myWidgetApi->getProperty(CONTAINER_PAGE_NAME));
257 ModuleBase_WidgetCheckGroupBox* aPage = new ModuleBase_WidgetCheckGroupBox(theParent,
259 aPage->setTitle(aGroupName);
263 aResult = ModuleBase_WidgetCreatorFactory::get()->createPageByType(theType, theParent,
266 ModuleBase_ModelWidget* aWidget = dynamic_cast<ModuleBase_ModelWidget*>(aResult);
268 myModelWidgets.append(aWidget);
273 ModuleBase_ModelWidget* ModuleBase_WidgetFactory::createWidgetByType(const std::string& theType,
276 ModuleBase_ModelWidget* result = NULL;
278 if (theType == WDG_INFO) {
279 result = new ModuleBase_WidgetLabel(theParent, myWidgetApi);
280 } else if (theType == WDG_ERRORINFO) {
281 result = new ModuleBase_WidgetErrorLabel(theParent, myWidgetApi);
282 } else if (theType == WDG_DOUBLEVALUE) {
283 result = new ModuleBase_WidgetDoubleValue(theParent, myWidgetApi);
284 } else if (theType == WDG_INTEGERVALUE) {
285 result = new ModuleBase_WidgetIntValue(theParent, myWidgetApi);
286 } else if (theType == WDG_SHAPE_SELECTOR) {
287 result = new ModuleBase_WidgetShapeSelector(theParent, myWorkshop, myWidgetApi);
288 } else if (theType == WDG_BOOLVALUE) {
289 result = new ModuleBase_WidgetBoolValue(theParent, myWidgetApi);
290 //} else if (theType == WDG_DOUBLEVALUE_EDITOR) {
291 // result = new ModuleBase_WidgetEditor(theParent, myWidgetApi);
292 } else if (theType == WDG_FILE_SELECTOR) {
293 result = new ModuleBase_WidgetFileSelector(theParent, myWidgetApi);
294 } else if (theType == WDG_CHOICE) {
295 result = new ModuleBase_WidgetChoice(theParent, myWidgetApi);
296 } else if (theType == WDG_STRINGVALUE) {
297 std::string aPlaceHolder = myWidgetApi->getProperty( WDG_PLACE_HOLDER );
298 result = new ModuleBase_WidgetLineEdit( theParent, myWidgetApi, aPlaceHolder );
299 } else if (theType == WDG_EXPR_EDITOR) {
300 std::string aPlaceHolder = myWidgetApi->getProperty( WDG_PLACE_HOLDER );
301 result = new ModuleBase_WidgetExprEditor( theParent, myWidgetApi, aPlaceHolder );
302 } else if (theType == WDG_MULTISELECTOR) {
303 result = new ModuleBase_WidgetMultiSelector(theParent, myWorkshop, myWidgetApi);
304 } else if (theType == WDG_TOOLBOX) {
305 result = new ModuleBase_WidgetToolbox(theParent, myWidgetApi);
306 } else if (theType == WDG_SWITCH) {
307 result = new ModuleBase_WidgetSwitch(theParent, myWidgetApi);
308 } else if (theType == WDG_TOOLBOX_BOX || theType == WDG_SWITCH_CASE ||
309 theType == NODE_VALIDATOR) {
310 // Do nothing for "box" and "case"
313 result = myWorkshop->module()->createWidgetByType(theType, theParent, myWidgetApi);
315 result = ModuleBase_WidgetCreatorFactory::get()->createWidgetByType(theType, theParent,
316 myWidgetApi, myWorkshop);
319 qDebug("ModuleBase_WidgetFactory::fillWidget: find bad widget type %s", theType.c_str());
324 myModelWidgets.append(result);
328 QString ModuleBase_WidgetFactory::qs(const std::string& theStdString)
330 return QString::fromStdString(theStdString);