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