Salome HOME
Processing of 'selection_filter' xml nodes added.
[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_WidgetFeatureOrAttribute.h>
13 //#include <ModuleBase_WidgetFeature.h>
14 #include <ModuleBase_WidgetEditor.h>
15 #include <ModuleBase_WidgetSwitch.h>
16 #include <ModuleBase_WidgetShapeSelector.h>
17 #include <ModuleBase_WidgetDoubleValue.h>
18 #include <ModuleBase_WidgetBoolValue.h>
19 //#include <ModuleBase_WidgetPoint2dDistance.h>
20 #include <ModuleBase_WidgetFileSelector.h>
21 #include <ModuleBase_WidgetChoice.h>
22 #include <ModuleBase_IWorkshop.h>
23 #include <ModuleBase_IModule.h>
24 #include <ModuleBase_Tools.h>
25 #include <ModuleBase_WidgetLineEdit.h>
26 #include <ModuleBase_WidgetMultiSelector.h>
27 #include <ModuleBase_WidgetLabel.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(ATTR_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
113 QWidget* ModuleBase_WidgetFactory::labelControl(QWidget* theParent)
114 {
115   ModuleBase_WidgetLabel* aWgt =
116       new ModuleBase_WidgetLabel(theParent, myWidgetApi, myParentId);
117   myModelWidgets.append(aWgt);
118   return aWgt->getControl();
119 }
120
121
122 QWidget* ModuleBase_WidgetFactory::createWidgetByType(const std::string& theType,
123                                                       QWidget* theParent)
124 {
125   QWidget* result = NULL;
126   if (theType == WDG_DOUBLEVALUE) {
127     result = doubleSpinBoxControl(theParent);
128
129   } else if (theType == WDG_INFO) {
130     result = labelControl(theParent);
131
132   } else if (theType == WDG_SHAPE_SELECTOR) {
133     result = shapeSelectorControl(theParent);
134
135   } else if (theType == WDG_BOOLVALUE) {
136     result = booleanControl(theParent);
137
138   //} else if (theType == WDG_FEATURE_SELECTOR) {
139   //  result = featureSelectorControl(theParent);
140
141   //} else if (theType == WDG_FEATURE_OR_ATTRIBUTE_SELECTOR) {
142   //  result = featureOrAttributeSelectorControl(theParent);
143
144   } else if (theType == WDG_DOUBLEVALUE_EDITOR) {
145     result = doubleValueEditor(theParent);
146
147   } else if (theType == WDG_FILE_SELECTOR) {
148     result = fileSelectorControl(theParent);
149
150   } else if (theType == WDG_CHOICE) {
151     result = choiceControl(theParent);
152
153   } else if (theType == WDG_STRINGVALUE) {
154     result = lineEditControl(theParent);
155
156   } else if (theType == WDG_MULTISELECTOR) {
157     result = multiSelectorControl(theParent);
158
159   } else if (myWidgetApi->isContainerWidget() || myWidgetApi->isPagedWidget()) {
160     result = createContainer(theType, theParent);
161   } else {
162     result = myWorkshop->module()->createWidgetByType(theType, theParent, myWidgetApi,
163                                                       myParentId, myModelWidgets);
164 #ifdef _DEBUG
165     if (!result) {qDebug("ModuleBase_WidgetFactory::fillWidget: find bad widget type");}
166 #endif
167   }
168   return result;
169 }
170
171 QWidget* ModuleBase_WidgetFactory::createContainer(const std::string& theType, QWidget* theParent)
172 {
173   QWidget* result = NULL;
174   if (theType == WDG_GROUP || theType == WDG_CHECK_GROUP) {
175     QGroupBox* aGroupBox = new QGroupBox(theParent);
176     aGroupBox->setCheckable(theType == WDG_CHECK_GROUP);
177     result = aGroupBox;
178   } else if (theType == WDG_TOOLBOX) {
179     result = new QToolBox(theParent);
180   } else if (theType == WDG_SWITCH) {
181     result = new ModuleBase_WidgetSwitch(theParent);
182   } else if (theType == WDG_TOOLBOX_BOX || theType == WDG_SWITCH_CASE) {
183     result = NULL;
184   }
185 #ifdef _DEBUG
186   else {qDebug() << "ModuleBase_WidgetFactory::fillWidget: find bad container type";}
187 #endif
188   return result;
189 }
190
191 QWidget* ModuleBase_WidgetFactory::doubleSpinBoxControl(QWidget* theParent)
192 {
193   ModuleBase_WidgetDoubleValue* aDblWgt =
194       new ModuleBase_WidgetDoubleValue(theParent, myWidgetApi, myParentId);
195   myModelWidgets.append(aDblWgt);
196   return aDblWgt->getControl();
197 }
198
199 QWidget* ModuleBase_WidgetFactory::doubleValueEditor(QWidget* theParent)
200 {
201   ModuleBase_WidgetEditor* aWidget =
202       new ModuleBase_WidgetEditor(theParent, myWidgetApi, myParentId);
203   myModelWidgets.append(aWidget);
204   return aWidget->getControl();
205 }
206
207 QWidget* ModuleBase_WidgetFactory::shapeSelectorControl(QWidget* theParent)
208 {
209   ModuleBase_WidgetShapeSelector* aSelector =
210       new ModuleBase_WidgetShapeSelector(theParent, myWorkshop, myWidgetApi, myParentId);
211   myModelWidgets.append(aSelector);
212   return aSelector->getControl();
213 }
214
215 QWidget* ModuleBase_WidgetFactory::booleanControl(QWidget* theParent)
216 {
217   ModuleBase_WidgetBoolValue* aBoolWgt =
218       new ModuleBase_WidgetBoolValue(theParent, myWidgetApi, myParentId);
219   myModelWidgets.append(aBoolWgt);
220   return aBoolWgt->getControl();
221 }
222
223 QWidget* ModuleBase_WidgetFactory::fileSelectorControl(QWidget* theParent)
224 {
225   ModuleBase_WidgetFileSelector* aFileSelectorWgt =
226       new ModuleBase_WidgetFileSelector(theParent, myWidgetApi, myParentId);
227   myModelWidgets.append(aFileSelectorWgt);
228   return aFileSelectorWgt->getControl();
229 }
230
231 QWidget* ModuleBase_WidgetFactory::choiceControl(QWidget* theParent)
232 {
233   ModuleBase_WidgetChoice* aChoiceWgt =
234       new ModuleBase_WidgetChoice(theParent, myWidgetApi,myParentId);
235   myModelWidgets.append(aChoiceWgt);
236   return aChoiceWgt->getControl();
237 }
238
239 QWidget* ModuleBase_WidgetFactory::lineEditControl(QWidget* theParent)
240 {
241   ModuleBase_WidgetLineEdit* aLineEditWgt =
242       new ModuleBase_WidgetLineEdit(theParent, myWidgetApi,myParentId);
243   myModelWidgets.append(aLineEditWgt);
244   return aLineEditWgt->getControl();
245 }
246
247 QWidget* ModuleBase_WidgetFactory::multiSelectorControl(QWidget* theParent)
248 {
249   ModuleBase_WidgetMultiSelector* aMultiselectorWgt =
250       new ModuleBase_WidgetMultiSelector(theParent, myWorkshop, myWidgetApi,myParentId);
251   myModelWidgets.append(aMultiselectorWgt);
252   return aMultiselectorWgt->getControl();
253 }
254
255 QString ModuleBase_WidgetFactory::qs(const std::string& theStdString) const
256 {
257   return QString::fromStdString(theStdString);
258 }
259