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