Salome HOME
Action button to call customAction of a feature.
[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 #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)
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( anIconPath );
126               aContainer->addPage( aPage, aPageName, aCaseId, anIcon );
127             }
128           } while (myWidgetApi->toNextWidget());
129         }
130       }
131     }
132   } while (myWidgetApi->toNextWidget());
133
134   thePage->alignToTop();
135 }
136
137 void ModuleBase_WidgetFactory::createPanel(ModuleBase_PageBase* thePage,
138                                            const FeaturePtr& theFeature)
139 {
140   std::string aPanelName = myWidgetApi->getProperty(PROPERTY_PANEL_ID);
141   if (!aPanelName.empty() && ModuleBase_WidgetCreatorFactory::get()->hasPanelWidget(aPanelName)) {
142     QWidget* aPanel = ModuleBase_WidgetCreatorFactory::get()->createPanelByType(aPanelName,
143                                                                thePage->pageWidget(), theFeature);
144     thePage->addWidget(aPanel);
145     thePage->alignToTop();
146   }
147 }
148
149 void ModuleBase_WidgetFactory::createWidget(ModuleBase_PageBase* thePage,
150                                             const std::string& theWidgetId)
151 {
152   bool aFound = false;
153   moveToWidgetId(theWidgetId, aFound);
154   if (aFound) {
155     std::string aWdgType = myWidgetApi->widgetType();
156
157     // Create a ModelWidget
158     ModuleBase_ModelWidget* aWidget = createWidgetByType(aWdgType, thePage->pageWidget());
159     if (aWidget) {
160       if (!myWidgetApi->getBooleanAttribute(ATTR_INTERNAL, false)) {
161         thePage->addModelWidget(aWidget);
162       }
163       else {
164         aWidget->setVisible(false);
165       }
166     }
167   }
168   thePage->alignToTop();
169 }
170
171 void ModuleBase_WidgetFactory::getAttributeTitle(const std::string& theAttributeId,
172                                                  std::string& theTitle)
173 {
174   bool aFound = false;
175   moveToWidgetId(theAttributeId, aFound);
176   if (aFound) {
177     theTitle = QString::fromStdString(myWidgetApi->widgetLabel()).toStdString().c_str();
178     if (theTitle.empty())
179       theTitle = QString::fromStdString(myWidgetApi->getProperty(CONTAINER_PAGE_NAME)).toStdString().c_str();
180   }
181 }
182
183 void ModuleBase_WidgetFactory::getGreedAttribute(std::string& theAttributeId)
184 {
185   if (!theAttributeId.empty())
186     return;
187
188   if (!myWidgetApi->toChildWidget())
189     return;
190
191   do {  //Iterate over each node
192     std::string aWdgType = myWidgetApi->widgetType();
193     // Find title under PageGroup
194     if (myWidgetApi->isGroupBoxWidget() ||
195       ModuleBase_WidgetCreatorFactory::get()->hasPageWidget(aWdgType)) {
196       getGreedAttribute(theAttributeId);
197     }
198     else {
199       // Find title here
200       std::string anAttributeId = myWidgetApi->widgetId();
201       if (myWidgetApi->getBooleanAttribute(ATTR_GREED, false))
202         theAttributeId = anAttributeId;
203       if (theAttributeId.empty() && myWidgetApi->isPagedWidget()) {
204         //If current widget is toolbox or switch-casebox then fetch all
205         //it's pages recursively and setup into the widget.
206         if (myWidgetApi->toChildWidget()) {
207           do {
208             getGreedAttribute(theAttributeId);
209           } while (theAttributeId.empty() && myWidgetApi->toNextWidget());
210         }
211       }
212     }
213   } while (theAttributeId.empty() && myWidgetApi->toNextWidget());
214 }
215
216 void ModuleBase_WidgetFactory::moveToWidgetId(const std::string& theWidgetId, bool& theFound)
217 {
218   if (theFound)
219     return;
220
221   if (!myWidgetApi->toChildWidget())
222     return;
223
224   do {  //Iterate over each node
225     std::string aWdgType = myWidgetApi->widgetType();
226     // Find title under PageGroup
227     if (myWidgetApi->isGroupBoxWidget() ||
228       ModuleBase_WidgetCreatorFactory::get()->hasPageWidget(aWdgType)) {
229       moveToWidgetId(theWidgetId, theFound);
230     }
231     else {
232       // Find title here
233       std::string anAttributeId = myWidgetApi->widgetId();
234       theFound = anAttributeId == theWidgetId;
235       if (!theFound && myWidgetApi->isPagedWidget()) {
236         //If current widget is toolbox or switch-casebox then fetch all
237         //it's pages recursively and setup into the widget.
238         if (myWidgetApi->toChildWidget()) {
239           do {
240             moveToWidgetId(theWidgetId, theFound);
241           } while (!theFound && myWidgetApi->toNextWidget());
242         }
243       }
244     }
245   } while (!theFound && myWidgetApi->toNextWidget());
246 }
247
248 ModuleBase_PageBase* ModuleBase_WidgetFactory::createPageByType(const std::string& theType,
249                                                                 QWidget* theParent)
250 {
251   ModuleBase_PageBase* aResult = NULL;
252
253   if (theType == WDG_GROUP) {
254     QString aGroupName = qs(myWidgetApi->getProperty(CONTAINER_PAGE_NAME));
255     ModuleBase_PageGroupBox* aPage = new ModuleBase_PageGroupBox(theParent);
256     aPage->setTitle(aGroupName);
257     aResult = aPage;
258   }
259   else if (theType == WDG_CHECK_GROUP) {
260     QString aGroupName = qs(myWidgetApi->getProperty(CONTAINER_PAGE_NAME));
261     ModuleBase_WidgetCheckGroupBox* aPage = new ModuleBase_WidgetCheckGroupBox(theParent,
262                                                                 myWidgetApi);
263     aPage->setTitle(aGroupName);
264     aResult = aPage;
265   }
266   if (!aResult)
267     aResult = ModuleBase_WidgetCreatorFactory::get()->createPageByType(theType, theParent,
268                                                                        myWidgetApi);
269
270   ModuleBase_ModelWidget* aWidget = dynamic_cast<ModuleBase_ModelWidget*>(aResult);
271   if (aWidget)
272     myModelWidgets.append(aWidget);
273
274   return aResult;
275 }
276
277 ModuleBase_ModelWidget* ModuleBase_WidgetFactory::createWidgetByType(const std::string& theType,
278                                                                      QWidget* theParent)
279 {
280   ModuleBase_ModelWidget* result = NULL;
281
282   if (theType == WDG_INFO) {
283     result = new ModuleBase_WidgetLabel(theParent, myWidgetApi);
284   } else if (theType == WDG_ERRORINFO) {
285     result = new ModuleBase_WidgetErrorLabel(theParent, myWidgetApi);
286   } else if (theType == WDG_DOUBLEVALUE) {
287     result = new ModuleBase_WidgetDoubleValue(theParent, myWidgetApi);
288   } else if (theType == WDG_INTEGERVALUE) {
289     result = new ModuleBase_WidgetIntValue(theParent, myWidgetApi);
290   } else if (theType == WDG_SHAPE_SELECTOR) {
291     result = new ModuleBase_WidgetShapeSelector(theParent, myWorkshop, myWidgetApi);
292   } else if (theType == WDG_BOOLVALUE) {
293     result = new ModuleBase_WidgetBoolValue(theParent, myWidgetApi);
294   //} else if (theType == WDG_DOUBLEVALUE_EDITOR) {
295   //  result = new ModuleBase_WidgetEditor(theParent, myWidgetApi);
296   } else if (theType == WDG_FILE_SELECTOR) {
297     result = new ModuleBase_WidgetFileSelector(theParent, myWidgetApi);
298   } else if (theType == WDG_CHOICE) {
299     result = new ModuleBase_WidgetChoice(theParent, myWidgetApi);
300   } else if (theType == WDG_STRINGVALUE) {
301     std::string aPlaceHolder = myWidgetApi->getProperty( WDG_PLACE_HOLDER );
302     result = new ModuleBase_WidgetLineEdit( theParent, myWidgetApi, aPlaceHolder );
303   } else if (theType == WDG_EXPR_EDITOR) {
304     std::string aPlaceHolder = myWidgetApi->getProperty( WDG_PLACE_HOLDER );
305     result = new ModuleBase_WidgetExprEditor( theParent, myWidgetApi, aPlaceHolder );
306   } else if (theType == WDG_MULTISELECTOR) {
307     result = new ModuleBase_WidgetMultiSelector(theParent, myWorkshop, myWidgetApi);
308   } else if (theType == WDG_TOOLBOX) {
309     result = new ModuleBase_WidgetToolbox(theParent, myWidgetApi);
310   } else if (theType == WDG_SWITCH) {
311     result = new ModuleBase_WidgetSwitch(theParent, myWidgetApi);
312   } else if (theType == WDG_TOOLBOX_BOX || theType == WDG_SWITCH_CASE ||
313              theType == NODE_VALIDATOR) {
314     // Do nothing for "box" and "case"
315     result = NULL;
316   } else if (theType == WDG_ACTION) {
317     result = new ModuleBase_WidgetAction(theParent, myWidgetApi);
318   } else {
319     result = myWorkshop->module()->createWidgetByType(theType, theParent, myWidgetApi);
320     if (!result)
321       result = ModuleBase_WidgetCreatorFactory::get()->createWidgetByType(theType, theParent,
322                                                               myWidgetApi, myWorkshop);
323     #ifdef _DEBUG
324     if (!result) {
325       qDebug("ModuleBase_WidgetFactory::fillWidget: find bad widget type %s", theType.c_str());
326     }
327     #endif
328   }
329   if (result)
330     myModelWidgets.append(result);
331   return result;
332 }
333
334 QString ModuleBase_WidgetFactory::qs(const std::string& theStdString)
335 {
336   return QString::fromStdString(theStdString);
337 }
338