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