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