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