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