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