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 if (!myWidgetApi->toChildWidget())
77 do { //Iterate over each node
78 std::string aWdgType = myWidgetApi->widgetType();
79 // Create PageGroup TODO: extract
80 if (myWidgetApi->isGroupBoxWidget() ||
81 ModuleBase_WidgetCreatorFactory::get()->hasPageWidget(aWdgType)) {
83 //if current widget is groupbox (container) process it's children recursively
84 ModuleBase_PageBase* aPage = createPageByType(aWdgType, thePage->pageWidget());
87 thePage->addPageWidget(aPage);
89 // Create a ModelWidget
90 ModuleBase_ModelWidget* aWidget = createWidgetByType(aWdgType, thePage->pageWidget());
92 if (!myWidgetApi->getBooleanAttribute(ATTR_INTERNAL, false)) {
93 thePage->addModelWidget(aWidget);
95 aWidget->setVisible(false);
98 // Create PagedContainer TODO: extract
99 if (myWidgetApi->isPagedWidget()) {
100 //If current widget is toolbox or switch-casebox then fetch all
101 //it's pages recursively and setup into the widget.
102 myWidgetApi->toChildWidget();
104 QString aPageName = qs(myWidgetApi->getProperty(CONTAINER_PAGE_NAME));
105 QString aCaseId = qs(myWidgetApi->getProperty(_ID));
106 ModuleBase_PageBase* aPage = new ModuleBase_PageWidget(aWidget);
108 if (aWdgType == WDG_SWITCH || aWdgType == WDG_TOOLBOX) {
109 ModuleBase_PagedContainer* aContainer = qobject_cast<ModuleBase_PagedContainer*>(aWidget);
111 QString anIconPath = qs( myWidgetApi->getProperty( CONTAINER_PAGE_ICON ) );
112 QPixmap anIcon( anIconPath );
113 aContainer->addPage( aPage, aPageName, aCaseId, anIcon );
115 } while (myWidgetApi->toNextWidget());
118 } while (myWidgetApi->toNextWidget());
120 thePage->alignToTop();
123 void ModuleBase_WidgetFactory::createWidget(ModuleBase_PageBase* thePage,
124 const std::string& theWidgetId)
127 moveToWidgetId(theWidgetId, aFound);
129 std::string aWdgType = myWidgetApi->widgetType();
131 // Create a ModelWidget
132 ModuleBase_ModelWidget* aWidget = createWidgetByType(aWdgType, thePage->pageWidget());
134 if (!myWidgetApi->getBooleanAttribute(ATTR_INTERNAL, false)) {
135 thePage->addModelWidget(aWidget);
138 aWidget->setVisible(false);
142 thePage->alignToTop();
145 void ModuleBase_WidgetFactory::getAttributeTitle(const std::string& theAttributeId,
146 std::string& theTitle)
149 moveToWidgetId(theAttributeId, aFound);
151 theTitle = QString::fromStdString(myWidgetApi->widgetLabel()).toStdString().c_str();
152 if (theTitle.empty())
153 theTitle = QString::fromStdString(myWidgetApi->getProperty(CONTAINER_PAGE_NAME)).toStdString().c_str();
157 void ModuleBase_WidgetFactory::getGreedAttribute(std::string& theAttributeId)
159 if (!theAttributeId.empty())
162 if (!myWidgetApi->toChildWidget())
165 do { //Iterate over each node
166 std::string aWdgType = myWidgetApi->widgetType();
167 // Find title under PageGroup
168 if (myWidgetApi->isGroupBoxWidget() ||
169 ModuleBase_WidgetCreatorFactory::get()->hasPageWidget(aWdgType)) {
170 getGreedAttribute(theAttributeId);
174 std::string anAttributeId = myWidgetApi->widgetId();
175 if (myWidgetApi->getBooleanAttribute(ATTR_GREED, false))
176 theAttributeId = anAttributeId;
177 if (theAttributeId.empty() && myWidgetApi->isPagedWidget()) {
178 //If current widget is toolbox or switch-casebox then fetch all
179 //it's pages recursively and setup into the widget.
180 myWidgetApi->toChildWidget();
182 getGreedAttribute(theAttributeId);
183 } while (theAttributeId.empty() && myWidgetApi->toNextWidget());
186 } while (theAttributeId.empty() && myWidgetApi->toNextWidget());
189 void ModuleBase_WidgetFactory::moveToWidgetId(const std::string& theWidgetId, bool& theFound)
194 if (!myWidgetApi->toChildWidget())
197 do { //Iterate over each node
198 std::string aWdgType = myWidgetApi->widgetType();
199 // Find title under PageGroup
200 if (myWidgetApi->isGroupBoxWidget() ||
201 ModuleBase_WidgetCreatorFactory::get()->hasPageWidget(aWdgType)) {
202 moveToWidgetId(theWidgetId, theFound);
206 std::string anAttributeId = myWidgetApi->widgetId();
207 theFound = anAttributeId == theWidgetId;
208 if (!theFound && myWidgetApi->isPagedWidget()) {
209 //If current widget is toolbox or switch-casebox then fetch all
210 //it's pages recursively and setup into the widget.
211 myWidgetApi->toChildWidget();
213 moveToWidgetId(theWidgetId, theFound);
214 } while (!theFound && myWidgetApi->toNextWidget());
217 } while (!theFound && myWidgetApi->toNextWidget());
220 ModuleBase_PageBase* ModuleBase_WidgetFactory::createPageByType(const std::string& theType,
223 ModuleBase_PageBase* aResult = NULL;
225 if (theType == WDG_GROUP) {
226 QString aGroupName = qs(myWidgetApi->getProperty(CONTAINER_PAGE_NAME));
227 ModuleBase_PageGroupBox* aPage = new ModuleBase_PageGroupBox(theParent);
228 aPage->setTitle(aGroupName);
231 else if (theType == WDG_CHECK_GROUP) {
232 QString aGroupName = qs(myWidgetApi->getProperty(CONTAINER_PAGE_NAME));
233 ModuleBase_WidgetCheckGroupBox* aPage = new ModuleBase_WidgetCheckGroupBox(theParent,
235 aPage->setTitle(aGroupName);
239 aResult = ModuleBase_WidgetCreatorFactory::get()->createPageByType(theType, theParent,
242 ModuleBase_ModelWidget* aWidget = dynamic_cast<ModuleBase_ModelWidget*>(aResult);
244 myModelWidgets.append(aWidget);
249 ModuleBase_ModelWidget* ModuleBase_WidgetFactory::createWidgetByType(const std::string& theType,
252 ModuleBase_ModelWidget* result = NULL;
254 if (theType == WDG_INFO) {
255 result = new ModuleBase_WidgetLabel(theParent, myWidgetApi);
256 } else if (theType == WDG_ERRORINFO) {
257 result = new ModuleBase_WidgetErrorLabel(theParent, myWidgetApi);
258 } else if (theType == WDG_DOUBLEVALUE) {
259 result = new ModuleBase_WidgetDoubleValue(theParent, myWidgetApi);
260 } else if (theType == WDG_INTEGERVALUE) {
261 result = new ModuleBase_WidgetIntValue(theParent, myWidgetApi);
262 } else if (theType == WDG_SHAPE_SELECTOR) {
263 result = new ModuleBase_WidgetShapeSelector(theParent, myWorkshop, myWidgetApi);
264 } else if (theType == WDG_BOOLVALUE) {
265 result = new ModuleBase_WidgetBoolValue(theParent, myWidgetApi);
266 //} else if (theType == WDG_DOUBLEVALUE_EDITOR) {
267 // result = new ModuleBase_WidgetEditor(theParent, myWidgetApi);
268 } else if (theType == WDG_FILE_SELECTOR) {
269 result = new ModuleBase_WidgetFileSelector(theParent, myWidgetApi);
270 } else if (theType == WDG_CHOICE) {
271 result = new ModuleBase_WidgetChoice(theParent, myWidgetApi);
272 } else if (theType == WDG_STRINGVALUE) {
273 std::string aPlaceHolder = myWidgetApi->getProperty( WDG_PLACE_HOLDER );
274 result = new ModuleBase_WidgetLineEdit( theParent, myWidgetApi, aPlaceHolder );
275 } else if (theType == WDG_EXPR_EDITOR) {
276 std::string aPlaceHolder = myWidgetApi->getProperty( WDG_PLACE_HOLDER );
277 result = new ModuleBase_WidgetExprEditor( theParent, myWidgetApi, aPlaceHolder );
278 } else if (theType == WDG_MULTISELECTOR) {
279 result = new ModuleBase_WidgetMultiSelector(theParent, myWorkshop, myWidgetApi);
280 } else if (theType == WDG_TOOLBOX) {
281 result = new ModuleBase_WidgetToolbox(theParent, myWidgetApi);
282 } else if (theType == WDG_SWITCH) {
283 result = new ModuleBase_WidgetSwitch(theParent, myWidgetApi);
284 } else if (theType == WDG_TOOLBOX_BOX || theType == WDG_SWITCH_CASE ||
285 theType == NODE_VALIDATOR) {
286 // Do nothing for "box" and "case"
289 result = myWorkshop->module()->createWidgetByType(theType, theParent, myWidgetApi);
291 result = ModuleBase_WidgetCreatorFactory::get()->createWidgetByType(theType, theParent,
292 myWidgetApi, myWorkshop);
295 qDebug("ModuleBase_WidgetFactory::fillWidget: find bad widget type %s", theType.c_str());
300 myModelWidgets.append(result);
304 QString ModuleBase_WidgetFactory::qs(const std::string& theStdString)
306 return QString::fromStdString(theStdString);