]> SALOME platform Git repositories - modules/shaper.git/blob - src/ModuleBase/ModuleBase_WidgetFactory.cpp
Salome HOME
Merge branch 'master' of newgeom:newgeom
[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       aWidgetLay->addWidget(aWidget);
65     }
66     if (myWidgetApi->isContainerWidget()) {
67       //if current widget is groupbox (container) process it's children recursively
68       QString aGroupName = qs(myWidgetApi->getProperty(CONTAINER_PAGE_NAME));
69       createWidget(aWidget);
70       QGroupBox* aGrBox = qobject_cast<QGroupBox*>(aWidget);
71       aGrBox->setTitle(aGroupName);
72     }
73     if (myWidgetApi->isPagedWidget()) {
74       //If current widget is toolbox or switch-casebox then fetch all
75       //it's pages recursively and setup into the widget.
76       myWidgetApi->toChildWidget();
77       do {
78         QString aPageName = qs(myWidgetApi->getProperty(CONTAINER_PAGE_NAME));
79         QWidget* aPage = new QWidget(aWidget);
80         createWidget(aPage);
81         if (aWdgType == WDG_SWITCH) {
82           ModuleBase_WidgetSwitch* aSwitch = qobject_cast<ModuleBase_WidgetSwitch*>(aWidget);
83           aSwitch->addPage(aPage, aPageName);
84         } else if (aWdgType == WDG_TOOLBOX){
85           QToolBox* aToolbox = qobject_cast<QToolBox*>(aWidget);
86           aToolbox->addItem(aPage, aPageName);
87         }
88       } while(myWidgetApi->toNextWidget());
89     }
90   } while(myWidgetApi->toNextWidget());
91   theParent->setLayout(aWidgetLay);
92 }
93
94 QWidget* ModuleBase_WidgetFactory::labelControl(QWidget* theParent)
95 {
96   QWidget* result = new QWidget(theParent);
97   QVBoxLayout* aLabelLay = new QVBoxLayout(result);
98   QLabel* aLabel = new QLabel(result);
99   aLabel->setText(qs(myWidgetApi->getProperty(INFO_WDG_TEXT)));
100   aLabel->setToolTip(qs(myWidgetApi->getProperty(INFO_WDG_TOOLTIP)));
101   aLabelLay->addWidget(aLabel);
102   aLabelLay->addStretch(1);
103   result->setLayout(aLabelLay);
104   return result;
105 }
106
107 QWidget* ModuleBase_WidgetFactory::createWidgetByType(const std::string& theType, QWidget* theParent)
108 {
109   QWidget* result = NULL;
110   if (theType == WDG_DOUBLEVALUE) {
111     result = doubleSpinBoxControl(theParent);
112
113   } else if (theType == WDG_INFO) {
114     result = labelControl(theParent);
115
116   } else if (theType == WDG_SELECTOR) {
117     result = selectorControl(theParent);
118
119   } else if (theType == WDG_BOOLVALUE) {
120     result = booleanControl(theParent);
121
122   } else if (theType == WDG_POINT_SELECTOR) {
123     result = pointSelectorControl(theParent);
124
125   } else if (theType == WDG_FEATURE_SELECTOR) {
126     result = featureSelectorControl(theParent);
127
128   } else if (theType == WDG_DOUBLEVALUE_EDITOR) {
129     result = doubleValueEditor(theParent);
130   
131   } else if (theType == WDG_POINT2D_DISTANCE) {
132     result = point2dDistanceControl(theParent);
133
134   }
135   else if (myWidgetApi->isContainerWidget() || myWidgetApi->isPagedWidget()) {
136     result = createContainer(theType, theParent);
137   }
138 #ifdef _DEBUG
139   else { qDebug() << "ModuleBase_WidgetFactory::fillWidget: find bad widget type"; }
140 #endif
141   return result;
142 }
143
144 QWidget* ModuleBase_WidgetFactory::createContainer(const std::string& theType, QWidget* theParent)
145 {
146   QWidget* result = NULL;
147   if (theType == WDG_GROUP || theType == WDG_CHECK_GROUP) {
148     QGroupBox* aGroupBox = new QGroupBox(theParent);
149     aGroupBox->setCheckable(theType == WDG_CHECK_GROUP);
150     result = aGroupBox;
151   } else if (theType == WDG_TOOLBOX) {
152     result = new QToolBox(theParent);
153   } else if (theType == WDG_SWITCH) {
154     result = new ModuleBase_WidgetSwitch(theParent);
155   } else if (theType == WDG_TOOLBOX_BOX || theType == WDG_SWITCH_CASE) {
156     result = NULL;
157   }
158 #ifdef _DEBUG
159   else { qDebug() << "ModuleBase_WidgetFactory::fillWidget: find bad container type"; }
160 #endif
161   return result;
162 }
163
164 QWidget* ModuleBase_WidgetFactory::doubleSpinBoxControl(QWidget* theParent)
165 {
166   ModuleBase_WidgetDoubleValue* aDblWgt = new ModuleBase_WidgetDoubleValue(theParent, myWidgetApi);
167   myModelWidgets.append(aDblWgt);
168
169   return aDblWgt->getControl();
170 }
171
172 QWidget* ModuleBase_WidgetFactory::pointSelectorControl(QWidget* theParent)
173 {
174   ModuleBase_WidgetPoint2D* aWidget = new ModuleBase_WidgetPoint2D(theParent, myWidgetApi);
175   myModelWidgets.append(aWidget);
176   return aWidget->getControl();
177 }
178
179 QWidget* ModuleBase_WidgetFactory::featureSelectorControl(QWidget* theParent)
180 {
181   ModuleBase_WidgetFeature* aWidget = new ModuleBase_WidgetFeature(theParent, myWidgetApi);
182   myModelWidgets.append(aWidget);
183   return aWidget->getControl();
184 }
185
186 QWidget* ModuleBase_WidgetFactory::doubleValueEditor(QWidget* theParent)
187 {
188   ModuleBase_WidgetEditor* aWidget = new ModuleBase_WidgetEditor(theParent, myWidgetApi);
189   myModelWidgets.append(aWidget);
190   return 0;
191 }
192
193 QString ModuleBase_WidgetFactory::qs(const std::string& theStdString) const
194 {
195   return QString::fromStdString(theStdString);
196 }
197
198
199 QWidget* ModuleBase_WidgetFactory::selectorControl(QWidget* theParent)
200 {
201   ModuleBase_WidgetSelector* aSelector = new ModuleBase_WidgetSelector(theParent, myWorkshop, myWidgetApi);
202   myModelWidgets.append(aSelector);
203   return aSelector->getControl();
204 }
205
206
207 QWidget* ModuleBase_WidgetFactory::booleanControl(QWidget* theParent)
208 {
209   ModuleBase_WidgetBoolValue* aBoolWgt = new ModuleBase_WidgetBoolValue(theParent, myWidgetApi);
210   myModelWidgets.append(aBoolWgt);
211
212   return aBoolWgt->getControl();
213 }
214
215
216 QWidget* ModuleBase_WidgetFactory::point2dDistanceControl(QWidget* theParent)
217 {
218   ModuleBase_WidgetPoint2dDistance* aDistWgt = new ModuleBase_WidgetPoint2dDistance(theParent, myWidgetApi);
219   myModelWidgets.append(aDistWgt);
220
221   return aDistWgt->getControl();
222 }