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