Salome HOME
Make Spline panel for editing aligned to top
[modules/shaper.git] / src / ModuleBase / ModuleBase_WidgetFactory.cpp
1 // Copyright (C) 2014-2019  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     : myWorkshop(theWorkshop)
82 {
83   myWidgetApi = new Config_WidgetAPI(theXmlRepresentation);
84 }
85
86 ModuleBase_WidgetFactory::~ModuleBase_WidgetFactory()
87 {
88   delete myWidgetApi;
89 }
90
91 void ModuleBase_WidgetFactory::createWidget(ModuleBase_PageBase* thePage, bool alignToTop)
92 {
93   std::string aWType = myWidgetApi->widgetType();
94   if (aWType == NODE_FEATURE) {
95     // if XML definition of the feature contains the next key, the widgets should not be created,
96     // but a specific panel should be made. However, to provide persistent of the panel values,
97     // we need to get into the panel the feature of the operation. As a result this panel should
98     // be created after the feature creating(create operation). The method setPanel() of this
99     // class is used for this. Here, we just return to avoid the widgets creation.
100     std::string aPanelName = myWidgetApi->getProperty(PROPERTY_PANEL_ID);
101     if (!aPanelName.empty())
102       return;
103   }
104
105   if (!myWidgetApi->toChildWidget())
106     return;
107
108   do {  //Iterate over each node
109     std::string aWdgType = myWidgetApi->widgetType();
110     // Create PageGroup TODO: extract
111     if (myWidgetApi->isGroupBoxWidget() ||
112         ModuleBase_WidgetCreatorFactory::get()->hasPageWidget(aWdgType)) {
113
114       //if current widget is groupbox (container) process it's children recursively
115       ModuleBase_PageBase* aPage = createPageByType(aWdgType, thePage->pageWidget());
116
117       createWidget(aPage);
118       thePage->addPageWidget(aPage);
119     } else {
120       // Create a ModelWidget
121       ModuleBase_ModelWidget* aWidget = createWidgetByType(aWdgType, thePage->pageWidget());
122       if (aWidget) {
123         if (!myWidgetApi->getBooleanAttribute(ATTR_INTERNAL, false)) {
124           thePage->addModelWidget(aWidget);
125         } else {
126           aWidget->setVisible(false);
127         }
128       }
129       // Create PagedContainer TODO: extract
130       if (myWidgetApi->isPagedWidget()) {
131         //If current widget is toolbox or switch-casebox then fetch all
132         //it's pages recursively and setup into the widget.
133         if (myWidgetApi->toChildWidget()) {
134           do {
135             QString aPageName = qs(myWidgetApi->getProperty(CONTAINER_PAGE_NAME));
136             QString aTooltip = qs(myWidgetApi->getProperty(FEATURE_TOOLTIP));
137             QString aCaseId = qs(myWidgetApi->getProperty(_ID));
138             ModuleBase_PageBase* aPage = new ModuleBase_PageWidget(aWidget);
139             createWidget(aPage);
140             if (aWdgType == WDG_SWITCH || aWdgType == WDG_TOOLBOX || aWdgType == WDG_RADIOBOX) {
141               ModuleBase_PagedContainer* aContainer =
142                 qobject_cast<ModuleBase_PagedContainer*>(aWidget);
143
144               QString anIconPath = qs( myWidgetApi->getProperty( CONTAINER_PAGE_ICON ) );
145               QPixmap anIcon = ModuleBase_IconFactory::loadPixmap( anIconPath );
146               aContainer->addPage( aPage, aPageName, aCaseId, anIcon, aTooltip);
147             }
148           } while (myWidgetApi->toNextWidget());
149         }
150       }
151     }
152   } while (myWidgetApi->toNextWidget());
153
154   if (alignToTop)
155     thePage->alignToTop();
156 }
157
158 void ModuleBase_WidgetFactory::createPanel(ModuleBase_PageBase* thePage,
159                                            const FeaturePtr& theFeature)
160 {
161   std::string aPanelName = myWidgetApi->getProperty(PROPERTY_PANEL_ID);
162   if (!aPanelName.empty() && ModuleBase_WidgetCreatorFactory::get()->hasPanelWidget(aPanelName)) {
163     QWidget* aPanel = ModuleBase_WidgetCreatorFactory::get()->createPanelByType(aPanelName,
164         thePage->pageWidget(), theFeature, myWidgetApi);
165     ModuleBase_ModelWidget* aModelWdg = dynamic_cast<ModuleBase_ModelWidget*>(aPanel);
166     if (aModelWdg)
167       thePage->addModelWidget(aModelWdg);
168     else
169       thePage->addWidget(aPanel);
170     thePage->alignToTop();
171   }
172 }
173
174 void ModuleBase_WidgetFactory::createWidget(ModuleBase_PageBase* thePage,
175                                             const std::string& theWidgetId)
176 {
177   bool aFound = false;
178   moveToWidgetId(theWidgetId, aFound);
179   if (aFound) {
180     std::string aWdgType = myWidgetApi->widgetType();
181
182     // Create a ModelWidget
183     ModuleBase_ModelWidget* aWidget = createWidgetByType(aWdgType, thePage->pageWidget());
184     if (aWidget) {
185       if (!myWidgetApi->getBooleanAttribute(ATTR_INTERNAL, false)) {
186         thePage->addModelWidget(aWidget);
187       }
188       else {
189         aWidget->setVisible(false);
190       }
191     }
192     thePage->alignToTop();
193   }
194 }
195
196 void ModuleBase_WidgetFactory::getAttributeTitle(const std::string& theAttributeId,
197                                                  std::string& theTitle)
198 {
199   bool aFound = false;
200   moveToWidgetId(theAttributeId, aFound);
201   if (aFound) {
202     theTitle = QString::fromStdString(myWidgetApi->widgetLabel()).toStdString().c_str();
203     if (theTitle.empty())
204       theTitle =
205       QString::fromStdString(myWidgetApi->getProperty(CONTAINER_PAGE_NAME)).toStdString().c_str();
206   }
207 }
208
209 void ModuleBase_WidgetFactory::getGreedAttribute(std::string& theAttributeId)
210 {
211   if (!theAttributeId.empty())
212     return;
213
214   if (!myWidgetApi->toChildWidget())
215     return;
216
217   do {  //Iterate over each node
218     std::string aWdgType = myWidgetApi->widgetType();
219     // Find title under PageGroup
220     if (myWidgetApi->isGroupBoxWidget() ||
221       ModuleBase_WidgetCreatorFactory::get()->hasPageWidget(aWdgType)) {
222       getGreedAttribute(theAttributeId);
223     }
224     else {
225       // Find title here
226       std::string anAttributeId = myWidgetApi->widgetId();
227       if (myWidgetApi->getBooleanAttribute(ATTR_GREED, false))
228         theAttributeId = anAttributeId;
229       if (theAttributeId.empty() && myWidgetApi->isPagedWidget()) {
230         //If current widget is toolbox or switch-casebox then fetch all
231         //it's pages recursively and setup into the widget.
232         if (myWidgetApi->toChildWidget()) {
233           do {
234             getGreedAttribute(theAttributeId);
235           } while (theAttributeId.empty() && myWidgetApi->toNextWidget());
236         }
237       }
238     }
239   } while (theAttributeId.empty() && myWidgetApi->toNextWidget());
240 }
241
242 void ModuleBase_WidgetFactory::moveToWidgetId(const std::string& theWidgetId, bool& theFound)
243 {
244   if (theFound)
245     return;
246
247   if (!myWidgetApi->toChildWidget())
248     return;
249
250   do {  //Iterate over each node
251     std::string aWdgType = myWidgetApi->widgetType();
252     // Find title under PageGroup
253     if (myWidgetApi->isGroupBoxWidget() ||
254       ModuleBase_WidgetCreatorFactory::get()->hasPageWidget(aWdgType)) {
255       moveToWidgetId(theWidgetId, theFound);
256     }
257     else {
258       // Find title here
259       std::string anAttributeId = myWidgetApi->widgetId();
260       theFound = anAttributeId == theWidgetId;
261       if (!theFound && myWidgetApi->isPagedWidget()) {
262         //If current widget is toolbox or switch-casebox then fetch all
263         //it's pages recursively and setup into the widget.
264         if (myWidgetApi->toChildWidget()) {
265           do {
266             moveToWidgetId(theWidgetId, theFound);
267           } while (!theFound && myWidgetApi->toNextWidget());
268         }
269       }
270     }
271   } while (!theFound && myWidgetApi->toNextWidget());
272 }
273
274 ModuleBase_PageBase* ModuleBase_WidgetFactory::createPageByType(const std::string& theType,
275                                                                 QWidget* theParent)
276 {
277   ModuleBase_PageBase* aResult = NULL;
278
279   if (theType == WDG_GROUP) {
280     QString aGroupName = qs(myWidgetApi->getProperty(CONTAINER_PAGE_NAME));
281     ModuleBase_PageGroupBox* aPage = new ModuleBase_PageGroupBox(theParent);
282     aPage->setTitle(ModuleBase_Tools::translate(
283       myWidgetApi->myFeatureId, aGroupName.toStdString()));
284     aResult = aPage;
285   }
286   else if (theType == WDG_OPTIONALBOX) {
287     ModuleBase_WidgetOptionalBox* aPage = new ModuleBase_WidgetOptionalBox(theParent,
288                                                                 myWidgetApi);
289     aResult = aPage;
290   }
291   if (!aResult)
292     aResult = ModuleBase_WidgetCreatorFactory::get()->createPageByType(theType, theParent,
293                                                                        myWidgetApi);
294
295   ModuleBase_ModelWidget* aWidget = dynamic_cast<ModuleBase_ModelWidget*>(aResult);
296   if (aWidget)
297     myModelWidgets.append(aWidget);
298
299   return aResult;
300 }
301
302 ModuleBase_ModelWidget* ModuleBase_WidgetFactory::createWidgetByType(const std::string& theType,
303                                                                      QWidget* theParent)
304 {
305   ModuleBase_ModelWidget* result = NULL;
306
307   if (theType == WDG_INFO) {
308     result = new ModuleBase_WidgetLabel(theParent, myWidgetApi);
309   } else if (theType == WDG_DOUBLEVALUE) {
310     result = new ModuleBase_WidgetDoubleValue(theParent, myWidgetApi);
311   } else if (theType == WDG_UNDOLABEL) {
312     result = new ModuleBase_WidgetUndoLabel(theParent, myWorkshop, myWidgetApi);
313   } else if (theType == WDG_DOUBLEVALUELABEL) {
314     result = new ModuleBase_WidgetLabelValue(theParent, myWidgetApi);
315   } else if (theType == WDG_INTEGERVALUE) {
316     result = new ModuleBase_WidgetIntValue(theParent, myWidgetApi);
317   } else if (theType == WDG_SHAPE_SELECTOR) {
318     result = new ModuleBase_WidgetShapeSelector(theParent, myWorkshop, myWidgetApi);
319   } else if (theType == WDG_FEATURE_SELECTOR) {
320     result = new ModuleBase_WidgetFeatureSelector(theParent, myWorkshop, myWidgetApi);
321   } else if (theType == WDG_BOOLVALUE) {
322     result = new ModuleBase_WidgetBoolValue(theParent, myWidgetApi);
323   //} else if (theType == WDG_DOUBLEVALUE_EDITOR) {
324   //  result = new ModuleBase_WidgetEditor(theParent, myWidgetApi);
325   } else if (theType == WDG_FILE_SELECTOR) {
326     result = new ModuleBase_WidgetFileSelector(theParent, myWidgetApi);
327   } else if (theType == WDG_CHOICE) {
328     result = new ModuleBase_WidgetChoice(theParent, myWidgetApi);
329   } else if (theType == WDG_STRINGVALUE) {
330     std::string aPlaceHolder = myWidgetApi->getProperty( WDG_PLACE_HOLDER );
331     result = new ModuleBase_WidgetLineEdit( theParent, myWidgetApi, aPlaceHolder );
332   } else if (theType == WDG_NAMEVALUE) {
333     std::string aPlaceHolder = myWidgetApi->getProperty( WDG_PLACE_HOLDER );
334     result = new ModuleBase_WidgetNameEdit( theParent, myWidgetApi, aPlaceHolder );
335   } else if (theType == WDG_EXPR_EDITOR) {
336     std::string aPlaceHolder = myWidgetApi->getProperty( WDG_PLACE_HOLDER );
337     result = new ModuleBase_WidgetExprEditor( theParent, myWidgetApi, aPlaceHolder );
338   } else if (theType == WDG_MULTISELECTOR) {
339     result = new ModuleBase_WidgetMultiSelector(theParent, myWorkshop, myWidgetApi);
340   } else if (theType == WDG_CONCEALED_OBJECTS_VIEW) {
341     result = new ModuleBase_WidgetConcealedObjects(theParent, myWidgetApi);
342   } else if (theType == WDG_TOOLBOX) {
343     result = new ModuleBase_WidgetToolbox(theParent, myWidgetApi);
344   } else if (theType == WDG_RADIOBOX) {
345     result = new ModuleBase_WidgetRadiobox(theParent, myWidgetApi);
346   } else if (theType == WDG_SWITCH) {
347     result = new ModuleBase_WidgetSwitch(theParent, myWidgetApi);
348   } else if (theType == WDG_TOOLBOX_BOX || theType == WDG_SWITCH_CASE ||
349              theType == NODE_VALIDATOR) {
350     // Do nothing for "box" and "case"
351     result = NULL;
352   } else if (theType == WDG_ACTION) {
353     result = new ModuleBase_WidgetAction(theParent, myWidgetApi);
354   } else if (theType == WDG_POINT_INPUT) {
355     result = new ModuleBase_WidgetPointInput(theParent, myWorkshop, myWidgetApi);
356   } else if (theType == WDG_SELECTION_FILTERS) {
357     result = new ModuleBase_WidgetSelectionFilter(theParent, myWorkshop, myWidgetApi);
358   } else {
359     result = myWorkshop->module()->createWidgetByType(theType, theParent, myWidgetApi);
360     if (!result)
361       result = ModuleBase_WidgetCreatorFactory::get()->createWidgetByType(theType, theParent,
362                                                               myWidgetApi, myWorkshop);
363     #ifdef _DEBUG
364     if (!result) {
365       qDebug("ModuleBase_WidgetFactory::fillWidget: find bad widget type %s", theType.c_str());
366     }
367     #endif
368   }
369   if (result)
370     myModelWidgets.append(result);
371   return result;
372 }
373
374 QString ModuleBase_WidgetFactory::qs(const std::string& theStdString)
375 {
376   return QString::fromStdString(theStdString);
377 }
378