]> SALOME platform Git repositories - modules/shaper.git/blob - src/ModuleBase/ModuleBase_WidgetFactory.cpp
Salome HOME
Merge branch 'Dev_1.1.0' of newgeom:newgeom into Dev_1.1.0
[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
12 #include <ModuleBase_Operation.h>
13 #include <ModuleBase_OperationDescription.h>
14 //#include <ModuleBase_WidgetFeatureOrAttribute.h>
15 //#include <ModuleBase_WidgetFeature.h>
16 #include <ModuleBase_WidgetEditor.h>
17 #include <ModuleBase_WidgetSwitch.h>
18 #include <ModuleBase_WidgetShapeSelector.h>
19 #include <ModuleBase_WidgetDoubleValue.h>
20 #include <ModuleBase_WidgetBoolValue.h>
21 //#include <ModuleBase_WidgetPoint2dDistance.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_WidgetLabel.h>
30 #include <ModuleBase_WidgetToolbox.h>
31
32 #include <ModelAPI_Validator.h>
33 #include <ModelAPI_Session.h>
34
35 #include <Config_Keywords.h>
36 #include <Config_WidgetAPI.h>
37
38 #include <QWidget>
39 #include <QHBoxLayout>
40 #include <QGridLayout>
41 #include <QSpinBox>
42 #include <QMetaProperty>
43 #include <QLabel>
44 #include <QPixmap>
45 #include <QGroupBox>
46 #include <QToolBox>
47
48 #ifdef _DEBUG
49 #include <QDebug>
50 #endif
51
52 #include <cfloat>
53 #include <climits>
54
55 ModuleBase_WidgetFactory::ModuleBase_WidgetFactory(const std::string& theXmlRepresentation,
56                                                    ModuleBase_IWorkshop* theWorkshop)
57     : myWorkshop(theWorkshop)
58 {
59   myWidgetApi = new Config_WidgetAPI(theXmlRepresentation);
60 }
61
62 ModuleBase_WidgetFactory::~ModuleBase_WidgetFactory()
63 {
64   delete myWidgetApi;
65 }
66
67 void ModuleBase_WidgetFactory::createWidget(QWidget* theParent)
68 {
69   myParentId = myWidgetApi->widgetId();
70   if (!myWidgetApi->toChildWidget())
71     return;
72
73   QVBoxLayout* aWidgetLay = new QVBoxLayout(theParent);
74   bool isStretchLayout = false;
75   do {  //Iterate over each node
76     std::string aWdgType = myWidgetApi->widgetType();
77     //Create a widget (doublevalue, groupbox, toolbox, etc.
78     QWidget* aWidget = createWidgetByType(aWdgType, theParent);
79     if (aWidget) {
80       if (!myWidgetApi->getBooleanAttribute(ATTR_INTERNAL, false)) {
81         aWidgetLay->addWidget(aWidget);
82       } else {
83         aWidget->setVisible(false);
84       }
85     }
86     if (myWidgetApi->isContainerWidget()) {
87       //if current widget is groupbox (container) process it's children recursively
88       QString aGroupName = qs(myWidgetApi->getProperty(CONTAINER_PAGE_NAME));
89       createWidget(aWidget);
90       ModuleBase_Tools::adjustMargins(aWidget);
91       QGroupBox* aGrBox = qobject_cast<QGroupBox*>(aWidget);
92       aGrBox->setTitle(aGroupName);
93     }
94     if (myWidgetApi->isPagedWidget()) {
95       //If current widget is toolbox or switch-casebox then fetch all
96       //it's pages recursively and setup into the widget.
97       myWidgetApi->toChildWidget();
98       do {
99         QString aPageName = qs(myWidgetApi->getProperty(CONTAINER_PAGE_NAME));
100         QString aCaseId = qs(myWidgetApi->getProperty(_ID));
101         QWidget* aPage = new QWidget(aWidget);
102         createWidget(aPage);
103         ModuleBase_Tools::adjustMargins(aPage);
104         if (aWdgType == WDG_SWITCH) {
105           ModuleBase_WidgetSwitch* aSwitch = qobject_cast<ModuleBase_WidgetSwitch*>(aWidget);
106           aSwitch->addPage(aPage, aPageName);
107         } else if (aWdgType == WDG_TOOLBOX) {
108           ModuleBase_WidgetToolbox* aToolbox = qobject_cast<ModuleBase_WidgetToolbox*>(aWidget);
109           aToolbox->addPage(aPage, aPageName, aCaseId);
110         }
111       } while (myWidgetApi->toNextWidget());
112     }
113     if (aWidget && !isStretchLayout) {
114       isStretchLayout = !hasExpandingControls(aWidget);
115     }
116   } while (myWidgetApi->toNextWidget());
117   if (isStretchLayout) {
118     aWidgetLay->addStretch(1);
119   }
120   theParent->setLayout(aWidgetLay);
121 }
122
123 bool ModuleBase_WidgetFactory::hasExpandingControls(QWidget* theParent)
124 {
125   bool result = false;
126   QList<QWidget *> aListToCheck;
127   aListToCheck << theParent;
128   ModuleBase_ModelWidget* aModelWidget = qobject_cast<ModuleBase_ModelWidget*>(theParent);
129   if(aModelWidget) {
130     aListToCheck << aModelWidget->getControls();
131   }
132   foreach(QWidget* eachWidget, aListToCheck) {
133     QSizePolicy::Policy aVPolicy = eachWidget->sizePolicy().verticalPolicy();
134     if(aVPolicy & QSizePolicy::ExpandFlag) {
135       result = true;
136     }
137   }
138   return result;
139 }
140
141 QWidget* ModuleBase_WidgetFactory::createWidgetByType(const std::string& theType,
142                                                       QWidget* theParent)
143 {
144   QWidget* result = NULL;
145   if (theType == WDG_DOUBLEVALUE) {
146     result = doubleSpinBoxControl(theParent);
147
148   } else if (theType == WDG_INFO) {
149     result = labelControl(theParent);
150
151   } else if (theType == WDG_SHAPE_SELECTOR) {
152     result = shapeSelectorControl(theParent);
153
154   } else if (theType == WDG_BOOLVALUE) {
155     result = booleanControl(theParent);
156
157   } else if (theType == WDG_DOUBLEVALUE_EDITOR) {
158     result = doubleValueEditor(theParent);
159
160   } else if (theType == WDG_FILE_SELECTOR) {
161     result = fileSelectorControl(theParent);
162
163   } else if (theType == WDG_CHOICE) {
164     result = choiceControl(theParent);
165
166   } else if (theType == WDG_STRINGVALUE) {
167     result = lineEditControl(theParent);
168
169   } else if (theType == WDG_MULTISELECTOR) {
170     result = multiSelectorControl(theParent);
171
172   } else if (myWidgetApi->isContainerWidget() || myWidgetApi->isPagedWidget()) {
173     result = createContainer(theType, theParent);
174   } else {
175     result = myWorkshop->module()->createWidgetByType(theType, theParent, myWidgetApi,
176                                                       myParentId, myModelWidgets);
177 #ifdef _DEBUG
178     if (!result) {qDebug("ModuleBase_WidgetFactory::fillWidget: find bad widget type");}
179 #endif
180   }
181   return result;
182 }
183
184 QWidget* ModuleBase_WidgetFactory::createContainer(const std::string& theType, QWidget* theParent)
185 {
186   QWidget* aResult = NULL;
187   if (theType == WDG_GROUP || theType == WDG_CHECK_GROUP) {
188     QGroupBox* aGroupBox = new QGroupBox(theParent);
189     aGroupBox->setCheckable(theType == WDG_CHECK_GROUP);
190     aResult = aGroupBox;
191   } else if (theType == WDG_TOOLBOX) {
192     ModuleBase_WidgetToolbox* aWdg = new ModuleBase_WidgetToolbox(theParent, myWidgetApi, myParentId);
193     myModelWidgets.append(aWdg);
194     aResult = aWdg;
195   } else if (theType == WDG_SWITCH) {
196     aResult = new ModuleBase_WidgetSwitch(theParent);
197   } else if (theType == WDG_TOOLBOX_BOX || theType == WDG_SWITCH_CASE) {
198     // Do nothing for "box" and "case"
199     aResult = NULL;
200   }
201 #ifdef _DEBUG
202   else {qDebug() << "ModuleBase_WidgetFactory::fillWidget: find bad container type";}
203 #endif
204   return aResult;
205 }
206
207 QWidget* ModuleBase_WidgetFactory::labelControl(QWidget* theParent)
208 {
209   ModuleBase_WidgetLabel* aWgt =
210       new ModuleBase_WidgetLabel(theParent, myWidgetApi, myParentId);
211   myModelWidgets.append(aWgt);
212   return aWgt;
213 }
214
215 QWidget* ModuleBase_WidgetFactory::doubleSpinBoxControl(QWidget* theParent)
216 {
217   ModuleBase_WidgetDoubleValue* aDblWgt =
218       new ModuleBase_WidgetDoubleValue(theParent, myWidgetApi, myParentId);
219   myModelWidgets.append(aDblWgt);
220   return aDblWgt;
221 }
222
223 QWidget* ModuleBase_WidgetFactory::doubleValueEditor(QWidget* theParent)
224 {
225   ModuleBase_WidgetEditor* aWidget =
226       new ModuleBase_WidgetEditor(theParent, myWidgetApi, myParentId);
227   myModelWidgets.append(aWidget);
228   return aWidget;
229 }
230
231 QWidget* ModuleBase_WidgetFactory::shapeSelectorControl(QWidget* theParent)
232 {
233   ModuleBase_WidgetShapeSelector* aSelector =
234       new ModuleBase_WidgetShapeSelector(theParent, myWorkshop, myWidgetApi, myParentId);
235   myModelWidgets.append(aSelector);
236   return aSelector;
237 }
238
239 QWidget* ModuleBase_WidgetFactory::booleanControl(QWidget* theParent)
240 {
241   ModuleBase_WidgetBoolValue* aBoolWgt =
242       new ModuleBase_WidgetBoolValue(theParent, myWidgetApi, myParentId);
243   myModelWidgets.append(aBoolWgt);
244   return aBoolWgt;
245 }
246
247 QWidget* ModuleBase_WidgetFactory::fileSelectorControl(QWidget* theParent)
248 {
249   ModuleBase_WidgetFileSelector* aFileSelectorWgt =
250       new ModuleBase_WidgetFileSelector(theParent, myWidgetApi, myParentId);
251   myModelWidgets.append(aFileSelectorWgt);
252   return aFileSelectorWgt;
253 }
254
255 QWidget* ModuleBase_WidgetFactory::choiceControl(QWidget* theParent)
256 {
257   ModuleBase_WidgetChoice* aChoiceWgt =
258       new ModuleBase_WidgetChoice(theParent, myWidgetApi,myParentId);
259   myModelWidgets.append(aChoiceWgt);
260   return aChoiceWgt;
261 }
262
263 QWidget* ModuleBase_WidgetFactory::lineEditControl(QWidget* theParent)
264 {
265   ModuleBase_WidgetLineEdit* aLineEditWgt =
266       new ModuleBase_WidgetLineEdit(theParent, myWidgetApi,myParentId);
267   myModelWidgets.append(aLineEditWgt);
268   return aLineEditWgt;
269 }
270
271 QWidget* ModuleBase_WidgetFactory::multiSelectorControl(QWidget* theParent)
272 {
273   ModuleBase_WidgetMultiSelector* aMultiselectorWgt =
274       new ModuleBase_WidgetMultiSelector(theParent, myWorkshop, myWidgetApi,myParentId);
275   myModelWidgets.append(aMultiselectorWgt);
276   return aMultiselectorWgt;
277 }
278
279 QString ModuleBase_WidgetFactory::qs(const std::string& theStdString)
280 {
281   return QString::fromStdString(theStdString);
282 }
283