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