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