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