Salome HOME
#1659 New widget for supporting optional inputs
[modules/shaper.git] / src / ModuleBase / ModuleBase_WidgetFactory.cpp
1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D
2
3 /*
4  * ModuleBase_WidgetFactory.cpp
5  *
6  *  Created on: Apr 3, 2014
7  *      Author: sbh
8  */
9
10 #include <ModuleBase_WidgetFactory.h>
11 #include <ModuleBase_IconFactory.h>
12
13 #include <ModuleBase_Operation.h>
14 #include <ModuleBase_OperationDescription.h>
15 #include <ModuleBase_WidgetEditor.h>
16 #include <ModuleBase_WidgetSwitch.h>
17 #include <ModuleBase_WidgetShapeSelector.h>
18 #include <ModuleBase_WidgetDoubleValue.h>
19 #include <ModuleBase_WidgetIntValue.h>
20 #include <ModuleBase_WidgetBoolValue.h>
21 #include <ModuleBase_WidgetFileSelector.h>
22 #include <ModuleBase_WidgetChoice.h>
23 #include <ModuleBase_IWorkshop.h>
24 #include <ModuleBase_IModule.h>
25 #include <ModuleBase_Tools.h>
26 #include <ModuleBase_WidgetLineEdit.h>
27 #include <ModuleBase_WidgetMultiSelector.h>
28 #include <ModuleBase_WidgetLabel.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>
36 #include <ModuleBase_WidgetAction.h>
37
38 #include <ModelAPI_Validator.h>
39 #include <ModelAPI_Session.h>
40
41 #include <Config_Keywords.h>
42 #include <Config_WidgetAPI.h>
43
44 #include <QWidget>
45 #include <QHBoxLayout>
46 #include <QGridLayout>
47 #include <QSpinBox>
48 #include <QMetaProperty>
49 #include <QLabel>
50 #include <QPixmap>
51 #include <QGroupBox>
52 #include <QToolBox>
53
54 #ifdef _DEBUG
55 #include <QDebug>
56 #endif
57
58 #include <cfloat>
59 #include <climits>
60
61 ModuleBase_WidgetFactory::ModuleBase_WidgetFactory(const std::string& theXmlRepresentation,
62                                                    ModuleBase_IWorkshop* theWorkshop)
63     : myWorkshop(theWorkshop)
64 {
65   myWidgetApi = new Config_WidgetAPI(theXmlRepresentation);
66 }
67
68 ModuleBase_WidgetFactory::~ModuleBase_WidgetFactory()
69 {
70   delete myWidgetApi;
71 }
72
73 void ModuleBase_WidgetFactory::createWidget(ModuleBase_PageBase* thePage, bool alignToTop)
74 {
75   std::string aWType = myWidgetApi->widgetType();
76   if (aWType == NODE_FEATURE) {
77     // if XML definition of the feature contains the next key, the widgets should not be created,
78     // but a specific panel should be made. However, to provide persistent of the panel values,
79     // we need to get into the panel the feature of the operation. As a result this panel should
80     // be created after the feature creating(create operation). The method setPanel() of this
81     // class is used for this. Here, we just return to avoid the widgets creation.
82     std::string aPanelName = myWidgetApi->getProperty(PROPERTY_PANEL_ID);
83     if (!aPanelName.empty())
84       return;
85   }
86
87   if (!myWidgetApi->toChildWidget())
88     return;
89
90   do {  //Iterate over each node
91     std::string aWdgType = myWidgetApi->widgetType();
92     // Create PageGroup TODO: extract
93     if (myWidgetApi->isGroupBoxWidget() ||
94         ModuleBase_WidgetCreatorFactory::get()->hasPageWidget(aWdgType)) {
95
96       //if current widget is groupbox (container) process it's children recursively
97       ModuleBase_PageBase* aPage = createPageByType(aWdgType, thePage->pageWidget());
98
99       createWidget(aPage);
100       thePage->addPageWidget(aPage);
101     } else {
102       // Create a ModelWidget
103       ModuleBase_ModelWidget* aWidget = createWidgetByType(aWdgType, thePage->pageWidget());
104       if (aWidget) {
105         if (!myWidgetApi->getBooleanAttribute(ATTR_INTERNAL, false)) {
106           thePage->addModelWidget(aWidget);
107         } else {
108           aWidget->setVisible(false);
109         }
110       }
111       // Create PagedContainer TODO: extract
112       if (myWidgetApi->isPagedWidget()) {
113         //If current widget is toolbox or switch-casebox then fetch all
114         //it's pages recursively and setup into the widget.
115         if (myWidgetApi->toChildWidget()) {
116           do {
117             QString aPageName = qs(myWidgetApi->getProperty(CONTAINER_PAGE_NAME));
118             QString aCaseId = qs(myWidgetApi->getProperty(_ID));
119             ModuleBase_PageBase* aPage = new ModuleBase_PageWidget(aWidget);
120             createWidget(aPage);
121             if (aWdgType == WDG_SWITCH || aWdgType == WDG_TOOLBOX) {
122               ModuleBase_PagedContainer* aContainer = qobject_cast<ModuleBase_PagedContainer*>(aWidget);
123
124               QString anIconPath = qs( myWidgetApi->getProperty( CONTAINER_PAGE_ICON ) );
125               QPixmap anIcon = ModuleBase_IconFactory::loadPixmap( anIconPath );
126               aContainer->addPage( aPage, aPageName, aCaseId, anIcon );
127             }
128           } while (myWidgetApi->toNextWidget());
129         }
130       }
131     }
132   } while (myWidgetApi->toNextWidget());
133
134   if (alignToTop)
135     thePage->alignToTop();
136 }
137
138 void ModuleBase_WidgetFactory::createPanel(ModuleBase_PageBase* thePage,
139                                            const FeaturePtr& theFeature)
140 {
141   std::string aPanelName = myWidgetApi->getProperty(PROPERTY_PANEL_ID);
142   if (!aPanelName.empty() && ModuleBase_WidgetCreatorFactory::get()->hasPanelWidget(aPanelName)) {
143     QWidget* aPanel = ModuleBase_WidgetCreatorFactory::get()->createPanelByType(aPanelName,
144                                                                thePage->pageWidget(), theFeature);
145     thePage->addWidget(aPanel);
146     thePage->alignToTop();
147   }
148 }
149
150 void ModuleBase_WidgetFactory::createWidget(ModuleBase_PageBase* thePage,
151                                             const std::string& theWidgetId)
152 {
153   bool aFound = false;
154   moveToWidgetId(theWidgetId, aFound);
155   if (aFound) {
156     std::string aWdgType = myWidgetApi->widgetType();
157
158     // Create a ModelWidget
159     ModuleBase_ModelWidget* aWidget = createWidgetByType(aWdgType, thePage->pageWidget());
160     if (aWidget) {
161       if (!myWidgetApi->getBooleanAttribute(ATTR_INTERNAL, false)) {
162         thePage->addModelWidget(aWidget);
163       }
164       else {
165         aWidget->setVisible(false);
166       }
167     }
168   }
169   thePage->alignToTop();
170 }
171
172 void ModuleBase_WidgetFactory::getAttributeTitle(const std::string& theAttributeId,
173                                                  std::string& theTitle)
174 {
175   bool aFound = false;
176   moveToWidgetId(theAttributeId, aFound);
177   if (aFound) {
178     theTitle = QString::fromStdString(myWidgetApi->widgetLabel()).toStdString().c_str();
179     if (theTitle.empty())
180       theTitle = QString::fromStdString(myWidgetApi->getProperty(CONTAINER_PAGE_NAME)).toStdString().c_str();
181   }
182 }
183
184 void ModuleBase_WidgetFactory::getGreedAttribute(std::string& theAttributeId)
185 {
186   if (!theAttributeId.empty())
187     return;
188
189   if (!myWidgetApi->toChildWidget())
190     return;
191
192   do {  //Iterate over each node
193     std::string aWdgType = myWidgetApi->widgetType();
194     // Find title under PageGroup
195     if (myWidgetApi->isGroupBoxWidget() ||
196       ModuleBase_WidgetCreatorFactory::get()->hasPageWidget(aWdgType)) {
197       getGreedAttribute(theAttributeId);
198     }
199     else {
200       // Find title here
201       std::string anAttributeId = myWidgetApi->widgetId();
202       if (myWidgetApi->getBooleanAttribute(ATTR_GREED, false))
203         theAttributeId = anAttributeId;
204       if (theAttributeId.empty() && myWidgetApi->isPagedWidget()) {
205         //If current widget is toolbox or switch-casebox then fetch all
206         //it's pages recursively and setup into the widget.
207         if (myWidgetApi->toChildWidget()) {
208           do {
209             getGreedAttribute(theAttributeId);
210           } while (theAttributeId.empty() && myWidgetApi->toNextWidget());
211         }
212       }
213     }
214   } while (theAttributeId.empty() && myWidgetApi->toNextWidget());
215 }
216
217 void ModuleBase_WidgetFactory::moveToWidgetId(const std::string& theWidgetId, bool& theFound)
218 {
219   if (theFound)
220     return;
221
222   if (!myWidgetApi->toChildWidget())
223     return;
224
225   do {  //Iterate over each node
226     std::string aWdgType = myWidgetApi->widgetType();
227     // Find title under PageGroup
228     if (myWidgetApi->isGroupBoxWidget() ||
229       ModuleBase_WidgetCreatorFactory::get()->hasPageWidget(aWdgType)) {
230       moveToWidgetId(theWidgetId, theFound);
231     }
232     else {
233       // Find title here
234       std::string anAttributeId = myWidgetApi->widgetId();
235       theFound = anAttributeId == theWidgetId;
236       if (!theFound && myWidgetApi->isPagedWidget()) {
237         //If current widget is toolbox or switch-casebox then fetch all
238         //it's pages recursively and setup into the widget.
239         if (myWidgetApi->toChildWidget()) {
240           do {
241             moveToWidgetId(theWidgetId, theFound);
242           } while (!theFound && myWidgetApi->toNextWidget());
243         }
244       }
245     }
246   } while (!theFound && myWidgetApi->toNextWidget());
247 }
248
249 ModuleBase_PageBase* ModuleBase_WidgetFactory::createPageByType(const std::string& theType,
250                                                                 QWidget* theParent)
251 {
252   ModuleBase_PageBase* aResult = NULL;
253
254   if (theType == WDG_GROUP) {
255     QString aGroupName = qs(myWidgetApi->getProperty(CONTAINER_PAGE_NAME));
256     ModuleBase_PageGroupBox* aPage = new ModuleBase_PageGroupBox(theParent);
257     aPage->setTitle(aGroupName);
258     aResult = aPage;
259   }
260   else if (theType == WDG_CHECK_GROUP) {
261     ModuleBase_WidgetCheckGroupBox* aPage = new ModuleBase_WidgetCheckGroupBox(theParent,
262                                                                 myWidgetApi);
263     aResult = aPage;
264   }
265   if (!aResult)
266     aResult = ModuleBase_WidgetCreatorFactory::get()->createPageByType(theType, theParent,
267                                                                        myWidgetApi);
268
269   ModuleBase_ModelWidget* aWidget = dynamic_cast<ModuleBase_ModelWidget*>(aResult);
270   if (aWidget)
271     myModelWidgets.append(aWidget);
272
273   return aResult;
274 }
275
276 ModuleBase_ModelWidget* ModuleBase_WidgetFactory::createWidgetByType(const std::string& theType,
277                                                                      QWidget* theParent)
278 {
279   ModuleBase_ModelWidget* result = NULL;
280
281   if (theType == WDG_INFO) {
282     result = new ModuleBase_WidgetLabel(theParent, myWidgetApi);
283   } else if (theType == WDG_DOUBLEVALUE) {
284     result = new ModuleBase_WidgetDoubleValue(theParent, myWidgetApi);
285   } else if (theType == WDG_INTEGERVALUE) {
286     result = new ModuleBase_WidgetIntValue(theParent, myWidgetApi);
287   } else if (theType == WDG_SHAPE_SELECTOR) {
288     result = new ModuleBase_WidgetShapeSelector(theParent, myWorkshop, myWidgetApi);
289   } else if (theType == WDG_BOOLVALUE) {
290     result = new ModuleBase_WidgetBoolValue(theParent, myWidgetApi);
291   //} else if (theType == WDG_DOUBLEVALUE_EDITOR) {
292   //  result = new ModuleBase_WidgetEditor(theParent, myWidgetApi);
293   } else if (theType == WDG_FILE_SELECTOR) {
294     result = new ModuleBase_WidgetFileSelector(theParent, myWidgetApi);
295   } else if (theType == WDG_CHOICE) {
296     result = new ModuleBase_WidgetChoice(theParent, myWidgetApi);
297   } else if (theType == WDG_STRINGVALUE) {
298     std::string aPlaceHolder = myWidgetApi->getProperty( WDG_PLACE_HOLDER );
299     result = new ModuleBase_WidgetLineEdit( theParent, myWidgetApi, aPlaceHolder );
300   } else if (theType == WDG_EXPR_EDITOR) {
301     std::string aPlaceHolder = myWidgetApi->getProperty( WDG_PLACE_HOLDER );
302     result = new ModuleBase_WidgetExprEditor( theParent, myWidgetApi, aPlaceHolder );
303   } else if (theType == WDG_MULTISELECTOR) {
304     result = new ModuleBase_WidgetMultiSelector(theParent, myWorkshop, myWidgetApi);
305   } else if (theType == WDG_TOOLBOX) {
306     result = new ModuleBase_WidgetToolbox(theParent, myWidgetApi);
307   } else if (theType == WDG_SWITCH) {
308     result = new ModuleBase_WidgetSwitch(theParent, myWidgetApi);
309   } else if (theType == WDG_TOOLBOX_BOX || theType == WDG_SWITCH_CASE ||
310              theType == NODE_VALIDATOR) {
311     // Do nothing for "box" and "case"
312     result = NULL;
313   } else if (theType == WDG_ACTION) {
314     result = new ModuleBase_WidgetAction(theParent, myWidgetApi);
315   } else {
316     result = myWorkshop->module()->createWidgetByType(theType, theParent, myWidgetApi);
317     if (!result)
318       result = ModuleBase_WidgetCreatorFactory::get()->createWidgetByType(theType, theParent,
319                                                               myWidgetApi, myWorkshop);
320     #ifdef _DEBUG
321     if (!result) {
322       qDebug("ModuleBase_WidgetFactory::fillWidget: find bad widget type %s", theType.c_str());
323     }
324     #endif
325   }
326   if (result)
327     myModelWidgets.append(result);
328   return result;
329 }
330
331 QString ModuleBase_WidgetFactory::qs(const std::string& theStdString)
332 {
333   return QString::fromStdString(theStdString);
334 }
335