]> SALOME platform Git repositories - modules/shaper.git/blob - src/ModuleBase/ModuleBase_WidgetFactory.cpp
Salome HOME
Redesign of operations architecture
[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
28 #include <ModelAPI_Validator.h>
29 #include <ModelAPI_Session.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   delete myWidgetApi;
61 }
62
63 void ModuleBase_WidgetFactory::createWidget(QWidget* theParent)
64 {
65   myParentId = myWidgetApi->widgetId();
66   if (!myWidgetApi->toChildWidget())
67     return;
68
69   QVBoxLayout* aWidgetLay = new QVBoxLayout(theParent);
70   do {  //Iterate over each node
71     std::string aWdgType = myWidgetApi->widgetType();
72     //Create a widget (doublevalue, groupbox, toolbox, etc.
73     QWidget* aWidget = createWidgetByType(aWdgType, theParent);
74     if (aWidget) {
75       if (!myWidgetApi->getBooleanAttribute(ATTRIBUTE_INTERNAL, false)) {
76         aWidgetLay->addWidget(aWidget);
77       } else {
78         aWidget->setVisible(false);
79       }
80     }
81     if (myWidgetApi->isContainerWidget()) {
82       //if current widget is groupbox (container) process it's children recursively
83       QString aGroupName = qs(myWidgetApi->getProperty(CONTAINER_PAGE_NAME));
84       createWidget(aWidget);
85       ModuleBase_Tools::adjustMargins(aWidget);
86       QGroupBox* aGrBox = qobject_cast<QGroupBox*>(aWidget);
87       aGrBox->setTitle(aGroupName);
88     }
89     if (myWidgetApi->isPagedWidget()) {
90       //If current widget is toolbox or switch-casebox then fetch all
91       //it's pages recursively and setup into the widget.
92       myWidgetApi->toChildWidget();
93       do {
94         QString aPageName = qs(myWidgetApi->getProperty(CONTAINER_PAGE_NAME));
95         QWidget* aPage = new QWidget(aWidget);
96         ModuleBase_Tools::adjustMargins(aPage);
97         createWidget(aPage);
98         if (aWdgType == WDG_SWITCH) {
99           ModuleBase_WidgetSwitch* aSwitch = qobject_cast<ModuleBase_WidgetSwitch*>(aWidget);
100           aSwitch->addPage(aPage, aPageName);
101         } else if (aWdgType == WDG_TOOLBOX) {
102           QToolBox* aToolbox = qobject_cast<QToolBox*>(aWidget);
103           aToolbox->addItem(aPage, aPageName);
104         }
105       } while (myWidgetApi->toNextWidget());
106     }
107   } while (myWidgetApi->toNextWidget());
108   theParent->setLayout(aWidgetLay);
109 }
110
111 QWidget* ModuleBase_WidgetFactory::labelControl(QWidget* theParent)
112 {
113   QWidget* result = new QWidget(theParent);
114   QVBoxLayout* aLabelLay = new QVBoxLayout(result);
115   QLabel* aLabel = new QLabel(result);
116   aLabel->setWordWrap(true);
117   aLabel->setText(qs(myWidgetApi->getProperty(INFO_WDG_TEXT)));
118   aLabel->setToolTip(qs(myWidgetApi->getProperty(INFO_WDG_TOOLTIP)));
119   aLabelLay->addWidget(aLabel);
120   aLabelLay->addStretch(1);
121   result->setLayout(aLabelLay);
122   return result;
123 }
124
125 QWidget* ModuleBase_WidgetFactory::createWidgetByType(const std::string& theType,
126                                                       QWidget* theParent)
127 {
128   QWidget* result = NULL;
129   if (theType == WDG_DOUBLEVALUE) {
130     result = doubleSpinBoxControl(theParent);
131
132   } else if (theType == WDG_INFO) {
133     result = labelControl(theParent);
134
135   } else if (theType == WDG_SHAPE_SELECTOR) {
136     result = shapeSelectorControl(theParent);
137
138   } else if (theType == WDG_BOOLVALUE) {
139     result = booleanControl(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_FILE_SELECTOR) {
151     result = fileSelectorControl(theParent);
152
153   } else if (theType == WDG_CHOICE) {
154     result = choiceControl(theParent);
155
156   } else if (theType == WDG_STRINGVALUE) {
157     result = lineEditControl(theParent);
158
159   } else if (theType == WDG_MULTISELECTOR) {
160     result = multiSelectorControl(theParent);
161
162   } else if (myWidgetApi->isContainerWidget() || myWidgetApi->isPagedWidget()) {
163     result = createContainer(theType, theParent);
164   } else {
165     result = myWorkshop->module()->createWidgetByType(theType, theParent, myWidgetApi,
166                                                       myParentId, myModelWidgets);
167 #ifdef _DEBUG
168     if (!result) {qDebug("ModuleBase_WidgetFactory::fillWidget: find bad widget type");}
169 #endif
170   }
171   return result;
172 }
173
174 QWidget* ModuleBase_WidgetFactory::createContainer(const std::string& theType, QWidget* theParent)
175 {
176   QWidget* result = NULL;
177   if (theType == WDG_GROUP || theType == WDG_CHECK_GROUP) {
178     QGroupBox* aGroupBox = new QGroupBox(theParent);
179     aGroupBox->setCheckable(theType == WDG_CHECK_GROUP);
180     result = aGroupBox;
181   } else if (theType == WDG_TOOLBOX) {
182     result = new QToolBox(theParent);
183   } else if (theType == WDG_SWITCH) {
184     result = new ModuleBase_WidgetSwitch(theParent);
185   } else if (theType == WDG_TOOLBOX_BOX || theType == WDG_SWITCH_CASE) {
186     result = NULL;
187   }
188 #ifdef _DEBUG
189   else {qDebug() << "ModuleBase_WidgetFactory::fillWidget: find bad container type";}
190 #endif
191   return result;
192 }
193
194 QWidget* ModuleBase_WidgetFactory::doubleSpinBoxControl(QWidget* theParent)
195 {
196   ModuleBase_WidgetDoubleValue* aDblWgt =
197       new ModuleBase_WidgetDoubleValue(theParent, myWidgetApi, myParentId);
198   myModelWidgets.append(aDblWgt);
199   return aDblWgt->getControl();
200 }
201
202 //QWidget* ModuleBase_WidgetFactory::featureSelectorControl(QWidget* theParent)
203 //{
204 //  ModuleBase_WidgetFeature* aWidget =
205 //      new ModuleBase_WidgetFeature(theParent, myWidgetApi,myParentId);
206 //  myModelWidgets.append(aWidget);
207 //  return aWidget->getControl();
208 //}
209
210 //QWidget* ModuleBase_WidgetFactory::featureOrAttributeSelectorControl(QWidget* theParent)
211 //{
212 //  ModuleBase_WidgetFeatureOrAttribute* aWidget =
213 //      new ModuleBase_WidgetFeatureOrAttribute(theParent, myWidgetApi, myParentId);
214 //  myModelWidgets.append(aWidget);
215 //  return aWidget->getControl();
216 //}
217
218 QWidget* ModuleBase_WidgetFactory::doubleValueEditor(QWidget* theParent)
219 {
220   ModuleBase_WidgetEditor* aWidget =
221       new ModuleBase_WidgetEditor(theParent, myWidgetApi, myParentId);
222   myModelWidgets.append(aWidget);
223   return aWidget->getControl();
224 }
225
226 QWidget* ModuleBase_WidgetFactory::shapeSelectorControl(QWidget* theParent)
227 {
228   ModuleBase_WidgetShapeSelector* aSelector =
229       new ModuleBase_WidgetShapeSelector(theParent, myWorkshop, myWidgetApi, myParentId);
230   myModelWidgets.append(aSelector);
231   return aSelector->getControl();
232 }
233
234 QWidget* ModuleBase_WidgetFactory::booleanControl(QWidget* theParent)
235 {
236   ModuleBase_WidgetBoolValue* aBoolWgt =
237       new ModuleBase_WidgetBoolValue(theParent, myWidgetApi, myParentId);
238   myModelWidgets.append(aBoolWgt);
239   return aBoolWgt->getControl();
240 }
241
242 QWidget* ModuleBase_WidgetFactory::fileSelectorControl(QWidget* theParent)
243 {
244   ModuleBase_WidgetFileSelector* aFileSelectorWgt =
245       new ModuleBase_WidgetFileSelector(theParent, myWidgetApi, myParentId);
246   myModelWidgets.append(aFileSelectorWgt);
247   return aFileSelectorWgt->getControl();
248 }
249
250 QWidget* ModuleBase_WidgetFactory::choiceControl(QWidget* theParent)
251 {
252   ModuleBase_WidgetChoice* aChoiceWgt =
253       new ModuleBase_WidgetChoice(theParent, myWidgetApi,myParentId);
254   myModelWidgets.append(aChoiceWgt);
255   return aChoiceWgt->getControl();
256 }
257
258 QWidget* ModuleBase_WidgetFactory::lineEditControl(QWidget* theParent)
259 {
260   ModuleBase_WidgetLineEdit* aLineEditWgt =
261       new ModuleBase_WidgetLineEdit(theParent, myWidgetApi,myParentId);
262   myModelWidgets.append(aLineEditWgt);
263   return aLineEditWgt->getControl();
264 }
265
266 QWidget* ModuleBase_WidgetFactory::multiSelectorControl(QWidget* theParent)
267 {
268   ModuleBase_WidgetMultiSelector* aMultiselectorWgt =
269       new ModuleBase_WidgetMultiSelector(theParent, myWorkshop, myWidgetApi,myParentId);
270   myModelWidgets.append(aMultiselectorWgt);
271   return aMultiselectorWgt->getControl();
272 }
273
274 QString ModuleBase_WidgetFactory::qs(const std::string& theStdString) const
275 {
276   return QString::fromStdString(theStdString);
277 }