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