Salome HOME
[bos #35140] [EDF] (2023-T1) Memory communication between SHAPER and GEOM
[modules/shaper.git] / src / ModuleBase / ModuleBase_WidgetFactory.cpp
1 // Copyright (C) 2014-2023  CEA, EDF
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             if (myWidgetApi->getBooleanAttribute(ATTR_INTERNAL, false)) continue;
137             QString aPageName = qs(myWidgetApi->getProperty(CONTAINER_PAGE_NAME));
138             QString aTooltip = qs(myWidgetApi->getProperty(FEATURE_TOOLTIP));
139             QString aCaseId = qs(myWidgetApi->getProperty(_ID));
140             ModuleBase_PageBase* aPage = new ModuleBase_PageWidget(aWidget);
141             createWidget(aPage);
142             if (aWdgType == WDG_SWITCH || aWdgType == WDG_TOOLBOX || aWdgType == WDG_RADIOBOX) {
143               ModuleBase_PagedContainer* aContainer =
144                 qobject_cast<ModuleBase_PagedContainer*>(aWidget);
145
146               QString anIconPath = qs( myWidgetApi->getProperty( CONTAINER_PAGE_ICON ) );
147               QPixmap anIcon = ModuleBase_IconFactory::loadPixmap( anIconPath );
148               aContainer->addPage( aPage, aPageName, aCaseId, anIcon, aTooltip);
149             }
150           } while (myWidgetApi->toNextWidget());
151         }
152       }
153     }
154   } while (myWidgetApi->toNextWidget());
155
156   if (alignToTop)
157     thePage->alignToTop();
158 }
159
160 void ModuleBase_WidgetFactory::createPanel(ModuleBase_PageBase* thePage,
161                                            const FeaturePtr& theFeature)
162 {
163   std::string aPanelName = myWidgetApi->getProperty(PROPERTY_PANEL_ID);
164   if (!aPanelName.empty() && ModuleBase_WidgetCreatorFactory::get()->hasPanelWidget(aPanelName)) {
165     QWidget* aPanel = ModuleBase_WidgetCreatorFactory::get()->createPanelByType(aPanelName,
166         thePage->pageWidget(), theFeature, myWidgetApi);
167     ModuleBase_ModelWidget* aModelWdg = dynamic_cast<ModuleBase_ModelWidget*>(aPanel);
168     if (aModelWdg)
169       thePage->addModelWidget(aModelWdg);
170     else
171       thePage->addWidget(aPanel);
172     thePage->alignToTop();
173   }
174 }
175
176 void ModuleBase_WidgetFactory::createWidget(ModuleBase_PageBase* thePage,
177                                             const std::string& theWidgetId)
178 {
179   bool aFound = false;
180   moveToWidgetId(theWidgetId, aFound);
181   if (aFound) {
182     std::string aWdgType = myWidgetApi->widgetType();
183
184     // Create a ModelWidget
185     ModuleBase_ModelWidget* aWidget = createWidgetByType(aWdgType, thePage->pageWidget());
186     if (aWidget) {
187       if (!myWidgetApi->getBooleanAttribute(ATTR_INTERNAL, false)) {
188         thePage->addModelWidget(aWidget);
189       }
190       else {
191         aWidget->setVisible(false);
192       }
193     }
194     thePage->alignToTop();
195   }
196 }
197
198 void ModuleBase_WidgetFactory::getAttributeTitle(const std::string& theAttributeId,
199                                                  std::string& theTitle)
200 {
201   bool aFound = false;
202   moveToWidgetId(theAttributeId, aFound);
203   if (aFound) {
204     theTitle = QString::fromStdString(myWidgetApi->widgetLabel()).toStdString().c_str();
205     if (theTitle.empty())
206       theTitle =
207       QString::fromStdString(myWidgetApi->getProperty(CONTAINER_PAGE_NAME)).toStdString().c_str();
208   }
209   else
210     theTitle = theAttributeId;
211 }
212
213 void ModuleBase_WidgetFactory::getGreedAttribute(std::string& theAttributeId)
214 {
215   if (!theAttributeId.empty())
216     return;
217
218   if (!myWidgetApi->toChildWidget())
219     return;
220
221   do {  //Iterate over each node
222     std::string aWdgType = myWidgetApi->widgetType();
223     // Find title under PageGroup
224     if (myWidgetApi->isGroupBoxWidget() ||
225       ModuleBase_WidgetCreatorFactory::get()->hasPageWidget(aWdgType)) {
226       getGreedAttribute(theAttributeId);
227     }
228     else {
229       // Find title here
230       std::string anAttributeId = myWidgetApi->widgetId();
231       if (myWidgetApi->getBooleanAttribute(ATTR_GREED, false))
232         theAttributeId = anAttributeId;
233       if (theAttributeId.empty() && myWidgetApi->isPagedWidget()) {
234         //If current widget is toolbox or switch-casebox then fetch all
235         //it's pages recursively and setup into the widget.
236         if (myWidgetApi->toChildWidget()) {
237           do {
238             getGreedAttribute(theAttributeId);
239           } while (theAttributeId.empty() && myWidgetApi->toNextWidget());
240         }
241       }
242     }
243   } while (theAttributeId.empty() && myWidgetApi->toNextWidget());
244 }
245
246 void ModuleBase_WidgetFactory::moveToWidgetId(const std::string& theWidgetId, bool& theFound)
247 {
248   if (theFound)
249     return;
250
251   if (!myWidgetApi->toChildWidget())
252     return;
253
254   do {  //Iterate over each node
255     std::string aWdgType = myWidgetApi->widgetType();
256     // Find title under PageGroup
257     if (myWidgetApi->isGroupBoxWidget() ||
258       ModuleBase_WidgetCreatorFactory::get()->hasPageWidget(aWdgType)) {
259       moveToWidgetId(theWidgetId, theFound);
260     }
261     else {
262       // Find title here
263       std::string anAttributeId = myWidgetApi->widgetId();
264       theFound = anAttributeId == theWidgetId;
265       if (!theFound && myWidgetApi->isPagedWidget()) {
266         //If current widget is toolbox or switch-casebox then fetch all
267         //it's pages recursively and setup into the widget.
268         if (myWidgetApi->toChildWidget()) {
269           do {
270             moveToWidgetId(theWidgetId, theFound);
271           } while (!theFound && myWidgetApi->toNextWidget());
272         }
273       }
274     }
275   } while (!theFound && myWidgetApi->toNextWidget());
276 }
277
278 ModuleBase_PageBase* ModuleBase_WidgetFactory::createPageByType(const std::string& theType,
279                                                                 QWidget* theParent)
280 {
281   ModuleBase_PageBase* aResult = NULL;
282
283   if (theType == WDG_GROUP) {
284     QString aGroupName = qs(myWidgetApi->getProperty(CONTAINER_PAGE_NAME));
285     ModuleBase_PageGroupBox* aPage = new ModuleBase_PageGroupBox(theParent);
286     aPage->setTitle(ModuleBase_Tools::translate(
287       myWidgetApi->myFeatureId, aGroupName.toStdString()));
288     aResult = aPage;
289   }
290   else if (theType == WDG_OPTIONALBOX) {
291     ModuleBase_WidgetOptionalBox* aPage = new ModuleBase_WidgetOptionalBox(theParent,
292                                                                 myWidgetApi);
293     aResult = aPage;
294   }
295   if (!aResult)
296     aResult = ModuleBase_WidgetCreatorFactory::get()->createPageByType(theType, theParent,
297                                                                        myWidgetApi);
298
299   ModuleBase_ModelWidget* aWidget = dynamic_cast<ModuleBase_ModelWidget*>(aResult);
300   if (aWidget)
301     myModelWidgets.append(aWidget);
302
303   return aResult;
304 }
305
306 ModuleBase_ModelWidget* ModuleBase_WidgetFactory::createWidgetByType(const std::string& theType,
307                                                                      QWidget* theParent)
308 {
309   ModuleBase_ModelWidget* result = NULL;
310
311   if (theType == WDG_INFO) {
312     result = new ModuleBase_WidgetLabel(theParent, myWidgetApi);
313   } else if (theType == WDG_DOUBLEVALUE) {
314     result = new ModuleBase_WidgetDoubleValue(theParent, myWidgetApi);
315   } else if (theType == WDG_UNDOLABEL) {
316     result = new ModuleBase_WidgetUndoLabel(theParent, myWorkshop, myWidgetApi);
317   } else if (theType == WDG_DOUBLEVALUELABEL) {
318     result = new ModuleBase_WidgetLabelValue(theParent, myWidgetApi);
319   } else if (theType == WDG_INTEGERVALUE) {
320     result = new ModuleBase_WidgetIntValue(theParent, myWidgetApi);
321   } else if (theType == WDG_SHAPE_SELECTOR) {
322     result = new ModuleBase_WidgetShapeSelector(theParent, myWorkshop, myWidgetApi);
323   } else if (theType == WDG_FEATURE_SELECTOR) {
324     result = new ModuleBase_WidgetFeatureSelector(theParent, myWorkshop, myWidgetApi);
325   } else if (theType == WDG_BOOLVALUE) {
326     result = new ModuleBase_WidgetBoolValue(theParent, myWidgetApi);
327   //} else if (theType == WDG_DOUBLEVALUE_EDITOR) {
328   //  result = new ModuleBase_WidgetEditor(theParent, myWidgetApi);
329   } else if (theType == WDG_FILE_SELECTOR) {
330     result = new ModuleBase_WidgetFileSelector(theParent, myWidgetApi);
331   } else if (theType == WDG_CHOICE) {
332     result = new ModuleBase_WidgetChoice(theParent, myWidgetApi);
333   } else if (theType == WDG_STRINGVALUE) {
334     std::string aPlaceHolder = myWidgetApi->getProperty( WDG_PLACE_HOLDER );
335     result = new ModuleBase_WidgetLineEdit( theParent, myWidgetApi, aPlaceHolder );
336   } else if (theType == WDG_NAMEVALUE) {
337     std::string aPlaceHolder = myWidgetApi->getProperty( WDG_PLACE_HOLDER );
338     result = new ModuleBase_WidgetNameEdit( theParent, myWidgetApi, aPlaceHolder );
339   } else if (theType == WDG_EXPR_EDITOR) {
340     std::string aPlaceHolder = myWidgetApi->getProperty( WDG_PLACE_HOLDER );
341     result = new ModuleBase_WidgetExprEditor( theParent, myWidgetApi, aPlaceHolder );
342   } else if (theType == WDG_MULTISELECTOR) {
343     result = new ModuleBase_WidgetMultiSelector(theParent, myWorkshop, myWidgetApi);
344   } else if (theType == WDG_CONCEALED_OBJECTS_VIEW) {
345     result = new ModuleBase_WidgetConcealedObjects(theParent, myWidgetApi);
346   } else if (theType == WDG_TOOLBOX) {
347     result = new ModuleBase_WidgetToolbox(theParent, myWidgetApi);
348   } else if (theType == WDG_RADIOBOX) {
349     result = new ModuleBase_WidgetRadiobox(theParent, myWidgetApi);
350   } else if (theType == WDG_SWITCH) {
351     result = new ModuleBase_WidgetSwitch(theParent, myWidgetApi);
352   } else if (theType == WDG_TOOLBOX_BOX || theType == WDG_SWITCH_CASE ||
353              theType == NODE_VALIDATOR) {
354     // Do nothing for "box" and "case"
355     result = NULL;
356   } else if (theType == WDG_ACTION) {
357     result = new ModuleBase_WidgetAction(theParent, myWidgetApi);
358   } else if (theType == WDG_POINT_INPUT) {
359     result = new ModuleBase_WidgetPointInput(theParent, myWorkshop, myWidgetApi);
360   } else if (theType == WDG_SELECTION_FILTERS) {
361     result = new ModuleBase_WidgetSelectionFilter(theParent, myWorkshop, myWidgetApi);
362   } else {
363     result = myWorkshop->module()->createWidgetByType(theType, theParent, myWidgetApi);
364     if (!result)
365       result = ModuleBase_WidgetCreatorFactory::get()->createWidgetByType(theType, theParent,
366                                                               myWidgetApi, myWorkshop);
367     #ifdef _DEBUG
368     if (!result) {
369       qDebug("ModuleBase_WidgetFactory::fillWidget: find bad widget type %s", theType.c_str());
370     }
371     #endif
372   }
373   if (result)
374     myModelWidgets.append(result);
375   return result;
376 }
377
378 QString ModuleBase_WidgetFactory::qs(const std::string& theStdString)
379 {
380   return QString::fromStdString(theStdString);
381 }
382