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