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