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