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