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 QWidget* aPanel = createPanel(thePage->pageWidget());
77 thePage->addWidget(aPanel);
81 if (!myWidgetApi->toChildWidget())
84 do { //Iterate over each node
85 std::string aWdgType = myWidgetApi->widgetType();
86 // Create PageGroup TODO: extract
87 if (myWidgetApi->isGroupBoxWidget() ||
88 ModuleBase_WidgetCreatorFactory::get()->hasPageWidget(aWdgType)) {
90 //if current widget is groupbox (container) process it's children recursively
91 ModuleBase_PageBase* aPage = createPageByType(aWdgType, thePage->pageWidget());
94 thePage->addPageWidget(aPage);
96 // Create a ModelWidget
97 ModuleBase_ModelWidget* aWidget = createWidgetByType(aWdgType, thePage->pageWidget());
99 if (!myWidgetApi->getBooleanAttribute(ATTR_INTERNAL, false)) {
100 thePage->addModelWidget(aWidget);
102 aWidget->setVisible(false);
105 // Create PagedContainer TODO: extract
106 if (myWidgetApi->isPagedWidget()) {
107 //If current widget is toolbox or switch-casebox then fetch all
108 //it's pages recursively and setup into the widget.
109 myWidgetApi->toChildWidget();
111 QString aPageName = qs(myWidgetApi->getProperty(CONTAINER_PAGE_NAME));
112 QString aCaseId = qs(myWidgetApi->getProperty(_ID));
113 ModuleBase_PageBase* aPage = new ModuleBase_PageWidget(aWidget);
115 if (aWdgType == WDG_SWITCH || aWdgType == WDG_TOOLBOX) {
116 ModuleBase_PagedContainer* aContainer = qobject_cast<ModuleBase_PagedContainer*>(aWidget);
118 QString anIconPath = qs( myWidgetApi->getProperty( CONTAINER_PAGE_ICON ) );
119 QPixmap anIcon( anIconPath );
120 aContainer->addPage( aPage, aPageName, aCaseId, anIcon );
122 } while (myWidgetApi->toNextWidget());
125 } while (myWidgetApi->toNextWidget());
127 thePage->alignToTop();
130 void ModuleBase_WidgetFactory::createWidget(ModuleBase_PageBase* thePage,
131 const std::string& theWidgetId)
134 moveToWidgetId(theWidgetId, aFound);
136 std::string aWdgType = myWidgetApi->widgetType();
138 // Create a ModelWidget
139 ModuleBase_ModelWidget* aWidget = createWidgetByType(aWdgType, thePage->pageWidget());
141 if (!myWidgetApi->getBooleanAttribute(ATTR_INTERNAL, false)) {
142 thePage->addModelWidget(aWidget);
145 aWidget->setVisible(false);
149 thePage->alignToTop();
152 void ModuleBase_WidgetFactory::getAttributeTitle(const std::string& theAttributeId,
153 std::string& theTitle)
156 moveToWidgetId(theAttributeId, aFound);
158 theTitle = QString::fromStdString(myWidgetApi->widgetLabel()).toStdString().c_str();
159 if (theTitle.empty())
160 theTitle = QString::fromStdString(myWidgetApi->getProperty(CONTAINER_PAGE_NAME)).toStdString().c_str();
164 void ModuleBase_WidgetFactory::getGreedAttribute(std::string& theAttributeId)
166 if (!theAttributeId.empty())
169 if (!myWidgetApi->toChildWidget())
172 do { //Iterate over each node
173 std::string aWdgType = myWidgetApi->widgetType();
174 // Find title under PageGroup
175 if (myWidgetApi->isGroupBoxWidget() ||
176 ModuleBase_WidgetCreatorFactory::get()->hasPageWidget(aWdgType)) {
177 getGreedAttribute(theAttributeId);
181 std::string anAttributeId = myWidgetApi->widgetId();
182 if (myWidgetApi->getBooleanAttribute(ATTR_GREED, false))
183 theAttributeId = anAttributeId;
184 if (theAttributeId.empty() && myWidgetApi->isPagedWidget()) {
185 //If current widget is toolbox or switch-casebox then fetch all
186 //it's pages recursively and setup into the widget.
187 myWidgetApi->toChildWidget();
189 getGreedAttribute(theAttributeId);
190 } while (theAttributeId.empty() && myWidgetApi->toNextWidget());
193 } while (theAttributeId.empty() && myWidgetApi->toNextWidget());
196 void ModuleBase_WidgetFactory::moveToWidgetId(const std::string& theWidgetId, bool& theFound)
201 if (!myWidgetApi->toChildWidget())
204 do { //Iterate over each node
205 std::string aWdgType = myWidgetApi->widgetType();
206 // Find title under PageGroup
207 if (myWidgetApi->isGroupBoxWidget() ||
208 ModuleBase_WidgetCreatorFactory::get()->hasPageWidget(aWdgType)) {
209 moveToWidgetId(theWidgetId, theFound);
213 std::string anAttributeId = myWidgetApi->widgetId();
214 theFound = anAttributeId == theWidgetId;
215 if (!theFound && myWidgetApi->isPagedWidget()) {
216 //If current widget is toolbox or switch-casebox then fetch all
217 //it's pages recursively and setup into the widget.
218 myWidgetApi->toChildWidget();
220 moveToWidgetId(theWidgetId, theFound);
221 } while (!theFound && myWidgetApi->toNextWidget());
224 } while (!theFound && myWidgetApi->toNextWidget());
227 QWidget* ModuleBase_WidgetFactory::createPanel(QWidget* theParent)
230 std::string aPanelName = myWidgetApi->getProperty(PROPERTY_PANEL_ID);
231 if (!aPanelName.empty() && ModuleBase_WidgetCreatorFactory::get()->hasPanelWidget(aPanelName))
232 aPanel = ModuleBase_WidgetCreatorFactory::get()->createPanel(aPanelName, theParent);
236 ModuleBase_PageBase* ModuleBase_WidgetFactory::createPageByType(const std::string& theType,
239 ModuleBase_PageBase* aResult = NULL;
241 if (theType == WDG_GROUP) {
242 QString aGroupName = qs(myWidgetApi->getProperty(CONTAINER_PAGE_NAME));
243 ModuleBase_PageGroupBox* aPage = new ModuleBase_PageGroupBox(theParent);
244 aPage->setTitle(aGroupName);
247 else if (theType == WDG_CHECK_GROUP) {
248 QString aGroupName = qs(myWidgetApi->getProperty(CONTAINER_PAGE_NAME));
249 ModuleBase_WidgetCheckGroupBox* aPage = new ModuleBase_WidgetCheckGroupBox(theParent,
251 aPage->setTitle(aGroupName);
255 aResult = ModuleBase_WidgetCreatorFactory::get()->createPageByType(theType, theParent,
258 ModuleBase_ModelWidget* aWidget = dynamic_cast<ModuleBase_ModelWidget*>(aResult);
260 myModelWidgets.append(aWidget);
265 ModuleBase_ModelWidget* ModuleBase_WidgetFactory::createWidgetByType(const std::string& theType,
268 ModuleBase_ModelWidget* result = NULL;
270 if (theType == WDG_INFO) {
271 result = new ModuleBase_WidgetLabel(theParent, myWidgetApi);
272 } else if (theType == WDG_ERRORINFO) {
273 result = new ModuleBase_WidgetErrorLabel(theParent, myWidgetApi);
274 } else if (theType == WDG_DOUBLEVALUE) {
275 result = new ModuleBase_WidgetDoubleValue(theParent, myWidgetApi);
276 } else if (theType == WDG_INTEGERVALUE) {
277 result = new ModuleBase_WidgetIntValue(theParent, myWidgetApi);
278 } else if (theType == WDG_SHAPE_SELECTOR) {
279 result = new ModuleBase_WidgetShapeSelector(theParent, myWorkshop, myWidgetApi);
280 } else if (theType == WDG_BOOLVALUE) {
281 result = new ModuleBase_WidgetBoolValue(theParent, myWidgetApi);
282 //} else if (theType == WDG_DOUBLEVALUE_EDITOR) {
283 // result = new ModuleBase_WidgetEditor(theParent, myWidgetApi);
284 } else if (theType == WDG_FILE_SELECTOR) {
285 result = new ModuleBase_WidgetFileSelector(theParent, myWidgetApi);
286 } else if (theType == WDG_CHOICE) {
287 result = new ModuleBase_WidgetChoice(theParent, myWidgetApi);
288 } else if (theType == WDG_STRINGVALUE) {
289 std::string aPlaceHolder = myWidgetApi->getProperty( WDG_PLACE_HOLDER );
290 result = new ModuleBase_WidgetLineEdit( theParent, myWidgetApi, aPlaceHolder );
291 } else if (theType == WDG_EXPR_EDITOR) {
292 std::string aPlaceHolder = myWidgetApi->getProperty( WDG_PLACE_HOLDER );
293 result = new ModuleBase_WidgetExprEditor( theParent, myWidgetApi, aPlaceHolder );
294 } else if (theType == WDG_MULTISELECTOR) {
295 result = new ModuleBase_WidgetMultiSelector(theParent, myWorkshop, myWidgetApi);
296 } else if (theType == WDG_TOOLBOX) {
297 result = new ModuleBase_WidgetToolbox(theParent, myWidgetApi);
298 } else if (theType == WDG_SWITCH) {
299 result = new ModuleBase_WidgetSwitch(theParent, myWidgetApi);
300 } else if (theType == WDG_TOOLBOX_BOX || theType == WDG_SWITCH_CASE ||
301 theType == NODE_VALIDATOR) {
302 // Do nothing for "box" and "case"
305 result = myWorkshop->module()->createWidgetByType(theType, theParent, myWidgetApi);
307 result = ModuleBase_WidgetCreatorFactory::get()->createWidgetByType(theType, theParent,
308 myWidgetApi, myWorkshop);
311 qDebug("ModuleBase_WidgetFactory::fillWidget: find bad widget type %s", theType.c_str());
316 myModelWidgets.append(result);
320 QString ModuleBase_WidgetFactory::qs(const std::string& theStdString)
322 return QString::fromStdString(theStdString);