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