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