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 myParentId = myWidgetApi->widgetId();
75 if (!myWidgetApi->toChildWidget())
78 do { //Iterate over each node
79 std::string aWdgType = myWidgetApi->widgetType();
80 // Create PageGroup TODO: extract
81 if (myWidgetApi->isGroupBoxWidget() ||
82 ModuleBase_WidgetCreatorFactory::get()->hasPageWidget(aWdgType)) {
84 //if current widget is groupbox (container) process it's children recursively
85 ModuleBase_PageBase* aPage = createPageByType(aWdgType, thePage->pageWidget());
88 thePage->addPageWidget(aPage);
90 // Create a ModelWidget
91 ModuleBase_ModelWidget* aWidget = createWidgetByType(aWdgType, thePage->pageWidget());
93 if (!myWidgetApi->getBooleanAttribute(ATTR_INTERNAL, false)) {
94 thePage->addModelWidget(aWidget);
96 aWidget->setVisible(false);
99 // Create PagedContainer TODO: extract
100 if (myWidgetApi->isPagedWidget()) {
101 //If current widget is toolbox or switch-casebox then fetch all
102 //it's pages recursively and setup into the widget.
103 myWidgetApi->toChildWidget();
105 QString aPageName = qs(myWidgetApi->getProperty(CONTAINER_PAGE_NAME));
106 QString aCaseId = qs(myWidgetApi->getProperty(_ID));
107 ModuleBase_PageBase* aPage = new ModuleBase_PageWidget(aWidget);
109 if (aWdgType == WDG_SWITCH || aWdgType == WDG_TOOLBOX) {
110 ModuleBase_PagedContainer* aContainer = qobject_cast<ModuleBase_PagedContainer*>(aWidget);
112 QString anIconPath = qs( myWidgetApi->getProperty( CONTAINER_PAGE_ICON ) );
113 QPixmap anIcon( anIconPath );
114 aContainer->addPage( aPage, aPageName, aCaseId, anIcon );
116 } while (myWidgetApi->toNextWidget());
119 } while (myWidgetApi->toNextWidget());
121 thePage->alignToTop();
124 void ModuleBase_WidgetFactory::createWidget(ModuleBase_PageBase* thePage,
125 const std::string& theWidgetId)
128 moveToWidgetId(theWidgetId, aFound);
130 std::string aWdgType = myWidgetApi->widgetType();
132 // Create a ModelWidget
133 ModuleBase_ModelWidget* aWidget = createWidgetByType(aWdgType, thePage->pageWidget());
135 if (!myWidgetApi->getBooleanAttribute(ATTR_INTERNAL, false)) {
136 thePage->addModelWidget(aWidget);
139 aWidget->setVisible(false);
143 thePage->alignToTop();
146 void ModuleBase_WidgetFactory::getAttributeTitle(const std::string& theAttributeId,
147 std::string& theTitle)
150 moveToWidgetId(theAttributeId, aFound);
152 theTitle = QString::fromStdString(myWidgetApi->widgetLabel()).toStdString().c_str();
153 if (theTitle.empty())
154 theTitle = QString::fromStdString(myWidgetApi->getProperty(CONTAINER_PAGE_NAME)).toStdString().c_str();
158 void ModuleBase_WidgetFactory::getGreedAttribute(std::string& theAttributeId)
160 if (!theAttributeId.empty())
163 myParentId = myWidgetApi->widgetId();
164 if (!myWidgetApi->toChildWidget())
167 do { //Iterate over each node
168 std::string aWdgType = myWidgetApi->widgetType();
169 // Find title under PageGroup
170 if (myWidgetApi->isGroupBoxWidget() ||
171 ModuleBase_WidgetCreatorFactory::get()->hasPageWidget(aWdgType)) {
172 getGreedAttribute(theAttributeId);
176 std::string anAttributeId = myWidgetApi->widgetId();
177 if (myWidgetApi->getBooleanAttribute(ATTR_GREED, false))
178 theAttributeId = anAttributeId;
179 if (theAttributeId.empty() && myWidgetApi->isPagedWidget()) {
180 //If current widget is toolbox or switch-casebox then fetch all
181 //it's pages recursively and setup into the widget.
182 myWidgetApi->toChildWidget();
184 getGreedAttribute(theAttributeId);
185 } while (theAttributeId.empty() && myWidgetApi->toNextWidget());
188 } while (theAttributeId.empty() && myWidgetApi->toNextWidget());
191 void ModuleBase_WidgetFactory::moveToWidgetId(const std::string& theWidgetId, bool& theFound)
196 myParentId = myWidgetApi->widgetId();
197 if (!myWidgetApi->toChildWidget())
200 do { //Iterate over each node
201 std::string aWdgType = myWidgetApi->widgetType();
202 // Find title under PageGroup
203 if (myWidgetApi->isGroupBoxWidget() ||
204 ModuleBase_WidgetCreatorFactory::get()->hasPageWidget(aWdgType)) {
205 moveToWidgetId(theWidgetId, theFound);
209 std::string anAttributeId = myWidgetApi->widgetId();
210 theFound = anAttributeId == theWidgetId;
211 if (!theFound && myWidgetApi->isPagedWidget()) {
212 //If current widget is toolbox or switch-casebox then fetch all
213 //it's pages recursively and setup into the widget.
214 myWidgetApi->toChildWidget();
216 moveToWidgetId(theWidgetId, theFound);
217 } while (!theFound && myWidgetApi->toNextWidget());
220 } while (!theFound && myWidgetApi->toNextWidget());
223 ModuleBase_PageBase* ModuleBase_WidgetFactory::createPageByType(const std::string& theType,
226 ModuleBase_PageBase* aResult = NULL;
228 if (theType == WDG_GROUP) {
229 QString aGroupName = qs(myWidgetApi->getProperty(CONTAINER_PAGE_NAME));
230 ModuleBase_PageGroupBox* aPage = new ModuleBase_PageGroupBox(theParent);
231 aPage->setTitle(aGroupName);
234 else if (theType == WDG_CHECK_GROUP) {
235 QString aGroupName = qs(myWidgetApi->getProperty(CONTAINER_PAGE_NAME));
236 ModuleBase_WidgetCheckGroupBox* aPage = new ModuleBase_WidgetCheckGroupBox(theParent,
237 myWidgetApi, myParentId);
238 aPage->setTitle(aGroupName);
242 aResult = ModuleBase_WidgetCreatorFactory::get()->createPageByType(theType, theParent,
243 myWidgetApi, myParentId);
245 ModuleBase_ModelWidget* aWidget = dynamic_cast<ModuleBase_ModelWidget*>(aResult);
247 myModelWidgets.append(aWidget);
252 ModuleBase_ModelWidget* ModuleBase_WidgetFactory::createWidgetByType(const std::string& theType,
255 ModuleBase_ModelWidget* result = NULL;
257 if (theType == WDG_INFO) {
258 result = new ModuleBase_WidgetLabel(theParent, myWidgetApi, myParentId);
259 } else if (theType == WDG_ERRORINFO) {
260 result = new ModuleBase_WidgetErrorLabel(theParent, myWidgetApi, myParentId);
261 } else if (theType == WDG_DOUBLEVALUE) {
262 result = new ModuleBase_WidgetDoubleValue(theParent, myWidgetApi, myParentId);
263 } else if (theType == WDG_INTEGERVALUE) {
264 result = new ModuleBase_WidgetIntValue(theParent, myWidgetApi, myParentId);
265 } else if (theType == WDG_SHAPE_SELECTOR) {
266 result = new ModuleBase_WidgetShapeSelector(theParent, myWorkshop, myWidgetApi, myParentId);
267 } else if (theType == WDG_BOOLVALUE) {
268 result = new ModuleBase_WidgetBoolValue(theParent, myWidgetApi, myParentId);
269 //} else if (theType == WDG_DOUBLEVALUE_EDITOR) {
270 // result = new ModuleBase_WidgetEditor(theParent, myWidgetApi, myParentId);
271 } else if (theType == WDG_FILE_SELECTOR) {
272 result = new ModuleBase_WidgetFileSelector(theParent, myWidgetApi, myParentId);
273 } else if (theType == WDG_CHOICE) {
274 result = new ModuleBase_WidgetChoice(theParent, myWidgetApi, myParentId);
275 } else if (theType == WDG_STRINGVALUE) {
276 std::string aPlaceHolder = myWidgetApi->getProperty( WDG_PLACE_HOLDER );
277 result = new ModuleBase_WidgetLineEdit( theParent, myWidgetApi, myParentId, aPlaceHolder );
278 } else if (theType == WDG_EXPR_EDITOR) {
279 std::string aPlaceHolder = myWidgetApi->getProperty( WDG_PLACE_HOLDER );
280 result = new ModuleBase_WidgetExprEditor( theParent, myWidgetApi, myParentId, aPlaceHolder );
281 } else if (theType == WDG_MULTISELECTOR) {
282 result = new ModuleBase_WidgetMultiSelector(theParent, myWorkshop, myWidgetApi, myParentId);
283 } else if (theType == WDG_TOOLBOX) {
284 result = new ModuleBase_WidgetToolbox(theParent, myWidgetApi, myParentId);
285 } else if (theType == WDG_SWITCH) {
286 result = new ModuleBase_WidgetSwitch(theParent, myWidgetApi, myParentId);
287 } else if (theType == WDG_TOOLBOX_BOX || theType == WDG_SWITCH_CASE ||
288 theType == NODE_VALIDATOR) {
289 // Do nothing for "box" and "case"
292 result = myWorkshop->module()->createWidgetByType(theType, theParent, myWidgetApi, myParentId);
294 result = ModuleBase_WidgetCreatorFactory::get()->createWidgetByType(theType, theParent,
295 myWidgetApi, myParentId, myWorkshop);
298 qDebug("ModuleBase_WidgetFactory::fillWidget: find bad widget type %s", theType.c_str());
303 myModelWidgets.append(result);
307 QString ModuleBase_WidgetFactory::qs(const std::string& theStdString)
309 return QString::fromStdString(theStdString);